add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / processors / WildcardQueryNodeProcessor.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.util.List;
21
22 import org.apache.lucene.queryParser.core.QueryNodeException;
23 import org.apache.lucene.queryParser.core.nodes.FieldQueryNode;
24 import org.apache.lucene.queryParser.core.nodes.FuzzyQueryNode;
25 import org.apache.lucene.queryParser.core.nodes.ParametricQueryNode;
26 import org.apache.lucene.queryParser.core.nodes.QueryNode;
27 import org.apache.lucene.queryParser.core.nodes.QuotedFieldQueryNode;
28 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
29 import org.apache.lucene.queryParser.core.util.UnescapedCharSequence;
30 import org.apache.lucene.queryParser.standard.nodes.PrefixWildcardQueryNode;
31 import org.apache.lucene.queryParser.standard.nodes.WildcardQueryNode;
32 import org.apache.lucene.queryParser.standard.parser.StandardSyntaxParser;
33 import org.apache.lucene.search.PrefixQuery;
34
35 /**
36  * The {@link StandardSyntaxParser} creates {@link PrefixWildcardQueryNode} nodes which
37  * have values containing the prefixed wildcard. However, Lucene
38  * {@link PrefixQuery} cannot contain the prefixed wildcard. So, this processor
39  * basically removed the prefixed wildcard from the
40  * {@link PrefixWildcardQueryNode} value. <br/>
41  * 
42  * @see PrefixQuery
43  * @see PrefixWildcardQueryNode
44  */
45 public class WildcardQueryNodeProcessor extends QueryNodeProcessorImpl {
46
47   public WildcardQueryNodeProcessor() {
48     // empty constructor
49   }
50
51   @Override
52   protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
53
54     // the old Lucene Parser ignores FuzzyQueryNode that are also PrefixWildcardQueryNode or WildcardQueryNode
55     // we do the same here, also ignore empty terms
56     if (node instanceof FieldQueryNode || node instanceof FuzzyQueryNode) {      
57       FieldQueryNode fqn = (FieldQueryNode) node;      
58       CharSequence text = fqn.getText(); 
59       
60       // do not process wildcards for ParametricQueryNode and 
61       // QuotedFieldQueryNode to reproduce the old parser behavior
62       if (fqn instanceof ParametricQueryNode 
63           || fqn instanceof QuotedFieldQueryNode 
64           || text.length() <= 0){
65         // Ignore empty terms
66         return node;
67       }
68       
69       // Code below simulates the old lucene parser behavior for wildcards
70       
71       if (isPrefixWildcard(text)) {        
72         PrefixWildcardQueryNode prefixWildcardQN = new PrefixWildcardQueryNode(fqn);
73         return prefixWildcardQN;
74         
75       } else if (isWildcard(text)){
76         WildcardQueryNode wildcardQN = new WildcardQueryNode(fqn);
77         return wildcardQN;
78       }
79              
80     }
81
82     return node;
83
84   }
85
86   private boolean isWildcard(CharSequence text) {
87     if (text ==null || text.length() <= 0) return false;
88     
89     // If a un-escaped '*' or '?' if found return true
90     // start at the end since it's more common to put wildcards at the end
91     for(int i=text.length()-1; i>=0; i--){
92       if ((text.charAt(i) == '*' || text.charAt(i) == '?') && !UnescapedCharSequence.wasEscaped(text, i)){
93         return true;
94       }
95     }
96     
97     return false;
98   }
99
100   private boolean isPrefixWildcard(CharSequence text) {
101     if (text == null || text.length() <= 0 || !isWildcard(text)) return false;
102     
103     // Validate last character is a '*' and was not escaped
104     // If single '*' is is a wildcard not prefix to simulate old queryparser
105     if (text.charAt(text.length()-1) != '*') return false;
106     if (UnescapedCharSequence.wasEscaped(text, text.length()-1)) return false;
107     if (text.length() == 1) return false;
108       
109     // Only make a prefix if there is only one single star at the end and no '?' or '*' characters
110     // If single wildcard return false to mimic old queryparser
111     for(int i=0; i<text.length(); i++){
112       if (text.charAt(i) == '?') return false;
113       if (text.charAt(i) == '*' && !UnescapedCharSequence.wasEscaped(text, i)){        
114         if (i == text.length()-1) 
115           return true;
116         else 
117           return false;
118       }
119     }
120     
121     return false;
122   }
123
124   @Override
125   protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
126
127     return node;
128
129   }
130
131   @Override
132   protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
133       throws QueryNodeException {
134
135     return children;
136
137   }
138
139 }