1 | /*
|
---|
2 | * AbstractDocumentRetrieve.java
|
---|
3 | * a base class for retrieval services
|
---|
4 |
|
---|
5 | * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
|
---|
6 | *
|
---|
7 | * This program is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This program is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
20 | */
|
---|
21 | package org.greenstone.gsdl3.service;
|
---|
22 |
|
---|
23 | // Greenstone classes
|
---|
24 | //import org.greenstone.gdbm.*;
|
---|
25 | import org.greenstone.gsdl3.util.GSXML;
|
---|
26 | import org.greenstone.gsdl3.util.GSPath;
|
---|
27 | import org.greenstone.gsdl3.util.MacroResolver;
|
---|
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.io.File;
|
---|
36 | import java.util.StringTokenizer;
|
---|
37 | import java.util.Set;
|
---|
38 | import java.util.Iterator;
|
---|
39 | import java.util.ArrayList;
|
---|
40 |
|
---|
41 | /** Abstract class for Document Retrieval Services
|
---|
42 | *
|
---|
43 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
44 | */
|
---|
45 |
|
---|
46 | public abstract class AbstractDocumentRetrieve
|
---|
47 | extends ServiceRack {
|
---|
48 |
|
---|
49 | // the services on offer
|
---|
50 | protected static final String DOCUMENT_STRUCTURE_RETRIEVE_SERVICE = "DocumentStructureRetrieve";
|
---|
51 | protected static final String DOCUMENT_METADATA_RETRIEVE_SERVICE = "DocumentMetadataRetrieve";
|
---|
52 | protected static final String DOCUMENT_CONTENT_RETRIEVE_SERVICE = "DocumentContentRetrieve";
|
---|
53 |
|
---|
54 | protected static final String STRUCT_PARAM = "structure";
|
---|
55 | protected static final String INFO_PARAM = "info";
|
---|
56 |
|
---|
57 | protected static final String STRUCT_ANCESTORS = "ancestors";
|
---|
58 | protected static final String STRUCT_PARENT = "parent";
|
---|
59 | protected static final String STRUCT_SIBS = "siblings";
|
---|
60 | protected static final String STRUCT_CHILDREN = "children";
|
---|
61 | protected static final String STRUCT_DESCENDS = "descendants";
|
---|
62 | protected static final String STRUCT_ENTIRE = "entire";
|
---|
63 |
|
---|
64 | protected static final String INFO_NUM_SIBS = "numSiblings";
|
---|
65 | protected static final String INFO_NUM_CHILDREN = "numChildren";
|
---|
66 | protected static final String INFO_SIB_POS = "siblingPosition";
|
---|
67 |
|
---|
68 | // means the id is not a greenstone id and needs translating
|
---|
69 | protected static final String EXTID_PARAM = "ext";
|
---|
70 |
|
---|
71 | protected Element config_info = null; // the xml from the config file
|
---|
72 |
|
---|
73 | protected String default_document_type = null;
|
---|
74 | protected MacroResolver macro_resolver = null;
|
---|
75 |
|
---|
76 | /** does this class provide the service?? */
|
---|
77 | protected boolean does_metadata = true;
|
---|
78 | protected boolean does_content = true;
|
---|
79 | protected boolean does_structure = true;
|
---|
80 |
|
---|
81 | /** constructor */
|
---|
82 | public AbstractDocumentRetrieve()
|
---|
83 | {
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** configure this service */
|
---|
87 | public boolean configure(Element info, Element extra_info)
|
---|
88 | {
|
---|
89 | System.out.println("Configuring AbstractDocumentRetrieve...");
|
---|
90 | this.config_info = info;
|
---|
91 |
|
---|
92 | // set up short_service_info_ - for now just has name and type
|
---|
93 | if (does_structure) {
|
---|
94 | Element dsr_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
95 | dsr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
96 | dsr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_STRUCTURE_RETRIEVE_SERVICE);
|
---|
97 | this.short_service_info.appendChild(dsr_service);
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (does_metadata) {
|
---|
101 | Element dmr_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
102 | dmr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
103 | dmr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
|
---|
104 | this.short_service_info.appendChild(dmr_service);
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (does_content) {
|
---|
108 | Element dcr_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
109 | dcr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
110 | dcr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_CONTENT_RETRIEVE_SERVICE);
|
---|
111 | this.short_service_info.appendChild(dcr_service);
|
---|
112 | }
|
---|
113 |
|
---|
114 | // look for document display format
|
---|
115 | String path = GSPath.appendLink(GSXML.DISPLAY_ELEM, GSXML.FORMAT_ELEM);
|
---|
116 | Element display_format = (Element)GSXML.getNodeByPath(extra_info, path);
|
---|
117 | if (display_format != null) {
|
---|
118 | this.format_info_map.put(DOCUMENT_CONTENT_RETRIEVE_SERVICE, this.doc.importNode(display_format, true));
|
---|
119 | // should we keep a copy?
|
---|
120 | // check for docType option.
|
---|
121 | Element doc_type_opt = GSXML.getNamedElement(display_format, "gsf:option", GSXML.NAME_ATT, "documentType");
|
---|
122 | if (doc_type_opt != null) {
|
---|
123 | String value = doc_type_opt.getAttribute(GSXML.VALUE_ATT);
|
---|
124 | if (!value.equals("")) {
|
---|
125 | this.default_document_type = value;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (macro_resolver != null) {
|
---|
131 | // set up the macro resolver
|
---|
132 | Element replacement_elem = (Element)GSXML.getChildByTagName(extra_info, "replaceList");
|
---|
133 | if (replacement_elem != null) {
|
---|
134 | macro_resolver.addMacros(replacement_elem);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected Element getServiceDescription(String service_id, String lang, String subset) {
|
---|
142 |
|
---|
143 | // these ones are probably never called, but put them here just in case
|
---|
144 | Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
145 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
146 | service_elem.setAttribute(GSXML.NAME_ATT, service_id);
|
---|
147 | return service_elem;
|
---|
148 | }
|
---|
149 |
|
---|
150 | protected Element processDocumentMetadataRetrieve(Element request) {
|
---|
151 |
|
---|
152 | // Create a new (empty) result message
|
---|
153 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
154 |
|
---|
155 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
156 | result.setAttribute(GSXML.FROM_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
|
---|
157 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
158 |
|
---|
159 | if (!does_metadata) {
|
---|
160 | // shouldn't get here
|
---|
161 | return result;
|
---|
162 | }
|
---|
163 |
|
---|
164 | // Get the parameters of the request
|
---|
165 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
166 | if (param_list == null) {
|
---|
167 | System.err.println("AbstractDocumentRetrieve, DocumentMetadataRetrieve Error: missing paramList.\n");
|
---|
168 | return result; // Return the empty result
|
---|
169 | }
|
---|
170 |
|
---|
171 | boolean external_id = false;
|
---|
172 | // The metadata information required
|
---|
173 | ArrayList metadata_names_list = new ArrayList();
|
---|
174 | boolean all_metadata = false;
|
---|
175 | // Process the request parameters
|
---|
176 | Element param = (Element) param_list.getFirstChild();
|
---|
177 | while (param != null) {
|
---|
178 | // Identify the metadata information desired
|
---|
179 | if (param.getAttribute(GSXML.NAME_ATT).equals("metadata")) {
|
---|
180 | String metadata = GSXML.getValue(param);
|
---|
181 | if (metadata.equals("all")) {
|
---|
182 | all_metadata = true;
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | metadata_names_list.add(metadata);
|
---|
186 | } else if (param.getAttribute(GSXML.NAME_ATT).equals(EXTID_PARAM)&& GSXML.getValue(param).equals("1")) {
|
---|
187 | external_id = true;
|
---|
188 | }
|
---|
189 | param = (Element) param.getNextSibling();
|
---|
190 | }
|
---|
191 |
|
---|
192 | Element node_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
193 | result.appendChild(node_list);
|
---|
194 |
|
---|
195 | // Get the documents
|
---|
196 | Element request_node_list = (Element) GSXML.getChildByTagName(request, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
197 | if (request_node_list == null) {
|
---|
198 | System.err.println("Error: DocumentMetadataRetrieve request had no "+GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
199 | return result;
|
---|
200 | }
|
---|
201 |
|
---|
202 | NodeList request_nodes = request_node_list.getChildNodes();
|
---|
203 | for (int i = 0; i < request_nodes.getLength(); i++) {
|
---|
204 | Element request_node = (Element) request_nodes.item(i);
|
---|
205 | String node_id = request_node.getAttribute(GSXML.NODE_ID_ATT);
|
---|
206 |
|
---|
207 | // Add the document to the results list
|
---|
208 | Element new_node = (Element)this.doc.importNode(request_node, false);
|
---|
209 | node_list.appendChild(new_node);
|
---|
210 |
|
---|
211 | if (external_id) {
|
---|
212 | // can we have .pr etc extensions with external ids?
|
---|
213 | node_id = translateExternalId(node_id);
|
---|
214 | } else if (idNeedsTranslating(node_id)) {
|
---|
215 | node_id = translateId(node_id);
|
---|
216 | }
|
---|
217 |
|
---|
218 | if (node_id == null) {
|
---|
219 | continue;
|
---|
220 | }
|
---|
221 |
|
---|
222 | Element metadata_list = getMetadataList(node_id, all_metadata, metadata_names_list);
|
---|
223 | new_node.appendChild(metadata_list);
|
---|
224 | }
|
---|
225 |
|
---|
226 | return result;
|
---|
227 | }
|
---|
228 |
|
---|
229 | protected Element processDocumentStructureRetrieve(Element request) {
|
---|
230 |
|
---|
231 | // Create a new (empty) result message
|
---|
232 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
233 | result.setAttribute(GSXML.FROM_ATT, DOCUMENT_STRUCTURE_RETRIEVE_SERVICE);
|
---|
234 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
235 |
|
---|
236 | if (!does_structure) {
|
---|
237 | // shouldn't get here
|
---|
238 | return result;
|
---|
239 | }
|
---|
240 |
|
---|
241 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
242 | Element query_doc_list = (Element) GSXML.getChildByTagName(request, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
243 | if (query_doc_list == null) {
|
---|
244 | System.err.println("AbstractDocumentRetrieve Error: DocumentStructureRetrieve request specified no doc nodes.\n");
|
---|
245 | return result;
|
---|
246 | }
|
---|
247 |
|
---|
248 | // Get the parameters of the request
|
---|
249 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
250 | if (param_list == null) {
|
---|
251 | System.err.println("AbstractDocumentRetrieve Error: DocumentStructureRetrieve request had no paramList.");
|
---|
252 | return result; // Return the empty result
|
---|
253 | }
|
---|
254 | Element extid_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, EXTID_PARAM);
|
---|
255 | boolean external_id = false;
|
---|
256 | if (extid_param != null && GSXML.getValue(extid_param).equals("1")) {
|
---|
257 | external_id = true;
|
---|
258 | }
|
---|
259 |
|
---|
260 | // the type of info required
|
---|
261 | boolean want_structure = false;
|
---|
262 | boolean want_info = false;
|
---|
263 |
|
---|
264 | ArrayList info_types=new ArrayList();
|
---|
265 | // The document structure information desired
|
---|
266 | boolean want_ancestors = false;
|
---|
267 | boolean want_parent = false;
|
---|
268 | boolean want_siblings = false;
|
---|
269 | boolean want_children = false;
|
---|
270 | boolean want_descendants = false;
|
---|
271 |
|
---|
272 | boolean want_entire_structure = false;
|
---|
273 | // Process the request parameters
|
---|
274 | NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
|
---|
275 | for (int i=0; i<params.getLength();i++) {
|
---|
276 |
|
---|
277 | Element param = (Element)params.item(i);
|
---|
278 | String p_name = param.getAttribute(GSXML.NAME_ATT);
|
---|
279 | String p_value = GSXML.getValue(param);
|
---|
280 | // Identify the structure information desired
|
---|
281 | if (p_name.equals(STRUCT_PARAM)) {
|
---|
282 | want_structure = true;
|
---|
283 |
|
---|
284 | // This is NOT locale sensitive
|
---|
285 | if (p_value.equals(STRUCT_ANCESTORS))
|
---|
286 | want_ancestors = true;
|
---|
287 | else if (p_value.equals(STRUCT_PARENT))
|
---|
288 | want_parent = true;
|
---|
289 | else if (p_value.equals(STRUCT_SIBS))
|
---|
290 | want_siblings = true;
|
---|
291 | else if (p_value.equals(STRUCT_CHILDREN))
|
---|
292 | want_children = true;
|
---|
293 | else if (p_value.equals(STRUCT_DESCENDS))
|
---|
294 | want_descendants = true;
|
---|
295 | else if (p_value.equals(STRUCT_ENTIRE))
|
---|
296 | want_entire_structure = true;
|
---|
297 | else
|
---|
298 | System.err.println("AbstractDocumentRetrieve Warning: Unknown value \"" + p_value + "\".");
|
---|
299 | } else if (p_name.equals(INFO_PARAM)) {
|
---|
300 | want_info = true;
|
---|
301 | info_types.add(p_value);
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | // Make sure there is no repeated information
|
---|
306 | if (want_ancestors)
|
---|
307 | want_parent = false;
|
---|
308 | if (want_descendants)
|
---|
309 | want_children = false;
|
---|
310 |
|
---|
311 | // the document list to hold the results
|
---|
312 | Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
313 | result.appendChild(doc_list);
|
---|
314 |
|
---|
315 | // Get the documents
|
---|
316 | String[] doc_ids = GSXML.getAttributeValuesFromList(query_doc_list,
|
---|
317 | GSXML.NODE_ID_ATT);
|
---|
318 | for (int i = 0; i < doc_ids.length; i++) {
|
---|
319 | // Add the document to the list
|
---|
320 | Element doc = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
321 | doc_list.appendChild(doc);
|
---|
322 |
|
---|
323 | String doc_id = doc_ids[i];
|
---|
324 | doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
|
---|
325 | if (external_id) {
|
---|
326 | doc_id = translateExternalId(doc_id);
|
---|
327 | doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
|
---|
328 | } else if (idNeedsTranslating(doc_id)) {
|
---|
329 | doc_id = translateId(doc_id);
|
---|
330 | doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
|
---|
331 | }
|
---|
332 | if (doc_id == null) {
|
---|
333 | continue;
|
---|
334 | }
|
---|
335 |
|
---|
336 | if (want_info) {
|
---|
337 |
|
---|
338 | Element node_info_elem = this.doc.createElement("nodeStructureInfo");
|
---|
339 | doc.appendChild(node_info_elem);
|
---|
340 |
|
---|
341 | for (int j=0; j<info_types.size(); j++) {
|
---|
342 | String info_type = (String)info_types.get(j);
|
---|
343 | String info_value = getStructureInfo(doc_id, info_type);
|
---|
344 | if (info_value != null) {
|
---|
345 | Element info_elem = this.doc.createElement("info");
|
---|
346 | info_elem.setAttribute(GSXML.NAME_ATT, info_type);
|
---|
347 | info_elem.setAttribute(GSXML.VALUE_ATT, info_value);
|
---|
348 | node_info_elem.appendChild(info_elem);
|
---|
349 | }
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | if (want_structure) {
|
---|
354 | // all structure info goes into a nodeStructure elem
|
---|
355 | Element structure_elem = this.doc.createElement(GSXML.NODE_STRUCTURE_ELEM);
|
---|
356 | doc.appendChild(structure_elem);
|
---|
357 |
|
---|
358 | if (want_entire_structure) {
|
---|
359 | String root_id = getRootId(doc_id);
|
---|
360 | Element root_node = createDocNode(root_id); //, true, false);
|
---|
361 | addDescendants(root_node, root_id, true);
|
---|
362 | structure_elem.appendChild(root_node);
|
---|
363 | continue; // with the next document, we dont need to do any more here
|
---|
364 | }
|
---|
365 |
|
---|
366 | // Add the requested structure information
|
---|
367 | Element base_node = createDocNode(doc_id); //, false, false);
|
---|
368 |
|
---|
369 | //Ancestors: continually add parent nodes until the root is reached
|
---|
370 | Element top_node = base_node; // the top node so far
|
---|
371 | if (want_ancestors) {
|
---|
372 | System.err.println("want ancestors");
|
---|
373 | String current_id = doc_id;
|
---|
374 | while (true) {
|
---|
375 | String parent_id = getParentId(current_id);
|
---|
376 | //Element parent = getParent(current_id);
|
---|
377 | if (parent_id == null)
|
---|
378 | break; // no parent
|
---|
379 | Element parent_node = createDocNode(parent_id);
|
---|
380 | parent_node.appendChild(top_node);
|
---|
381 | current_id = parent_id;//.getAttribute(GSXML.NODE_ID_ATT);
|
---|
382 | top_node = parent_node;
|
---|
383 | }
|
---|
384 | }
|
---|
385 | // Parent: get the parent of the selected node
|
---|
386 | else if (want_parent) {
|
---|
387 | String parent_id = getParentId(doc_id);
|
---|
388 | if (parent_id != null) {
|
---|
389 | Element parent_node = createDocNode(parent_id);
|
---|
390 | parent_node.appendChild(base_node);
|
---|
391 | top_node = parent_node;
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | // now the top node is the root of the structure
|
---|
396 | structure_elem.appendChild(top_node);
|
---|
397 |
|
---|
398 | //Siblings: get the other descendants of the selected node's parent
|
---|
399 | if (want_siblings) {
|
---|
400 |
|
---|
401 | Element parent_node = (Element)base_node.getParentNode(); // this may be the structure element if there has been no request for parents or ancestors
|
---|
402 | String parent_id = getParentId(doc_id);
|
---|
403 | // add siblings, - returns a pointer to the new current node
|
---|
404 | base_node = addSiblings(parent_node, parent_id, doc_id);
|
---|
405 | }
|
---|
406 |
|
---|
407 | // Children: get the descendants, but only one level deep
|
---|
408 | if (want_children) {
|
---|
409 | System.err.println("want children");
|
---|
410 | addDescendants(base_node, doc_id, false);
|
---|
411 | }
|
---|
412 | // Descendants: recursively get every descendant
|
---|
413 | else if (want_descendants) {
|
---|
414 | System.err.println("want descends");
|
---|
415 | addDescendants(base_node, doc_id, true);
|
---|
416 | }
|
---|
417 | } // if want structure
|
---|
418 | } // for each doc
|
---|
419 | return result;
|
---|
420 | }
|
---|
421 |
|
---|
422 | /** Retrieve the content of a document */
|
---|
423 | protected Element processDocumentContentRetrieve(Element request)
|
---|
424 | {
|
---|
425 | // Create a new (empty) result message
|
---|
426 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
427 | result.setAttribute(GSXML.FROM_ATT, DOCUMENT_CONTENT_RETRIEVE_SERVICE);
|
---|
428 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
429 |
|
---|
430 | if (!does_content) {
|
---|
431 | // shouldn't get here
|
---|
432 | return result;
|
---|
433 | }
|
---|
434 |
|
---|
435 | // Get the parameters of the request - no parameters at this stage
|
---|
436 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
437 | Element extid_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, EXTID_PARAM);
|
---|
438 | boolean external_id = false;
|
---|
439 | if (extid_param != null && GSXML.getValue(extid_param).equals("1")) {
|
---|
440 | external_id = true;
|
---|
441 | }
|
---|
442 | // Get the request content
|
---|
443 | Element query_doc_list = (Element) GSXML.getChildByTagName(request, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
444 | if (query_doc_list == null) {
|
---|
445 | System.err.println("Error: DocumentContentRetrieve request specified no doc nodes.\n");
|
---|
446 | return result;
|
---|
447 | }
|
---|
448 |
|
---|
449 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
450 | Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
451 | result.appendChild(doc_list);
|
---|
452 |
|
---|
453 | // set up the retrieval??
|
---|
454 |
|
---|
455 | // Get the documents
|
---|
456 | String[] doc_ids = GSXML.getAttributeValuesFromList(query_doc_list,
|
---|
457 | GSXML.NODE_ID_ATT);
|
---|
458 | for (int i = 0; i < doc_ids.length; i++) {
|
---|
459 | String doc_id = doc_ids[i];
|
---|
460 | // Create the document node
|
---|
461 | Element doc = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
462 | doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
|
---|
463 | doc_list.appendChild(doc);
|
---|
464 |
|
---|
465 | if (external_id) {
|
---|
466 | doc_id = translateExternalId(doc_id);
|
---|
467 | doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
|
---|
468 | } else if (idNeedsTranslating(doc_id)) {
|
---|
469 | doc_id = translateId(doc_id);
|
---|
470 | doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
|
---|
471 | }
|
---|
472 | if (doc_id == null) {
|
---|
473 | continue;
|
---|
474 | }
|
---|
475 |
|
---|
476 | Element node_content = getNodeContent(doc_id);
|
---|
477 | doc.appendChild(node_content);
|
---|
478 | }
|
---|
479 | return result;
|
---|
480 | }
|
---|
481 |
|
---|
482 | /** create an element to go into the structure. A node element
|
---|
483 | * has the form
|
---|
484 | * <docNode nodeId='xxx' nodeType='leaf' docType='hierarchy'/>
|
---|
485 | */
|
---|
486 | protected Element createDocNode(String node_id) {
|
---|
487 | Element node = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
488 | node.setAttribute(GSXML.NODE_ID_ATT, node_id);
|
---|
489 |
|
---|
490 | String doc_type = null;
|
---|
491 | if (default_document_type != null) {
|
---|
492 | doc_type = default_document_type;
|
---|
493 | } else {
|
---|
494 | doc_type = getDocType(node_id);
|
---|
495 | }
|
---|
496 | node.setAttribute(GSXML.DOC_TYPE_ATT, doc_type);
|
---|
497 | String node_type = getNodeType(node_id, doc_type);
|
---|
498 | node.setAttribute(GSXML.NODE_TYPE_ATT, node_type);
|
---|
499 | return node;
|
---|
500 | }
|
---|
501 |
|
---|
502 | /** adds all the children of doc_id the the doc element,
|
---|
503 | * and if recursive=true, adds all their children as well*/
|
---|
504 | protected void addDescendants(Element doc, String doc_id,
|
---|
505 | boolean recursive)
|
---|
506 | {
|
---|
507 | ArrayList child_ids = getChildrenIds(doc_id);
|
---|
508 | if (child_ids==null) return;
|
---|
509 | for (int i=0; i< child_ids.size(); i++) {
|
---|
510 | String child_id = (String)child_ids.get(i);
|
---|
511 | Element child_elem = createDocNode(child_id);
|
---|
512 | doc.appendChild(child_elem);
|
---|
513 | if (recursive && !child_elem.getAttribute(GSXML.NODE_TYPE_ATT).equals(GSXML.NODE_TYPE_LEAF)) {
|
---|
514 | addDescendants(child_elem, child_id, recursive);
|
---|
515 | }
|
---|
516 | }
|
---|
517 | }
|
---|
518 |
|
---|
519 | /** adds all the siblings of current_id to the parent element.
|
---|
520 | returns the new current element*/
|
---|
521 | protected Element addSiblings(Element parent_node, String parent_id,
|
---|
522 | String current_id) {
|
---|
523 | Element current_node = (Element)parent_node.getFirstChild();
|
---|
524 | if (current_node == null) {
|
---|
525 | // create a sensible error message
|
---|
526 | System.err.println("AbstractDocumentRetrieve Error: there should be a first child.");
|
---|
527 | return null;
|
---|
528 | }
|
---|
529 | // remove the current child,- will add it in later in its correct place
|
---|
530 | parent_node.removeChild(current_node);
|
---|
531 |
|
---|
532 | // add in all the siblings,
|
---|
533 | addDescendants(parent_node, parent_id, false);
|
---|
534 |
|
---|
535 | // find the node that is now the current node
|
---|
536 | // this assumes that the new node that was created is the same as
|
---|
537 | // the old one that was removed - we may want to replace the new one
|
---|
538 | // with the old one.
|
---|
539 | Element new_current = GSXML.getNamedElement(parent_node, current_node.getNodeName(), GSXML.NODE_ID_ATT, current_id);
|
---|
540 | return new_current;
|
---|
541 | }
|
---|
542 |
|
---|
543 | /** returns true if oid ends in
|
---|
544 | .fc (firstchild),
|
---|
545 | .lc (lastchild),
|
---|
546 | .pr (parent),
|
---|
547 | .ns (next sibling),
|
---|
548 | .ps (prev sibling),
|
---|
549 | .rt (root)
|
---|
550 | .ss (specified sibling),
|
---|
551 | false otherwise
|
---|
552 | */
|
---|
553 | protected boolean idNeedsTranslating(String id) {
|
---|
554 | String tail = id.substring(id.length()-3);
|
---|
555 | return (tail.equals(".fc") || tail.equals(".lc") ||
|
---|
556 | tail.equals(".pr") || tail.equals(".ns") ||
|
---|
557 | tail.equals(".ps") || tail.equals(".rt") ||
|
---|
558 | tail.equals(".ss"));
|
---|
559 |
|
---|
560 | }
|
---|
561 |
|
---|
562 | /** returns the list of sibling ids, including the specified node_id */
|
---|
563 | protected ArrayList getSiblingIds(String node_id) {
|
---|
564 | String parent_id = getParentId(node_id);
|
---|
565 | if (parent_id == null) {
|
---|
566 | return null;
|
---|
567 | }
|
---|
568 | return getChildrenIds(parent_id);
|
---|
569 |
|
---|
570 | }
|
---|
571 |
|
---|
572 | /** returns the node type of the specified node.
|
---|
573 | should be one of
|
---|
574 | GSXML.NODE_TYPE_LEAF,
|
---|
575 | GSXML.NODE_TYPE_INTERNAL,
|
---|
576 | GSXML.NODE_TYPE_ROOT
|
---|
577 | */
|
---|
578 | protected String getNodeType(String node_id, String doc_type) {
|
---|
579 | if (doc_type.equals(GSXML.DOC_TYPE_SIMPLE)) {
|
---|
580 | return GSXML.NODE_TYPE_LEAF;
|
---|
581 | }
|
---|
582 |
|
---|
583 | if (getParentId(node_id)==null) {
|
---|
584 | return GSXML.NODE_TYPE_ROOT;
|
---|
585 | }
|
---|
586 | if (doc_type.equals(GSXML.DOC_TYPE_PAGED)) {
|
---|
587 | return GSXML.NODE_TYPE_LEAF;
|
---|
588 | }
|
---|
589 | if (getChildrenIds(node_id)==null) {
|
---|
590 | return GSXML.NODE_TYPE_LEAF;
|
---|
591 | }
|
---|
592 | return GSXML.NODE_TYPE_INTERNAL;
|
---|
593 |
|
---|
594 | }
|
---|
595 |
|
---|
596 | /** if id ends in .fc, .pc etc, then translate it to the correct id */
|
---|
597 | abstract protected String translateId(String id);
|
---|
598 |
|
---|
599 | /** if an id is not a greenstone id (an external id) then translate
|
---|
600 | it to a greenstone one*/
|
---|
601 | abstract protected String translateExternalId(String id);
|
---|
602 | /** returns the document type of the doc that the specified node
|
---|
603 | belongs to. should be one of
|
---|
604 | GSXML.DOC_TYPE_SIMPLE,
|
---|
605 | GSXML.DOC_TYPE_PAGED,
|
---|
606 | GSXML.DOC_TYPE_HIERARCHY
|
---|
607 | */
|
---|
608 | abstract protected String getDocType(String node_id);
|
---|
609 |
|
---|
610 | /** returns the id of the root node of the document containing node node_id. . may be the same as node_id */
|
---|
611 | abstract protected String getRootId(String node_id);
|
---|
612 | /** returns a list of the child ids in order, null if no children */
|
---|
613 | abstract protected ArrayList getChildrenIds(String node_id);
|
---|
614 | /** returns the node id of the parent node, null if no parent */
|
---|
615 | abstract protected String getParentId(String node_id);
|
---|
616 |
|
---|
617 | /** get the metadata for the doc node doc_id
|
---|
618 | * returns a metadataList element:
|
---|
619 | * <metadataList><metadata name="xxx">value</metadata></metadataList>
|
---|
620 | */
|
---|
621 | abstract protected Element getMetadataList(String doc_id,
|
---|
622 | boolean all_metadata,
|
---|
623 | ArrayList metadata_names);
|
---|
624 | /** returns the content of a node
|
---|
625 | * shoudl return a nodeContent element:
|
---|
626 | * <nodeContent>text content or other elements</nodeContent>
|
---|
627 | */
|
---|
628 | abstract protected Element getNodeContent(String doc_id);
|
---|
629 |
|
---|
630 | /** returns the structural information asked for.
|
---|
631 | * info_type may be one of
|
---|
632 | * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS
|
---|
633 | */
|
---|
634 | abstract protected String getStructureInfo(String doc_id, String info_type);
|
---|
635 |
|
---|
636 | }
|
---|