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