[3621] | 1 | /*
|
---|
| 2 | * XSLTServices.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.service;
|
---|
| 20 |
|
---|
| 21 | import org.greenstone.gsdl3.util.*;
|
---|
| 22 | import org.greenstone.gsdl3.selfContained.*;
|
---|
| 23 |
|
---|
| 24 | import javax.xml.transform.Source;
|
---|
| 25 | import javax.xml.transform.Transformer;
|
---|
| 26 | import javax.xml.transform.TransformerFactory;
|
---|
| 27 | import javax.xml.transform.dom.DOMSource;
|
---|
| 28 | import javax.xml.transform.stream.StreamResult;
|
---|
| 29 | import org.apache.xerces.dom.DocumentImpl;
|
---|
| 30 | import org.apache.xerces.dom.ElementImpl;
|
---|
| 31 | import org.apache.xerces.dom.TextImpl;
|
---|
| 32 | import org.w3c.dom.Document;
|
---|
| 33 | import org.w3c.dom.Node;
|
---|
| 34 | import org.w3c.dom.Text;
|
---|
| 35 | import org.w3c.dom.Element;
|
---|
| 36 | import org.w3c.dom.NodeList;
|
---|
| 37 |
|
---|
| 38 | import java.util.HashMap;
|
---|
| 39 | import java.util.Vector;
|
---|
| 40 | import java.util.Set;
|
---|
| 41 | import java.util.Map;
|
---|
| 42 | import java.util.Iterator;
|
---|
| 43 | import java.util.Locale;
|
---|
| 44 | import java.io.*;
|
---|
| 45 | import java.net.URLEncoder;
|
---|
| 46 | import java.net.URLDecoder;
|
---|
| 47 |
|
---|
[13124] | 48 | import org.apache.log4j.*;
|
---|
| 49 |
|
---|
[3621] | 50 | /**
|
---|
[5663] | 51 | * A ServiceRack class for XSLT query.
|
---|
[3621] | 52 | *
|
---|
| 53 | * Document ids are formed by encoding the document using standard URLEncoding
|
---|
| 54 | *
|
---|
[28966] | 55 | * @author Stuart Yeates
|
---|
[5663] | 56 | * @see ServiceRack
|
---|
| 57 | * @see java.net.URLEncoder
|
---|
| 58 | * @see java.net.URLDecoder
|
---|
[3621] | 59 | */
|
---|
| 60 |
|
---|
| 61 | public class XSLTServices
|
---|
[3648] | 62 | extends ServiceRack {
|
---|
[3621] | 63 |
|
---|
[13270] | 64 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.XSLTServices.class.getName());
|
---|
[13124] | 65 |
|
---|
[3621] | 66 | // these strings must match what is found in the properties file
|
---|
| 67 | // the services on offer
|
---|
| 68 | private static final String XSLT_QUERY_SERVICE = "XSLTQuery";
|
---|
| 69 | private static final String RESOURCE_RETRIEVE_SERVICE = "ResourceRetrieve";
|
---|
| 70 | // params used
|
---|
| 71 | private static final String QUERY_PARAM = "query";
|
---|
| 72 | private static final String ALPHABET_PARAM = "alphabet";
|
---|
| 73 |
|
---|
| 74 | private static final int RESULT_SET_SIZE = 10;
|
---|
| 75 |
|
---|
| 76 | // alphabets
|
---|
| 77 | private static final String MINIMUM_ALPHABET = "minimum";
|
---|
| 78 | private static final String ASCII_ALPHABET = "ascii";
|
---|
| 79 | private static final String UNICODE_ALPHABET = "unicode";
|
---|
| 80 |
|
---|
| 81 | private static final String DEFAULT_QUERY_STRING =
|
---|
| 82 | "<xsl:stylesheet version=\"1.0\""+
|
---|
| 83 | " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"+
|
---|
| 84 | " <xsl:template match=\"*|/\">"+
|
---|
| 85 | " <xsl:apply-templates/>"+
|
---|
| 86 | " </xsl:template>"+
|
---|
| 87 | " <xsl:template match=\"*|/\" mode=\"m\">"+
|
---|
| 88 | " <xsl:apply-templates mode=\"m\"/>"+
|
---|
| 89 | " </xsl:template>"+
|
---|
| 90 | " <xsl:template match=\"text()|@*\">"+
|
---|
| 91 | " <xsl:value-of select=\".\"/>"+
|
---|
| 92 | " </xsl:template>"+
|
---|
| 93 | " <xsl:template match=\"processing-instruction()|comment()\"/>"+
|
---|
| 94 | "</xsl:stylesheet>";
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 | private String alphabet = MINIMUM_ALPHABET;
|
---|
| 98 | private Element config_info_ = null;
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | public XSLTServices() {
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[3851] | 104 | /** configure this service */
|
---|
[3937] | 105 | public boolean configure(Element info, Element extra_info) {
|
---|
[10093] | 106 | if (!super.configure(info, extra_info)){
|
---|
| 107 | return false;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[13124] | 110 | logger.info("configuring XSLTServices");
|
---|
[5098] | 111 | this.config_info = info;
|
---|
[3623] | 112 | try {
|
---|
[13124] | 113 | logger.info("called with:");
|
---|
[3623] | 114 | TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
---|
[14225] | 115 | Element doc = GSXML.getFirstElementChild(info);//(Element)info.getFirstChild();
|
---|
[3623] | 116 | Transformer transformer = transformerFactory.newTransformer();
|
---|
| 117 | StreamResult result = new StreamResult(System.out);
|
---|
| 118 | Source source = new DOMSource(doc);
|
---|
| 119 | transformer.transform(source,result);
|
---|
| 120 | } catch (Throwable t) {
|
---|
[13124] | 121 | logger.error("Error printing XML in XSLTService::configure()");
|
---|
[3623] | 122 | }
|
---|
[3621] | 123 | return true;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[28966] | 126 | protected Element getServiceDescription(Document doc, String service, String lang, String subset) {
|
---|
[4903] | 127 |
|
---|
| 128 | return null;
|
---|
| 129 | }
|
---|
[3621] | 130 |
|
---|
| 131 | /** process method for specific services - must be implemented by all
|
---|
| 132 | * subclasses
|
---|
| 133 | * should implement all services supported by the servicesImpl except
|
---|
| 134 | * for describe, which is implemented by this base class
|
---|
| 135 | *
|
---|
| 136 | */
|
---|
| 137 | protected Element processService(String service, Element request){
|
---|
| 138 | if (service.equals(XSLT_QUERY_SERVICE)) {
|
---|
| 139 | return processXSLTQuery(request);
|
---|
| 140 | } else if (service.equals(RESOURCE_RETRIEVE_SERVICE)) {
|
---|
| 141 | return processResourceRetrieve(request);
|
---|
| 142 | }
|
---|
| 143 | // create the response so we can report the error
|
---|
[28966] | 144 | Document result_doc = XMLConverter.newDOM();
|
---|
| 145 | Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[5098] | 146 | String from = GSPath.appendLink(this.cluster_name, RESOURCE_RETRIEVE_SERVICE);
|
---|
[3621] | 147 | response.setAttribute(GSXML.FROM_ATT, from);
|
---|
[6273] | 148 | response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
[3621] | 149 |
|
---|
[13124] | 150 | logger.error("should never get here. service type wrong:"+service);
|
---|
[28966] | 151 | GSXML.addError(response,"XSLTServices:should never get here. service type wrong:"+service);
|
---|
[3621] | 152 | return response;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /** process a document resquest query */
|
---|
| 156 | protected Element processResourceRetrieve(Element request) {
|
---|
[28966] | 157 | Document result_doc = XMLConverter.newDOM();
|
---|
[3621] | 158 | // create the result and set the path so we know where we are
|
---|
[28966] | 159 | Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[5098] | 160 | String from = GSPath.appendLink(this.cluster_name, RESOURCE_RETRIEVE_SERVICE);
|
---|
[3621] | 161 | response.setAttribute(GSXML.FROM_ATT, from);
|
---|
[6273] | 162 | response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
[3621] | 163 |
|
---|
[3945] | 164 | // get param list
|
---|
[3621] | 165 | Element param_elem=null;
|
---|
| 166 | Node n = request.getFirstChild();
|
---|
| 167 | while (n!=null) {
|
---|
[3945] | 168 | String node_name = n.getNodeName();
|
---|
| 169 | if (node_name.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
| 170 | param_elem = (Element)n;
|
---|
| 171 | break;
|
---|
| 172 | }
|
---|
| 173 | n = n.getNextSibling();
|
---|
[3621] | 174 | }
|
---|
| 175 |
|
---|
[3945] | 176 | if (param_elem==null) {
|
---|
[13124] | 177 | logger.error("bad query request");
|
---|
[28966] | 178 | GSXML.addError(response,"bad query request in XSLTServices");
|
---|
[3945] | 179 | return response;
|
---|
[3621] | 180 | }
|
---|
| 181 |
|
---|
| 182 | // Documents are just the ids decoding using standard URL decoding
|
---|
| 183 |
|
---|
[3945] | 184 | Element doc_list = (Element)GSXML.getChildByTagName(request, GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER);
|
---|
[3868] | 185 | String []ids = GSXML.getAttributeValuesFromList(doc_list, GSXML.NAME_ATT);
|
---|
[3621] | 186 | for (int j=0; j<ids.length; j++) {
|
---|
[3623] | 187 | String document = null;
|
---|
| 188 | try {
|
---|
| 189 | document = URLDecoder.decode(ids[j],"UTF-8");
|
---|
| 190 | } catch (UnsupportedEncodingException e) {
|
---|
| 191 | throw new Error(e.toString());
|
---|
| 192 | }
|
---|
[3621] | 193 | // something funny with the doc -
|
---|
[28966] | 194 | Element new_doc = result_doc.createElement(GSXML.DOCUMENT_ELEM);
|
---|
[28970] | 195 | new_doc.setAttribute(GSXML.NAME_ATT, ids[j]);
|
---|
[28966] | 196 | GSXML.addDocText(new_doc, document);
|
---|
[3621] | 197 | response.appendChild(new_doc);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | return response;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | /** process a XSLT query */
|
---|
| 204 | protected Element processXSLTQuery(Element request) {
|
---|
[28966] | 205 | Document result_doc = XMLConverter.newDOM();
|
---|
[3621] | 206 | // create the result and set the path so we know where we are
|
---|
[28966] | 207 | Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[5098] | 208 | String from = GSPath.appendLink(this.cluster_name, XSLT_QUERY_SERVICE);
|
---|
[3621] | 209 | response.setAttribute(GSXML.FROM_ATT, from);
|
---|
[6273] | 210 | response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
[3621] | 211 |
|
---|
| 212 | // get the parameter list
|
---|
| 213 | Element param_list =
|
---|
| 214 | (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 215 |
|
---|
| 216 | // extract the only parameter we care about
|
---|
[25635] | 217 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[3621] | 218 | String query = (String)params.get(QUERY_PARAM);
|
---|
| 219 |
|
---|
[4012] | 220 | if (query == null || query.equals(""))
|
---|
[3621] | 221 | query = DEFAULT_QUERY_STRING;
|
---|
| 222 |
|
---|
| 223 |
|
---|
| 224 | DocumentStream stream = null;
|
---|
| 225 | if (alphabet.equals(MINIMUM_ALPHABET)){
|
---|
| 226 | stream = new GeneratedDocumentStream('a','c');
|
---|
| 227 | } else if(alphabet.equals(ASCII_ALPHABET)){
|
---|
| 228 | stream = new GeneratedDocumentStream();
|
---|
| 229 | } else if(alphabet.equals(UNICODE_ALPHABET)){
|
---|
| 230 | stream = new GeneratedDocumentStream(Character.MIN_VALUE,
|
---|
| 231 | Character.MAX_VALUE);
|
---|
| 232 | } else {
|
---|
[13124] | 233 | logger.error("bad alphabet name : "+ alphabet);
|
---|
[28966] | 234 | GSXML.addError(response,"XSLTServices: bad alphabet name : "+ alphabet);
|
---|
[3621] | 235 | stream = new GeneratedDocumentStream();
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[28966] | 238 | Element resource_list = result_doc.createElement(GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER);
|
---|
[3945] | 239 | response.appendChild(resource_list);
|
---|
[3621] | 240 |
|
---|
| 241 | // Framework to stringise the document
|
---|
| 242 | TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
---|
| 243 |
|
---|
| 244 | // add each resource
|
---|
| 245 | for (int d=0; d<RESULT_SET_SIZE; d++) {
|
---|
| 246 | try {
|
---|
| 247 | Document document = stream.nextDocument();
|
---|
[14225] | 248 | Element doc = GSXML.getFirstElementChild(document);//(Element)document.getFirstChild();
|
---|
[3621] | 249 | Transformer transformer = transformerFactory.newTransformer();
|
---|
| 250 | StringWriter writer = new StringWriter();
|
---|
| 251 | StreamResult result = new StreamResult(writer);
|
---|
| 252 | Source source = new DOMSource(doc);
|
---|
| 253 | transformer.transform(source,result);
|
---|
| 254 | String id = writer.toString();
|
---|
[28966] | 255 | Element e = result_doc.createElement(GSXML.DOCUMENT_ELEM);
|
---|
[3868] | 256 | e.setAttribute(GSXML.NAME_ATT, id);
|
---|
| 257 | resource_list.appendChild(e);
|
---|
[3621] | 258 | } catch (Throwable t) {
|
---|
[28966] | 259 | GSXML.addError(response, "Error in XSLTServices finding results:" + t.toString());
|
---|
[3621] | 260 | }
|
---|
| 261 | }
|
---|
| 262 | return response;
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 |
|
---|