~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/core/nodes/PathQueryNode.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.ArrayList;
21
 
import java.util.List;
22
 
import java.util.Locale;
23
 
 
24
 
import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
25
 
import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax.Type;
26
 
 
27
 
/**
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.
31
 
 * <p>
32
 
 * Example how the text parser creates these objects:
33
 
 * </p>
34
 
 * <pre>
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);
40
 
 * </pre>
41
 
 */
42
 
public class PathQueryNode extends QueryNodeImpl {
43
 
 
44
 
  private static final long serialVersionUID = -8325921322405804789L;
45
 
 
46
 
  public static class QueryText implements Cloneable {
47
 
    CharSequence value = null;
48
 
    /**
49
 
     * != null The term's begin position.
50
 
     */
51
 
    int begin;
52
 
 
53
 
    /**
54
 
     * The term's end position.
55
 
     */
56
 
    int end;
57
 
 
58
 
    /**
59
 
     * @param value
60
 
     *          - text value
61
 
     * @param begin
62
 
     *          - position in the query string
63
 
     * @param end
64
 
     *          - position in the query string
65
 
     */
66
 
    public QueryText(CharSequence value, int begin, int end) {
67
 
      super();
68
 
      this.value = value;
69
 
      this.begin = begin;
70
 
      this.end = end;
71
 
    }
72
 
 
73
 
    @Override
74
 
    public QueryText clone() throws CloneNotSupportedException {
75
 
      QueryText clone = (QueryText) super.clone();
76
 
      clone.value = this.value;
77
 
      clone.begin = this.begin;
78
 
      clone.end = this.end;
79
 
      return clone;
80
 
    }
81
 
 
82
 
    /**
83
 
     * @return the value
84
 
     */
85
 
    public CharSequence getValue() {
86
 
      return value;
87
 
    }
88
 
 
89
 
    /**
90
 
     * @return the begin
91
 
     */
92
 
    public int getBegin() {
93
 
      return begin;
94
 
    }
95
 
 
96
 
    /**
97
 
     * @return the end
98
 
     */
99
 
    public int getEnd() {
100
 
      return end;
101
 
    }
102
 
 
103
 
    @Override
104
 
    public String toString() {
105
 
      return value + ", " + begin + ", " + end;
106
 
    }
107
 
  }
108
 
 
109
 
  private List<QueryText> values = null;
110
 
 
111
 
  /**
112
 
   * @param pathElements
113
 
   *          - List of QueryText objects
114
 
   */
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.");
121
 
    }
122
 
  }
123
 
 
124
 
  /**
125
 
   * Returns the a List with all QueryText elements
126
 
   * 
127
 
   * @return QueryText List size
128
 
   */
129
 
  public List<QueryText> getPathElements() {
130
 
    return values;
131
 
  }
132
 
 
133
 
  /**
134
 
   * Returns the a List with all QueryText elements
135
 
   */
136
 
  public void setPathElements(List<QueryText> elements) {
137
 
    this.values = elements;
138
 
  }
139
 
 
140
 
  /**
141
 
   * Returns the a specific QueryText element
142
 
   * 
143
 
   * @return QueryText List size
144
 
   */
145
 
  public QueryText getPathElement(int index) {
146
 
    return values.get(index);
147
 
  }
148
 
 
149
 
  /**
150
 
   * Returns the CharSequence value of a specific QueryText element
151
 
   * 
152
 
   * @return the CharSequence for a specific QueryText element
153
 
   */
154
 
  public CharSequence getFirstPathElement() {
155
 
    return values.get(0).value;
156
 
  }
157
 
 
158
 
  /**
159
 
   * Returns a List QueryText element from position startIndex
160
 
   * 
161
 
   * @return a List QueryText element from position startIndex
162
 
   */
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++) {
166
 
      try {
167
 
        rValues.add(this.values.get(i).clone());
168
 
      } catch (CloneNotSupportedException e) {
169
 
        // this will not happen
170
 
      }
171
 
    }
172
 
    return rValues;
173
 
  }
174
 
 
175
 
  private CharSequence getPathString() {
176
 
    StringBuilder path = new StringBuilder();
177
 
 
178
 
    for (QueryText pathelement : values) {
179
 
      path.append("/").append(pathelement.value);
180
 
    }
181
 
    return path.toString();
182
 
  }
183
 
 
184
 
  public CharSequence toQueryString(EscapeQuerySyntax escaper) {
185
 
    StringBuilder path = new StringBuilder();
186
 
    path.append("/").append(getFirstPathElement());
187
 
 
188
 
    for (QueryText pathelement : getPathElements(1)) {
189
 
      CharSequence value = escaper.escape(pathelement.value, Locale
190
 
          .getDefault(), Type.STRING);
191
 
      path.append("/\"").append(value).append("\"");
192
 
    }
193
 
    return path.toString();
194
 
  }
195
 
 
196
 
  @Override
197
 
  public String toString() {
198
 
    QueryText text = this.values.get(0);
199
 
 
200
 
    return "<path start='" + text.begin + "' end='" + text.end + "' path='"
201
 
        + getPathString() + "'/>";
202
 
  }
203
 
 
204
 
  @Override
205
 
  public QueryNode cloneTree() throws CloneNotSupportedException {
206
 
    PathQueryNode clone = (PathQueryNode) super.cloneTree();
207
 
 
208
 
    // copy children
209
 
    if (this.values != null) {
210
 
      List<QueryText> localValues = new ArrayList<QueryText>();
211
 
      for (QueryText value : this.values) {
212
 
        localValues.add(value.clone());
213
 
      }
214
 
      clone.values = localValues;
215
 
    }
216
 
 
217
 
    return clone;
218
 
  }
219
 
 
220
 
}