add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / surround / query / SimpleTerm.java
1 package org.apache.lucene.queryParser.surround.query;
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.index.Term;
25 import org.apache.lucene.search.BooleanClause;
26 import org.apache.lucene.search.Query;
27
28 public abstract class SimpleTerm
29   extends SrndQuery
30   implements DistanceSubQuery, Comparable<SimpleTerm>
31 {
32   public SimpleTerm(boolean q) {quoted = q;}
33   
34   private boolean quoted;
35   boolean isQuoted() {return quoted;}
36   
37   public String getQuote() {return "\"";}
38   public String getFieldOperator() {return "/";}
39   
40   public abstract String toStringUnquoted();
41   
42   public int compareTo(SimpleTerm ost) {
43     /* for ordering terms and prefixes before using an index, not used */
44     return this.toStringUnquoted().compareTo( ost.toStringUnquoted());
45   }
46   
47   protected void suffixToString(StringBuilder r) {} /* override for prefix query */
48   
49   @Override
50   public String toString() {
51     StringBuilder r = new StringBuilder();
52     if (isQuoted()) {
53       r.append(getQuote());
54     }
55     r.append(toStringUnquoted());
56     if (isQuoted()) {
57       r.append(getQuote());
58     }
59     suffixToString(r);
60     weightToString(r);
61     return r.toString();
62   }
63   
64   public abstract void visitMatchingTerms(
65                             IndexReader reader,
66                             String fieldName,
67                             MatchingTermVisitor mtv) throws IOException;
68   
69   public interface MatchingTermVisitor {
70     void visitMatchingTerm(Term t)throws IOException;
71   }
72
73   public String distanceSubQueryNotAllowed() {return null;}
74
75   
76   @Override
77   public Query makeLuceneQueryFieldNoBoost(final String fieldName, final BasicQueryFactory qf) {
78     return new Query() {
79       @Override
80       public String toString(String fn) {
81         return getClass().toString() + " " + fieldName + " (" + fn + "?)";
82       }
83       
84       @Override
85       public Query rewrite(IndexReader reader) throws IOException {
86         final List<Query> luceneSubQueries = new ArrayList<Query>();
87         visitMatchingTerms( reader, fieldName,
88             new MatchingTermVisitor() {
89               public void visitMatchingTerm(Term term) throws IOException {
90                 luceneSubQueries.add(qf.newTermQuery(term));
91               }
92             });
93         return  (luceneSubQueries.size() == 0) ? SrndQuery.theEmptyLcnQuery
94               : (luceneSubQueries.size() == 1) ? luceneSubQueries.get(0)
95               : SrndBooleanQuery.makeBooleanQuery(
96                   /* luceneSubQueries all have default weight */
97                   luceneSubQueries, BooleanClause.Occur.SHOULD); /* OR the subquery terms */ 
98       }
99     };
100   }
101     
102   public void addSpanQueries(final SpanNearClauseFactory sncf) throws IOException {
103     visitMatchingTerms(
104           sncf.getIndexReader(),
105           sncf.getFieldName(),
106           new MatchingTermVisitor() {
107             public void visitMatchingTerm(Term term) throws IOException {
108               sncf.addTermWeighted(term, getWeight());
109             }
110           });
111   }
112 }
113
114
115