1 | /*
|
---|
2 | * GS2Browse.java
|
---|
3 | * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 | package org.greenstone.gsdl3.service;
|
---|
20 |
|
---|
21 | // Greenstone classes
|
---|
22 | import org.greenstone.gsdl3.util.OID;
|
---|
23 | import org.greenstone.gsdl3.util.GSXML;
|
---|
24 | import org.greenstone.gsdl3.util.GSFile;
|
---|
25 | import org.greenstone.gsdl3.util.MacroResolver;
|
---|
26 | import org.greenstone.gsdl3.util.GS2MacroResolver;
|
---|
27 | import org.greenstone.gsdl3.util.GDBMWrapper;
|
---|
28 | import org.greenstone.gsdl3.util.DBInfo;
|
---|
29 | // XML classes
|
---|
30 | import org.w3c.dom.Document;
|
---|
31 | import org.w3c.dom.Element;
|
---|
32 | import org.w3c.dom.NodeList;
|
---|
33 |
|
---|
34 | // General Java classes
|
---|
35 | import java.util.ArrayList;
|
---|
36 | import java.util.StringTokenizer;
|
---|
37 | import java.util.Set;
|
---|
38 | import java.util.Iterator;
|
---|
39 |
|
---|
40 | /** Greenstone 2 collection classifier service
|
---|
41 | *
|
---|
42 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
43 | * @author <a href="mailto:[email protected]">Michael Dewsnip</a>
|
---|
44 | */
|
---|
45 |
|
---|
46 | public class GS2Browse
|
---|
47 | extends AbstractBrowse
|
---|
48 | {
|
---|
49 |
|
---|
50 | protected GDBMWrapper gdbm_src = null;
|
---|
51 |
|
---|
52 | public GS2Browse()
|
---|
53 | {
|
---|
54 | this.gdbm_src = new GDBMWrapper();
|
---|
55 | this.macro_resolver = new GS2MacroResolver(this.gdbm_src);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public void cleanUp() {
|
---|
59 | super.cleanUp();
|
---|
60 | this.gdbm_src.closeDatabase();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public boolean configure(Element info, Element extra_info)
|
---|
64 | {
|
---|
65 | if (!super.configure(info, extra_info)){
|
---|
66 | return false;
|
---|
67 | }
|
---|
68 |
|
---|
69 | System.err.println("Configuring GS2Browse...");
|
---|
70 | // Open GDBM database for querying
|
---|
71 | String gdbm_db_file = GSFile.GDBMDatabaseFile(this.site_home, this.cluster_name);
|
---|
72 | if (!this.gdbm_src.openDatabase(gdbm_db_file, GDBMWrapper.READER)) {
|
---|
73 | System.err.println("GS2Browse Error: Could not open GDBM database!");
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** if id ends in .fc, .pc etc, then translate it to the correct id */
|
---|
80 | protected String translateId(String node_id) {
|
---|
81 | return this.gdbm_src.translateOID(node_id);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** returns the document type of the doc that the specified node
|
---|
85 | belongs to. should be one of
|
---|
86 | GSXML.DOC_TYPE_SIMPLE,
|
---|
87 | GSXML.DOC_TYPE_PAGED,
|
---|
88 | GSXML.DOC_TYPE_HIERARCHY
|
---|
89 | */
|
---|
90 | protected String getDocType(String node_id) {
|
---|
91 | DBInfo info = this.gdbm_src.getInfo(node_id);
|
---|
92 | if (info == null) {
|
---|
93 | return GSXML.DOC_TYPE_SIMPLE;
|
---|
94 | }
|
---|
95 | String doc_type = info.getInfo("doctype");
|
---|
96 | if (!doc_type.equals("")&&!doc_type.equals("doc")) {
|
---|
97 | return doc_type;
|
---|
98 | }
|
---|
99 |
|
---|
100 | String top_id = OID.getTop(node_id);
|
---|
101 | boolean is_top = (top_id.equals(node_id) ? true : false);
|
---|
102 |
|
---|
103 | String children = info.getInfo("contains");
|
---|
104 | boolean is_leaf = (children.equals("") ? true : false);
|
---|
105 |
|
---|
106 | if (is_top && is_leaf) { // a single section document
|
---|
107 | return GSXML.DOC_TYPE_SIMPLE;
|
---|
108 | }
|
---|
109 |
|
---|
110 | // now we just check the top node
|
---|
111 | if (!is_top) { // we need to look at the top info
|
---|
112 | info = this.gdbm_src.getInfo(top_id);
|
---|
113 | }
|
---|
114 | if (info == null) {
|
---|
115 | return GSXML.DOC_TYPE_HIERARCHY;
|
---|
116 | }
|
---|
117 |
|
---|
118 | String childtype = info.getInfo("childtype");
|
---|
119 | if (childtype.equals("Paged")) {
|
---|
120 | return GSXML.DOC_TYPE_PAGED;
|
---|
121 | }
|
---|
122 | return GSXML.DOC_TYPE_HIERARCHY;
|
---|
123 |
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** returns the id of the root node of the document containing node node_id. . may be the same as node_id */
|
---|
127 | protected String getRootId(String node_id) {
|
---|
128 | return OID.getTop(node_id);
|
---|
129 | }
|
---|
130 | /** returns a list of the child ids in order, null if no children */
|
---|
131 | protected ArrayList getChildrenIds(String node_id) {
|
---|
132 | DBInfo info = this.gdbm_src.getInfo(node_id);
|
---|
133 | if (info == null) {
|
---|
134 | return null;
|
---|
135 | }
|
---|
136 |
|
---|
137 | ArrayList children = new ArrayList();
|
---|
138 |
|
---|
139 | String contains = info.getInfo("contains");
|
---|
140 | StringTokenizer st = new StringTokenizer(contains, ";");
|
---|
141 | while (st.hasMoreTokens()) {
|
---|
142 | String child_id = st.nextToken().replaceAll("\"", node_id);
|
---|
143 | children.add(child_id);
|
---|
144 | }
|
---|
145 | return children;
|
---|
146 |
|
---|
147 | }
|
---|
148 | /** returns the node id of the parent node, null if no parent */
|
---|
149 | protected String getParentId(String node_id){
|
---|
150 | String parent = OID.getParent(node_id);
|
---|
151 | if (parent.equals(node_id)) {
|
---|
152 | return null;
|
---|
153 | }
|
---|
154 | return parent;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** get the metadata for the classifier node node_id
|
---|
158 | * returns a metadataList element:
|
---|
159 | * <metadataList><metadata name="xxx">value</metadata></metadataList>
|
---|
160 | * if all_metadata is true, returns all available metadata, otherwise just
|
---|
161 | * returns requested metadata
|
---|
162 | */
|
---|
163 | // assumes only one value per metadata
|
---|
164 | protected Element getMetadataList(String node_id, boolean all_metadata,
|
---|
165 | ArrayList metadata_names) {
|
---|
166 | String lang = "en";
|
---|
167 | Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
168 | DBInfo info = this.gdbm_src.getInfo(node_id);
|
---|
169 | if (info == null) {
|
---|
170 | return null;
|
---|
171 | }
|
---|
172 | if (all_metadata) {
|
---|
173 | // return everything out of the database
|
---|
174 | Set keys = info.getKeys();
|
---|
175 | Iterator it = keys.iterator();
|
---|
176 | while(it.hasNext()) {
|
---|
177 | String key = (String)it.next();
|
---|
178 | String value = info.getInfo(key);
|
---|
179 | GSXML.addMetadata(this.doc, metadata_list, key, this.macro_resolver.resolve(value, lang, GS2MacroResolver.SCOPE_META, node_id));
|
---|
180 | }
|
---|
181 |
|
---|
182 | } else {
|
---|
183 | for (int i=0; i<metadata_names.size(); i++) {
|
---|
184 | String meta_name = (String) metadata_names.get(i);
|
---|
185 | String value = (String)info.getInfo(meta_name);
|
---|
186 | GSXML.addMetadata(this.doc, metadata_list, meta_name, value);
|
---|
187 | }
|
---|
188 | }
|
---|
189 | return metadata_list;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /** returns the structural information asked for.
|
---|
193 | * info_type may be one of
|
---|
194 | * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS
|
---|
195 | */
|
---|
196 | protected String getStructureInfo(String doc_id, String info_type) {
|
---|
197 | String value="";
|
---|
198 | if (info_type.equals(INFO_NUM_SIBS)) {
|
---|
199 | String parent_id = OID.getParent(doc_id);
|
---|
200 | if (parent_id.equals(doc_id)) {
|
---|
201 | value="0";
|
---|
202 | } else {
|
---|
203 | value = String.valueOf(getNumChildren(parent_id));
|
---|
204 | }
|
---|
205 | return value;
|
---|
206 | }
|
---|
207 |
|
---|
208 | if (info_type.equals(INFO_NUM_CHILDREN)) {
|
---|
209 | return String.valueOf(getNumChildren(doc_id));
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | if (info_type.equals(INFO_SIB_POS)) {
|
---|
214 | String parent_id = OID.getParent(doc_id);
|
---|
215 | if (parent_id.equals(doc_id)) {
|
---|
216 | return "-1";
|
---|
217 | }
|
---|
218 |
|
---|
219 | DBInfo info = this.gdbm_src.getInfo(parent_id);
|
---|
220 | if (info==null) {
|
---|
221 | return "-1";
|
---|
222 | }
|
---|
223 |
|
---|
224 | String contains = info.getInfo("contains");
|
---|
225 | contains = contains.replaceAll("\"", parent_id);
|
---|
226 | String [] children = contains.split(";");
|
---|
227 | for (int i=0;i<children.length;i++) {
|
---|
228 | String child_id = children[i];
|
---|
229 | if (child_id.equals(doc_id)) {
|
---|
230 | return String.valueOf(i+1); // make it from 1 to length
|
---|
231 |
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | return "-1";
|
---|
236 | } else {
|
---|
237 | return null;
|
---|
238 | }
|
---|
239 |
|
---|
240 | }
|
---|
241 |
|
---|
242 | protected int getNumChildren(String node_id) {
|
---|
243 | DBInfo info = this.gdbm_src.getInfo(node_id);
|
---|
244 | if (info == null) {
|
---|
245 | return 0;
|
---|
246 | }
|
---|
247 | String contains = info.getInfo("contains");
|
---|
248 | if (contains.equals("")) {
|
---|
249 | return 0;
|
---|
250 | }
|
---|
251 | String [] children = contains.split(";");
|
---|
252 | return children.length;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /** returns true if the id refers to a document (rather than
|
---|
256 | * a classifier node)
|
---|
257 | */
|
---|
258 | protected boolean isDocumentId(String node_id){
|
---|
259 | if (node_id.startsWith("CL")) {
|
---|
260 | return false;
|
---|
261 | }
|
---|
262 | return true;
|
---|
263 | }
|
---|
264 |
|
---|
265 | }
|
---|