001: package org.apache.lucene.search.regex;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.index.Term;
021: import org.apache.lucene.index.IndexReader;
022: import org.apache.lucene.search.Query;
023: import org.apache.lucene.search.BooleanQuery;
024: import org.apache.lucene.search.BooleanClause;
025: import org.apache.lucene.search.TermQuery;
026: import org.apache.lucene.search.spans.SpanOrQuery;
027: import org.apache.lucene.search.spans.SpanQuery;
028: import org.apache.lucene.search.spans.SpanTermQuery;
029: import org.apache.lucene.search.spans.Spans;
030: import org.apache.lucene.util.ToStringUtils;
031:
032: import java.io.IOException;
033: import java.util.Collection;
034: import java.util.ArrayList;
035:
036: /**
037: * A SpanQuery version of {@link RegexQuery} allowing regular expression
038: * queries to be nested within other SpanQuery subclasses.
039: */
040: public class SpanRegexQuery extends SpanQuery implements
041: RegexQueryCapable {
042: private RegexCapabilities regexImpl = new JavaUtilRegexCapabilities();
043: private Term term;
044:
045: public SpanRegexQuery(Term term) {
046: this .term = term;
047: }
048:
049: public Term getTerm() {
050: return term;
051: }
052:
053: public Query rewrite(IndexReader reader) throws IOException {
054: RegexQuery orig = new RegexQuery(term);
055: orig.setRegexImplementation(regexImpl);
056:
057: // RegexQuery (via MultiTermQuery).rewrite always returns a BooleanQuery
058: BooleanQuery bq = (BooleanQuery) orig.rewrite(reader);
059:
060: BooleanClause[] clauses = bq.getClauses();
061: SpanQuery[] sqs = new SpanQuery[clauses.length];
062: for (int i = 0; i < clauses.length; i++) {
063: BooleanClause clause = clauses[i];
064:
065: // Clauses from RegexQuery.rewrite are always TermQuery's
066: TermQuery tq = (TermQuery) clause.getQuery();
067:
068: sqs[i] = new SpanTermQuery(tq.getTerm());
069: sqs[i].setBoost(tq.getBoost());
070: }
071:
072: SpanOrQuery query = new SpanOrQuery(sqs);
073: query.setBoost(orig.getBoost());
074:
075: return query;
076: }
077:
078: public Spans getSpans(IndexReader reader) throws IOException {
079: throw new UnsupportedOperationException(
080: "Query should have been rewritten");
081: }
082:
083: public String getField() {
084: return term.field();
085: }
086:
087: public Collection getTerms() {
088: Collection terms = new ArrayList();
089: terms.add(term);
090: return terms;
091: }
092:
093: /* generated by IntelliJ IDEA */
094: public boolean equals(Object o) {
095: if (this == o)
096: return true;
097: if (o == null || getClass() != o.getClass())
098: return false;
099:
100: final SpanRegexQuery that = (SpanRegexQuery) o;
101:
102: if (!regexImpl.equals(that.regexImpl))
103: return false;
104: if (!term.equals(that.term))
105: return false;
106:
107: return true;
108: }
109:
110: /* generated by IntelliJ IDEA */
111: public int hashCode() {
112: int result;
113: result = regexImpl.hashCode();
114: result = 29 * result + term.hashCode();
115: return result;
116: }
117:
118: public String toString(String field) {
119: StringBuffer buffer = new StringBuffer();
120: buffer.append("spanRegexQuery(");
121: buffer.append(term);
122: buffer.append(")");
123: buffer.append(ToStringUtils.boost(getBoost()));
124: return buffer.toString();
125: }
126:
127: public void setRegexImplementation(RegexCapabilities impl) {
128: this .regexImpl = impl;
129: }
130:
131: public RegexCapabilities getRegexImplementation() {
132: return regexImpl;
133: }
134: }
|