/*
* XSLTServices.java
* Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.greenstone.gsdl3.service;
import org.greenstone.gsdl3.util.*;
import org.greenstone.gsdl3.selfContained.*;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.dom.ElementImpl;
import org.apache.xerces.dom.TextImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.util.HashMap;
import java.util.Vector;
import java.util.Set;
import java.util.Map;
import java.util.Iterator;
import java.util.Locale;
import java.io.*;
import java.net.URLEncoder;
import java.net.URLDecoder;
/**
* A ServicesImpl class for XSLT query.
*
* Document ids are formed by encoding the document using standard URLEncoding
*
* @author Katherine Don
* @author Stuart Yeates
* @version $Revision: 3648 $
* @see ServicesImpl
* @see java.net.URLEncode
* @see java.net.URLDecode
*/
public class XSLTServices
extends ServiceRack {
// these strings must match what is found in the properties file
// the services on offer
private static final String XSLT_QUERY_SERVICE = "XSLTQuery";
private static final String RESOURCE_RETRIEVE_SERVICE = "ResourceRetrieve";
// params used
private static final String QUERY_PARAM = "query";
private static final String ALPHABET_PARAM = "alphabet";
private static final int RESULT_SET_SIZE = 10;
// alphabets
private static final String MINIMUM_ALPHABET = "minimum";
private static final String ASCII_ALPHABET = "ascii";
private static final String UNICODE_ALPHABET = "unicode";
private static final String DEFAULT_QUERY_STRING =
""+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
"";
private String alphabet = MINIMUM_ALPHABET;
private Element config_info_ = null;
public XSLTServices() {
}
/** configure this service */
public boolean configure(Element info) {
System.out.println("configuring XSLTServices");
config_info_ = info;
try {
System.out.println("XSLTService::configure() called with:");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Element doc = (Element)info.getFirstChild();
Transformer transformer = transformerFactory.newTransformer();
StreamResult result = new StreamResult(System.out);
Source source = new DOMSource(doc);
transformer.transform(source,result);
} catch (Throwable t) {
System.out.println("Error printing XML in XSLTService::configure()");
}
return true;
}
/**
* creates a display element containing all the text strings
* needed to display the service page, in the language specified
*/
protected Element createServiceDisplay(String service, String lang) {
return null;
}
/** process method for specific services - must be implemented by all
* subclasses
* should implement all services supported by the servicesImpl except
* for describe, which is implemented by this base class
*
*/
protected Element processService(String service, Element request){
if (service.equals(XSLT_QUERY_SERVICE)) {
return processXSLTQuery(request);
} else if (service.equals(RESOURCE_RETRIEVE_SERVICE)) {
return processResourceRetrieve(request);
}
// create the response so we can report the error
Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
String from = GSPath.appendLink(cluster_name_, RESOURCE_RETRIEVE_SERVICE);
response.setAttribute(GSXML.FROM_ATT, from);
response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
System.err.println("XSLTServices:should never get here. service type wrong:"+service);
GSXML.addError(doc_, response,"XSLTServices:should never get here. service type wrong:"+service);
return response;
}
/** process a document resquest query */
protected Element processResourceRetrieve(Element request) {
// create the result and set the path so we know where we are
Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
String from = GSPath.appendLink(cluster_name_, RESOURCE_RETRIEVE_SERVICE);
response.setAttribute(GSXML.FROM_ATT, from);
response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
// get param list and content - this code same as for TextQuery - put
// somewhere else?
Element param_elem=null;
Element content_elem=null;
Node n = request.getFirstChild();
while (n!=null) {
String node_name = n.getNodeName();
if (node_name.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
param_elem = (Element)n;
} else if (node_name.equals(GSXML.CONTENT_ELEM)) {
content_elem = (Element)n;
}
n = n.getNextSibling();
}
if (param_elem==null || content_elem==null) {
System.out.println("bad query request");
GSXML.addError(doc_, response,"bad query request in XSLTServices");
return response;
}
// Documents are just the ids decoding using standard URL decoding
String []ids = GSXML.getDocumentNameList(content_elem);
for (int j=0; j