/**
* Copyright 2014 National University of Ireland, Galway.
*
* This file is part of the SIREn project. Project and contact information:
*
* https://github.com/rdelbru/SIREn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sindice.siren.search.node;
import java.io.IOException;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.RegexpQuery;
import org.apache.lucene.util.ToStringUtils;
import org.apache.lucene.util.automaton.Automaton;
import org.apache.lucene.util.automaton.AutomatonProvider;
import org.apache.lucene.util.automaton.RegExp;
/**
* A {@link NodePrimitiveQuery} that provides a fast regular expression matching
* based on the {@link org.apache.lucene.util.automaton} package.
*
* <p>
*
* <ul>
* <li>Comparisons are <a
* href="http://tusker.org/regex/regex_benchmark.html">fast</a>
* <li>The term dictionary is enumerated in an intelligent way, to avoid
* comparisons. See {@link NodeAutomatonQuery} for more details.
* </ul>
*
* <p>
*
* The supported syntax is documented in the {@link RegExp} class.
* Note this might be different than other regular expression implementations.
* For some alternatives with different syntax, look under the sandbox.
*
* <p>
*
* Note this query can be slow, as it needs to iterate over many terms. In order
* to prevent extremely slow RegexpQueries, a Regexp term should not start with
* the expression <code>.*</code>
*
* <p> Code taken from {@link RegexpQuery} and adapted for SIREn.
*/
public class NodeRegexpQuery extends NodeAutomatonQuery {
/**
* A provider that provides no named automata
*/
private static AutomatonProvider defaultProvider = new AutomatonProvider() {
public Automaton getAutomaton(final String name) throws IOException {
return null;
}
};
/**
* Constructs a query for terms matching <code>term</code>.
* <p>
* By default, all regular expression features are enabled.
* </p>
*
* @param term regular expression.
*/
public NodeRegexpQuery(final Term term) {
this(term, RegExp.ALL);
}
/**
* Constructs a query for terms matching <code>term</code>.
*
* @param term regular expression.
* @param flags optional RegExp features from {@link RegExp}
*/
public NodeRegexpQuery(final Term term, final int flags) {
this(term, flags, defaultProvider);
}
/**
* Constructs a query for terms matching <code>term</code>.
*
* @param term regular expression.
* @param flags optional RegExp features from {@link RegExp}
* @param provider custom AutomatonProvider for named automata
*/
public NodeRegexpQuery(final Term term, final int flags, final AutomatonProvider provider) {
super(term, new RegExp(term.text(), flags).toAutomaton(provider));
}
/** Prints a user-readable version of this query. */
@Override
public String toString(final String field) {
final StringBuffer buffer = new StringBuffer();
buffer.append('/');
buffer.append(term.text());
buffer.append('/');
buffer.append(ToStringUtils.boost(this.getBoost()));
return this.wrapToStringWithDatatype(buffer).toString();
}
}