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.gdbm.*;
|
---|
23 | import org.greenstone.gsdl3.util.OID;
|
---|
24 | import org.greenstone.gsdl3.util.GSXML;
|
---|
25 | import org.greenstone.gsdl3.util.GSFile;
|
---|
26 | import org.greenstone.gsdl3.util.MacroResolver;
|
---|
27 | import org.greenstone.gsdl3.util.GS2MacroResolver;
|
---|
28 |
|
---|
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 boolean configure(Element info, Element extra_info)
|
---|
59 | {
|
---|
60 | System.err.println("Configuring GS2Browse...");
|
---|
61 | // Open GDBM database for querying
|
---|
62 | String gdbm_db_file = GSFile.GDBMDatabaseFile(this.site_home, this.cluster_name);
|
---|
63 | if (!this.gdbm_src.openDatabase(gdbm_db_file, GDBMWrapper.READER)) {
|
---|
64 | System.err.println("GS2Browse Error: Could not open GDBM database!");
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 | return super.configure(info, extra_info);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** if id ends in .fc, .pc etc, then translate it to the correct id */
|
---|
71 | protected String translateId(String node_id) {
|
---|
72 | return this.gdbm_src.translateOID(node_id);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** returns the document type of the doc that the specified node
|
---|
76 | belongs to. should be one of
|
---|
77 | GSXML.DOC_TYPE_SIMPLE,
|
---|
78 | GSXML.DOC_TYPE_PAGED,
|
---|
79 | GSXML.DOC_TYPE_HIERARCHY
|
---|
80 | */
|
---|
81 | protected String getDocType(String node_id) {
|
---|
82 | DBInfo info = this.gdbm_src.getInfo(node_id);
|
---|
83 | if (info == null) {
|
---|
84 | return GSXML.DOC_TYPE_SIMPLE;
|
---|
85 | }
|
---|
86 | String doc_type = info.getInfo("doctype");
|
---|
87 | if (!doc_type.equals("")&&!doc_type.equals("doc")) {
|
---|
88 | return doc_type;
|
---|
89 | }
|
---|
90 |
|
---|
91 | String top_id = OID.getTop(node_id);
|
---|
92 | boolean is_top = (top_id.equals(node_id) ? true : false);
|
---|
93 |
|
---|
94 | String children = info.getInfo("contains");
|
---|
95 | boolean is_leaf = (children.equals("") ? true : false);
|
---|
96 |
|
---|
97 | if (is_top && is_leaf) { // a single section document
|
---|
98 | return GSXML.DOC_TYPE_SIMPLE;
|
---|
99 | }
|
---|
100 |
|
---|
101 | // now we just check the top node
|
---|
102 | if (!is_top) { // we need to look at the top info
|
---|
103 | info = this.gdbm_src.getInfo(top_id);
|
---|
104 | }
|
---|
105 | if (info == null) {
|
---|
106 | return GSXML.DOC_TYPE_HIERARCHY;
|
---|
107 | }
|
---|
108 |
|
---|
109 | String childtype = info.getInfo("childtype");
|
---|
110 | if (childtype.equals("Paged")) {
|
---|
111 | return GSXML.DOC_TYPE_PAGED;
|
---|
112 | }
|
---|
113 | return GSXML.DOC_TYPE_HIERARCHY;
|
---|
114 |
|
---|
115 | }
|
---|
116 |
|
---|
117 | /** returns the id of the root node of the document containing node node_id. . may be the same as node_id */
|
---|
118 | protected String getRootId(String node_id) {
|
---|
119 | return OID.getTop(node_id);
|
---|
120 | }
|
---|
121 | /** returns a list of the child ids in order, null if no children */
|
---|
122 | protected ArrayList getChildrenIds(String node_id) {
|
---|
123 | DBInfo info = this.gdbm_src.getInfo(node_id);
|
---|
124 | if (info == null) {
|
---|
125 | return null;
|
---|
126 | }
|
---|
127 |
|
---|
128 | ArrayList children = new ArrayList();
|
---|
129 |
|
---|
130 | String contains = info.getInfo("contains");
|
---|
131 | StringTokenizer st = new StringTokenizer(contains, ";");
|
---|
132 | while (st.hasMoreTokens()) {
|
---|
133 | String child_id = st.nextToken().replaceAll("\"", node_id);
|
---|
134 | children.add(child_id);
|
---|
135 | }
|
---|
136 | return children;
|
---|
137 |
|
---|
138 | }
|
---|
139 | /** returns the node id of the parent node, null if no parent */
|
---|
140 | protected String getParentId(String node_id){
|
---|
141 | String parent = OID.getParent(node_id);
|
---|
142 | if (parent.equals(node_id)) {
|
---|
143 | return null;
|
---|
144 | }
|
---|
145 | return parent;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** get the metadata for the classifier node node_id
|
---|
149 | * returns a metadataList element:
|
---|
150 | * <metadataList><metadata name="xxx">value</metadata></metadataList>
|
---|
151 | * if all_metadata is true, returns all available metadata, otherwise just
|
---|
152 | * returns requested metadata
|
---|
153 | */
|
---|
154 | // assumes only one value per metadata
|
---|
155 | protected Element getMetadataList(String node_id, boolean all_metadata,
|
---|
156 | ArrayList metadata_names) {
|
---|
157 | String lang = "en";
|
---|
158 | Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
159 | DBInfo info = this.gdbm_src.getInfo(node_id);
|
---|
160 | if (info == null) {
|
---|
161 | return null;
|
---|
162 | }
|
---|
163 | if (all_metadata) {
|
---|
164 | // return everything out of the database
|
---|
165 | Set keys = info.getKeys();
|
---|
166 | Iterator it = keys.iterator();
|
---|
167 | while(it.hasNext()) {
|
---|
168 | String key = (String)it.next();
|
---|
169 | String value = info.getInfo(key);
|
---|
170 | GSXML.addMetadata(this.doc, metadata_list, key, this.macro_resolver.resolve(value, lang, GS2MacroResolver.SCOPE_META, node_id));
|
---|
171 | }
|
---|
172 |
|
---|
173 | } else {
|
---|
174 | for (int i=0; i<metadata_names.size(); i++) {
|
---|
175 | String meta_name = (String) metadata_names.get(i);
|
---|
176 | String value = (String)info.getInfo(meta_name);
|
---|
177 | GSXML.addMetadata(this.doc, metadata_list, meta_name, value);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | return metadata_list;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** returns the structural information asked for.
|
---|
184 | * info_type may be one of
|
---|
185 | * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS
|
---|
186 | */
|
---|
187 | protected String getStructureInfo(String doc_id, String info_type) {
|
---|
188 | String value="";
|
---|
189 | if (info_type.equals(INFO_NUM_SIBS)) {
|
---|
190 | String parent_id = OID.getParent(doc_id);
|
---|
191 | if (parent_id.equals(doc_id)) {
|
---|
192 | value="0";
|
---|
193 | } else {
|
---|
194 | value = String.valueOf(getNumChildren(parent_id));
|
---|
195 | }
|
---|
196 | return value;
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (info_type.equals(INFO_NUM_CHILDREN)) {
|
---|
200 | return String.valueOf(getNumChildren(doc_id));
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | if (info_type.equals(INFO_SIB_POS)) {
|
---|
205 | String parent_id = OID.getParent(doc_id);
|
---|
206 | if (parent_id.equals(doc_id)) {
|
---|
207 | return "-1";
|
---|
208 | }
|
---|
209 |
|
---|
210 | DBInfo info = this.gdbm_src.getInfo(parent_id);
|
---|
211 | if (info==null) {
|
---|
212 | return "-1";
|
---|
213 | }
|
---|
214 |
|
---|
215 | String contains = info.getInfo("contains");
|
---|
216 | contains = contains.replaceAll("\"", parent_id);
|
---|
217 | String [] children = contains.split(";");
|
---|
218 | for (int i=0;i<children.length;i++) {
|
---|
219 | String child_id = children[i];
|
---|
220 | if (child_id.equals(doc_id)) {
|
---|
221 | return String.valueOf(i+1); // make it from 1 to length
|
---|
222 |
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | return "-1";
|
---|
227 | } else {
|
---|
228 | return null;
|
---|
229 | }
|
---|
230 |
|
---|
231 | }
|
---|
232 |
|
---|
233 | protected int getNumChildren(String node_id) {
|
---|
234 | DBInfo info = this.gdbm_src.getInfo(node_id);
|
---|
235 | if (info == null) {
|
---|
236 | return 0;
|
---|
237 | }
|
---|
238 | String contains = info.getInfo("contains");
|
---|
239 | if (contains.equals("")) {
|
---|
240 | return 0;
|
---|
241 | }
|
---|
242 | String [] children = contains.split(";");
|
---|
243 | return children.length;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /** returns true if the id refers to a document (rather than
|
---|
247 | * a classifier node)
|
---|
248 | */
|
---|
249 | protected boolean isDocumentId(String node_id){
|
---|
250 | if (node_id.startsWith("CL")) {
|
---|
251 | return false;
|
---|
252 | }
|
---|
253 | return true;
|
---|
254 | }
|
---|
255 |
|
---|
256 | }
|
---|