1 | /*
|
---|
2 | * DocumentAction.java
|
---|
3 | * Copyright (C) 2002 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.action;
|
---|
20 |
|
---|
21 | // Greenstone classes
|
---|
22 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
23 | import org.greenstone.gsdl3.util.*;
|
---|
24 |
|
---|
25 | // XML classes
|
---|
26 | import org.w3c.dom.Document;
|
---|
27 | import org.w3c.dom.Element;
|
---|
28 | import org.w3c.dom.Node;
|
---|
29 | import org.w3c.dom.NodeList;
|
---|
30 |
|
---|
31 | // General Java classes
|
---|
32 | import java.util.HashMap;
|
---|
33 | import java.io.File;
|
---|
34 |
|
---|
35 |
|
---|
36 | /** Action class for retrieving Documents via the message router
|
---|
37 | */
|
---|
38 | public class DocumentAction extends Action {
|
---|
39 |
|
---|
40 | public Element process (Element message)
|
---|
41 | {
|
---|
42 | // for now, no subaction eventually we may want to have subactions such as text assoc or something ?
|
---|
43 |
|
---|
44 | // get the request - assume only one
|
---|
45 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
46 | Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
47 | HashMap params = GSXML.extractParams(cgi_paramList, false);
|
---|
48 |
|
---|
49 | String collection = (String) params.get(GSCGI.COLLECTION_ARG);
|
---|
50 | String to;
|
---|
51 | String document_name = (String) params.get(GSCGI.DOCUMENT_ARG);
|
---|
52 | if (document_name == null || document_name.equals("")) {
|
---|
53 | System.err.println("DocumentAction Error: no document specified!");
|
---|
54 | return null;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | // Build a request to obtain the document structure
|
---|
59 | Element ds_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
60 | Element ds_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
61 | ds_message.appendChild(ds_request);
|
---|
62 | to = GSPath.appendLink(collection, "DocumentStructureRetrieve"); // Hard-wired?
|
---|
63 | ds_request.setAttribute(GSXML.TO_ATT, to);
|
---|
64 | ds_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
65 | ds_request.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
66 |
|
---|
67 | // Create a parameter list to specify the required structure information
|
---|
68 | Element ds_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
69 | ds_request.appendChild(ds_param_list);
|
---|
70 | Element ds_param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
71 | ds_param_list.appendChild(ds_param);
|
---|
72 | ds_param.setAttribute(GSXML.NAME_ATT, "structure");
|
---|
73 | ds_param.setAttribute(GSXML.VALUE_ATT, "ancestors");
|
---|
74 | ds_param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
75 | ds_param_list.appendChild(ds_param);
|
---|
76 | ds_param.setAttribute(GSXML.NAME_ATT, "structure");
|
---|
77 | ds_param.setAttribute(GSXML.VALUE_ATT, "children");
|
---|
78 |
|
---|
79 | // create a doc_node_list and put in the doc_node that we are interested in
|
---|
80 | Element ds_doc_list = doc_.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
81 | ds_request.appendChild(ds_doc_list);
|
---|
82 | Element ds_doc = doc_.createElement(GSXML.DOC_NODE_ELEM);
|
---|
83 | ds_doc_list.appendChild(ds_doc);
|
---|
84 | ds_doc.setAttribute(GSXML.DOC_NODE_ID_ATT, document_name);
|
---|
85 |
|
---|
86 | // Process the document structure retrieve message
|
---|
87 | // System.out.println("(DocumentAction) Structure request:\n" + converter_.getString(ds_message));
|
---|
88 | Element ds_response_message = (Element) mr_.process(ds_message);
|
---|
89 | // System.out.println("(DocumentAction) Structure response:\n" + converter_.getString(ds_response_message));
|
---|
90 |
|
---|
91 | // get the doc_node bit
|
---|
92 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
93 | path = GSPath.appendLink(path, GSXML.DOC_NODE_ELEM);
|
---|
94 | path = GSPath.appendLink(path, GSXML.DOC_NODE_STRUCTURE_ELEM);
|
---|
95 | Element ds_response_structure = (Element) GSXML.getNodeByPath(ds_response_message, path);
|
---|
96 |
|
---|
97 | // the_document is where all the extra info is added into, to be returned in the page
|
---|
98 | Element the_document = doc_.createElement(GSXML.DOCUMENT_ELEM);
|
---|
99 | // add the contents of the structure bit into the_document
|
---|
100 | NodeList structs = ds_response_structure.getChildNodes();
|
---|
101 | for (int i=0; i<structs.getLength();i++) {
|
---|
102 | the_document.appendChild(doc_.importNode(structs.item(i), true));
|
---|
103 | }
|
---|
104 | //Element ds_response = (Element) GSXML.getChildByTagName(ds_response_message, GSXML.RESPONSE_ELEM);
|
---|
105 | //Element ds_response_doc_list = (Element) GSXML.getChildByTagName(ds_response, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
106 | //Element ds_response_document = (Element) GSXML.getChildByTagName(ds_response_doc_list, GSXML.DOC_NODE_ELEM);
|
---|
107 |
|
---|
108 | // Detach the document from the list, remove the list, and add the document back
|
---|
109 | // ds_response is the re
|
---|
110 | //ds_response.removeChild(ds_response_doc_list);
|
---|
111 | //ds_response.appendChild(ds_response_document);
|
---|
112 |
|
---|
113 |
|
---|
114 | // Build a request to obtain some document metadata
|
---|
115 | Element dm_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
116 | Element dm_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
117 | dm_message.appendChild(dm_request);
|
---|
118 | to = GSPath.appendLink(collection, "DocumentMetadataRetrieve"); // Hard-wired?
|
---|
119 | dm_request.setAttribute(GSXML.TO_ATT, to);
|
---|
120 | dm_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
121 | dm_request.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
122 |
|
---|
123 | // Create a parameter list to specify the required metadata information
|
---|
124 | // for now get Title
|
---|
125 | Element dm_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
126 | dm_request.appendChild(dm_param_list);
|
---|
127 | Element dm_param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
128 | dm_param_list.appendChild(dm_param);
|
---|
129 | dm_param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
130 | dm_param.setAttribute(GSXML.VALUE_ATT, "Title");
|
---|
131 |
|
---|
132 | // create the doc node list for the metadata request
|
---|
133 | Element dm_doc_list = doc_.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
134 | dm_request.appendChild(dm_doc_list);
|
---|
135 |
|
---|
136 | // Add each node from the structure response into the metadata request
|
---|
137 | NodeList doc_nodes = the_document.getElementsByTagName(GSXML.DOC_NODE_ELEM);
|
---|
138 | for (int i = 0; i < doc_nodes.getLength(); i++) {
|
---|
139 | Element doc_node = (Element) doc_nodes.item(i);
|
---|
140 | String doc_node_id = doc_node.getAttribute(GSXML.DOC_NODE_ID_ATT);
|
---|
141 |
|
---|
142 | // Add the documentNode to the list
|
---|
143 | Element dm_doc_node = doc_.createElement(GSXML.DOC_NODE_ELEM);
|
---|
144 | dm_doc_list.appendChild(dm_doc_node);
|
---|
145 | dm_doc_node.setAttribute(GSXML.DOC_NODE_ID_ATT, doc_node_id);
|
---|
146 | dm_doc_node.setAttribute(GSXML.DOC_NODE_TYPE_ATT,
|
---|
147 | doc_node.getAttribute(GSXML.DOC_NODE_TYPE_ATT));
|
---|
148 | }
|
---|
149 |
|
---|
150 | // System.out.println("(DocumentAction) Metadata request:\n" + converter_.getString(dm_request));
|
---|
151 | Element dm_response_message = (Element) mr_.process(dm_message);
|
---|
152 | // System.out.println("(DocumentAction) Metadata response:\n" + converter_.getString(dm_response));
|
---|
153 |
|
---|
154 | path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
155 | Element dm_response_doc_list = (Element) GSXML.getNodeByPath(dm_response_message, path);
|
---|
156 |
|
---|
157 | //Element dm_response_content = (Element) GSXML.getNodeByPath(dm_response, "response/content");
|
---|
158 | //Element dm_response_doc_list = (Element) GSXML.getNodeByPath(dm_response_content, "documentList");
|
---|
159 |
|
---|
160 | // Merge the metadata with the structure information
|
---|
161 | NodeList dm_response_docs = dm_response_doc_list.getChildNodes();
|
---|
162 | for (int i = 0; i < doc_nodes.getLength(); i++) {
|
---|
163 | GSXML.mergeMetadataLists(doc_nodes.item(i), dm_response_docs.item(i));
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | // Build a request to obtain some document content
|
---|
168 | Element dc_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
169 | Element dc_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
170 | dc_message.appendChild(dc_request);
|
---|
171 | to = GSPath.appendLink(collection, "DocumentContentRetrieve"); // Hard-wired?
|
---|
172 | dc_request.setAttribute(GSXML.TO_ATT, to);
|
---|
173 | dc_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
174 | dc_request.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
175 |
|
---|
176 | // Create a parameter list to specify the request parameters - empty for now
|
---|
177 | Element dc_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
178 | dc_request.appendChild(dc_param_list);
|
---|
179 |
|
---|
180 | // the doc list for the content request is the same as the one for the structure request
|
---|
181 | dc_request.appendChild(ds_doc_list);
|
---|
182 | // Element dc_doc_list = doc_.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
183 | // dc_request.appendChild(dc_doc_list);
|
---|
184 | // Element dc_doc = doc_.createElement(GSXML.DOC_NODE_ELEM);
|
---|
185 | // dc_doc_list.appendChild(dc_doc);
|
---|
186 | // dc_doc.setAttribute(GSXML.DOC_NODE_ID, document_name);
|
---|
187 |
|
---|
188 | // System.out.println("(DocumentAction) Content request:\n" + converter_.getString(dc_request));
|
---|
189 | Element dc_response_message = (Element) mr_.process(dc_message);
|
---|
190 | // System.out.println("(DocumentAction) Content response:\n" + converter_.getString(dc_response));
|
---|
191 |
|
---|
192 | path = GSPath.appendLink(path, GSXML.DOC_NODE_ELEM);
|
---|
193 | path = GSPath.appendLink(path, GSXML.DOC_NODE_CONTENT_ELEM);
|
---|
194 | Element dc_response_doc_content = (Element) GSXML.getNodeByPath(dc_response_message, path);
|
---|
195 |
|
---|
196 | // Merge the document content with the metadata and structure information
|
---|
197 | for (int i = 0; i < doc_nodes.getLength(); i++) {
|
---|
198 | Node doc_node = doc_nodes.item(i);
|
---|
199 | Node doc_node_id = doc_node.getAttributes().getNamedItem(GSXML.DOC_NODE_ID_ATT);
|
---|
200 | String dc_doc_id = doc_node_id.getNodeValue();
|
---|
201 | if (dc_doc_id == document_name) {
|
---|
202 | doc_node.appendChild(doc_.importNode(dc_response_doc_content, true));
|
---|
203 | break;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | // Create the return page tree
|
---|
208 | Element page = doc_.createElement(GSXML.PAGE_ELEM);
|
---|
209 | page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
210 |
|
---|
211 | // Add the display element, config stuff, and original request from the message
|
---|
212 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
|
---|
213 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
|
---|
214 | page.appendChild(doc_.importNode(request, true));
|
---|
215 |
|
---|
216 | // Add the results of the action to the page
|
---|
217 | Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
218 | page.appendChild(response);
|
---|
219 | response.appendChild(the_document);
|
---|
220 |
|
---|
221 | // System.out.println("Finished page: " + converter_.getString(page));
|
---|
222 |
|
---|
223 |
|
---|
224 | // Finally, process using the stylesheet, and return
|
---|
225 | String stylesheet = GSFile.stylesheetFile(config_, "document.xsl");
|
---|
226 | if (stylesheet == null) {
|
---|
227 | System.err.println("DocumentAction Error: document stylesheet not found!");
|
---|
228 | return null;
|
---|
229 | }
|
---|
230 |
|
---|
231 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
232 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
233 | return (Element) transformer_.transform(style_doc, page);
|
---|
234 | }
|
---|
235 | }
|
---|