1 package org.apache.lucene.xmlparser.builders;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.search.DocIdSet;
24 import org.apache.lucene.search.Filter;
25 import org.apache.lucene.search.NumericRangeFilter;
26 import org.apache.lucene.util.NumericUtils;
27 import org.apache.lucene.xmlparser.DOMUtils;
28 import org.apache.lucene.xmlparser.FilterBuilder;
29 import org.apache.lucene.xmlparser.ParserException;
30 import org.w3c.dom.Element;
33 * Creates a {@link NumericRangeFilter}. The table below specifies the required
34 * attributes and the defaults if optional attributes are omitted. For more
35 * detail on what each of the attributes actually do, consult the documentation
36 * for {@link NumericRangeFilter}:
39 * <th>Attribute name</th>
52 * <td>Specified by <tt>type</tt></td>
58 * <td>Specified by <tt>type</tt></td>
64 * <td>int, long, float, double</td>
69 * <td>includeLower</td>
70 * <td>true, false</td>
75 * <td>includeUpper</td>
76 * <td>true, false</td>
81 * <td>precisionStep</td>
88 * If an error occurs parsing the supplied <tt>lowerTerm</tt> or
89 * <tt>upperTerm</tt> into the numeric type specified by <tt>type</tt>, then the
90 * error will be silently ignored and the resulting filter will not match any
93 public class NumericRangeFilterBuilder implements FilterBuilder {
94 private static final NoMatchFilter NO_MATCH_FILTER = new NoMatchFilter();
96 private boolean strictMode = false;
99 * Specifies how this {@link NumericRangeFilterBuilder} will handle errors.
101 * If this is set to true, {@link #getFilter(Element)} will throw a
102 * {@link ParserException} if it is unable to parse the lowerTerm or upperTerm
103 * into the appropriate numeric type. If this is set to false, then this
104 * exception will be silently ignored and the resulting filter will not match
111 public void setStrictMode(boolean strictMode) {
112 this.strictMode = strictMode;
115 public Filter getFilter(Element e) throws ParserException {
116 String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
117 String lowerTerm = DOMUtils.getAttributeOrFail(e, "lowerTerm");
118 String upperTerm = DOMUtils.getAttributeOrFail(e, "upperTerm");
119 boolean lowerInclusive = DOMUtils.getAttribute(e, "includeLower", true);
120 boolean upperInclusive = DOMUtils.getAttribute(e, "includeUpper", true);
121 int precisionStep = DOMUtils.getAttribute(e, "precisionStep", NumericUtils.PRECISION_STEP_DEFAULT);
123 String type = DOMUtils.getAttribute(e, "type", "int");
126 if (type.equalsIgnoreCase("int")) {
127 filter = NumericRangeFilter.newIntRange(field, precisionStep, Integer
128 .valueOf(lowerTerm), Integer.valueOf(upperTerm), lowerInclusive,
130 } else if (type.equalsIgnoreCase("long")) {
131 filter = NumericRangeFilter.newLongRange(field, precisionStep, Long
132 .valueOf(lowerTerm), Long.valueOf(upperTerm), lowerInclusive,
134 } else if (type.equalsIgnoreCase("double")) {
135 filter = NumericRangeFilter.newDoubleRange(field, precisionStep, Double
136 .valueOf(lowerTerm), Double.valueOf(upperTerm), lowerInclusive,
138 } else if (type.equalsIgnoreCase("float")) {
139 filter = NumericRangeFilter.newFloatRange(field, precisionStep, Float
140 .valueOf(lowerTerm), Float.valueOf(upperTerm), lowerInclusive,
143 throw new ParserException(
144 "type attribute must be one of: [long, int, double, float]");
147 } catch (NumberFormatException nfe) {
149 throw new ParserException(
150 "Could not parse lowerTerm or upperTerm into a number", nfe);
152 return NO_MATCH_FILTER;
156 static class NoMatchFilter extends Filter {
157 private static final long serialVersionUID = 1L;
160 public DocIdSet getDocIdSet(IndexReader reader) throws IOException {