1 package org.apache.lucene.xmlparser;
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.Enumeration;
7 import java.util.HashMap;
8 import java.util.Properties;
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13 import javax.xml.transform.Result;
14 import javax.xml.transform.Templates;
15 import javax.xml.transform.Transformer;
16 import javax.xml.transform.TransformerConfigurationException;
17 import javax.xml.transform.TransformerException;
18 import javax.xml.transform.TransformerFactory;
19 import javax.xml.transform.dom.DOMResult;
20 import javax.xml.transform.dom.DOMSource;
21 import javax.xml.transform.stream.StreamResult;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25 import org.xml.sax.SAXException;
28 * Licensed to the Apache Software Foundation (ASF) under one or more
29 * contributor license agreements. See the NOTICE file distributed with
30 * this work for additional information regarding copyright ownership.
31 * The ASF licenses this file to You under the Apache License, Version 2.0
32 * (the "License"); you may not use this file except in compliance with
33 * the License. You may obtain a copy of the License at
35 * http://www.apache.org/licenses/LICENSE-2.0
37 * Unless required by applicable law or agreed to in writing, software
38 * distributed under the License is distributed on an "AS IS" BASIS,
39 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40 * See the License for the specific language governing permissions and
41 * limitations under the License.
44 * Provides utilities for turning query form input (such as from a web page or Swing gui) into
45 * Lucene XML queries by using XSL templates. This approach offers a convenient way of externalizing
46 * and changing how user input is turned into Lucene queries.
47 * Database applications often adopt similar practices by externalizing SQL in template files that can
48 * be easily changed/optimized by a DBA.
49 * The static methods can be used on their own or by creating an instance of this class you can store and
50 * re-use compiled stylesheets for fast use (e.g. in a server environment)
52 public class QueryTemplateManager
54 static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
55 static TransformerFactory tFactory = TransformerFactory.newInstance();
57 HashMap<String,Templates> compiledTemplatesCache=new HashMap<String,Templates>();
58 Templates defaultCompiledTemplates=null;
61 public QueryTemplateManager()
65 public QueryTemplateManager(InputStream xslIs) throws TransformerConfigurationException, ParserConfigurationException, SAXException, IOException
67 addDefaultQueryTemplate(xslIs);
69 public void addDefaultQueryTemplate(InputStream xslIs) throws TransformerConfigurationException, ParserConfigurationException, SAXException, IOException
71 defaultCompiledTemplates=getTemplates(xslIs);
73 public void addQueryTemplate(String name, InputStream xslIs) throws TransformerConfigurationException, ParserConfigurationException, SAXException, IOException
75 compiledTemplatesCache.put(name,getTemplates(xslIs));
77 public String getQueryAsXmlString(Properties formProperties,String queryTemplateName) throws SAXException, IOException, ParserConfigurationException, TransformerException
79 Templates ts= compiledTemplatesCache.get(queryTemplateName);
80 return getQueryAsXmlString(formProperties, ts);
83 public Document getQueryAsDOM(Properties formProperties,String queryTemplateName) throws SAXException, IOException, ParserConfigurationException, TransformerException
85 Templates ts= compiledTemplatesCache.get(queryTemplateName);
86 return getQueryAsDOM(formProperties, ts);
88 public String getQueryAsXmlString(Properties formProperties) throws SAXException, IOException, ParserConfigurationException, TransformerException
90 return getQueryAsXmlString(formProperties, defaultCompiledTemplates);
93 public Document getQueryAsDOM(Properties formProperties) throws SAXException, IOException, ParserConfigurationException, TransformerException
95 return getQueryAsDOM(formProperties, defaultCompiledTemplates);
100 * Fast means of constructing query using a precompiled stylesheet
102 public static String getQueryAsXmlString(Properties formProperties, Templates template) throws SAXException, IOException, ParserConfigurationException, TransformerException
104 ByteArrayOutputStream baos=new ByteArrayOutputStream();
105 StreamResult result=new StreamResult(baos);
106 transformCriteria(formProperties,template,result);
107 return baos.toString();
111 * Slow means of constructing query parsing a stylesheet from an input stream
113 public static String getQueryAsXmlString(Properties formProperties, InputStream xslIs) throws SAXException, IOException, ParserConfigurationException, TransformerException
115 ByteArrayOutputStream baos=new ByteArrayOutputStream();
116 StreamResult result=new StreamResult(baos);
117 transformCriteria(formProperties,xslIs,result);
118 return baos.toString();
123 * Fast means of constructing query using a cached,precompiled stylesheet
125 public static Document getQueryAsDOM(Properties formProperties, Templates template) throws SAXException, IOException, ParserConfigurationException, TransformerException
127 DOMResult result=new DOMResult();
128 transformCriteria(formProperties,template,result);
129 return (Document)result.getNode();
134 * Slow means of constructing query - parses stylesheet from input stream
136 public static Document getQueryAsDOM(Properties formProperties, InputStream xslIs) throws SAXException, IOException, ParserConfigurationException, TransformerException
138 DOMResult result=new DOMResult();
139 transformCriteria(formProperties,xslIs,result);
140 return (Document)result.getNode();
147 * Slower transformation using an uncompiled stylesheet (suitable for development environment)
149 public static void transformCriteria(Properties formProperties, InputStream xslIs, Result result) throws SAXException, IOException, ParserConfigurationException, TransformerException
151 dbf.setNamespaceAware(true);
152 DocumentBuilder builder = dbf.newDocumentBuilder();
153 org.w3c.dom.Document xslDoc = builder.parse(xslIs);
154 DOMSource ds = new DOMSource(xslDoc);
156 Transformer transformer =null;
157 synchronized (tFactory)
159 transformer = tFactory.newTransformer(ds);
161 transformCriteria(formProperties,transformer,result);
165 * Fast transformation using a pre-compiled stylesheet (suitable for production environments)
167 public static void transformCriteria(Properties formProperties, Templates template, Result result) throws SAXException, IOException, ParserConfigurationException, TransformerException
169 transformCriteria(formProperties,template.newTransformer(),result);
174 public static void transformCriteria(Properties formProperties, Transformer transformer, Result result) throws SAXException, IOException, ParserConfigurationException, TransformerException
176 dbf.setNamespaceAware(true);
178 //Create an XML document representing the search index document.
179 DocumentBuilder db = dbf.newDocumentBuilder ();
180 org.w3c.dom.Document doc = db.newDocument ();
181 Element root = doc.createElement ("Document");
182 doc.appendChild (root);
184 Enumeration keysEnum = formProperties.keys();
185 while(keysEnum.hasMoreElements())
187 String propName=(String) keysEnum.nextElement();
188 String value=formProperties.getProperty(propName);
189 if((value!=null)&&(value.length()>0))
191 DOMUtils.insertChild(root,propName,value);
194 //Use XSLT to to transform into an XML query string using the queryTemplate
195 DOMSource xml=new DOMSource(doc);
196 transformer.transform(xml,result);
200 * Parses a query stylesheet for repeated use
202 public static Templates getTemplates(InputStream xslIs) throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException
204 dbf.setNamespaceAware(true);
205 DocumentBuilder builder = dbf.newDocumentBuilder();
206 org.w3c.dom.Document xslDoc = builder.parse(xslIs);
207 DOMSource ds = new DOMSource(xslDoc);
208 return tFactory.newTemplates(ds);