source: trunk/gsdl3/src/java/org/greenstone/gsdl3/action/DocumentAction.java@ 3913

Last change on this file since 3913 was 3896, checked in by kjdon, 21 years ago

added sib arg - if set to 1, get the siblings of a selected node. also tidied up the code a bit

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