001: package org.apache.lucene.xmlparser;
002:
003: import java.io.InputStream;
004:
005: import javax.xml.parsers.DocumentBuilder;
006: import javax.xml.parsers.DocumentBuilderFactory;
007:
008: import org.apache.lucene.analysis.Analyzer;
009: import org.apache.lucene.queryParser.QueryParser;
010: import org.apache.lucene.search.Query;
011: import org.apache.lucene.xmlparser.builders.BooleanQueryBuilder;
012: import org.apache.lucene.xmlparser.builders.ConstantScoreQueryBuilder;
013: import org.apache.lucene.xmlparser.builders.FilteredQueryBuilder;
014: import org.apache.lucene.xmlparser.builders.MatchAllDocsQueryBuilder;
015: import org.apache.lucene.xmlparser.builders.CachedFilterBuilder;
016: import org.apache.lucene.xmlparser.builders.RangeFilterBuilder;
017: import org.apache.lucene.xmlparser.builders.SpanFirstBuilder;
018: import org.apache.lucene.xmlparser.builders.SpanNearBuilder;
019: import org.apache.lucene.xmlparser.builders.SpanNotBuilder;
020: import org.apache.lucene.xmlparser.builders.SpanOrBuilder;
021: import org.apache.lucene.xmlparser.builders.SpanOrTermsBuilder;
022: import org.apache.lucene.xmlparser.builders.SpanQueryBuilderFactory;
023: import org.apache.lucene.xmlparser.builders.SpanTermBuilder;
024: import org.apache.lucene.xmlparser.builders.TermQueryBuilder;
025: import org.apache.lucene.xmlparser.builders.TermsQueryBuilder;
026: import org.apache.lucene.xmlparser.builders.UserInputQueryBuilder;
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029:
030: /**
031: * Licensed to the Apache Software Foundation (ASF) under one or more
032: * contributor license agreements. See the NOTICE file distributed with
033: * this work for additional information regarding copyright ownership.
034: * The ASF licenses this file to You under the Apache License, Version 2.0
035: * (the "License"); you may not use this file except in compliance with
036: * the License. You may obtain a copy of the License at
037: *
038: * http://www.apache.org/licenses/LICENSE-2.0
039: *
040: * Unless required by applicable law or agreed to in writing, software
041: * distributed under the License is distributed on an "AS IS" BASIS,
042: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
043: * See the License for the specific language governing permissions and
044: * limitations under the License.
045: */
046: /**
047: * Assembles a QueryBuilder which uses only core Lucene Query objects
048: * @author Mark
049: *
050: */
051: public class CoreParser implements QueryBuilder {
052:
053: protected Analyzer analyzer;
054: protected QueryParser parser;
055: protected QueryBuilderFactory queryFactory;
056: protected FilterBuilderFactory filterFactory;
057: //Controls the max size of the LRU cache used for QueryFilter objects parsed.
058: public static int maxNumCachedFilters = 20;
059:
060: public CoreParser(Analyzer analyzer, QueryParser parser) {
061: this .analyzer = analyzer;
062: this .parser = parser;
063: filterFactory = new FilterBuilderFactory();
064: filterFactory.addBuilder("RangeFilter",
065: new RangeFilterBuilder());
066:
067: queryFactory = new QueryBuilderFactory();
068: queryFactory.addBuilder("TermQuery", new TermQueryBuilder());
069: queryFactory.addBuilder("TermsQuery", new TermsQueryBuilder(
070: analyzer));
071: queryFactory.addBuilder("MatchAllDocsQuery",
072: new MatchAllDocsQueryBuilder());
073: queryFactory.addBuilder("BooleanQuery",
074: new BooleanQueryBuilder(queryFactory));
075: queryFactory.addBuilder("UserQuery", new UserInputQueryBuilder(
076: parser));
077: queryFactory.addBuilder("FilteredQuery",
078: new FilteredQueryBuilder(filterFactory, queryFactory));
079: queryFactory.addBuilder("ConstantScoreQuery",
080: new ConstantScoreQueryBuilder(filterFactory));
081:
082: filterFactory.addBuilder("CachedFilter",
083: new CachedFilterBuilder(queryFactory, filterFactory,
084: maxNumCachedFilters));
085:
086: SpanQueryBuilderFactory sqof = new SpanQueryBuilderFactory();
087:
088: SpanNearBuilder snb = new SpanNearBuilder(sqof);
089: sqof.addBuilder("SpanNear", snb);
090: queryFactory.addBuilder("SpanNear", snb);
091:
092: SpanTermBuilder snt = new SpanTermBuilder();
093: sqof.addBuilder("SpanTerm", snt);
094: queryFactory.addBuilder("SpanTerm", snt);
095:
096: SpanOrBuilder sot = new SpanOrBuilder(sqof);
097: sqof.addBuilder("SpanOr", sot);
098: queryFactory.addBuilder("SpanOr", sot);
099:
100: SpanOrTermsBuilder sots = new SpanOrTermsBuilder(analyzer);
101: sqof.addBuilder("SpanOrTerms", sots);
102: queryFactory.addBuilder("SpanOrTerms", sots);
103:
104: SpanFirstBuilder sft = new SpanFirstBuilder(sqof);
105: sqof.addBuilder("SpanFirst", sft);
106: queryFactory.addBuilder("SpanFirst", sft);
107:
108: SpanNotBuilder snot = new SpanNotBuilder(sqof);
109: sqof.addBuilder("SpanNot", snot);
110: queryFactory.addBuilder("SpanNot", snot);
111: }
112:
113: public Query parse(InputStream xmlStream) throws ParserException {
114: return getQuery(parseXML(xmlStream).getDocumentElement());
115: }
116:
117: public void addQueryBuilder(String nodeName, QueryBuilder builder) {
118: queryFactory.addBuilder(nodeName, builder);
119: }
120:
121: public void addFilterBuilder(String nodeName, FilterBuilder builder) {
122: filterFactory.addBuilder(nodeName, builder);
123: }
124:
125: private static Document parseXML(InputStream pXmlFile)
126: throws ParserException {
127: DocumentBuilderFactory dbf = DocumentBuilderFactory
128: .newInstance();
129: DocumentBuilder db = null;
130: try {
131: db = dbf.newDocumentBuilder();
132: } catch (Exception se) {
133: throw new ParserException("XML Parser configuration error",
134: se);
135: }
136: org.w3c.dom.Document doc = null;
137: try {
138: doc = db.parse(pXmlFile);
139: } catch (Exception se) {
140: throw new ParserException("Error parsing XML stream:" + se,
141: se);
142: }
143: return doc;
144: }
145:
146: public Query getQuery(Element e) throws ParserException {
147: return queryFactory.getQuery(e);
148: }
149: }
|