Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
56   198   25   9.33
30   115   0.45   6
6     4.17  
1    
 
  WildcardTermEnum       Line # 33 56 25 96.7% 0.9673913
 
  (2)
 
1    package org.apache.lucene.search;
2   
3    /**
4    * Copyright 2004 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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 java.io.IOException;
20   
21    import org.apache.lucene.index.IndexReader;
22    import org.apache.lucene.index.Term;
23   
24    /**
25    * Subclass of FilteredTermEnum for enumerating all terms that match the
26    * specified wildcard filter term.
27    * <p>
28    * Term enumerations are always ordered by Term.compareTo(). Each term in
29    * the enumeration is greater than all that precede it.
30    *
31    * @version $Id: WildcardTermEnum.java 329859 2005-10-31 17:05:36Z bmesser $
32    */
 
33    public class WildcardTermEnum extends FilteredTermEnum {
34    Term searchTerm;
35    String field = "";
36    String text = "";
37    String pre = "";
38    int preLen = 0;
39    boolean endEnum = false;
40   
41    /**
42    * Creates a new <code>WildcardTermEnum</code>. Passing in a
43    * {@link org.apache.lucene.index.Term Term} that does not contain a
44    * <code>WILDCARD_CHAR</code> will cause an exception to be thrown.
45    * <p>
46    * After calling the constructor the enumeration is already pointing to the first
47    * valid term if such a term exists.
48    */
 
49  17 toggle public WildcardTermEnum(IndexReader reader, Term term) throws IOException {
50  17 super();
51  17 searchTerm = term;
52  17 field = searchTerm.field();
53  17 text = searchTerm.text();
54   
55  17 int sidx = text.indexOf(WILDCARD_STRING);
56  17 int cidx = text.indexOf(WILDCARD_CHAR);
57  17 int idx = sidx;
58  17 if (idx == -1) {
59  6 idx = cidx;
60    }
61  11 else if (cidx >= 0) {
62  0 idx = Math.min(idx, cidx);
63    }
64   
65  17 pre = searchTerm.text().substring(0,idx);
66  17 preLen = pre.length();
67  17 text = text.substring(preLen);
68  17 setEnum(reader.terms(new Term(searchTerm.field(), pre)));
69    }
70   
 
71  38 toggle protected final boolean termCompare(Term term) {
72  38 if (field == term.field()) {
73  38 String searchText = term.text();
74  38 if (searchText.startsWith(pre)) {
75  33 return wildcardEquals(text, 0, searchText, preLen);
76    }
77    }
78  5 endEnum = true;
79  5 return false;
80    }
81   
 
82  18 toggle public final float difference() {
83  18 return 1.0f;
84    }
85   
 
86  42 toggle public final boolean endEnum() {
87  42 return endEnum;
88    }
89   
90    /********************************************
91    * String equality with support for wildcards
92    ********************************************/
93   
94    public static final char WILDCARD_STRING = '*';
95    public static final char WILDCARD_CHAR = '?';
96   
97    /**
98    * Determines if a word matches a wildcard pattern.
99    * <small>Work released by Granta Design Ltd after originally being done on
100    * company time.</small>
101    */
 
102  128 toggle public static final boolean wildcardEquals(String pattern, int patternIdx,
103    String string, int stringIdx)
104    {
105  128 int p = patternIdx;
106   
107  128 for (int s = stringIdx; ; ++p, ++s)
108    {
109    // End of string yet?
110  222 boolean sEnd = (s >= string.length());
111    // End of pattern yet?
112  222 boolean pEnd = (p >= pattern.length());
113   
114    // If we're looking at the end of the string...
115  222 if (sEnd)
116    {
117    // Assume the only thing left on the pattern is/are wildcards
118  44 boolean justWildcardsLeft = true;
119   
120    // Current wildcard position
121  44 int wildcardSearchPos = p;
122    // While we haven't found the end of the pattern,
123    // and haven't encountered any non-wildcard characters
124  70 while (wildcardSearchPos < pattern.length() && justWildcardsLeft)
125    {
126    // Check the character at the current position
127  29 char wildchar = pattern.charAt(wildcardSearchPos);
128   
129    // If it's not a wildcard character, then there is more
130    // pattern information after this/these wildcards.
131  29 if (wildchar != WILDCARD_CHAR && wildchar != WILDCARD_STRING)
132    {
133  23 justWildcardsLeft = false;
134    }
135    else
136    {
137    // to prevent "cat" matches "ca??"
138  6 if (wildchar == WILDCARD_CHAR) {
139  3 return false;
140    }
141   
142    // Look at the next character
143  3 wildcardSearchPos++;
144    }
145    }
146   
147    // This was a prefix wildcard search, and we've matched, so
148    // return true.
149  41 if (justWildcardsLeft)
150    {
151  18 return true;
152    }
153    }
154   
155    // If we've gone past the end of the string, or the pattern,
156    // return false.
157  201 if (sEnd || pEnd)
158    {
159  28 break;
160    }
161   
162    // Match a single character, so continue.
163  173 if (pattern.charAt(p) == WILDCARD_CHAR)
164    {
165  16 continue;
166    }
167   
168    //
169  157 if (pattern.charAt(p) == WILDCARD_STRING)
170    {
171    // Look at the character beyond the '*'.
172  21 ++p;
173    // Examine the string, starting at the last character.
174  103 for (int i = string.length(); i >= s; --i)
175    {
176  95 if (wildcardEquals(pattern, p, string, i))
177    {
178  13 return true;
179    }
180    }
181  8 break;
182    }
183  136 if (pattern.charAt(p) != string.charAt(s))
184    {
185  58 break;
186    }
187    }
188  94 return false;
189    }
190   
 
191  17 toggle public void close() throws IOException
192    {
193  17 super.close();
194  17 searchTerm = null;
195  17 field = null;
196  17 text = null;
197    }
198    }