Home » lucene-3.0.1-src » org.apache » lucene » xmlparser » [javadoc | source]

    1   package org.apache.lucene.xmlparser;
    2   
    3   import java.io.InputStream;
    4   
    5   import javax.xml.parsers.DocumentBuilder;
    6   import javax.xml.parsers.DocumentBuilderFactory;
    7   
    8   import org.apache.lucene.analysis.Analyzer;
    9   import org.apache.lucene.queryParser.QueryParser;
   10   import org.apache.lucene.search.Query;
   11   import org.apache.lucene.xmlparser.builders;
   12   import org.w3c.dom.Document;
   13   import org.w3c.dom.Element;
   14   
   15   /**
   16    * Licensed to the Apache Software Foundation (ASF) under one or more
   17    * contributor license agreements.  See the NOTICE file distributed with
   18    * this work for additional information regarding copyright ownership.
   19    * The ASF licenses this file to You under the Apache License, Version 2.0
   20    * (the "License"); you may not use this file except in compliance with
   21    * the License.  You may obtain a copy of the License at
   22    *
   23    *     http://www.apache.org/licenses/LICENSE-2.0
   24    *
   25    * Unless required by applicable law or agreed to in writing, software
   26    * distributed under the License is distributed on an "AS IS" BASIS,
   27    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   28    * See the License for the specific language governing permissions and
   29    * limitations under the License.
   30    */
   31   /**
   32    * Assembles a QueryBuilder which uses only core Lucene Query objects
   33    *
   34    */
   35   public class CoreParser implements QueryBuilder
   36   {
   37   	
   38   	protected Analyzer analyzer;
   39   	protected QueryParser parser;
   40   	protected QueryBuilderFactory queryFactory;
   41   	protected FilterBuilderFactory filterFactory;
   42   	//Controls the max size of the LRU cache used for QueryFilter objects parsed.
   43   	public static int maxNumCachedFilters=20;
   44   
   45   
   46   	/**
   47   	 * Construct an XML parser that uses a single instance QueryParser for handling 
   48   	 * UserQuery tags - all parse operations are synchronised on this parser
   49   	 * @param analyzer
   50   	 * @param parser A QueryParser which will be synchronized on during parse calls.
   51   	 */
   52   	public CoreParser(Analyzer analyzer, QueryParser parser)
   53   	{
   54   		this(null,analyzer,parser);
   55   	}
   56   	
   57   	/**
   58   	 * Constructs an XML parser that creates a QueryParser for each UserQuery request.
   59   	 * @param defaultField The default field name used by QueryParsers constructed for UserQuery tags 
   60   	 * @param analyzer 
   61   	 */
   62   	public CoreParser(String defaultField, Analyzer analyzer)
   63   	{
   64   		this(defaultField,analyzer,null);
   65   	}	
   66   	
   67   	protected CoreParser(String defaultField,Analyzer analyzer, QueryParser parser)
   68   	{
   69   		this.analyzer=analyzer;
   70   		this.parser=parser;
   71   		filterFactory = new FilterBuilderFactory();
   72   		filterFactory.addBuilder("RangeFilter",new RangeFilterBuilder());
   73   		
   74   		
   75   		queryFactory = new QueryBuilderFactory();
   76   		queryFactory.addBuilder("TermQuery",new TermQueryBuilder());
   77   		queryFactory.addBuilder("TermsQuery",new TermsQueryBuilder(analyzer));
   78   		queryFactory.addBuilder("MatchAllDocsQuery",new MatchAllDocsQueryBuilder());
   79   		queryFactory.addBuilder("BooleanQuery",new BooleanQueryBuilder(queryFactory));
   80   		if(parser!=null)
   81   		{
   82   			queryFactory.addBuilder("UserQuery",new UserInputQueryBuilder(parser));
   83   		}
   84   		else
   85   		{
   86   			queryFactory.addBuilder("UserQuery",new UserInputQueryBuilder(defaultField,analyzer));			
   87   		}
   88   		queryFactory.addBuilder("FilteredQuery",new FilteredQueryBuilder(filterFactory,queryFactory));
   89   		queryFactory.addBuilder("ConstantScoreQuery",new ConstantScoreQueryBuilder(filterFactory));
   90   		
   91   		filterFactory.addBuilder("CachedFilter",new CachedFilterBuilder(queryFactory,
   92   							filterFactory, maxNumCachedFilters));
   93   		
   94   		
   95   		SpanQueryBuilderFactory sqof=new SpanQueryBuilderFactory();
   96   
   97   		SpanNearBuilder snb=new SpanNearBuilder(sqof);
   98   		sqof.addBuilder("SpanNear",snb);
   99   		queryFactory.addBuilder("SpanNear",snb);
  100   
  101       BoostingTermBuilder btb=new BoostingTermBuilder();
  102       sqof.addBuilder("BoostingTermQuery",btb);
  103       queryFactory.addBuilder("BoostingTermQuery",btb);        
  104   
  105       SpanTermBuilder snt=new SpanTermBuilder();
  106   		sqof.addBuilder("SpanTerm",snt);
  107   		queryFactory.addBuilder("SpanTerm",snt);
  108   		
  109   		SpanOrBuilder sot=new SpanOrBuilder(sqof);
  110   		sqof.addBuilder("SpanOr",sot);
  111   		queryFactory.addBuilder("SpanOr",sot);
  112   
  113   		SpanOrTermsBuilder sots=new SpanOrTermsBuilder(analyzer);
  114   		sqof.addBuilder("SpanOrTerms",sots);
  115   		queryFactory.addBuilder("SpanOrTerms",sots);		
  116   		
  117   		SpanFirstBuilder sft=new SpanFirstBuilder(sqof);
  118   		sqof.addBuilder("SpanFirst",sft);
  119   		queryFactory.addBuilder("SpanFirst",sft);
  120   		
  121   		SpanNotBuilder snot=new SpanNotBuilder(sqof);
  122   		sqof.addBuilder("SpanNot",snot);
  123   		queryFactory.addBuilder("SpanNot",snot);	
  124   	}
  125   	
  126   	public Query parse(InputStream xmlStream) throws ParserException
  127   	{
  128   		return getQuery(parseXML(xmlStream).getDocumentElement());
  129   	}
  130   	
  131   	public void addQueryBuilder(String nodeName,QueryBuilder builder)
  132   	{
  133   		queryFactory.addBuilder(nodeName,builder);
  134   	}
  135   	public void addFilterBuilder(String nodeName,FilterBuilder builder)
  136   	{
  137   		filterFactory.addBuilder(nodeName,builder);
  138   	}
  139   	
  140   	private static Document parseXML(InputStream pXmlFile) throws ParserException
  141   	{
  142   		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  143   		DocumentBuilder db = null;
  144   		try
  145   		{
  146   			db = dbf.newDocumentBuilder();
  147   		}
  148   		catch (Exception se)
  149   		{
  150   			throw new ParserException("XML Parser configuration error", se);
  151   		}
  152   		org.w3c.dom.Document doc = null;
  153   		try
  154   		{
  155   			doc = db.parse(pXmlFile);
  156   		}
  157   		catch (Exception se)
  158   		{
  159   			throw new ParserException("Error parsing XML stream:" + se, se);
  160   		}
  161   		return doc;
  162   	}
  163   	
  164   
  165   	public Query getQuery(Element e) throws ParserException
  166   	{
  167   		return queryFactory.getQuery(e);
  168   	}
  169   }

Home » lucene-3.0.1-src » org.apache » lucene » xmlparser » [javadoc | source]