add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / builders / StandardBooleanQueryNodeBuilder.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.ModifierQueryNode;
27 import org.apache.lucene.queryParser.core.nodes.QueryNode;
28 import org.apache.lucene.queryParser.core.nodes.ModifierQueryNode.Modifier;
29 import org.apache.lucene.queryParser.standard.nodes.StandardBooleanQueryNode;
30 import org.apache.lucene.queryParser.standard.parser.EscapeQuerySyntaxImpl;
31 import org.apache.lucene.search.BooleanClause;
32 import org.apache.lucene.search.BooleanQuery;
33 import org.apache.lucene.search.Query;
34 import org.apache.lucene.search.Similarity;
35 import org.apache.lucene.search.BooleanQuery.TooManyClauses;
36
37 /**
38  * This builder does the same as the {@link BooleanQueryNodeBuilder}, but this
39  * considers if the built {@link BooleanQuery} should have its coord disabled or
40  * not. <br/>
41  * 
42  * @see BooleanQueryNodeBuilder
43  * @see BooleanQuery
44  * @see Similarity#coord(int, int)
45  */
46 public class StandardBooleanQueryNodeBuilder implements StandardQueryBuilder {
47
48   public StandardBooleanQueryNodeBuilder() {
49     // empty constructor
50   }
51
52   public BooleanQuery build(QueryNode queryNode) throws QueryNodeException {
53     StandardBooleanQueryNode booleanNode = (StandardBooleanQueryNode) queryNode;
54
55     BooleanQuery bQuery = new BooleanQuery(booleanNode.isDisableCoord());
56     List<QueryNode> children = booleanNode.getChildren();
57
58     if (children != null) {
59
60       for (QueryNode child : children) {
61         Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
62
63         if (obj != null) {
64           Query query = (Query) obj;
65
66           try {
67             bQuery.add(query, getModifierValue(child));
68           } catch (TooManyClauses ex) {
69
70             throw new QueryNodeException(new MessageImpl(
71                 QueryParserMessages.TOO_MANY_BOOLEAN_CLAUSES, BooleanQuery
72                     .getMaxClauseCount(), queryNode
73                     .toQueryString(new EscapeQuerySyntaxImpl())), ex);
74
75           }
76
77         }
78
79       }
80
81     }
82
83     return bQuery;
84
85   }
86
87   private static BooleanClause.Occur getModifierValue(QueryNode node)
88       throws QueryNodeException {
89
90     if (node instanceof ModifierQueryNode) {
91       ModifierQueryNode mNode = ((ModifierQueryNode) node);
92       Modifier modifier = mNode.getModifier();
93
94       if (Modifier.MOD_NONE.equals(modifier)) {
95         return BooleanClause.Occur.SHOULD;
96
97       } else if (Modifier.MOD_NOT.equals(modifier)) {
98         return BooleanClause.Occur.MUST_NOT;
99
100       } else {
101         return BooleanClause.Occur.MUST;
102       }
103     }
104
105     return BooleanClause.Occur.SHOULD;
106
107   }
108
109 }