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 | db_.close();
|
---|
53 | } catch (GdbmException e) {
|
---|
54 | // should never get here - close never actually throws an exception
|
---|
55 | System.err.println("GDBMWrapper: error on close()");
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | /** returns a DBInfo object containing all the name-value pairs for
|
---|
60 | * main_key
|
---|
61 | * @see DBInfo
|
---|
62 | */
|
---|
63 | public DBInfo getInfo(String main_key) {
|
---|
64 | if (db_==null) {
|
---|
65 | return null;
|
---|
66 | }
|
---|
67 | String s_info;
|
---|
68 | try {
|
---|
69 | s_info = (String)db_.fetch(main_key);
|
---|
70 | } catch (GdbmException e) {
|
---|
71 | System.err.println("GDBMWrapper: couldn't get record");
|
---|
72 | return null;
|
---|
73 | }
|
---|
74 | if (s_info==null) {
|
---|
75 | // record not present
|
---|
76 | System.err.println("GDBMWrapper: key "+main_key+" not present in db");
|
---|
77 | return null;
|
---|
78 | }
|
---|
79 | DBInfo info = new DBInfo();
|
---|
80 |
|
---|
81 | String [] lines = s_info.split("\n");
|
---|
82 | String key;
|
---|
83 | String value;
|
---|
84 | for (int i=0; i<lines.length; i++) {
|
---|
85 | //System.out.println("line:"+lines[i]);
|
---|
86 | int a = lines[i].indexOf('<');
|
---|
87 | int b= lines[i].indexOf('>');
|
---|
88 | if (a==-1 || b==-1) {
|
---|
89 | System.err.println("error: bad format in db");
|
---|
90 | }
|
---|
91 | else {
|
---|
92 | key=lines[i].substring(a+1, b);
|
---|
93 | value=lines[i].substring(b+1);
|
---|
94 | //System.out.println("key="+key+", val="+value);
|
---|
95 | info.addInfo(key, value);
|
---|
96 |
|
---|
97 | }
|
---|
98 | }
|
---|
99 | return info;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** sets all the name-value pairs in info as the content for key key
|
---|
103 | * TODO - not implemented yet */
|
---|
104 | public boolean setInfo(String key, DBInfo info) {
|
---|
105 | if (db_==null) {
|
---|
106 | return false;
|
---|
107 | }
|
---|
108 | return true;
|
---|
109 | }
|
---|
110 | /** sets the key value as info
|
---|
111 | * TODO - not implemented yet */
|
---|
112 | public boolean setInfo (String key, String info) {
|
---|
113 | if (db_==null) {
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 | /** deletes the entry for key
|
---|
119 | * TODO - not implemented yet */
|
---|
120 | public boolean deleteKey(String key) {
|
---|
121 | if (db_==null) {
|
---|
122 | return false;
|
---|
123 | }
|
---|
124 | return true;
|
---|
125 | }
|
---|
126 |
|
---|
127 | // greenstone convenience methods - should these go into a separate class?
|
---|
128 | // yes I think so.
|
---|
129 | /** converts a greenstone OID to internal docnum */
|
---|
130 | public long OID2Docnum(String OID) {
|
---|
131 | DBInfo info = getInfo(OID);
|
---|
132 | if (info != null) {
|
---|
133 | long real_num = Long.parseLong(info.getInfo("docnum"));
|
---|
134 | return real_num;
|
---|
135 | }
|
---|
136 | return -1;
|
---|
137 | }
|
---|
138 | /** converts a docnum to greenstone OID */
|
---|
139 | public String docnum2OID(long docnum) {
|
---|
140 | DBInfo info = getInfo(Long.toString(docnum));
|
---|
141 | String oid = info.getInfo("section");
|
---|
142 | return oid;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /** converts an external id to greenstone OID */
|
---|
146 | public String externalId2OID(String extid) {
|
---|
147 | DBInfo info = getInfo(extid);
|
---|
148 | if (info != null) {
|
---|
149 | String oid = info.getInfo("section");
|
---|
150 | return oid;
|
---|
151 | }
|
---|
152 | return null;
|
---|
153 | }
|
---|
154 | /** translates relative oids into proper oids:
|
---|
155 | * .pr (parent), .rt (root) .fc (first child), .lc (last child),
|
---|
156 | * .ns (next sibling), .ps (previous sibling)
|
---|
157 | * a suffix is expected to be present so test before using
|
---|
158 | * TODO: fc, lc, ns, ps, .rt .ss*/
|
---|
159 | public String translateOID(String oid) {
|
---|
160 |
|
---|
161 | int p = oid.lastIndexOf('.');
|
---|
162 | if (p != oid.length()-3) {
|
---|
163 | System.out.println("translateoid error: '.' is not the third to last char!!");
|
---|
164 | return oid;
|
---|
165 | }
|
---|
166 |
|
---|
167 | String top = oid.substring(0, p);
|
---|
168 | String suff = oid.substring(p+1);
|
---|
169 | if (suff.equals("pr")) {
|
---|
170 | return OID.getParent(top);
|
---|
171 | } else if (suff.equals("rt")) {
|
---|
172 | return OID.getTop(top);
|
---|
173 | } else {
|
---|
174 | int sibling_num = 0;
|
---|
175 | if (suff.equals("ss")) {
|
---|
176 | // we have to remove the sib num before we get top
|
---|
177 | p = top.lastIndexOf('.');
|
---|
178 | sibling_num = Integer.parseInt(top.substring(p+1));
|
---|
179 | top = top.substring(0, p);
|
---|
180 | }
|
---|
181 |
|
---|
182 | // need to get info out of gdbm db -
|
---|
183 | String doc_id = top;
|
---|
184 | if (suff.endsWith("s")) {
|
---|
185 | doc_id = OID.getParent(top);
|
---|
186 | }
|
---|
187 | DBInfo info = getInfo(doc_id);
|
---|
188 | if (info==null) {
|
---|
189 | System.out.println("info is null!!");
|
---|
190 | return top;
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | /** ATTACK OF THE RANDOM BLARG!!!! **/
|
---|
195 | /** All your code are belong to me! **/
|
---|
196 |
|
---|
197 | String contains = info.getInfo("contains");
|
---|
198 | if (contains.equals("")) {
|
---|
199 | // something is wrong
|
---|
200 | return top;
|
---|
201 | }
|
---|
202 | contains = contains.replaceAll("\"", doc_id);
|
---|
203 | String [] children = contains.split(";");
|
---|
204 | if (suff.equals("fc")) {
|
---|
205 | return children[0];
|
---|
206 | } else if (suff.equals("lc")) {
|
---|
207 | return children[children.length-1];
|
---|
208 | } else {
|
---|
209 | if (suff.equals("ss")) {
|
---|
210 | return children[sibling_num-1];
|
---|
211 | }
|
---|
212 | // find the position that we are at.
|
---|
213 | int i=0;
|
---|
214 | while (i<children.length) {
|
---|
215 | if (children[i].equals(top)) {
|
---|
216 | break;
|
---|
217 | }
|
---|
218 | i++;
|
---|
219 | }
|
---|
220 |
|
---|
221 | if (suff.equals("ns")) {
|
---|
222 | if (i==children.length-1) {
|
---|
223 | return children[i];
|
---|
224 | }
|
---|
225 | return children[i+1];
|
---|
226 | } else if (suff.equals("ps")) {
|
---|
227 | if (i==0) {
|
---|
228 | return children[i];
|
---|
229 | }
|
---|
230 | return children[i-1];
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 | return top;
|
---|
235 | }
|
---|
236 | }
|
---|