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 |
|
---|
26 | import jdbm.RecordManager;
|
---|
27 | import jdbm.RecordManagerFactory;
|
---|
28 | import jdbm.helper.FastIterator;
|
---|
29 | import jdbm.htree.HTree;
|
---|
30 |
|
---|
31 | import java.io.IOException;
|
---|
32 | import java.io.UnsupportedEncodingException;
|
---|
33 | import java.io.OutputStreamWriter;
|
---|
34 | import java.io.PrintWriter;
|
---|
35 | import java.util.Properties;
|
---|
36 |
|
---|
37 |
|
---|
38 | public class Jdb2Txt
|
---|
39 | {
|
---|
40 | static String TNAME = "greenstone";
|
---|
41 |
|
---|
42 | RecordManager recman_;
|
---|
43 | HTree hashtable_;
|
---|
44 |
|
---|
45 | static private PrintWriter utf8out = null;
|
---|
46 |
|
---|
47 | static
|
---|
48 | {
|
---|
49 | try {
|
---|
50 | OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
|
---|
51 | utf8out = new PrintWriter(osw, true);
|
---|
52 | }
|
---|
53 | catch (UnsupportedEncodingException e) {
|
---|
54 | System.out.println(e);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public Jdb2Txt(String db_filename)
|
---|
59 | throws IOException
|
---|
60 | {
|
---|
61 | if (db_filename.endsWith(".jdb")) {
|
---|
62 | // remove file extension as JDBM does not expect it
|
---|
63 | db_filename = db_filename.substring(0,db_filename.length()-4);
|
---|
64 | }
|
---|
65 |
|
---|
66 | // create or open a record manager
|
---|
67 | Properties props = new Properties();
|
---|
68 | recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
|
---|
69 |
|
---|
70 | // create or load table
|
---|
71 | long recid = recman_.getNamedObject(TNAME);
|
---|
72 | if (recid != 0) {
|
---|
73 | hashtable_ = HTree.load(recman_, recid);
|
---|
74 | }
|
---|
75 | else {
|
---|
76 | System.err.println("Failed to find database table '" + TNAME +"' ...");
|
---|
77 | System.exit(-1);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | public void db2txt()
|
---|
83 | throws IOException
|
---|
84 | {
|
---|
85 | FastIterator iter;
|
---|
86 | String key;
|
---|
87 | String val;
|
---|
88 |
|
---|
89 | iter = hashtable_.keys();
|
---|
90 | key = (String) iter.next();
|
---|
91 | while (key != null) {
|
---|
92 | val = (String) hashtable_.get(key);
|
---|
93 | utf8out.println("[" + key + "]");
|
---|
94 | utf8out.println(val);
|
---|
95 | // 70 hypens
|
---|
96 | utf8out.println("----------------------------------------------------------------------");
|
---|
97 | key = (String) iter.next();
|
---|
98 | }
|
---|
99 |
|
---|
100 | recman_.close();
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | public static void print_usage()
|
---|
105 | {
|
---|
106 | System.err.println("Usage: java Jdb2Txt database-name");
|
---|
107 | System.exit(-1);
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | public static void main(String[] args)
|
---|
112 | {
|
---|
113 | if (args.length != 1) {
|
---|
114 | print_usage();
|
---|
115 | }
|
---|
116 |
|
---|
117 | String dbname = args[0];
|
---|
118 |
|
---|
119 | try {
|
---|
120 |
|
---|
121 | Jdb2Txt table = new Jdb2Txt(dbname);
|
---|
122 | table.db2txt();
|
---|
123 | }
|
---|
124 | catch (IOException e) {
|
---|
125 | e.printStackTrace();
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | } |
---|