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

Last change on this file since 3649 was 3648, checked in by kjdon, 21 years ago

brought it up to date with some naming changes

  • 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: 3648 $
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) {
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.out.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 and content - this code same as for TextQuery - put
162 // somewhere else?
163 Element param_elem=null;
164 Element content_elem=null;
165 Node n = request.getFirstChild();
166 while (n!=null) {
167 String node_name = n.getNodeName();
168 if (node_name.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
169 param_elem = (Element)n;
170 } else if (node_name.equals(GSXML.CONTENT_ELEM)) {
171 content_elem = (Element)n;
172 }
173 n = n.getNextSibling();
174 }
175
176 if (param_elem==null || content_elem==null) {
177 System.out.println("bad query request");
178 GSXML.addError(doc_, response,"bad query request in XSLTServices");
179 return response;
180 }
181
182 // Documents are just the ids decoding using standard URL decoding
183
184 String []ids = GSXML.getDocumentNameList(content_elem);
185 for (int j=0; j<ids.length; j++) {
186 String document = null;
187 try {
188 document = URLDecoder.decode(ids[j],"UTF-8");
189 } catch (UnsupportedEncodingException e) {
190 throw new Error(e.toString());
191 }
192 // something funny with the doc -
193 Element new_doc = GSXML.createDocumentElement(doc_, ids[j]);
194 GSXML.addDocText(doc_, new_doc, document);
195 response.appendChild(new_doc);
196 }
197
198 return response;
199 }
200
201 /** process a XSLT query */
202 protected Element processXSLTQuery(Element request) {
203
204 // create the result and set the path so we know where we are
205 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
206 String from = GSPath.appendLink(cluster_name_, XSLT_QUERY_SERVICE);
207 response.setAttribute(GSXML.FROM_ATT, from);
208 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
209
210 // get the parameter list
211 Element param_list =
212 (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
213
214 // extract the only parameter we care about
215 HashMap params = GSXML.extractParams(param_list);
216 String query = (String)params.get(QUERY_PARAM);
217
218 if (query == null || query == "")
219 query = DEFAULT_QUERY_STRING;
220
221
222 DocumentStream stream = null;
223 if (alphabet.equals(MINIMUM_ALPHABET)){
224 stream = new GeneratedDocumentStream('a','c');
225 } else if(alphabet.equals(ASCII_ALPHABET)){
226 stream = new GeneratedDocumentStream();
227 } else if(alphabet.equals(UNICODE_ALPHABET)){
228 stream = new GeneratedDocumentStream(Character.MIN_VALUE,
229 Character.MAX_VALUE);
230 } else {
231 System.err.println("XSLTServices: bad alphabet name : "+ alphabet);
232 GSXML.addError(doc_, response,"XSLTServices: bad alphabet name : "+ alphabet);
233 stream = new GeneratedDocumentStream();
234 }
235
236 Element c = doc_.createElement(GSXML.CONTENT_ELEM);
237 response.appendChild(c);
238 Element resource_list = doc_.createElement(GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER);
239 c.appendChild(resource_list);
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();
248 Element doc = (Element)document.getFirstChild();
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();
255 Node no = GSXML.createDocumentElement(doc_, id);
256 resource_list.appendChild(no);
257 } catch (Throwable t) {
258 GSXML.addError(doc_, response, "Error in XSLTServices finding results:" + t.toString());
259 }
260 }
261 return response;
262 }
263}
264
265
Note: See TracBrowser for help on using the repository browser.