add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / xml-query-parser / src / java / org / apache / lucene / xmlparser / QueryTemplateManager.java
1 package org.apache.lucene.xmlparser;
2
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;
9
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;
22
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25 import org.xml.sax.SAXException;
26
27 /**
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
34  *
35  *     http://www.apache.org/licenses/LICENSE-2.0
36  *
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.
42  */
43 /**
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)
51  */
52 public class QueryTemplateManager
53 {
54         static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
55         static TransformerFactory tFactory = TransformerFactory.newInstance();
56
57         HashMap<String,Templates> compiledTemplatesCache=new HashMap<String,Templates>();
58         Templates defaultCompiledTemplates=null;
59
60         
61         public QueryTemplateManager()
62         {
63                 
64         }
65         public QueryTemplateManager(InputStream xslIs) throws TransformerConfigurationException, ParserConfigurationException, SAXException, IOException
66         {
67                 addDefaultQueryTemplate(xslIs);
68         }
69         public void addDefaultQueryTemplate(InputStream xslIs) throws TransformerConfigurationException, ParserConfigurationException, SAXException, IOException
70         {
71                 defaultCompiledTemplates=getTemplates(xslIs);
72         }
73         public void addQueryTemplate(String name, InputStream xslIs) throws TransformerConfigurationException, ParserConfigurationException, SAXException, IOException
74         {
75                 compiledTemplatesCache.put(name,getTemplates(xslIs));
76         }
77         public String getQueryAsXmlString(Properties formProperties,String queryTemplateName) throws SAXException, IOException, ParserConfigurationException, TransformerException
78         {
79                 Templates ts= compiledTemplatesCache.get(queryTemplateName);
80                 return getQueryAsXmlString(formProperties, ts);
81         }
82         
83         public Document getQueryAsDOM(Properties formProperties,String queryTemplateName) throws SAXException, IOException, ParserConfigurationException, TransformerException
84         {
85                 Templates ts= compiledTemplatesCache.get(queryTemplateName);
86                 return getQueryAsDOM(formProperties, ts);
87         }
88         public String getQueryAsXmlString(Properties formProperties) throws SAXException, IOException, ParserConfigurationException, TransformerException
89         {
90                 return getQueryAsXmlString(formProperties, defaultCompiledTemplates);
91         }
92         
93         public Document getQueryAsDOM(Properties formProperties) throws SAXException, IOException, ParserConfigurationException, TransformerException
94         {
95                 return getQueryAsDOM(formProperties, defaultCompiledTemplates);
96         }
97         
98         
99         /**
100          * Fast means of constructing query using a precompiled stylesheet  
101          */             
102         public static String getQueryAsXmlString(Properties formProperties, Templates template) throws SAXException, IOException, ParserConfigurationException, TransformerException 
103         {
104                 ByteArrayOutputStream baos=new ByteArrayOutputStream();
105                 StreamResult result=new StreamResult(baos);
106                 transformCriteria(formProperties,template,result);
107                 return baos.toString();                 
108         }
109         
110         /**
111          * Slow means of constructing query parsing a stylesheet from an input stream  
112          */             
113         public static String getQueryAsXmlString(Properties formProperties, InputStream xslIs) throws SAXException, IOException, ParserConfigurationException, TransformerException 
114         {
115                 ByteArrayOutputStream baos=new ByteArrayOutputStream();
116                 StreamResult result=new StreamResult(baos);
117                 transformCriteria(formProperties,xslIs,result);
118                 return baos.toString();                 
119         }
120                         
121
122         /**
123          * Fast means of constructing query using a cached,precompiled stylesheet  
124          */     
125         public static Document getQueryAsDOM(Properties formProperties, Templates template) throws SAXException, IOException, ParserConfigurationException, TransformerException
126         {
127                 DOMResult result=new DOMResult();
128                 transformCriteria(formProperties,template,result);
129                 return (Document)result.getNode();
130         }
131
132         
133         /**
134          * Slow means of constructing query - parses stylesheet from input stream 
135          */
136         public static Document getQueryAsDOM(Properties formProperties, InputStream xslIs) throws SAXException, IOException, ParserConfigurationException, TransformerException
137         {
138                 DOMResult result=new DOMResult();
139                 transformCriteria(formProperties,xslIs,result);
140                 return (Document)result.getNode();
141         }
142         
143         
144         
145         
146         /**
147          * Slower transformation using an uncompiled stylesheet (suitable for development environment)
148          */
149         public static void transformCriteria(Properties formProperties, InputStream xslIs, Result result) throws SAXException, IOException, ParserConfigurationException, TransformerException
150         {
151         dbf.setNamespaceAware(true);        
152                 DocumentBuilder builder = dbf.newDocumentBuilder();
153                 org.w3c.dom.Document xslDoc = builder.parse(xslIs);
154                 DOMSource ds = new DOMSource(xslDoc);
155                 
156                 Transformer transformer =null;
157                 synchronized (tFactory)
158                 {
159                         transformer = tFactory.newTransformer(ds);                      
160                 }
161                 transformCriteria(formProperties,transformer,result);
162         }
163         
164         /**
165          * Fast transformation using a pre-compiled stylesheet (suitable for production environments)
166          */
167         public static void transformCriteria(Properties formProperties, Templates template, Result result) throws SAXException, IOException, ParserConfigurationException, TransformerException
168         {
169                 transformCriteria(formProperties,template.newTransformer(),result);
170         }
171         
172         
173         
174         public static void transformCriteria(Properties formProperties, Transformer transformer, Result result) throws SAXException, IOException, ParserConfigurationException, TransformerException
175         {
176         dbf.setNamespaceAware(true);
177         
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);
183                 
184                 Enumeration keysEnum = formProperties.keys();
185                 while(keysEnum.hasMoreElements())
186                 {
187                     String propName=(String) keysEnum.nextElement();
188                     String value=formProperties.getProperty(propName);
189                 if((value!=null)&&(value.length()>0))
190                 {
191                     DOMUtils.insertChild(root,propName,value);                          
192                 }
193                 }               
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);              
197         }
198         
199         /**
200          * Parses a query stylesheet for repeated use
201          */
202         public static Templates getTemplates(InputStream xslIs) throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException  
203         {
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);
209         }
210 }