FileDocCategorySizeDatePackage
QueryTemplateManager.javaAPI DocApache Lucene 2.1.08687Wed Feb 14 10:46:20 GMT 2007org.apache.lucene.xmlparser

QueryTemplateManager

public class QueryTemplateManager extends Object
Provides utilities for turning query form input (such as from a web page or Swing gui) into Lucene XML queries by using XSL templates. This approach offers a convenient way of externalizing and changing how user input is turned into Lucene queries. Database applications often adopt similar practices by externalizing SQL in template files that can be easily changed/optimized by a DBA. The static methods can be used on their own or by creating an instance of this class you can store and re-use compiled stylesheets for fast use (e.g. in a server environment)
author
Mark Harwood

Fields Summary
static DocumentBuilderFactory
dbf
static TransformerFactory
tFactory
HashMap
compiledTemplatesCache
Templates
defaultCompiledTemplates
Constructors Summary
public QueryTemplateManager()


	
	 
	
		
	
public QueryTemplateManager(InputStream xslIs)

		addDefaultQueryTemplate(xslIs);
	
Methods Summary
public voidaddDefaultQueryTemplate(java.io.InputStream xslIs)

		defaultCompiledTemplates=getTemplates(xslIs);
	
public voidaddQueryTemplate(java.lang.String name, java.io.InputStream xslIs)

		compiledTemplatesCache.put(name,getTemplates(xslIs));
	
public static org.w3c.dom.DocumentgetQueryAsDOM(java.util.Properties formProperties, javax.xml.transform.Templates template)
Fast means of constructing query using a cached,precompiled stylesheet

  		DOMResult result=new DOMResult();
  		transformCriteria(formProperties,template,result);
  		return (Document)result.getNode();
	
public static org.w3c.dom.DocumentgetQueryAsDOM(java.util.Properties formProperties, java.io.InputStream xslIs)
Slow means of constructing query - parses stylesheet from input stream

  		DOMResult result=new DOMResult();
  		transformCriteria(formProperties,xslIs,result);
  		return (Document)result.getNode();
	
public org.w3c.dom.DocumentgetQueryAsDOM(java.util.Properties formProperties, java.lang.String queryTemplateName)

		Templates ts=(Templates) compiledTemplatesCache.get(queryTemplateName);
		return getQueryAsDOM(formProperties, ts);
	
public org.w3c.dom.DocumentgetQueryAsDOM(java.util.Properties formProperties)

		return getQueryAsDOM(formProperties, defaultCompiledTemplates);
	
public static java.lang.StringgetQueryAsXmlString(java.util.Properties formProperties, java.io.InputStream xslIs)
Slow means of constructing query parsing a stylesheet from an input stream

  		ByteArrayOutputStream baos=new ByteArrayOutputStream();
  		StreamResult result=new StreamResult(baos);
  		transformCriteria(formProperties,xslIs,result);
  		return baos.toString();  		
	
public java.lang.StringgetQueryAsXmlString(java.util.Properties formProperties, java.lang.String queryTemplateName)

		Templates ts=(Templates) compiledTemplatesCache.get(queryTemplateName);
		return getQueryAsXmlString(formProperties, ts);
	
public java.lang.StringgetQueryAsXmlString(java.util.Properties formProperties)

		return getQueryAsXmlString(formProperties, defaultCompiledTemplates);
	
public static java.lang.StringgetQueryAsXmlString(java.util.Properties formProperties, javax.xml.transform.Templates template)
Fast means of constructing query using a precompiled stylesheet

  		ByteArrayOutputStream baos=new ByteArrayOutputStream();
  		StreamResult result=new StreamResult(baos);
  		transformCriteria(formProperties,template,result);
  		return baos.toString();  		
	
public static javax.xml.transform.TemplatesgetTemplates(java.io.InputStream xslIs)
Parses a query stylesheet for repeated use

        dbf.setNamespaceAware(true);	    
		DocumentBuilder builder = dbf.newDocumentBuilder();
		org.w3c.dom.Document xslDoc = builder.parse(xslIs);
		DOMSource ds = new DOMSource(xslDoc);
		return tFactory.newTemplates(ds);
	
public static voidtransformCriteria(java.util.Properties formProperties, java.io.InputStream xslIs, javax.xml.transform.Result result)
Slower transformation using an uncompiled stylesheet (suitable for development environment)

        dbf.setNamespaceAware(true);	    
		DocumentBuilder builder = dbf.newDocumentBuilder();
		org.w3c.dom.Document xslDoc = builder.parse(xslIs);
		DOMSource ds = new DOMSource(xslDoc);
		
		Transformer transformer =null;
		synchronized (tFactory)
		{
			transformer = tFactory.newTransformer(ds);			
		}
		transformCriteria(formProperties,transformer,result);
	
public static voidtransformCriteria(java.util.Properties formProperties, javax.xml.transform.Templates template, javax.xml.transform.Result result)
Fast transformation using a pre-compiled stylesheet (suitable for production environments)

		transformCriteria(formProperties,template.newTransformer(),result);
	
public static voidtransformCriteria(java.util.Properties formProperties, javax.xml.transform.Transformer transformer, javax.xml.transform.Result result)

        dbf.setNamespaceAware(true);
        
	    //Create an XML document representing the search index document.
		DocumentBuilder db = dbf.newDocumentBuilder ();
		org.w3c.dom.Document doc = db.newDocument ();
		Element root = doc.createElement ("Document");
		doc.appendChild (root);
		
		Enumeration keysEnum = formProperties.keys();
		while(keysEnum.hasMoreElements())
		{
		    String propName=(String) keysEnum.nextElement();
		    String value=formProperties.getProperty(propName);
    		if((value!=null)&&(value.length()>0))
    		{
    		    DOMUtils.insertChild(root,propName,value);    			
    		}
		}		
		//Use XSLT to to transform into an XML query string using the  queryTemplate
		DOMSource xml=new DOMSource(doc);
		transformer.transform(xml,result);