pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / surround / query / SrndTruncQuery.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 import java.util.regex.Pattern;
26 import java.util.regex.Matcher;
27
28
29 public class SrndTruncQuery extends SimpleTerm {
30   public SrndTruncQuery(String truncated, char unlimited, char mask) {
31     super(false); /* not quoted */
32     this.truncated = truncated;
33     this.unlimited = unlimited;
34     this.mask = mask;
35     truncatedToPrefixAndPattern();
36   }
37   
38   private final String truncated;
39   private final char unlimited;
40   private final char mask;
41   
42   private String prefix;
43   private Pattern pattern;
44   
45   
46   public String getTruncated() {return truncated;}
47   
48   @Override
49   public String toStringUnquoted() {return getTruncated();}
50
51   
52   protected boolean matchingChar(char c) {
53     return (c != unlimited) && (c != mask);
54   }
55
56   protected void appendRegExpForChar(char c, StringBuilder re) {
57     if (c == unlimited)
58       re.append(".*");
59     else if (c == mask)
60       re.append(".");
61     else
62       re.append(c);
63   }
64   
65   protected void truncatedToPrefixAndPattern() {
66     int i = 0;
67     while ((i < truncated.length()) && matchingChar(truncated.charAt(i))) {
68       i++;
69     }
70     prefix = truncated.substring(0, i);
71     
72     StringBuilder re = new StringBuilder();
73     while (i < truncated.length()) {
74       appendRegExpForChar(truncated.charAt(i), re);
75       i++;
76     }
77     pattern = Pattern.compile(re.toString());
78   }
79   
80   @Override
81   public void visitMatchingTerms(
82     IndexReader reader,
83     String fieldName,
84     MatchingTermVisitor mtv) throws IOException
85   {
86     int prefixLength = prefix.length();
87     TermEnum enumerator = reader.terms(new Term(fieldName, prefix));
88     Matcher matcher = pattern.matcher("");
89     try {
90       do {
91         Term term = enumerator.term();
92         if (term != null) {
93           String text = term.text();
94           if ((! text.startsWith(prefix)) || (! term.field().equals(fieldName))) {
95             break;
96           } else {
97             matcher.reset( text.substring(prefixLength));
98             if (matcher.matches()) {
99               mtv.visitMatchingTerm(term);
100             }
101           }
102         }
103       } while (enumerator.next());
104     } finally {
105       enumerator.close();
106       matcher.reset();
107     }
108   }
109 }