1
package org.apache.lucene.queryParser.core.nodes;
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
11
* http://www.apache.org/licenses/LICENSE-2.0
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.
20
import java.util.ArrayList;
21
import java.util.List;
22
import java.util.Locale;
24
import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
25
import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax.Type;
28
* A {@link PathQueryNode} is used to store queries like
29
* /company/USA/California /product/shoes/brown. QueryText are objects that
30
* contain the text, begin position and end position in the query.
32
* Example how the text parser creates these objects:
35
* List values = ArrayList();
36
* values.add(new PathQueryNode.QueryText("company", 1, 7));
37
* values.add(new PathQueryNode.QueryText("USA", 9, 12));
38
* values.add(new PathQueryNode.QueryText("California", 14, 23));
39
* QueryNode q = new PathQueryNode(values);
42
public class PathQueryNode extends QueryNodeImpl {
44
private static final long serialVersionUID = -8325921322405804789L;
46
public static class QueryText implements Cloneable {
47
CharSequence value = null;
49
* != null The term's begin position.
54
* The term's end position.
62
* - position in the query string
64
* - position in the query string
66
public QueryText(CharSequence value, int begin, int end) {
74
public QueryText clone() throws CloneNotSupportedException {
75
QueryText clone = (QueryText) super.clone();
76
clone.value = this.value;
77
clone.begin = this.begin;
85
public CharSequence getValue() {
92
public int getBegin() {
104
public String toString() {
105
return value + ", " + begin + ", " + end;
109
private List<QueryText> values = null;
112
* @param pathElements
113
* - List of QueryText objects
115
public PathQueryNode(List<QueryText> pathElements) {
116
this.values = pathElements;
117
if (pathElements.size() <= 1) {
118
// this should not happen
119
throw new RuntimeException(
120
"PathQuerynode requires more 2 or more path elements.");
125
* Returns the a List with all QueryText elements
127
* @return QueryText List size
129
public List<QueryText> getPathElements() {
134
* Returns the a List with all QueryText elements
136
public void setPathElements(List<QueryText> elements) {
137
this.values = elements;
141
* Returns the a specific QueryText element
143
* @return QueryText List size
145
public QueryText getPathElement(int index) {
146
return values.get(index);
150
* Returns the CharSequence value of a specific QueryText element
152
* @return the CharSequence for a specific QueryText element
154
public CharSequence getFirstPathElement() {
155
return values.get(0).value;
159
* Returns a List QueryText element from position startIndex
161
* @return a List QueryText element from position startIndex
163
public List<QueryText> getPathElements(int startIndex) {
164
List<PathQueryNode.QueryText> rValues = new ArrayList<PathQueryNode.QueryText>();
165
for (int i = startIndex; i < this.values.size(); i++) {
167
rValues.add(this.values.get(i).clone());
168
} catch (CloneNotSupportedException e) {
169
// this will not happen
175
private CharSequence getPathString() {
176
StringBuilder path = new StringBuilder();
178
for (QueryText pathelement : values) {
179
path.append("/").append(pathelement.value);
181
return path.toString();
184
public CharSequence toQueryString(EscapeQuerySyntax escaper) {
185
StringBuilder path = new StringBuilder();
186
path.append("/").append(getFirstPathElement());
188
for (QueryText pathelement : getPathElements(1)) {
189
CharSequence value = escaper.escape(pathelement.value, Locale
190
.getDefault(), Type.STRING);
191
path.append("/\"").append(value).append("\"");
193
return path.toString();
197
public String toString() {
198
QueryText text = this.values.get(0);
200
return "<path start='" + text.begin + "' end='" + text.end + "' path='"
201
+ getPathString() + "'/>";
205
public QueryNode cloneTree() throws CloneNotSupportedException {
206
PathQueryNode clone = (PathQueryNode) super.cloneTree();
209
if (this.values != null) {
210
List<QueryText> localValues = new ArrayList<QueryText>();
211
for (QueryText value : this.values) {
212
localValues.add(value.clone());
214
clone.values = localValues;