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

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

fixed up some bad javadoc

  • 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 ServiceRack 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: 5663 $
56 * @see ServiceRack
57 * @see java.net.URLEncoder
58 * @see java.net.URLDecoder
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 this.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 protected Element getServiceDescription(String service, String lang, String subset) {
122
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 = this.doc.createElement(GSXML.RESPONSE_ELEM);
140 String from = GSPath.appendLink(this.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(this.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 = this.doc.createElement(GSXML.RESPONSE_ELEM);
154 String from = GSPath.appendLink(this.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
159 Element param_elem=null;
160 Node n = request.getFirstChild();
161 while (n!=null) {
162 String node_name = n.getNodeName();
163 if (node_name.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
164 param_elem = (Element)n;
165 break;
166 }
167 n = n.getNextSibling();
168 }
169
170 if (param_elem==null) {
171 System.err.println("bad query request");
172 GSXML.addError(this.doc, response,"bad query request in XSLTServices");
173 return response;
174 }
175
176 // Documents are just the ids decoding using standard URL decoding
177
178 Element doc_list = (Element)GSXML.getChildByTagName(request, GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER);
179 String []ids = GSXML.getAttributeValuesFromList(doc_list, GSXML.NAME_ATT);
180 for (int j=0; j<ids.length; j++) {
181 String document = null;
182 try {
183 document = URLDecoder.decode(ids[j],"UTF-8");
184 } catch (UnsupportedEncodingException e) {
185 throw new Error(e.toString());
186 }
187 // something funny with the doc -
188 Element new_doc = this.doc.createElement(GSXML.DOCUMENT_ELEM);
189 new_doc.setAttribute(GSXML.NAME_ATT, ids[j]); //GSXML.createDocumentElement(this.doc, ids[j]);
190 GSXML.addDocText(this.doc, new_doc, document);
191 response.appendChild(new_doc);
192 }
193
194 return response;
195 }
196
197 /** process a XSLT query */
198 protected Element processXSLTQuery(Element request) {
199
200 // create the result and set the path so we know where we are
201 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
202 String from = GSPath.appendLink(this.cluster_name, XSLT_QUERY_SERVICE);
203 response.setAttribute(GSXML.FROM_ATT, from);
204 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
205
206 // get the parameter list
207 Element param_list =
208 (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
209
210 // extract the only parameter we care about
211 HashMap params = GSXML.extractParams(param_list, false);
212 String query = (String)params.get(QUERY_PARAM);
213
214 if (query == null || query.equals(""))
215 query = DEFAULT_QUERY_STRING;
216
217
218 DocumentStream stream = null;
219 if (alphabet.equals(MINIMUM_ALPHABET)){
220 stream = new GeneratedDocumentStream('a','c');
221 } else if(alphabet.equals(ASCII_ALPHABET)){
222 stream = new GeneratedDocumentStream();
223 } else if(alphabet.equals(UNICODE_ALPHABET)){
224 stream = new GeneratedDocumentStream(Character.MIN_VALUE,
225 Character.MAX_VALUE);
226 } else {
227 System.err.println("XSLTServices: bad alphabet name : "+ alphabet);
228 GSXML.addError(this.doc, response,"XSLTServices: bad alphabet name : "+ alphabet);
229 stream = new GeneratedDocumentStream();
230 }
231
232 Element resource_list = this.doc.createElement(GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER);
233 response.appendChild(resource_list);
234
235 // Framework to stringise the document
236 TransformerFactory transformerFactory = TransformerFactory.newInstance();
237
238 // add each resource
239 for (int d=0; d<RESULT_SET_SIZE; d++) {
240 try {
241 Document document = stream.nextDocument();
242 Element doc = (Element)document.getFirstChild();
243 Transformer transformer = transformerFactory.newTransformer();
244 StringWriter writer = new StringWriter();
245 StreamResult result = new StreamResult(writer);
246 Source source = new DOMSource(doc);
247 transformer.transform(source,result);
248 String id = writer.toString();
249 Element e = this.doc.createElement(GSXML.DOCUMENT_ELEM);
250 e.setAttribute(GSXML.NAME_ATT, id);
251 //Node no = GSXML.createDocumentElement(this.doc, id);
252 resource_list.appendChild(e);
253 } catch (Throwable t) {
254 GSXML.addError(this.doc, response, "Error in XSLTServices finding results:" + t.toString());
255 }
256 }
257 return response;
258 }
259}
260
261
Note: See TracBrowser for help on using the repository browser.