add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / nodes / FieldQueryNode.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.Locale;
21
22 import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
23 import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax.Type;
24
25 /**
26  * A {@link FieldQueryNode} represents a element that contains field/text tuple
27  */
28 public class FieldQueryNode extends QueryNodeImpl implements FieldValuePairQueryNode<CharSequence>, TextableQueryNode {
29
30   private static final long serialVersionUID = 3634521145130758265L;
31
32   /**
33    * The term's field
34    */
35   protected CharSequence field;
36
37   /**
38    * The term's text.
39    */
40   protected CharSequence text;
41
42   /**
43    * The term's begin position.
44    */
45   protected int begin;
46
47   /**
48    * The term's end position.
49    */
50   protected int end;
51
52   /**
53    * The term's position increment.
54    */
55   protected int positionIncrement;
56
57   /**
58    * @param field
59    *          - field name
60    * @param text
61    *          - value
62    * @param begin
63    *          - position in the query string
64    * @param end
65    *          - position in the query string
66    */
67   public FieldQueryNode(CharSequence field, CharSequence text, int begin,
68       int end) {
69     this.field = field;
70     this.text = text;
71     this.begin = begin;
72     this.end = end;
73     this.setLeaf(true);
74
75   }
76
77   protected CharSequence getTermEscaped(EscapeQuerySyntax escaper) {
78     return escaper.escape(this.text, Locale.getDefault(), Type.NORMAL);
79   }
80
81   protected CharSequence getTermEscapeQuoted(EscapeQuerySyntax escaper) {
82     return escaper.escape(this.text, Locale.getDefault(), Type.STRING);
83   }
84
85   public CharSequence toQueryString(EscapeQuerySyntax escaper) {
86     if (isDefaultField(this.field)) {
87       return getTermEscaped(escaper);
88     } else {
89       return this.field + ":" + getTermEscaped(escaper);
90     }
91   }
92
93   @Override
94   public String toString() {
95     return "<field start='" + this.begin + "' end='" + this.end + "' field='"
96         + this.field + "' text='" + this.text + "'/>";
97   }
98
99   /**
100    * @return the term
101    */
102   public String getTextAsString() {
103     if (this.text == null)
104       return null;
105     else
106       return this.text.toString();
107   }
108
109   /**
110    * returns null if the field was not specified in the query string
111    * 
112    * @return the field
113    */
114   public String getFieldAsString() {
115     if (this.field == null)
116       return null;
117     else
118       return this.field.toString();
119   }
120
121   public int getBegin() {
122     return this.begin;
123   }
124
125   public void setBegin(int begin) {
126     this.begin = begin;
127   }
128
129   public int getEnd() {
130     return this.end;
131   }
132
133   public void setEnd(int end) {
134     this.end = end;
135   }
136
137   public CharSequence getField() {
138     return this.field;
139   }
140
141   public void setField(CharSequence field) {
142     this.field = field;
143   }
144
145   public int getPositionIncrement() {
146     return this.positionIncrement;
147   }
148
149   public void setPositionIncrement(int pi) {
150     this.positionIncrement = pi;
151   }
152
153   /**
154    * Returns the term.
155    * 
156    * @return The "original" form of the term.
157    */
158   public CharSequence getText() {
159     return this.text;
160   }
161
162   /**
163    * @param text
164    *          the text to set
165    */
166   public void setText(CharSequence text) {
167     this.text = text;
168   }
169
170   @Override
171   public FieldQueryNode cloneTree() throws CloneNotSupportedException {
172     FieldQueryNode fqn = (FieldQueryNode) super.cloneTree();
173     fqn.begin = this.begin;
174     fqn.end = this.end;
175     fqn.field = this.field;
176     fqn.text = this.text;
177     fqn.positionIncrement = this.positionIncrement;
178     fqn.toQueryStringIgnoreFields = this.toQueryStringIgnoreFields;
179
180     return fqn;
181
182   }
183
184         public CharSequence getValue() {
185                 return getText();
186         }
187
188         public void setValue(CharSequence value) {
189                 setText(value);
190         }
191
192 }