pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / QueryParserHelper.java
1 package org.apache.lucene.queryParser.core;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.queryParser.core.builders.QueryBuilder;
21 import org.apache.lucene.queryParser.core.config.QueryConfigHandler;
22 import org.apache.lucene.queryParser.core.nodes.QueryNode;
23 import org.apache.lucene.queryParser.core.parser.SyntaxParser;
24 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessor;
25
26 /**
27  * <p>
28  * This class is a helper for the query parser framework, it does all the three
29  * query parser phrases at once: text parsing, query processing and query
30  * building.
31  * </p>
32  * <p>
33  * It contains methods that allows the user to change the implementation used on
34  * the three phases.
35  * </p>
36  * 
37  * @see QueryNodeProcessor
38  * @see SyntaxParser
39  * @see QueryBuilder
40  * @see QueryConfigHandler
41  */
42 public class QueryParserHelper {
43
44   private QueryNodeProcessor processor;
45
46   private SyntaxParser syntaxParser;
47
48   private QueryBuilder builder;
49
50   private QueryConfigHandler config;
51
52   /**
53    * Creates a query parser helper object using the specified configuration,
54    * text parser, processor and builder.
55    * 
56    * @param queryConfigHandler
57    *          the query configuration handler that will be initially set to this
58    *          helper
59    * @param syntaxParser
60    *          the text parser that will be initially set to this helper
61    * @param processor
62    *          the query processor that will be initially set to this helper
63    * @param builder
64    *          the query builder that will be initially set to this helper
65    * 
66    * @see QueryNodeProcessor
67    * @see SyntaxParser
68    * @see QueryBuilder
69    * @see QueryConfigHandler
70    */
71   public QueryParserHelper(QueryConfigHandler queryConfigHandler, SyntaxParser syntaxParser, QueryNodeProcessor processor,
72       QueryBuilder builder) {
73     this.syntaxParser = syntaxParser;
74     this.config = queryConfigHandler;
75     this.processor = processor;
76     this.builder = builder;
77
78     if (processor != null) {
79       processor.setQueryConfigHandler(queryConfigHandler);
80     }
81
82   }
83
84   /**
85    * Returns the processor object used to process the query node tree, it
86    * returns <code>null</code> if no processor is used.
87    * 
88    * @return the actual processor used to process the query node tree,
89    *         <code>null</code> if no processor is used
90    * 
91    * @see QueryNodeProcessor
92    * @see #setQueryNodeProcessor(QueryNodeProcessor)
93    */
94   public QueryNodeProcessor getQueryNodeProcessor() {
95     return processor;
96   }
97
98   /**
99    * Sets the processor that will be used to process the query node tree. If
100    * there is any {@link QueryConfigHandler} returned by
101    * {@link #getQueryConfigHandler()}, it will be set on the processor. The
102    * argument can be <code>null</code>, which means that no processor will be
103    * used to process the query node tree.
104    * 
105    * @param processor
106    *          the processor that will be used to process the query node tree,
107    *          this argument can be <code>null</code>
108    * 
109    * @see #getQueryNodeProcessor()
110    * @see QueryNodeProcessor
111    */
112   public void setQueryNodeProcessor(QueryNodeProcessor processor) {
113     this.processor = processor;
114     this.processor.setQueryConfigHandler(getQueryConfigHandler());
115
116   }
117
118   /**
119    * Sets the text parser that will be used to parse the query string, it cannot
120    * be <code>null</code>.
121    * 
122    * @param syntaxParser
123    *          the text parser that will be used to parse the query string
124    * 
125    * @see #getSyntaxParser()
126    * @see SyntaxParser
127    */
128   public void setSyntaxParser(SyntaxParser syntaxParser) {
129
130     if (syntaxParser == null) {
131       throw new IllegalArgumentException("textParser should not be null!");
132     }
133
134     this.syntaxParser = syntaxParser;
135
136   }
137
138   /**
139    * The query builder that will be used to build an object from the query node
140    * tree. It cannot be <code>null</code>.
141    * 
142    * @param queryBuilder
143    *          the query builder used to build something from the query node tree
144    * 
145    * @see #getQueryBuilder()
146    * @see QueryBuilder
147    */
148   public void setQueryBuilder(QueryBuilder queryBuilder) {
149
150     if (queryBuilder == null) {
151       throw new IllegalArgumentException("queryBuilder should not be null!");
152     }
153
154     this.builder = queryBuilder;
155
156   }
157
158   /**
159    * Returns the query configuration handler, which is used during the query
160    * node tree processing. It can be <code>null</code>.
161    * 
162    * @return the query configuration handler used on the query processing,
163    *         <code>null</code> if not query configuration handler is defined
164    * 
165    * @see QueryConfigHandler
166    * @see #setQueryConfigHandler(QueryConfigHandler)
167    */
168   public QueryConfigHandler getQueryConfigHandler() {
169     return config;
170   }
171
172   /**
173    * Returns the query builder used to build a object from the query node tree.
174    * The object produced by this builder is returned by
175    * {@link #parse(String, String)}.
176    * 
177    * @return the query builder
178    * 
179    * @see #setQueryBuilder(QueryBuilder)
180    * @see QueryBuilder
181    */
182   public QueryBuilder getQueryBuilder() {
183     return this.builder;
184   }
185
186   /**
187    * Returns the text parser used to build a query node tree from a query
188    * string. The default text parser instance returned by this method is a
189    * {@link SyntaxParser}.
190    * 
191    * @return the text parse used to build query node trees.
192    * 
193    * @see SyntaxParser
194    * @see #setSyntaxParser(SyntaxParser)
195    */
196   public SyntaxParser getSyntaxParser() {
197     return this.syntaxParser;
198   }
199
200   /**
201    * Sets the query configuration handler that will be used during query
202    * processing. It can be <code>null</code>. It's also set to the processor
203    * returned by {@link #getQueryNodeProcessor()}.
204    * 
205    * @param config
206    *          the query configuration handler used during query processing, it
207    *          can be <code>null</code>
208    * 
209    * @see #getQueryConfigHandler()
210    * @see QueryConfigHandler
211    */
212   public void setQueryConfigHandler(QueryConfigHandler config) {
213     this.config = config;
214     QueryNodeProcessor processor = getQueryNodeProcessor();
215
216     if (processor != null) {
217       processor.setQueryConfigHandler(config);
218     }
219
220   }
221
222   /**
223    * Parses a query string to an object, usually some query object. <br/>
224    * <br/>
225    * In this method the three phases are executed: <br/>
226    * <br/>
227    * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1st - the query string is parsed using the
228    * text parser returned by {@link #getSyntaxParser()}, the result is a query
229    * node tree <br/>
230    * <br/>
231    * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2nd - the query node tree is processed by the
232    * processor returned by {@link #getQueryNodeProcessor()} <br/>
233    * <br/>
234    * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3th - a object is built from the query node
235    * tree using the builder returned by {@link #getQueryBuilder()}
236    * 
237    * @param query
238    *          the query string
239    * @param defaultField
240    *          the default field used by the text parser
241    * 
242    * @return the object built from the query
243    * 
244    * @throws QueryNodeException
245    *           if something wrong happens along the three phases
246    */
247   public Object parse(String query, String defaultField)
248       throws QueryNodeException {
249     QueryNode queryTree = getSyntaxParser().parse(query, defaultField);
250
251     QueryNodeProcessor processor = getQueryNodeProcessor();
252
253     if (processor != null) {
254       queryTree = processor.process(queryTree);
255     }
256
257     return getQueryBuilder().build(queryTree);
258
259   }
260
261 }