add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / xml-query-parser / src / java / org / apache / lucene / xmlparser / builders / NumericRangeQueryBuilder.java
1 package org.apache.lucene.xmlparser.builders;
2
3 /**
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
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.NumericRangeQuery;
21 import org.apache.lucene.search.Query;
22 import org.apache.lucene.util.NumericUtils;
23 import org.apache.lucene.xmlparser.DOMUtils;
24 import org.apache.lucene.xmlparser.ParserException;
25 import org.apache.lucene.xmlparser.QueryBuilder;
26 import org.w3c.dom.Element;
27
28 /**
29  * Creates a {@link NumericRangeQuery}. The table below specifies the required
30  * attributes and the defaults if optional attributes are omitted. For more
31  * detail on what each of the attributes actually do, consult the documentation
32  * for {@link NumericRangeQuery}:
33  * <table>
34  * <tr>
35  * <th>Attribute name</th>
36  * <th>Values</th>
37  * <th>Required</th>
38  * <th>Default</th>
39  * </tr>
40  * <tr>
41  * <td>fieldName</td>
42  * <td>String</td>
43  * <td>Yes</td>
44  * <td>N/A</td>
45  * </tr>
46  * <tr>
47  * <td>lowerTerm</td>
48  * <td>Specified by <tt>type</tt></td>
49  * <td>Yes</td>
50  * <td>N/A</td>
51  * </tr>
52  * <tr>
53  * <td>upperTerm</td>
54  * <td>Specified by <tt>type</tt></td>
55  * <td>Yes</td>
56  * <td>N/A</td>
57  * </tr>
58  * <tr>
59  * <td>type</td>
60  * <td>int, long, float, double</td>
61  * <td>No</td>
62  * <td>int</td>
63  * </tr>
64  * <tr>
65  * <td>includeLower</td>
66  * <td>true, false</td>
67  * <td>No</td>
68  * <td>true</td>
69  * </tr>
70  * <tr>
71  * <td>includeUpper</td>
72  * <td>true, false</td>
73  * <td>No</td>
74  * <td>true</td>
75  * </tr>
76  * <tr>
77  * <td>precisionStep</td>
78  * <td>Integer</td>
79  * <td>No</td>
80  * <td>4</td>
81  * </tr>
82  * </table>
83  * <p>
84  * A {@link ParserException} will be thrown if an error occurs parsing the
85  * supplied <tt>lowerTerm</tt> or <tt>upperTerm</tt> into the numeric type
86  * specified by <tt>type</tt>.
87  */
88 public class NumericRangeQueryBuilder implements QueryBuilder {
89
90         public Query getQuery(Element e) throws ParserException {
91                 String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
92                 String lowerTerm = DOMUtils.getAttributeOrFail(e, "lowerTerm");
93                 String upperTerm = DOMUtils.getAttributeOrFail(e, "upperTerm");
94                 boolean lowerInclusive = DOMUtils.getAttribute(e, "includeLower", true);
95                 boolean upperInclusive = DOMUtils.getAttribute(e, "includeUpper", true);
96                 int precisionStep = DOMUtils.getAttribute(e, "precisionStep", NumericUtils.PRECISION_STEP_DEFAULT);
97
98                 String type = DOMUtils.getAttribute(e, "type", "int");
99                 try {
100                         Query filter;
101                         if (type.equalsIgnoreCase("int")) {
102                                 filter = NumericRangeQuery.newIntRange(field, precisionStep, Integer
103                                                 .valueOf(lowerTerm), Integer.valueOf(upperTerm), lowerInclusive,
104                                                 upperInclusive);
105                         } else if (type.equalsIgnoreCase("long")) {
106                                 filter = NumericRangeQuery.newLongRange(field, precisionStep, Long
107                                                 .valueOf(lowerTerm), Long.valueOf(upperTerm), lowerInclusive,
108                                                 upperInclusive);
109                         } else if (type.equalsIgnoreCase("double")) {
110                                 filter = NumericRangeQuery.newDoubleRange(field, precisionStep, Double
111                                                 .valueOf(lowerTerm), Double.valueOf(upperTerm), lowerInclusive,
112                                                 upperInclusive);
113                         } else if (type.equalsIgnoreCase("float")) {
114                                 filter = NumericRangeQuery.newFloatRange(field, precisionStep, Float
115                                                 .valueOf(lowerTerm), Float.valueOf(upperTerm), lowerInclusive,
116                                                 upperInclusive);
117                         } else {
118                                 throw new ParserException(
119                                                 "type attribute must be one of: [long, int, double, float]");
120                         }
121                         return filter;
122                 } catch (NumberFormatException nfe) {
123                         throw new ParserException(
124                                         "Could not parse lowerTerm or upperTerm into a number", nfe);
125                 }
126         }
127 }