source: greenstone3/trunk/src/java/org/greenstone/gsdl3/action/XMLDocumentAction.java@ 16688

Last change on this file since 16688 was 16688, checked in by davidb, 16 years ago

Changed 'Element process(Element)' in ModuleInterface to 'Node process(Node)'. After some deliberation is was decided this is a more useful (generic) layer of the DOM to pass information around in. Helps with the DocType problem when producing XSL Transformed pages, for example. When this was an Element, it would loose track of its DocType. Supporting method provided in XMLConverter 'Element nodeToElement(Node)' which checks a nodes docType and casts to Element if appropriate, or if a Document, typecasts to that and then extracts the top-level Element. With this fundamental change in ModuleInterface, around 20 files needed to be updated (Actions, Services, etc) that build on top of 'process()' to reflect this change, and use nodeToElement where necessary.

  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1package org.greenstone.gsdl3.action;
2
3import org.greenstone.gsdl3.core.ModuleInterface;
4import org.greenstone.gsdl3.util.*;
5import org.greenstone.gsdl3.service.TEIRetrieve;
6// XML classes
7import org.w3c.dom.Node;
8import org.w3c.dom.NodeList;
9import org.w3c.dom.Text;
10import org.w3c.dom.Document;
11import org.w3c.dom.Element;
12
13import java.util.HashMap;
14import java.util.HashSet;
15import java.util.Vector;
16import java.util.Map;
17import java.util.Iterator;
18import java.io.File;
19
20/** action class for retrieving parts of XML documents */
21public class XMLDocumentAction extends Action {
22
23
24 /** process - processes a request.
25 */
26 public Node process (Node message_node) {
27
28 Element message = this.converter.nodeToElement(message_node);
29
30 // get the request - assume there is only one
31 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
32
33 // create the return message
34 Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
35 Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
36 result.appendChild(page_response);
37
38 // extract the params from the cgi-request, and check that we have a coll specified
39 Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
40 HashMap params = GSXML.extractParams(cgi_param_list, false);
41
42 String collection = (String)params.get(GSParams.COLLECTION);
43 if (collection == null || collection.equals("")) {
44 return result;
45 }
46 String doc_name = (String) params.get(GSParams.DOCUMENT);
47 if (doc_name == null || doc_name.equals("")) {
48 return result;
49 }
50 String lang = request.getAttribute(GSXML.LANG_ATT);
51 String uid = request.getAttribute(GSXML.USER_ID_ATT);
52
53 // subaction used to decide if we are returning content or structure
54 String document_mode = request.getAttribute(GSXML.SUBACTION_ATT);
55 String to = null;
56 if (document_mode.equals("text")) {
57 to = GSPath.appendLink(collection, "DocumentContentRetrieve");
58 } else if (document_mode.equals("toc")) {
59 to = GSPath.appendLink(collection, "DocumentStructureRetrieve");
60 } else { // the default is text
61 to = GSPath.appendLink(collection, "DocumentContentRetrieve");
62 }
63
64 // make the request to the collection
65 Element mr_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
66
67 Element ret_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid);
68 mr_message.appendChild(ret_request);
69
70 Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
71 ret_request.appendChild(doc_list);
72 Element doc = this.doc.createElement(GSXML.DOC_NODE_ELEM);
73 doc.setAttribute(GSXML.NODE_ID_ATT, doc_name);
74 doc_list.appendChild(doc);
75
76 // also add in a request for the Title metadata
77 to = GSPath.appendLink(collection, "DocumentMetadataRetrieve");
78 Element meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid);
79 // copy the doc list
80 meta_request.appendChild(doc_list.cloneNode(true));
81 // add in a metadata param
82 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
83 meta_request.appendChild(param_list);
84 Element param = GSXML.createParameter(this.doc, "metadata", "root_Title");
85 param_list.appendChild(param);
86
87 // add the request to the message
88 mr_message.appendChild(meta_request);
89
90 Element ret_response = (Element)this.mr.process(mr_message);
91 String [] links = {GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER, GSXML.DOC_NODE_ELEM};
92 String path = GSPath.createPath(links);
93 Element doc_node = (Element)this.doc.importNode(GSXML.getNodeByPath(ret_response, path), true);
94 page_response.appendChild(doc_node);
95
96 // get the metadata list
97 Element meta_response = (Element)ret_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1);
98 String [] mlinks = {GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER, GSXML.DOC_NODE_ELEM, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER};
99 path = GSPath.createPath(mlinks);
100
101 Element meta_list = (Element)GSXML.getNodeByPath(meta_response, path);
102 if (meta_list != null) {
103 doc_node.appendChild(this.doc.importNode(meta_list, true));
104 }
105 return result;
106 }
107
108}
Note: See TracBrowser for help on using the repository browser.