add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / nodes / MultiPhraseQueryNode.java
1 package org.apache.lucene.queryParser.standard.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.nodes.FieldableNode;
23 import org.apache.lucene.queryParser.core.nodes.QueryNode;
24 import org.apache.lucene.queryParser.core.nodes.QueryNodeImpl;
25 import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
26 import org.apache.lucene.search.MultiPhraseQuery;
27 import org.apache.lucene.search.PhraseQuery;
28
29 /**
30  * A {@link MultiPhraseQueryNode} indicates that its children should be used to
31  * build a {@link MultiPhraseQuery} instead of {@link PhraseQuery}.
32  */
33 public class MultiPhraseQueryNode extends QueryNodeImpl implements
34     FieldableNode {
35
36   private static final long serialVersionUID = -2138501723963320158L;
37
38   public MultiPhraseQueryNode() {
39     setLeaf(false);
40     allocate();
41
42   }
43
44   @Override
45   public String toString() {
46     if (getChildren() == null || getChildren().size() == 0)
47       return "<multiPhrase/>";
48     StringBuilder sb = new StringBuilder();
49     sb.append("<multiPhrase>");
50     for (QueryNode child : getChildren()) {
51       sb.append("\n");
52       sb.append(child.toString());
53     }
54     sb.append("\n</multiPhrase>");
55     return sb.toString();
56   }
57
58   public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) {
59     if (getChildren() == null || getChildren().size() == 0)
60       return "";
61
62     StringBuilder sb = new StringBuilder();
63     String filler = "";
64     for (QueryNode child : getChildren()) {
65       sb.append(filler).append(child.toQueryString(escapeSyntaxParser));
66       filler = ",";
67     }
68
69     return "[MTP[" + sb.toString() + "]]";
70   }
71
72   @Override
73   public QueryNode cloneTree() throws CloneNotSupportedException {
74     MultiPhraseQueryNode clone = (MultiPhraseQueryNode) super.cloneTree();
75
76     // nothing to do
77
78     return clone;
79   }
80
81   public CharSequence getField() {
82     List<QueryNode> children = getChildren();
83
84     if (children == null || children.size() == 0) {
85       return null;
86
87     } else {
88       return ((FieldableNode) children.get(0)).getField();
89     }
90
91   }
92
93   public void setField(CharSequence fieldName) {
94     List<QueryNode> children = getChildren();
95
96     if (children != null) {
97
98       for (QueryNode child : children) {
99
100         if (child instanceof FieldableNode) {
101           ((FieldableNode) child).setField(fieldName);
102         }
103
104       }
105
106     }
107
108   }
109
110 } // end class MultitermQueryNode