add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / processors / NumericQueryNodeProcessor.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.FieldQueryNode;
31 import org.apache.lucene.queryParser.core.nodes.ParametricQueryNode;
32 import org.apache.lucene.queryParser.core.nodes.QueryNode;
33 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
34 import org.apache.lucene.queryParser.standard.config.NumericConfig;
35 import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.ConfigurationKeys;
36 import org.apache.lucene.queryParser.standard.nodes.NumericQueryNode;
37 import org.apache.lucene.queryParser.standard.nodes.NumericRangeQueryNode;
38
39 /**
40  * This processor is used to convert {@link FieldQueryNode}s to
41  * {@link NumericRangeQueryNode}s. It looks for
42  * {@link ConfigurationKeys#NUMERIC_CONFIG} set in the {@link FieldConfig} of
43  * every {@link FieldQueryNode} found. If
44  * {@link ConfigurationKeys#NUMERIC_CONFIG} is found, it considers that
45  * {@link FieldQueryNode} to be a numeric query and convert it to
46  * {@link NumericRangeQueryNode} with upper and lower inclusive and lower and
47  * upper equals to the value represented by the {@link FieldQueryNode} converted
48  * to {@link Number}. It means that <b>field:1</b> is converted to <b>field:[1 TO
49  * 1]</b>. <br/>
50  * <br/>
51  * Note that {@link ParametricQueryNode}s are ignored, even being a
52  * {@link FieldQueryNode}.
53  * 
54  * @see ConfigurationKeys#NUMERIC_CONFIG
55  * @see FieldQueryNode
56  * @see NumericConfig
57  * @see NumericQueryNode
58  */
59 public class NumericQueryNodeProcessor extends QueryNodeProcessorImpl {
60   
61   /**
62    * Constructs a {@link NumericQueryNodeProcessor} object.
63    */
64   public NumericQueryNodeProcessor() {
65   // empty constructor
66   }
67   
68   @Override
69   protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
70     
71     if (node instanceof FieldQueryNode
72         && !(node instanceof ParametricQueryNode)) {
73       
74       QueryConfigHandler config = getQueryConfigHandler();
75       
76       if (config != null) {
77         FieldQueryNode fieldNode = (FieldQueryNode) node;
78         FieldConfig fieldConfig = config.getFieldConfig(fieldNode
79             .getFieldAsString());
80         
81         if (fieldConfig != null) {
82           NumericConfig numericConfig = fieldConfig
83               .get(ConfigurationKeys.NUMERIC_CONFIG);
84           
85           if (numericConfig != null) {
86             
87             NumberFormat numberFormat = numericConfig.getNumberFormat();
88             Number number;
89             
90             try {
91               number = numberFormat.parse(fieldNode.getTextAsString());
92               
93             } catch (ParseException e) {
94               throw new QueryNodeParseException(new MessageImpl(
95                   QueryParserMessages.COULD_NOT_PARSE_NUMBER, fieldNode
96                       .getTextAsString(), numberFormat.getClass()
97                       .getCanonicalName()), e);
98             }
99             
100             switch (numericConfig.getType()) {
101               case LONG:
102                 number = number.longValue();
103                 break;
104               case INT:
105                 number = number.intValue();
106                 break;
107               case DOUBLE:
108                 number = number.doubleValue();
109                 break;
110               case FLOAT:
111                 number = number.floatValue();
112             }
113             
114             NumericQueryNode lowerNode = new NumericQueryNode(fieldNode
115                 .getField(), number, numberFormat);
116             NumericQueryNode upperNode = new NumericQueryNode(fieldNode
117                 .getField(), number, numberFormat);
118             
119             return new NumericRangeQueryNode(lowerNode, upperNode, true, true,
120                 numericConfig);
121             
122           }
123           
124         }
125         
126       }
127       
128     }
129     
130     return node;
131     
132   }
133   
134   @Override
135   protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
136     return node;
137   }
138   
139   @Override
140   protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
141       throws QueryNodeException {
142     return children;
143   }
144   
145 }