add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / surround / query / SrndPrefixQuery.java
1 package org.apache.lucene.queryParser.surround.query;
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import org.apache.lucene.index.Term;
20 import org.apache.lucene.index.TermEnum;
21 import org.apache.lucene.index.IndexReader;
22
23 import java.io.IOException;
24
25
26 public class SrndPrefixQuery extends SimpleTerm {
27   public SrndPrefixQuery(String prefix, boolean quoted, char truncator) {
28     super(quoted);
29     this.prefix = prefix;
30     this.truncator = truncator;
31   }
32
33   private final String prefix;
34   public String getPrefix() {return prefix;}
35   
36   private final char truncator;
37   public char getSuffixOperator() {return truncator;}
38   
39   public Term getLucenePrefixTerm(String fieldName) {
40     return new Term(fieldName, getPrefix());
41   }
42   
43   @Override
44   public String toStringUnquoted() {return getPrefix();}
45   
46   @Override
47   protected void suffixToString(StringBuilder r) {r.append(getSuffixOperator());}
48   
49   @Override
50   public void visitMatchingTerms(
51     IndexReader reader,
52     String fieldName,
53     MatchingTermVisitor mtv) throws IOException
54   {
55     /* inspired by PrefixQuery.rewrite(): */
56     TermEnum enumerator = reader.terms(getLucenePrefixTerm(fieldName));
57     try {
58       do {
59         Term term = enumerator.term();
60         if ((term != null)
61             && term.text().startsWith(getPrefix())
62             && term.field().equals(fieldName)) {
63           mtv.visitMatchingTerm(term);
64         } else {
65           break;
66         }
67       } while (enumerator.next());
68     } finally {
69       enumerator.close();
70     }
71   }
72 }