source: main/trunk/greenstone2/common-src/src/jdbmedit/Jdb2Txt.java@ 21395

Last change on this file since 21395 was 21395, checked in by davidb, 14 years ago

Initial implementation based on JDBM rather than GDBM to allow (one day) for Java-only based database to complement Java-based Lucene indexing

File size: 2.9 KB
Line 
1/**********************************************************************
2 *
3 * Jdb2Txt.java --
4 * A component of the Greenstone digital library software
5 * from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Copyright (C) 1999 The New Zealand Digital Library Project
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 **********************************************************************/
25
26import jdbm.RecordManager;
27import jdbm.RecordManagerFactory;
28import jdbm.helper.FastIterator;
29import jdbm.htree.HTree;
30
31import java.io.IOException;
32import java.util.Properties;
33
34
35public class Jdb2Txt
36{
37 static String TNAME = "greenstone";
38
39 RecordManager recman_;
40 HTree hashtable_;
41
42
43 public Jdb2Txt(String db_filename)
44 throws IOException
45 {
46 // create or open a record manager
47 Properties props = new Properties();
48 recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
49
50 // create or load table
51 long recid = recman_.getNamedObject(TNAME);
52 if (recid != 0) {
53 hashtable_ = HTree.load(recman_, recid);
54 }
55 else {
56 System.err.println("Failed to find database table '" + TNAME +"' ...");
57 System.exit(-1);
58 }
59 }
60
61
62 public void db2txt()
63 throws IOException
64 {
65 FastIterator iter;
66 String key;
67 String val;
68
69 iter = hashtable_.keys();
70 key = (String) iter.next();
71 while (key != null) {
72 val = (String) hashtable_.get(key);
73 System.out.println("[" + key + "]");
74 System.out.println(val);
75 // 70 hypens
76 System.out.println("----------------------------------------------------------------------");
77 key = (String) iter.next();
78 }
79
80 recman_.close();
81 }
82
83
84 public static void print_usage()
85 {
86 System.err.println("Usage: java Jdb2Txt database-name");
87 System.exit(-1);
88 }
89
90
91 public static void main(String[] args)
92 {
93 if (args.length != 1) {
94 print_usage();
95 }
96
97 String dbname = args[0];
98
99 try {
100
101 Jdb2Txt table = new Jdb2Txt(dbname);
102 table.db2txt();
103 }
104 catch (IOException e) {
105 e.printStackTrace();
106 }
107 }
108
109}
Note: See TracBrowser for help on using the repository browser.