01: package org.apache.lucene.search.regex;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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 org.apache.lucene.search.FilteredTermEnum;
21: import org.apache.lucene.index.IndexReader;
22: import org.apache.lucene.index.Term;
23:
24: import java.io.IOException;
25:
26: /**
27: * Subclass of FilteredTermEnum for enumerating all terms that match the
28: * specified regular expression term using the specified regular expression
29: * implementation.
30: * <p>
31: * Term enumerations are always ordered by Term.compareTo(). Each term in
32: * the enumeration is greater than all that precede it.
33: */
34:
35: public class RegexTermEnum extends FilteredTermEnum {
36: private String field = "";
37: private String pre = "";
38: private boolean endEnum = false;
39: private RegexCapabilities regexImpl;
40:
41: public RegexTermEnum(IndexReader reader, Term term,
42: RegexCapabilities regexImpl) throws IOException {
43: super ();
44: field = term.field();
45: String text = term.text();
46: this .regexImpl = regexImpl;
47:
48: regexImpl.compile(text);
49:
50: pre = regexImpl.prefix();
51: if (pre == null)
52: pre = "";
53:
54: setEnum(reader.terms(new Term(term.field(), pre)));
55: }
56:
57: protected final boolean termCompare(Term term) {
58: if (field == term.field()) {
59: String searchText = term.text();
60: if (searchText.startsWith(pre)) {
61: return regexImpl.match(searchText);
62: }
63: }
64: endEnum = true;
65: return false;
66: }
67:
68: public final float difference() {
69: // TODO: adjust difference based on distance of searchTerm.text() and term().text()
70: return 1.0f;
71: }
72:
73: public final boolean endEnum() {
74: return endEnum;
75: }
76:
77: public void close() throws IOException {
78: super.close();
79: field = null;
80: }
81: }
|