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 |
|
---|
48 | /**
|
---|
49 | * A ServicesImpl class for XSLT query.
|
---|
50 | *
|
---|
51 | * Document ids are formed by encoding the document using standard URLEncoding
|
---|
52 | *
|
---|
53 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
54 | * @author <a href="mailto:[email protected]">Stuart Yeates</a>
|
---|
55 | * @version $Revision: 3621 $
|
---|
56 | * @see ServicesImpl
|
---|
57 | * @see java.net.URLEncode
|
---|
58 | * @see java.net.URLDecode
|
---|
59 | */
|
---|
60 |
|
---|
61 | public class XSLTServices
|
---|
62 | extends ServicesImpl {
|
---|
63 |
|
---|
64 | // these strings must match what is found in the properties file
|
---|
65 | // the services on offer
|
---|
66 | private static final String XSLT_QUERY_SERVICE = "XSLTQuery";
|
---|
67 | private static final String RESOURCE_RETRIEVE_SERVICE = "ResourceRetrieve";
|
---|
68 | // params used
|
---|
69 | private static final String QUERY_PARAM = "query";
|
---|
70 | private static final String ALPHABET_PARAM = "alphabet";
|
---|
71 |
|
---|
72 | private static final int RESULT_SET_SIZE = 10;
|
---|
73 |
|
---|
74 | // alphabets
|
---|
75 | private static final String MINIMUM_ALPHABET = "minimum";
|
---|
76 | private static final String ASCII_ALPHABET = "ascii";
|
---|
77 | private static final String UNICODE_ALPHABET = "unicode";
|
---|
78 |
|
---|
79 | private static final String DEFAULT_QUERY_STRING =
|
---|
80 | "<xsl:stylesheet version=\"1.0\""+
|
---|
81 | " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"+
|
---|
82 | " <xsl:template match=\"*|/\">"+
|
---|
83 | " <xsl:apply-templates/>"+
|
---|
84 | " </xsl:template>"+
|
---|
85 | " <xsl:template match=\"*|/\" mode=\"m\">"+
|
---|
86 | " <xsl:apply-templates mode=\"m\"/>"+
|
---|
87 | " </xsl:template>"+
|
---|
88 | " <xsl:template match=\"text()|@*\">"+
|
---|
89 | " <xsl:value-of select=\".\"/>"+
|
---|
90 | " </xsl:template>"+
|
---|
91 | " <xsl:template match=\"processing-instruction()|comment()\"/>"+
|
---|
92 | "</xsl:stylesheet>";
|
---|
93 |
|
---|
94 |
|
---|
95 | private String alphabet = MINIMUM_ALPHABET;
|
---|
96 | private Element config_info_ = null;
|
---|
97 |
|
---|
98 |
|
---|
99 | public XSLTServices() {
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** configure this service */
|
---|
103 | public boolean configure(Element info) {
|
---|
104 |
|
---|
105 | System.out.println("configuring XSLTServices");
|
---|
106 | config_info_ = info;
|
---|
107 |
|
---|
108 | System.out.println("XSLTService::configure() called with:")
|
---|
109 | TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
---|
110 | Element doc = (Element)info.getFirstChild();
|
---|
111 | Transformer transformer = transformerFactory.newTransformer();
|
---|
112 | StreamResult result = new StreamResult(System.out);
|
---|
113 | Source source = new DOMSource(doc);
|
---|
114 | transformer.transform(source,result);
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * creates a display element containing all the text strings
|
---|
120 | * needed to display the service page, in the language specified
|
---|
121 | */
|
---|
122 | protected Element createServiceDisplay(String service, String lang) {
|
---|
123 | return null;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** process method for specific services - must be implemented by all
|
---|
127 | * subclasses
|
---|
128 | * should implement all services supported by the servicesImpl except
|
---|
129 | * for describe, which is implemented by this base class
|
---|
130 | *
|
---|
131 | */
|
---|
132 | protected Element processService(String service, Element request){
|
---|
133 | if (service.equals(XSLT_QUERY_SERVICE)) {
|
---|
134 | return processXSLTQuery(request);
|
---|
135 | } else if (service.equals(RESOURCE_RETRIEVE_SERVICE)) {
|
---|
136 | return processResourceRetrieve(request);
|
---|
137 | }
|
---|
138 | // create the response so we can report the error
|
---|
139 | Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
140 | String from = GSPath.appendLink(cluster_name_, RESOURCE_RETRIEVE_SERVICE);
|
---|
141 | response.setAttribute(GSXML.FROM_ATT, from);
|
---|
142 | response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
143 |
|
---|
144 | System.err.println("XSLTServices:should never get here. service type wrong:"+service);
|
---|
145 | GSXML.addError(doc_, response,"XSLTServices:should never get here. service type wrong:"+service);
|
---|
146 | return response;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /** process a document resquest query */
|
---|
150 | protected Element processResourceRetrieve(Element request) {
|
---|
151 |
|
---|
152 | // create the result and set the path so we know where we are
|
---|
153 | Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
154 | String from = GSPath.appendLink(cluster_name_, RESOURCE_RETRIEVE_SERVICE);
|
---|
155 | response.setAttribute(GSXML.FROM_ATT, from);
|
---|
156 | response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
157 |
|
---|
158 | // get param list and content - this code same as for TextQuery - put
|
---|
159 | // somewhere else?
|
---|
160 | Element param_elem=null;
|
---|
161 | Element content_elem=null;
|
---|
162 | Node n = request.getFirstChild();
|
---|
163 | while (n!=null) {
|
---|
164 | String node_name = n.getNodeName();
|
---|
165 | if (node_name.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
166 | param_elem = (Element)n;
|
---|
167 | } else if (node_name.equals(GSXML.CONTENT_ELEM)) {
|
---|
168 | content_elem = (Element)n;
|
---|
169 | }
|
---|
170 | n = n.getNextSibling();
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (param_elem==null || content_elem==null) {
|
---|
174 | System.out.println("bad query request");
|
---|
175 | GSXML.addError(doc_, response,"bad query request in XSLTServices");
|
---|
176 | return response;
|
---|
177 | }
|
---|
178 |
|
---|
179 | // Documents are just the ids decoding using standard URL decoding
|
---|
180 |
|
---|
181 | String []ids = GSXML.getResourceNameList(content_elem);
|
---|
182 | for (int j=0; j<ids.length; j++) {
|
---|
183 | String document = URLDecoder.decode(ids[j]);
|
---|
184 |
|
---|
185 | // something funny with the doc -
|
---|
186 | Element new_doc = GSXML.createResourceElement(doc_, ids[j]);
|
---|
187 | GSXML.addDocText(doc_, new_doc, document);
|
---|
188 | response.appendChild(new_doc);
|
---|
189 | }
|
---|
190 |
|
---|
191 | return response;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /** process a XSLT query */
|
---|
195 | protected Element processXSLTQuery(Element request) {
|
---|
196 |
|
---|
197 | // create the result and set the path so we know where we are
|
---|
198 | Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
199 | String from = GSPath.appendLink(cluster_name_, XSLT_QUERY_SERVICE);
|
---|
200 | response.setAttribute(GSXML.FROM_ATT, from);
|
---|
201 | response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
202 |
|
---|
203 | // get the parameter list
|
---|
204 | Element param_list =
|
---|
205 | (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
206 |
|
---|
207 | // extract the only parameter we care about
|
---|
208 | HashMap params = GSXML.extractParams(param_list);
|
---|
209 | String query = (String)params.get(QUERY_PARAM);
|
---|
210 |
|
---|
211 | if (query == null || query == "")
|
---|
212 | query = DEFAULT_QUERY_STRING;
|
---|
213 |
|
---|
214 |
|
---|
215 | DocumentStream stream = null;
|
---|
216 | if (alphabet.equals(MINIMUM_ALPHABET)){
|
---|
217 | stream = new GeneratedDocumentStream('a','c');
|
---|
218 | } else if(alphabet.equals(ASCII_ALPHABET)){
|
---|
219 | stream = new GeneratedDocumentStream();
|
---|
220 | } else if(alphabet.equals(UNICODE_ALPHABET)){
|
---|
221 | stream = new GeneratedDocumentStream(Character.MIN_VALUE,
|
---|
222 | Character.MAX_VALUE);
|
---|
223 | } else {
|
---|
224 | System.err.println("XSLTServices: bad alphabet name : "+ alphabet);
|
---|
225 | GSXML.addError(doc_, response,"XSLTServices: bad alphabet name : "+ alphabet);
|
---|
226 | stream = new GeneratedDocumentStream();
|
---|
227 | }
|
---|
228 |
|
---|
229 | Element c = doc_.createElement(GSXML.CONTENT_ELEM);
|
---|
230 | response.appendChild(c);
|
---|
231 | Element resource_list = doc_.createElement(GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER);
|
---|
232 | c.appendChild(resource_list);
|
---|
233 |
|
---|
234 | // Framework to stringise the document
|
---|
235 | TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
---|
236 |
|
---|
237 | // add each resource
|
---|
238 | for (int d=0; d<RESULT_SET_SIZE; d++) {
|
---|
239 | try {
|
---|
240 | Document document = stream.nextDocument();
|
---|
241 | Element doc = (Element)document.getFirstChild();
|
---|
242 | Transformer transformer = transformerFactory.newTransformer();
|
---|
243 | StringWriter writer = new StringWriter();
|
---|
244 | StreamResult result = new StreamResult(writer);
|
---|
245 | Source source = new DOMSource(doc);
|
---|
246 | transformer.transform(source,result);
|
---|
247 | String id = writer.toString();
|
---|
248 | Node no = GSXML.createResourceElement(doc_, id);
|
---|
249 | resource_list.appendChild(no);
|
---|
250 | } catch (Throwable t) {
|
---|
251 | GSXML.addError(doc_, response, "Error in XSLTServices finding results:" + t.toString());
|
---|
252 | }
|
---|
253 | }
|
---|
254 | return response;
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 |
|
---|