add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / builders / BooleanQueryNodeBuilder.java
1 package org.apache.lucene.queryParser.standard.builders;
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 java.util.List;
21
22 import org.apache.lucene.messages.MessageImpl;
23 import org.apache.lucene.queryParser.core.QueryNodeException;
24 import org.apache.lucene.queryParser.core.builders.QueryTreeBuilder;
25 import org.apache.lucene.queryParser.core.messages.QueryParserMessages;
26 import org.apache.lucene.queryParser.core.nodes.BooleanQueryNode;
27 import org.apache.lucene.queryParser.core.nodes.ModifierQueryNode;
28 import org.apache.lucene.queryParser.core.nodes.QueryNode;
29 import org.apache.lucene.queryParser.standard.parser.EscapeQuerySyntaxImpl;
30 import org.apache.lucene.search.BooleanClause;
31 import org.apache.lucene.search.BooleanQuery;
32 import org.apache.lucene.search.Query;
33 import org.apache.lucene.search.BooleanQuery.TooManyClauses;
34
35 /**
36  * Builds a {@link BooleanQuery} object from a {@link BooleanQueryNode} object.
37  * Every children in the {@link BooleanQueryNode} object must be already tagged
38  * using {@link QueryTreeBuilder#QUERY_TREE_BUILDER_TAGID} with a {@link Query}
39  * object. <br/>
40  * <br/>
41  * It takes in consideration if the children is a {@link ModifierQueryNode} to
42  * define the {@link BooleanClause}.
43  */
44 public class BooleanQueryNodeBuilder implements StandardQueryBuilder {
45
46   public BooleanQueryNodeBuilder() {
47     // empty constructor
48   }
49
50   public BooleanQuery build(QueryNode queryNode) throws QueryNodeException {
51     BooleanQueryNode booleanNode = (BooleanQueryNode) queryNode;
52
53     BooleanQuery bQuery = new BooleanQuery();
54     List<QueryNode> children = booleanNode.getChildren();
55
56     if (children != null) {
57
58       for (QueryNode child : children) {
59         Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
60
61         if (obj != null) {
62           Query query = (Query) obj;
63
64           try {
65             bQuery.add(query, getModifierValue(child));
66
67           } catch (TooManyClauses ex) {
68
69             throw new QueryNodeException(new MessageImpl(
70                 QueryParserMessages.TOO_MANY_BOOLEAN_CLAUSES, BooleanQuery
71                     .getMaxClauseCount(), queryNode
72                     .toQueryString(new EscapeQuerySyntaxImpl())), ex);
73
74           }
75
76         }
77
78       }
79
80     }
81
82     return bQuery;
83
84   }
85
86   private static BooleanClause.Occur getModifierValue(QueryNode node)
87       throws QueryNodeException {
88
89     if (node instanceof ModifierQueryNode) {
90       ModifierQueryNode mNode = ((ModifierQueryNode) node);
91       switch (mNode.getModifier()) {
92
93       case MOD_REQ:
94         return BooleanClause.Occur.MUST;
95
96       case MOD_NOT:
97         return BooleanClause.Occur.MUST_NOT;
98
99       case MOD_NONE:
100         return BooleanClause.Occur.SHOULD;
101
102       }
103
104     }
105
106     return BooleanClause.Occur.SHOULD;
107
108   }
109
110 }