add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / xml-query-parser / src / java / org / apache / lucene / xmlparser / builders / UserInputQueryBuilder.java
1 package org.apache.lucene.xmlparser.builders;
2
3 import org.apache.lucene.analysis.Analyzer;
4 import org.apache.lucene.queryParser.ParseException;
5 import org.apache.lucene.queryParser.QueryParser;
6 import org.apache.lucene.search.Query;
7 import org.apache.lucene.xmlparser.DOMUtils;
8 import org.apache.lucene.xmlparser.ParserException;
9 import org.apache.lucene.xmlparser.QueryBuilder;
10 import org.w3c.dom.Element;
11 import org.apache.lucene.util.Version;
12
13 /**
14  * Licensed to the Apache Software Foundation (ASF) under one or more
15  * contributor license agreements.  See the NOTICE file distributed with
16  * this work for additional information regarding copyright ownership.
17  * The ASF licenses this file to You under the Apache License, Version 2.0
18  * (the "License"); you may not use this file except in compliance with
19  * the License.  You may obtain a copy of the License at
20  *
21  *     http://www.apache.org/licenses/LICENSE-2.0
22  *
23  * Unless required by applicable law or agreed to in writing, software
24  * distributed under the License is distributed on an "AS IS" BASIS,
25  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26  * See the License for the specific language governing permissions and
27  * limitations under the License.
28  */
29
30 /**
31  * UserInputQueryBuilder uses 1 of 2 strategies for thread-safe parsing:
32  * 1) Synchronizing access to "parse" calls on a previously supplied QueryParser
33  * or..
34  * 2) creating a new QueryParser object for each parse request
35  */
36 public class UserInputQueryBuilder implements QueryBuilder {
37
38         QueryParser unSafeParser;
39         private Analyzer analyzer;
40         private String defaultField;
41         
42         /**
43          * This constructor has the disadvantage of not being able to change choice of default field name
44          * @param parser thread un-safe query parser
45          */
46         public UserInputQueryBuilder(QueryParser parser) {
47                 this.unSafeParser = parser;
48         }
49
50         public UserInputQueryBuilder(String defaultField, Analyzer analyzer) {
51                 this.analyzer = analyzer;
52                 this.defaultField = defaultField;
53         }
54         
55         /* (non-Javadoc)
56          * @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
57          */
58         public Query getQuery(Element e) throws ParserException {
59                 String text=DOMUtils.getText(e);
60                 try {
61                         Query q = null;
62                         if(unSafeParser!=null)
63                         {
64                                 //synchronize on unsafe parser
65                                 synchronized (unSafeParser)
66                                 {
67                                         q = unSafeParser.parse(text);
68                                 }
69                         }
70                         else
71                         {
72                                 String fieldName=DOMUtils.getAttribute(e, "fieldName", defaultField);
73                                 //Create new parser
74                                 QueryParser parser=createQueryParser(fieldName, analyzer);
75                                 q = parser.parse(text);                         
76                         }
77                         // use the boost of the original query here, too and multiply (which may be != 1.0f):
78                         q.setBoost(q.getBoost()*DOMUtils.getAttribute(e,"boost",1.0f));
79                         return q;
80                 } catch (ParseException e1) {
81                         throw new ParserException(e1.getMessage());
82                 }
83         }
84         
85         /**
86          * Method to create a QueryParser - designed to be overridden
87          * @param fieldName
88          * @param analyzer
89          * @return QueryParser
90          */
91         protected QueryParser createQueryParser(String fieldName, Analyzer analyzer)
92         {
93                 return new QueryParser(Version.LUCENE_CURRENT, fieldName,analyzer);
94         }
95
96 }