pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / processors / NumericRangeQueryNodeProcessor.java
1 package org.apache.lucene.queryParser.standard.processors;
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 java.text.NumberFormat;
21 import java.text.ParseException;
22 import java.util.List;
23
24 import org.apache.lucene.messages.MessageImpl;
25 import org.apache.lucene.queryParser.core.QueryNodeException;
26 import org.apache.lucene.queryParser.core.QueryNodeParseException;
27 import org.apache.lucene.queryParser.core.config.FieldConfig;
28 import org.apache.lucene.queryParser.core.config.QueryConfigHandler;
29 import org.apache.lucene.queryParser.core.messages.QueryParserMessages;
30 import org.apache.lucene.queryParser.core.nodes.ParametricQueryNode;
31 import org.apache.lucene.queryParser.core.nodes.ParametricRangeQueryNode;
32 import org.apache.lucene.queryParser.core.nodes.QueryNode;
33 import org.apache.lucene.queryParser.core.nodes.ParametricQueryNode.CompareOperator;
34 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
35 import org.apache.lucene.queryParser.core.util.StringUtils;
36 import org.apache.lucene.queryParser.standard.config.NumericConfig;
37 import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.ConfigurationKeys;
38 import org.apache.lucene.queryParser.standard.nodes.NumericQueryNode;
39 import org.apache.lucene.queryParser.standard.nodes.NumericRangeQueryNode;
40
41 /**
42  * This processor is used to convert {@link ParametricRangeQueryNode}s to
43  * {@link NumericRangeQueryNode}s. It looks for
44  * {@link ConfigurationKeys#NUMERIC_CONFIG} set in the {@link FieldConfig} of
45  * every {@link ParametricRangeQueryNode} found. If
46  * {@link ConfigurationKeys#NUMERIC_CONFIG} is found, it considers that
47  * {@link ParametricRangeQueryNode} to be a numeric range query and convert it to
48  * {@link NumericRangeQueryNode}.
49  * 
50  * @see ConfigurationKeys#NUMERIC_CONFIG
51  * @see ParametricRangeQueryNode
52  * @see NumericConfig
53  * @see NumericRangeQueryNode
54  */
55 public class NumericRangeQueryNodeProcessor extends QueryNodeProcessorImpl {
56   
57   /**
58    * Constructs an empty {@link NumericRangeQueryNode} object.
59    */
60   public NumericRangeQueryNodeProcessor() {
61   // empty constructor
62   }
63   
64   @Override
65   protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
66     
67     if (node instanceof ParametricRangeQueryNode) {
68       QueryConfigHandler config = getQueryConfigHandler();
69       
70       if (config != null) {
71         ParametricRangeQueryNode parametricRangeNode = (ParametricRangeQueryNode) node;
72         FieldConfig fieldConfig = config.getFieldConfig(StringUtils
73             .toString(parametricRangeNode.getField()));
74         
75         if (fieldConfig != null) {
76           
77           NumericConfig numericConfig = fieldConfig
78               .get(ConfigurationKeys.NUMERIC_CONFIG);
79           
80           if (numericConfig != null) {
81             
82             ParametricQueryNode lower = parametricRangeNode.getLowerBound();
83             ParametricQueryNode upper = parametricRangeNode.getUpperBound();
84             
85             NumberFormat numberFormat = numericConfig.getNumberFormat();
86             Number lowerNumber, upperNumber;
87             
88             try {
89               lowerNumber = numberFormat.parse(lower.getTextAsString());
90               
91             } catch (ParseException e) {
92               throw new QueryNodeParseException(new MessageImpl(
93                   QueryParserMessages.COULD_NOT_PARSE_NUMBER, lower
94                       .getTextAsString(), numberFormat.getClass()
95                       .getCanonicalName()), e);
96             }
97             
98             try {
99               upperNumber = numberFormat.parse(upper.getTextAsString());
100               
101             } catch (ParseException e) {
102               throw new QueryNodeParseException(new MessageImpl(
103                   QueryParserMessages.COULD_NOT_PARSE_NUMBER, upper
104                       .getTextAsString(), numberFormat.getClass()
105                       .getCanonicalName()), e);
106             }
107             
108             switch (numericConfig.getType()) {
109               case LONG:
110                 upperNumber = upperNumber.longValue();
111                 lowerNumber = lowerNumber.longValue();
112                 break;
113               case INT:
114                 upperNumber = upperNumber.intValue();
115                 lowerNumber = lowerNumber.intValue();
116                 break;
117               case DOUBLE:
118                 upperNumber = upperNumber.doubleValue();
119                 lowerNumber = lowerNumber.doubleValue();
120                 break;
121               case FLOAT:
122                 upperNumber = upperNumber.floatValue();
123                 lowerNumber = lowerNumber.floatValue();
124             }
125             
126             NumericQueryNode lowerNode = new NumericQueryNode(
127                 parametricRangeNode.getField(), lowerNumber, numberFormat);
128             NumericQueryNode upperNode = new NumericQueryNode(
129                 parametricRangeNode.getField(), upperNumber, numberFormat);
130             
131             boolean upperInclusive = upper.getOperator() == CompareOperator.LE;
132             boolean lowerInclusive = lower.getOperator() == CompareOperator.GE;
133             
134             return new NumericRangeQueryNode(lowerNode, upperNode,
135                 lowerInclusive, upperInclusive, numericConfig);
136             
137           }
138           
139         }
140         
141       }
142       
143     }
144     
145     return node;
146     
147   }
148   
149   @Override
150   protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
151     return node;
152   }
153   
154   @Override
155   protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
156       throws QueryNodeException {
157     return children;
158   }
159   
160 }