KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > WildcardTermEnum


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 JavaDoc;
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 JavaDoc field = "";
36   String JavaDoc text = "";
37   String JavaDoc 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   public WildcardTermEnum(IndexReader reader, Term term) throws IOException JavaDoc {
50     super();
51     searchTerm = term;
52     field = searchTerm.field();
53     text = searchTerm.text();
54
55     int sidx = text.indexOf(WILDCARD_STRING);
56     int cidx = text.indexOf(WILDCARD_CHAR);
57     int idx = sidx;
58     if (idx == -1) {
59       idx = cidx;
60     }
61     else if (cidx >= 0) {
62       idx = Math.min(idx, cidx);
63     }
64
65     pre = searchTerm.text().substring(0,idx);
66     preLen = pre.length();
67     text = text.substring(preLen);
68     setEnum(reader.terms(new Term(searchTerm.field(), pre)));
69   }
70
71   protected final boolean termCompare(Term term) {
72     if (field == term.field()) {
73       String JavaDoc searchText = term.text();
74       if (searchText.startsWith(pre)) {
75         return wildcardEquals(text, 0, searchText, preLen);
76       }
77     }
78     endEnum = true;
79     return false;
80   }
81
82   public final float difference() {
83     return 1.0f;
84   }
85
86   public final boolean endEnum() {
87     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   public static final boolean wildcardEquals(String JavaDoc pattern, int patternIdx,
103     String JavaDoc string, int stringIdx)
104   {
105     int p = patternIdx;
106     
107     for (int s = stringIdx; ; ++p, ++s)
108       {
109         // End of string yet?
110
boolean sEnd = (s >= string.length());
111         // End of pattern yet?
112
boolean pEnd = (p >= pattern.length());
113
114         // If we're looking at the end of the string...
115
if (sEnd)
116         {
117           // Assume the only thing left on the pattern is/are wildcards
118
boolean justWildcardsLeft = true;
119
120           // Current wildcard position
121
int wildcardSearchPos = p;
122           // While we haven't found the end of the pattern,
123
// and haven't encountered any non-wildcard characters
124
while (wildcardSearchPos < pattern.length() && justWildcardsLeft)
125           {
126             // Check the character at the current position
127
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
if (wildchar != WILDCARD_CHAR && wildchar != WILDCARD_STRING)
132             {
133               justWildcardsLeft = false;
134             }
135             else
136             {
137               // to prevent "cat" matches "ca??"
138
if (wildchar == WILDCARD_CHAR) {
139                 return false;
140               }
141               
142               // Look at the next character
143
wildcardSearchPos++;
144             }
145           }
146
147           // This was a prefix wildcard search, and we've matched, so
148
// return true.
149
if (justWildcardsLeft)
150           {
151             return true;
152           }
153         }
154
155         // If we've gone past the end of the string, or the pattern,
156
// return false.
157
if (sEnd || pEnd)
158         {
159           break;
160         }
161
162         // Match a single character, so continue.
163
if (pattern.charAt(p) == WILDCARD_CHAR)
164         {
165           continue;
166         }
167
168         //
169
if (pattern.charAt(p) == WILDCARD_STRING)
170         {
171           // Look at the character beyond the '*'.
172
++p;
173           // Examine the string, starting at the last character.
174
for (int i = string.length(); i >= s; --i)
175           {
176             if (wildcardEquals(pattern, p, string, i))
177             {
178               return true;
179             }
180           }
181           break;
182         }
183         if (pattern.charAt(p) != string.charAt(s))
184         {
185           break;
186         }
187       }
188       return false;
189   }
190
191   public void close() throws IOException JavaDoc
192   {
193     super.close();
194     searchTerm = null;
195     field = null;
196     text = null;
197   }
198 }
199
Popular Tags