pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / xml-query-parser / src / java / org / apache / lucene / xmlparser / builders / BooleanQueryBuilder.java
1 /*
2  * Created on 25-Jan-2006
3  */
4 package org.apache.lucene.xmlparser.builders;
5
6 import org.apache.lucene.search.BooleanClause;
7 import org.apache.lucene.search.BooleanQuery;
8 import org.apache.lucene.search.Query;
9 import org.apache.lucene.xmlparser.DOMUtils;
10 import org.apache.lucene.xmlparser.ParserException;
11 import org.apache.lucene.xmlparser.QueryBuilder;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.Node;
14 import org.w3c.dom.NodeList;
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 /**
33  * 
34  */
35 public class BooleanQueryBuilder implements QueryBuilder {
36         
37         private QueryBuilder factory;
38
39         public BooleanQueryBuilder(QueryBuilder factory)
40         {
41                 this.factory=factory;
42         }
43
44         /* (non-Javadoc)
45          * @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
46          */
47         public Query getQuery(Element e) throws ParserException {
48                 BooleanQuery bq=new BooleanQuery(DOMUtils.getAttribute(e,"disableCoord",false));
49                 bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e,"minimumNumberShouldMatch",0));
50                 bq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
51                 
52                 NodeList nl = e.getChildNodes();
53                 for(int i=0;i<nl.getLength();i++)
54                 {
55                         Node node = nl.item(i);
56                         if(node.getNodeName().equals("Clause"))
57                         {
58                                 Element clauseElem=(Element) node;
59                                 BooleanClause.Occur occurs=getOccursValue(clauseElem);
60                                 
61                                 Element clauseQuery=DOMUtils.getFirstChildOrFail(clauseElem);
62                                 Query q=factory.getQuery(clauseQuery);
63                                 bq.add(new BooleanClause(q,occurs));
64                         }
65                 }
66                 
67                 return bq;
68         }
69         static BooleanClause.Occur getOccursValue(Element clauseElem) throws ParserException
70         {
71                 String occs=clauseElem.getAttribute("occurs");
72                 BooleanClause.Occur occurs=BooleanClause.Occur.SHOULD;
73                 if("must".equalsIgnoreCase(occs))
74                 {
75                         occurs=BooleanClause.Occur.MUST;
76                 }
77                 else
78                 {
79                         if("mustNot".equalsIgnoreCase(occs))
80                         {
81                                 occurs=BooleanClause.Occur.MUST_NOT;
82                         }                       
83                         else
84                         {
85                                 if(("should".equalsIgnoreCase(occs))||("".equals(occs)))
86                                 {
87                                         occurs=BooleanClause.Occur.SHOULD;
88                                 }                       
89                                 else                            
90                                 {
91                                         if(occs!=null)
92                                         {
93                                                 throw new ParserException("Invalid value for \"occurs\" attribute of clause:"+occs);
94                                         }
95                                 }
96                         }
97                 }
98                 return occurs;
99                 
100         }
101
102 }