1 | package org.greenstone.gsdl3.util;
|
---|
2 |
|
---|
3 | import au.com.pharos.gdbm.GdbmFile;
|
---|
4 | import au.com.pharos.packing.*;
|
---|
5 | import au.com.pharos.gdbm.GdbmException;
|
---|
6 |
|
---|
7 | /** java wrapper class for gdbm - uses Java-GDBM written by Martin Pool
|
---|
8 | * replaces gdbmclass in the old version
|
---|
9 | */
|
---|
10 |
|
---|
11 | public class GDBMWrapper {
|
---|
12 |
|
---|
13 | // Values must match those from gdbm.h - uses the definitions from GdbmFile !
|
---|
14 | /** Indicates that the caller will just read the database. Many
|
---|
15 | * readers may share a database. */
|
---|
16 | public final static int READER = GdbmFile.READER;
|
---|
17 |
|
---|
18 | /** The caller wants read/write access to an existing database and
|
---|
19 | * requires exclusive access. */
|
---|
20 | public final static int WRITER = GdbmFile.WRITER;
|
---|
21 |
|
---|
22 | /** The caller wants exclusive read/write access, and the database
|
---|
23 | * should be created if it does not already exist. */
|
---|
24 | public final static int WRCREAT = GdbmFile.WRCREAT;
|
---|
25 |
|
---|
26 | /** The caller wants exclusive read/write access, and the database
|
---|
27 | * should be replaced if it already exists. */
|
---|
28 | public final static int NEWDB = GdbmFile.NEWDB;
|
---|
29 |
|
---|
30 | protected GdbmFile db_=null;
|
---|
31 |
|
---|
32 | /** open the database filename, with mode mode - uses the constants
|
---|
33 | above, eg GdbmFile.WRITER */
|
---|
34 | public boolean openDatabase(String filename, int mode){
|
---|
35 | try {
|
---|
36 | if (db_!=null) {
|
---|
37 | db_.close();
|
---|
38 | }
|
---|
39 | db_ = new GdbmFile(filename, mode);
|
---|
40 | } catch ( GdbmException e) { // the database wasn't opened or created
|
---|
41 | System.err.println("GDBMWrapper: couldn't open database "+filename);
|
---|
42 | return false;
|
---|
43 | }
|
---|
44 | db_.setKeyPacking(new StringPacking());
|
---|
45 | db_.setValuePacking(new StringPacking());
|
---|
46 | return true;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /** cloase the database associated with this wrapper */
|
---|
50 | public void closeDatabase() {
|
---|
51 | try {
|
---|
52 | if (db_ != null) {
|
---|
53 | db_.close();
|
---|
54 | db_ = null;
|
---|
55 | }
|
---|
56 | } catch (GdbmException e) {
|
---|
57 | // should never get here - close never actually throws an exception
|
---|
58 | System.err.println("GDBMWrapper: error on close()");
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** returns a DBInfo object containing all the name-value pairs for
|
---|
63 | * main_key
|
---|
64 | * @see DBInfo
|
---|
65 | */
|
---|
66 | public DBInfo getInfo(String main_key) {
|
---|
67 | if (db_==null) {
|
---|
68 | return null;
|
---|
69 | }
|
---|
70 | String s_info;
|
---|
71 | try {
|
---|
72 | s_info = (String)db_.fetch(main_key);
|
---|
73 | } catch (GdbmException e) {
|
---|
74 | System.err.println("GDBMWrapper: couldn't get record");
|
---|
75 | return null;
|
---|
76 | }
|
---|
77 | if (s_info==null) {
|
---|
78 | // record not present
|
---|
79 | System.err.println("GDBMWrapper: key "+main_key+" not present in db");
|
---|
80 | return null;
|
---|
81 | }
|
---|
82 | DBInfo info = new DBInfo();
|
---|
83 |
|
---|
84 | String [] lines = s_info.split("\n");
|
---|
85 | String key;
|
---|
86 | String value;
|
---|
87 | for (int i=0; i<lines.length; i++) {
|
---|
88 | //System.out.println("line:"+lines[i]);
|
---|
89 | int a = lines[i].indexOf('<');
|
---|
90 | int b= lines[i].indexOf('>');
|
---|
91 | if (a==-1 || b==-1) {
|
---|
92 | System.err.println("error: bad format in db");
|
---|
93 | }
|
---|
94 | else {
|
---|
95 | key=lines[i].substring(a+1, b);
|
---|
96 | value=lines[i].substring(b+1);
|
---|
97 | //System.out.println("key="+key+", val="+value);
|
---|
98 | info.addInfo(key, value);
|
---|
99 |
|
---|
100 | }
|
---|
101 | }
|
---|
102 | return info;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /** sets all the name-value pairs in info as the content for key key
|
---|
106 | * TODO - not implemented yet */
|
---|
107 | public boolean setInfo(String key, DBInfo info) {
|
---|
108 | if (db_==null) {
|
---|
109 | return false;
|
---|
110 | }
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 | /** sets the key value as info
|
---|
114 | * TODO - not implemented yet */
|
---|
115 | public boolean setInfo (String key, String info) {
|
---|
116 | if (db_==null) {
|
---|
117 | return false;
|
---|
118 | }
|
---|
119 | return true;
|
---|
120 | }
|
---|
121 | /** deletes the entry for key
|
---|
122 | * TODO - not implemented yet */
|
---|
123 | public boolean deleteKey(String key) {
|
---|
124 | if (db_==null) {
|
---|
125 | return false;
|
---|
126 | }
|
---|
127 | return true;
|
---|
128 | }
|
---|
129 |
|
---|
130 | // greenstone convenience methods - should these go into a separate class?
|
---|
131 | // yes I think so.
|
---|
132 | /** converts a greenstone OID to internal docnum */
|
---|
133 | public long OID2Docnum(String OID) {
|
---|
134 | DBInfo info = getInfo(OID);
|
---|
135 | if (info != null) {
|
---|
136 | long real_num = Long.parseLong(info.getInfo("docnum"));
|
---|
137 | return real_num;
|
---|
138 | }
|
---|
139 | return -1;
|
---|
140 | }
|
---|
141 | /** converts a docnum to greenstone OID */
|
---|
142 | public String docnum2OID(long docnum) {
|
---|
143 | DBInfo info = getInfo(Long.toString(docnum));
|
---|
144 | String oid = info.getInfo("section");
|
---|
145 | return oid;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** converts an external id to greenstone OID */
|
---|
149 | public String externalId2OID(String extid) {
|
---|
150 | DBInfo info = getInfo(extid);
|
---|
151 | if (info != null) {
|
---|
152 | String oid = info.getInfo("section");
|
---|
153 | return oid;
|
---|
154 | }
|
---|
155 | return null;
|
---|
156 | }
|
---|
157 | /** translates relative oids into proper oids:
|
---|
158 | * .pr (parent), .rt (root) .fc (first child), .lc (last child),
|
---|
159 | * .ns (next sibling), .ps (previous sibling)
|
---|
160 | * a suffix is expected to be present so test before using
|
---|
161 | * TODO: fc, lc, ns, ps, .rt .ss*/
|
---|
162 | public String translateOID(String oid) {
|
---|
163 |
|
---|
164 | int p = oid.lastIndexOf('.');
|
---|
165 | if (p != oid.length()-3) {
|
---|
166 | System.out.println("translateoid error: '.' is not the third to last char!!");
|
---|
167 | return oid;
|
---|
168 | }
|
---|
169 |
|
---|
170 | String top = oid.substring(0, p);
|
---|
171 | String suff = oid.substring(p+1);
|
---|
172 | if (suff.equals("pr")) {
|
---|
173 | return OID.getParent(top);
|
---|
174 | } else if (suff.equals("rt")) {
|
---|
175 | return OID.getTop(top);
|
---|
176 | } else {
|
---|
177 | int sibling_num = 0;
|
---|
178 | if (suff.equals("ss")) {
|
---|
179 | // we have to remove the sib num before we get top
|
---|
180 | p = top.lastIndexOf('.');
|
---|
181 | sibling_num = Integer.parseInt(top.substring(p+1));
|
---|
182 | top = top.substring(0, p);
|
---|
183 | }
|
---|
184 |
|
---|
185 | // need to get info out of gdbm db -
|
---|
186 | String doc_id = top;
|
---|
187 | if (suff.endsWith("s")) {
|
---|
188 | doc_id = OID.getParent(top);
|
---|
189 | }
|
---|
190 | DBInfo info = getInfo(doc_id);
|
---|
191 | if (info==null) {
|
---|
192 | System.out.println("info is null!!");
|
---|
193 | return top;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | /** ATTACK OF THE RANDOM BLARG!!!! **/
|
---|
198 | /** All your code are belong to me! **/
|
---|
199 |
|
---|
200 | String contains = info.getInfo("contains");
|
---|
201 | if (contains.equals("")) {
|
---|
202 | // something is wrong
|
---|
203 | return top;
|
---|
204 | }
|
---|
205 | contains = contains.replaceAll("\"", doc_id);
|
---|
206 | String [] children = contains.split(";");
|
---|
207 | if (suff.equals("fc")) {
|
---|
208 | return children[0];
|
---|
209 | } else if (suff.equals("lc")) {
|
---|
210 | return children[children.length-1];
|
---|
211 | } else {
|
---|
212 | if (suff.equals("ss")) {
|
---|
213 | return children[sibling_num-1];
|
---|
214 | }
|
---|
215 | // find the position that we are at.
|
---|
216 | int i=0;
|
---|
217 | while (i<children.length) {
|
---|
218 | if (children[i].equals(top)) {
|
---|
219 | break;
|
---|
220 | }
|
---|
221 | i++;
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (suff.equals("ns")) {
|
---|
225 | if (i==children.length-1) {
|
---|
226 | return children[i];
|
---|
227 | }
|
---|
228 | return children[i+1];
|
---|
229 | } else if (suff.equals("ps")) {
|
---|
230 | if (i==0) {
|
---|
231 | return children[i];
|
---|
232 | }
|
---|
233 | return children[i-1];
|
---|
234 | }
|
---|
235 | }
|
---|
236 | }
|
---|
237 | return top;
|
---|
238 | }
|
---|
239 | }
|
---|