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

Last change on this file since 30721 was 28667, checked in by ak19, 11 years ago

Jdb2Txt now supports sorting the output, to bring it in line with what db2txt does nowadays. This is needed for diffcol, since GS3 demo-lucene uses jdbm.

File size: 4.6 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.helper.StringComparator;
30import jdbm.htree.HTree;
31
32import java.io.IOException;
33import java.io.UnsupportedEncodingException;
34import java.io.OutputStreamWriter;
35import java.io.PrintWriter;
36import java.util.Collections;
37import java.util.Iterator;
38import java.util.Properties;
39import java.util.Vector;
40
41
42public class Jdb2Txt
43{
44 static String TNAME = "greenstone";
45
46 RecordManager recman_;
47 HTree hashtable_;
48 boolean sort_;
49
50 static private PrintWriter utf8out = null;
51
52 static
53 {
54 try {
55 OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
56 utf8out = new PrintWriter(osw, true);
57 }
58 catch (UnsupportedEncodingException e) {
59 System.out.println(e);
60 }
61 }
62
63 // the default constructor retains default behaviour of not sorting the keys
64 public Jdb2Txt(String db_filename)
65 throws IOException
66 {
67 this(db_filename, false);
68 }
69
70 public Jdb2Txt(String db_filename, boolean sort)
71 throws IOException
72 {
73 this.sort_ = sort;
74
75 if (db_filename.endsWith(".jdb")) {
76 // remove file extension as JDBM does not expect it
77 db_filename = db_filename.substring(0,db_filename.length()-4);
78 }
79
80 // create or open a record manager
81 Properties props = new Properties();
82 recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
83
84 // create or load table
85 long recid = recman_.getNamedObject(TNAME);
86 if (recid != 0) {
87 hashtable_ = HTree.load(recman_, recid);
88 }
89 else {
90 System.err.println("Failed to find database table '" + TNAME +"' ...");
91 System.exit(-1);
92 }
93 }
94
95
96 public void db2txt()
97 throws IOException
98 {
99 FastIterator iter;
100 String key;
101 String val;
102
103 Vector keylist = sort_ ? new Vector() : null;
104
105 iter = hashtable_.keys();
106 key = (String) iter.next();
107 while (key != null) {
108 val = (String) hashtable_.get(key);
109
110 if(sort_) {
111 keylist.add(key);
112 }
113 else {
114 print_entry(key, val);
115 }
116 key = (String) iter.next();
117 }
118
119 if(sort_) {
120 Collections.sort(keylist, new StringComparator());
121 Iterator i = keylist.iterator();
122 while(i.hasNext()) {
123 key = (String)i.next();
124 val = (String) hashtable_.get(key);
125 print_entry(key, val);
126 }
127 }
128
129 recman_.close();
130 }
131
132 public static void print_entry(String key, String value) {
133 utf8out.println("[" + key + "]");
134 utf8out.println(value);
135 // 70 hypens
136 utf8out.println("----------------------------------------------------------------------");
137
138 }
139
140 public static void print_usage()
141 {
142 System.err.println("Usage: java Jdb2Txt [options] database-name");
143 System.err.println("options: ");
144 System.err.println(" -sort\tsort the keys to the database before output\n");
145 System.exit(-1);
146 }
147
148
149 public static void main(String[] args)
150 {
151 if (args.length < 1 || args.length > 2) {
152 print_usage();
153 }
154
155 String dbname = args[0];
156 boolean sort = false;
157
158 if(args.length == 2) {
159 if(args[0].equals("-sort")) {
160 sort = true;
161 dbname = args[1];
162 } else {
163 System.err.println(args[0] + " is not a valid option\n");
164 print_usage();
165 }
166 }
167
168 try {
169
170 Jdb2Txt table = new Jdb2Txt(dbname, sort);
171 table.db2txt();
172 }
173 catch (IOException e) {
174 e.printStackTrace();
175 }
176 }
177
178}
Note: See TracBrowser for help on using the repository browser.