add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / processors / ParametricRangeQueryNodeProcessor.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.Collator;
21 import java.text.DateFormat;
22 import java.util.Calendar;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.Locale;
26
27 import org.apache.lucene.document.DateField;
28 import org.apache.lucene.document.DateTools;
29 import org.apache.lucene.document.DateTools.Resolution;
30 import org.apache.lucene.queryParser.core.QueryNodeException;
31 import org.apache.lucene.queryParser.core.config.FieldConfig;
32 import org.apache.lucene.queryParser.core.config.QueryConfigHandler;
33 import org.apache.lucene.queryParser.core.nodes.ParametricQueryNode;
34 import org.apache.lucene.queryParser.core.nodes.ParametricRangeQueryNode;
35 import org.apache.lucene.queryParser.core.nodes.QueryNode;
36 import org.apache.lucene.queryParser.core.nodes.ParametricQueryNode.CompareOperator;
37 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
38 import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.ConfigurationKeys;
39 import org.apache.lucene.queryParser.standard.nodes.RangeQueryNode;
40
41 /**
42  * This processor converts {@link ParametricRangeQueryNode} objects to
43  * {@link RangeQueryNode} objects. It reads the lower and upper bounds value
44  * from the {@link ParametricRangeQueryNode} object and try to parse their
45  * values using a {@link DateFormat}. If the values cannot be parsed to a date
46  * value, it will only create the {@link RangeQueryNode} using the
47  * non-parsed values. <br/>
48  * <br/>
49  * If a {@link ConfigurationKeys#LOCALE} is defined in the {@link QueryConfigHandler} it
50  * will be used to parse the date, otherwise {@link Locale#getDefault()} will be
51  * used. <br/>
52  * <br/>
53  * If a {@link ConfigurationKeys#DATE_RESOLUTION} is defined and the {@link Resolution} is
54  * not <code>null</code> it will also be used to parse the date value. <br/>
55  * <br/>
56  * 
57  * @see ConfigurationKeys#DATE_RESOLUTION
58  * @see ConfigurationKeys#LOCALE
59  * @see org.apache.lucene.queryParser.standard.nodes.TermRangeQueryNode
60  * @see ParametricRangeQueryNode
61  */
62 public class ParametricRangeQueryNodeProcessor extends QueryNodeProcessorImpl {
63   
64   public ParametricRangeQueryNodeProcessor() {
65   // empty constructor
66   }
67   
68   @Override
69   protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
70     
71     if (node instanceof ParametricRangeQueryNode) {
72       ParametricRangeQueryNode parametricRangeNode = (ParametricRangeQueryNode) node;
73       ParametricQueryNode upper = parametricRangeNode.getUpperBound();
74       ParametricQueryNode lower = parametricRangeNode.getLowerBound();
75       DateTools.Resolution dateRes = null;
76       boolean inclusive = false;
77       
78       Locale locale = getQueryConfigHandler().get(ConfigurationKeys.LOCALE,
79           Locale.getDefault());
80       
81       Collator collator = getQueryConfigHandler().get(
82           ConfigurationKeys.RANGE_COLLATOR);
83       
84       
85       CharSequence field = parametricRangeNode.getField();
86       String fieldStr = null;
87
88       if (field != null) {
89         fieldStr = field.toString();
90       }
91
92       FieldConfig fieldConfig = getQueryConfigHandler()
93           .getFieldConfig(fieldStr);
94
95       if (fieldConfig != null) {
96         dateRes = fieldConfig.get(ConfigurationKeys.DATE_RESOLUTION);
97       }
98
99       if (upper.getOperator() == CompareOperator.LE) {
100         inclusive = true;
101
102       } else if (lower.getOperator() == CompareOperator.GE) {
103         inclusive = true;
104       }
105
106       String part1 = lower.getTextAsString();
107       String part2 = upper.getTextAsString();
108
109       try {
110         DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
111         df.setLenient(true);
112         Date d1 = df.parse(part1);
113         Date d2 = df.parse(part2);
114         if (inclusive) {
115           // The user can only specify the date, not the time, so make sure
116           // the time is set to the latest possible time of that date to really
117           // include all documents:
118           Calendar cal = Calendar.getInstance(locale);
119           cal.setTime(d2);
120           cal.set(Calendar.HOUR_OF_DAY, 23);
121           cal.set(Calendar.MINUTE, 59);
122           cal.set(Calendar.SECOND, 59);
123           cal.set(Calendar.MILLISECOND, 999);
124           d2 = cal.getTime();
125         }
126
127         if (dateRes == null) {
128           // no default or field specific date resolution has been set,
129           // use deprecated DateField to maintain compatibilty with
130           // pre-1.9 Lucene versions.
131           part1 = DateField.dateToString(d1);
132           part2 = DateField.dateToString(d2);
133
134         } else {
135           part1 = DateTools.dateToString(d1, dateRes);
136           part2 = DateTools.dateToString(d2, dateRes);
137         }
138       } catch (Exception e) {
139         // do nothing
140       }
141
142       lower.setText(part1);
143       upper.setText(part2);
144
145       return new RangeQueryNode(lower, upper, collator);
146
147     }
148
149     return node;
150
151   }
152
153   @Override
154   protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
155
156     return node;
157
158   }
159
160   @Override
161   protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
162       throws QueryNodeException {
163
164     return children;
165
166   }
167
168 }