source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/XSLTServices.java@ 4012

Last change on this file since 4012 was 4012, checked in by mdewsnip, 21 years ago

Changed string comparison to use .equals().

  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 KB
Line 
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 */
19package org.greenstone.gsdl3.service;
20
21import org.greenstone.gsdl3.util.*;
22import org.greenstone.gsdl3.selfContained.*;
23
24import javax.xml.transform.Source;
25import javax.xml.transform.Transformer;
26import javax.xml.transform.TransformerFactory;
27import javax.xml.transform.dom.DOMSource;
28import javax.xml.transform.stream.StreamResult;
29import org.apache.xerces.dom.DocumentImpl;
30import org.apache.xerces.dom.ElementImpl;
31import org.apache.xerces.dom.TextImpl;
32import org.w3c.dom.Document;
33import org.w3c.dom.Node;
34import org.w3c.dom.Text;
35import org.w3c.dom.Element;
36import org.w3c.dom.NodeList;
37
38import java.util.HashMap;
39import java.util.Vector;
40import java.util.Set;
41import java.util.Map;
42import java.util.Iterator;
43import java.util.Locale;
44import java.io.*;
45import java.net.URLEncoder;
46import 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: 4012 $
56 * @see ServicesImpl
57 * @see java.net.URLEncode
58 * @see java.net.URLDecode
59 */
60
61public class XSLTServices
62 extends ServiceRack {
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, Element extra_info) {
104
105 System.out.println("configuring XSLTServices");
106 config_info_ = info;
107 try {
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 } catch (Throwable t) {
116 System.err.println("Error printing XML in XSLTService::configure()");
117 }
118 return true;
119 }
120
121 /**
122 * creates a display element containing all the text strings
123 * needed to display the service page, in the language specified
124 */
125 protected Element createServiceDisplay(String service, String lang) {
126 return null;
127 }
128
129 /** process method for specific services - must be implemented by all
130 * subclasses
131 * should implement all services supported by the servicesImpl except
132 * for describe, which is implemented by this base class
133 *
134 */
135 protected Element processService(String service, Element request){
136 if (service.equals(XSLT_QUERY_SERVICE)) {
137 return processXSLTQuery(request);
138 } else if (service.equals(RESOURCE_RETRIEVE_SERVICE)) {
139 return processResourceRetrieve(request);
140 }
141 // create the response so we can report the error
142 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
143 String from = GSPath.appendLink(cluster_name_, RESOURCE_RETRIEVE_SERVICE);
144 response.setAttribute(GSXML.FROM_ATT, from);
145 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
146
147 System.err.println("XSLTServices:should never get here. service type wrong:"+service);
148 GSXML.addError(doc_, response,"XSLTServices:should never get here. service type wrong:"+service);
149 return response;
150 }
151
152 /** process a document resquest query */
153 protected Element processResourceRetrieve(Element request) {
154
155 // create the result and set the path so we know where we are
156 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
157 String from = GSPath.appendLink(cluster_name_, RESOURCE_RETRIEVE_SERVICE);
158 response.setAttribute(GSXML.FROM_ATT, from);
159 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
160
161 // get param list
162 Element param_elem=null;
163 Node n = request.getFirstChild();
164 while (n!=null) {
165 String node_name = n.getNodeName();
166 if (node_name.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
167 param_elem = (Element)n;
168 break;
169 }
170 n = n.getNextSibling();
171 }
172
173 if (param_elem==null) {
174 System.err.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 Element doc_list = (Element)GSXML.getChildByTagName(request, GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER);
182 String []ids = GSXML.getAttributeValuesFromList(doc_list, GSXML.NAME_ATT);
183 for (int j=0; j<ids.length; j++) {
184 String document = null;
185 try {
186 document = URLDecoder.decode(ids[j],"UTF-8");
187 } catch (UnsupportedEncodingException e) {
188 throw new Error(e.toString());
189 }
190 // something funny with the doc -
191 Element new_doc = doc_.createElement(GSXML.DOCUMENT_ELEM);
192 new_doc.setAttribute(GSXML.NAME_ATT, ids[j]); //GSXML.createDocumentElement(doc_, ids[j]);
193 GSXML.addDocText(doc_, new_doc, document);
194 response.appendChild(new_doc);
195 }
196
197 return response;
198 }
199
200 /** process a XSLT query */
201 protected Element processXSLTQuery(Element request) {
202
203 // create the result and set the path so we know where we are
204 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
205 String from = GSPath.appendLink(cluster_name_, XSLT_QUERY_SERVICE);
206 response.setAttribute(GSXML.FROM_ATT, from);
207 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
208
209 // get the parameter list
210 Element param_list =
211 (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
212
213 // extract the only parameter we care about
214 HashMap params = GSXML.extractParams(param_list, false);
215 String query = (String)params.get(QUERY_PARAM);
216
217 if (query == null || query.equals(""))
218 query = DEFAULT_QUERY_STRING;
219
220
221 DocumentStream stream = null;
222 if (alphabet.equals(MINIMUM_ALPHABET)){
223 stream = new GeneratedDocumentStream('a','c');
224 } else if(alphabet.equals(ASCII_ALPHABET)){
225 stream = new GeneratedDocumentStream();
226 } else if(alphabet.equals(UNICODE_ALPHABET)){
227 stream = new GeneratedDocumentStream(Character.MIN_VALUE,
228 Character.MAX_VALUE);
229 } else {
230 System.err.println("XSLTServices: bad alphabet name : "+ alphabet);
231 GSXML.addError(doc_, response,"XSLTServices: bad alphabet name : "+ alphabet);
232 stream = new GeneratedDocumentStream();
233 }
234
235 Element resource_list = doc_.createElement(GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER);
236 response.appendChild(resource_list);
237
238 // Framework to stringise the document
239 TransformerFactory transformerFactory = TransformerFactory.newInstance();
240
241 // add each resource
242 for (int d=0; d<RESULT_SET_SIZE; d++) {
243 try {
244 Document document = stream.nextDocument();
245 Element doc = (Element)document.getFirstChild();
246 Transformer transformer = transformerFactory.newTransformer();
247 StringWriter writer = new StringWriter();
248 StreamResult result = new StreamResult(writer);
249 Source source = new DOMSource(doc);
250 transformer.transform(source,result);
251 String id = writer.toString();
252 Element e = doc_.createElement(GSXML.DOCUMENT_ELEM);
253 e.setAttribute(GSXML.NAME_ATT, id);
254 //Node no = GSXML.createDocumentElement(doc_, id);
255 resource_list.appendChild(e);
256 } catch (Throwable t) {
257 GSXML.addError(doc_, response, "Error in XSLTServices finding results:" + t.toString());
258 }
259 }
260 return response;
261 }
262}
263
264
Note: See TracBrowser for help on using the repository browser.