pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / nodes / BooleanQueryNode.java
1 package org.apache.lucene.queryParser.core.nodes;
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.queryParser.core.parser.EscapeQuerySyntax;
23
24 /**
25  * A {@link BooleanQueryNode} represents a list of elements which do not have an
26  * explicit boolean operator defined between them. It can be used to express a
27  * boolean query that intends to use the default boolean operator.
28  */
29 public class BooleanQueryNode extends QueryNodeImpl {
30
31   private static final long serialVersionUID = -2206623652088638072L;
32
33   /**
34    * @param clauses
35    *          - the query nodes to be and'ed
36    */
37   public BooleanQueryNode(List<QueryNode> clauses) {
38     setLeaf(false);
39     allocate();
40     set(clauses);
41   }
42
43   @Override
44   public String toString() {
45     if (getChildren() == null || getChildren().size() == 0)
46       return "<boolean operation='default'/>";
47     StringBuilder sb = new StringBuilder();
48     sb.append("<boolean operation='default'>");
49     for (QueryNode child : getChildren()) {
50       sb.append("\n");
51       sb.append(child.toString());
52     }
53     sb.append("\n</boolean>");
54     return sb.toString();
55   }
56
57   public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) {
58     if (getChildren() == null || getChildren().size() == 0)
59       return "";
60
61     StringBuilder sb = new StringBuilder();
62     String filler = "";
63     for (QueryNode child : getChildren()) {
64       sb.append(filler).append(child.toQueryString(escapeSyntaxParser));
65       filler = " ";
66     }
67
68     // in case is root or the parent is a group node avoid parenthesis
69     if ((getParent() != null && getParent() instanceof GroupQueryNode)
70         || isRoot())
71       return sb.toString();
72     else
73       return "( " + sb.toString() + " )";
74   }
75
76   @Override
77   public QueryNode cloneTree() throws CloneNotSupportedException {
78     BooleanQueryNode clone = (BooleanQueryNode) super.cloneTree();
79
80     // nothing to do here
81
82     return clone;
83   }
84
85 }