Changeset 3801
- Timestamp:
- 2003-03-05T14:04:09+13:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/action/DocumentAction.java
r3645 r3801 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 */ 1 19 package org.greenstone.gsdl3.action; 2 20 21 // Greenstone classes 3 22 import org.greenstone.gsdl3.core.ModuleInterface; 4 23 import org.greenstone.gsdl3.util.*; 24 5 25 // XML classes 6 import org.w3c.dom.Node;7 26 import org.w3c.dom.Document; 8 27 import org.w3c.dom.Element; 9 28 import org.w3c.dom.Node; 29 import org.w3c.dom.NodeList; 30 31 // General Java classes 10 32 import java.util.HashMap; 11 33 import java.io.File; 34 12 35 13 36 /** Action class for retrieving Documents via the message router … … 15 38 public class DocumentAction extends Action { 16 39 17 public Element process (Element message) { 18 19 // find the stylesheet 20 String stylesheet = GSFile.stylesheetFile(config_, "document.xsl"); 21 22 if (stylesheet==null) { 23 System.err.println("DocumentAction Error: document stylesheet not found!"); 24 return null; 25 } 26 40 public Element process (Element message) 41 { 27 42 // for now, no subaction eventually we may want to have subactions such as text assoc or something ? 28 43 … … 32 47 HashMap params = GSXML.extractParams(cgi_paramList); 33 48 34 String document_name = (String)params.get(GSCGI.DOCUMENT_ARG); 49 String collection = (String) params.get(GSCGI.COLLECTION_ARG); 50 String to; 51 String document_name = (String) params.get(GSCGI.DOCUMENT_ARG); 52 System.out.println("Document name: " + document_name); 35 53 if (document_name == null || document_name.equals("")) { 36 54 System.err.println("DocumentAction Error: no document specified!"); 37 55 return null; 38 56 } 39 40 // create the return page tree 57 58 59 // Build a request to obtain the document structure 60 Element ds_message = doc_.createElement(GSXML.MESSAGE_ELEM); 61 Element ds_request = doc_.createElement(GSXML.REQUEST_ELEM); 62 ds_message.appendChild(ds_request); 63 to = GSPath.appendLink(collection, "DocumentStructureRetrieve"); // Hard-wired? 64 ds_request.setAttribute(GSXML.TO_ATT, to); 65 ds_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS); 66 ds_request.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT)); 67 68 // Create a parameter list to specify the required structure information 69 Element ds_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 70 ds_request.appendChild(ds_param_list); 71 Element ds_param = doc_.createElement(GSXML.PARAM_ELEM); 72 ds_param_list.appendChild(ds_param); 73 ds_param.setAttribute(GSXML.NAME_ATT, "structure"); 74 ds_param.setAttribute(GSXML.VALUE_ATT, "ancestors"); 75 ds_param = doc_.createElement(GSXML.PARAM_ELEM); 76 ds_param_list.appendChild(ds_param); 77 ds_param.setAttribute(GSXML.NAME_ATT, "structure"); 78 ds_param.setAttribute(GSXML.VALUE_ATT, "children"); 79 80 // Create a content section to store the list of documents we are interested in 81 Element ds_content = doc_.createElement(GSXML.CONTENT_ELEM); 82 ds_request.appendChild(ds_content); 83 Element ds_doc_list = doc_.createElement(GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER); 84 ds_content.appendChild(ds_doc_list); 85 Element ds_doc = doc_.createElement(GSXML.DOCUMENT_ELEM); 86 ds_doc_list.appendChild(ds_doc); 87 ds_doc.setAttribute(GSXML.NAME_ATT, document_name); 88 89 // Process the document structure retrieve message 90 System.out.println("(DocumentAction) Structure request:\n" + converter_.getString(ds_message)); 91 Element ds_response = (Element) mr_.process(ds_message); 92 System.out.println("(DocumentAction) Structure response:\n" + converter_.getString(ds_response)); 93 94 Element ds_response_content = (Element) GSXML.getNodeByPath(ds_response, "response/content"); 95 Element ds_response_doc_list = (Element) GSXML.getNodeByPath(ds_response_content, "documentList"); 96 Element ds_response_document = (Element) GSXML.getNodeByPath(ds_response_doc_list, "document"); 97 98 // Detach the document from the list, remove the list, and add the document back 99 ds_response_content.removeChild(ds_response_doc_list); 100 ds_response_content.appendChild(ds_response_document); 101 102 103 // Build a request to obtain some document metadata 104 Element dm_message = doc_.createElement(GSXML.MESSAGE_ELEM); 105 Element dm_request = doc_.createElement(GSXML.REQUEST_ELEM); 106 dm_message.appendChild(dm_request); 107 to = GSPath.appendLink(collection, "DocumentMetadataRetrieve"); // Hard-wired? 108 dm_request.setAttribute(GSXML.TO_ATT, to); 109 dm_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS); 110 dm_request.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT)); 111 112 // Create a parameter list to specify the required metadata information 113 Element dm_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 114 dm_request.appendChild(dm_param_list); 115 Element dm_param = doc_.createElement(GSXML.PARAM_ELEM); 116 dm_param_list.appendChild(dm_param); 117 dm_param.setAttribute(GSXML.NAME_ATT, "metadata"); 118 dm_param.setAttribute(GSXML.VALUE_ATT, "Title"); 119 120 // Create a content section to store the list of documents we are interested in 121 Element dm_content = doc_.createElement(GSXML.CONTENT_ELEM); 122 dm_request.appendChild(dm_content); 123 Element dm_doc_list = doc_.createElement(GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER); 124 dm_content.appendChild(dm_doc_list); 125 126 // Get the title metadata of the document nodes 127 NodeList doc_nodes = ds_response_document.getElementsByTagName(GSXML.DOC_NODE_ELEM); 128 for (int i = 0; i < doc_nodes.getLength(); i++) { 129 Node doc_node = doc_nodes.item(i); 130 Node doc_node_id = doc_node.getAttributes().getNamedItem(GSXML.DOC_NODE_ID_ATT); 131 String dm_doc_id = doc_node_id.getNodeValue(); 132 dm_doc_list.appendChild(GSXML.createDocumentElement(doc_, dm_doc_id)); 133 } 134 135 System.out.println("(DocumentAction) Metadata request:\n" + converter_.getString(dm_request)); 136 Element dm_response = (Element) mr_.process(dm_message); 137 System.out.println("(DocumentAction) Metadata response:\n" + converter_.getString(dm_response)); 138 139 Element dm_response_content = (Element) GSXML.getNodeByPath(dm_response, "response/content"); 140 Element dm_response_doc_list = (Element) GSXML.getNodeByPath(dm_response_content, "documentList"); 141 142 // Merge the metadata with the structure information 143 NodeList dm_response_docs = dm_response_doc_list.getChildNodes(); 144 for (int i = 0; i < doc_nodes.getLength(); i++) { 145 GSXML.mergeMetadataLists(doc_nodes.item(i), dm_response_docs.item(i)); 146 } 147 148 149 // Build a request to obtain some document content 150 Element dc_message = doc_.createElement(GSXML.MESSAGE_ELEM); 151 Element dc_request = doc_.createElement(GSXML.REQUEST_ELEM); 152 dc_message.appendChild(dc_request); 153 to = GSPath.appendLink(collection, "DocumentContentRetrieve"); // Hard-wired? 154 dc_request.setAttribute(GSXML.TO_ATT, to); 155 dc_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS); 156 dc_request.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT)); 157 158 // Create a parameter list to specify the request parameters 159 Element dc_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 160 dc_request.appendChild(dc_param_list); 161 162 // Create a content section to store the list of documents we are interested in 163 Element dc_content = doc_.createElement(GSXML.CONTENT_ELEM); 164 dc_request.appendChild(dc_content); 165 Element dc_doc_list = doc_.createElement(GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER); 166 dc_content.appendChild(dc_doc_list); 167 Element dc_doc = doc_.createElement(GSXML.DOCUMENT_ELEM); 168 dc_doc_list.appendChild(dc_doc); 169 dc_doc.setAttribute(GSXML.NAME_ATT, document_name); 170 171 System.out.println("(DocumentAction) Content request:\n" + converter_.getString(dc_request)); 172 Element dc_response = (Element) mr_.process(dc_message); 173 System.out.println("(DocumentAction) Content response:\n" + converter_.getString(dc_response)); 174 175 Element dc_response_doc_content = (Element) GSXML.getNodeByPath(dc_response, "response/content/documentList/document/content"); 176 177 // Merge the document content with the metadata and structure information 178 for (int i = 0; i < doc_nodes.getLength(); i++) { 179 Node doc_node = doc_nodes.item(i); 180 Node doc_node_id = doc_node.getAttributes().getNamedItem(GSXML.DOC_NODE_ID_ATT); 181 String dc_doc_id = doc_node_id.getNodeValue(); 182 if (dc_doc_id == document_name) { 183 doc_node.appendChild(dc_response_doc_content); 184 break; 185 } 186 } 187 188 189 // Create the return page tree 41 190 Element page = doc_.createElement(GSXML.PAGE_ELEM); 42 191 page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT)); 43 // add the lang stuff from message 192 193 // Add the display element, config stuff, and original request from the message 44 194 page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true)); 45 // add the config stuff from message46 195 page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true)); 47 48 // build up the mr request49 Element mr_message = doc_.createElement(GSXML.MESSAGE_ELEM);50 Element mr_request = doc_.createElement(GSXML.REQUEST_ELEM);51 mr_message.appendChild(mr_request);52 mr_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);53 mr_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));54 String to = (String)params.get(GSCGI.COLLECTION_ARG); // collection name55 to = GSPath.appendLink(to, "DocumentRetrieve");56 mr_request.setAttribute(GSXML.TO_ATT, to);57 58 // all query requests must have a paramList and a content59 Element paramList = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);60 mr_request.appendChild(paramList);61 62 Element content = doc_.createElement(GSXML.CONTENT_ELEM);63 mr_request.appendChild(content);64 65 Element document_list = doc_.createElement(GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER);66 Element document = doc_.createElement(GSXML.DOCUMENT_ELEM);67 document.setAttribute(GSXML.NAME_ATT, document_name);68 document_list.appendChild(document);69 content.appendChild(document_list);70 71 Element mr_result = (Element)mr_.process(mr_message);72 73 // add the results to the page74 196 page.appendChild(doc_.importNode(request, true)); 75 page.appendChild(doc_.importNode(GSXML.getChildByTagName(mr_result, GSXML.RESPONSE_ELEM), true)); 76 77 // process using the stylesheet 197 198 // Add the results of the action to the page 199 Element response = doc_.createElement(GSXML.RESPONSE_ELEM); 200 page.appendChild(response); 201 response.appendChild(doc_.importNode(ds_response_content, true)); 202 203 System.out.println("Finished page: " + converter_.getString(page)); 204 205 206 // Finally, process using the stylesheet, and return 207 String stylesheet = GSFile.stylesheetFile(config_, "document.xsl"); 208 if (stylesheet == null) { 209 System.err.println("DocumentAction Error: document stylesheet not found!"); 210 return null; 211 } 212 78 213 Document style_doc = converter_.getDOM(new File(stylesheet)); 79 214 GSXSLT.absoluteIncludePaths(style_doc, config_); 80 return (Element)transformer_.transform(style_doc, page); 81 215 return (Element) transformer_.transform(style_doc, page); 82 216 } 83 84 85 217 }
Note:
See TracChangeset
for help on using the changeset viewer.