KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > bean > query > SimpleLuceneCriterionBean


1 /*
2   Copyright 1999-2004 The Apache Software Foundation
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 */

16
17 package org.apache.cocoon.bean.query;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.util.Vector JavaDoc;
23 import org.apache.cocoon.components.search.LuceneXMLIndexer;
24 import org.apache.lucene.analysis.Analyzer;
25 import org.apache.lucene.analysis.Token;
26 import org.apache.lucene.analysis.TokenStream;
27 import org.apache.lucene.index.Term;
28 import org.apache.lucene.search.BooleanQuery;
29 import org.apache.lucene.search.FuzzyQuery;
30 import org.apache.lucene.search.PhraseQuery;
31 import org.apache.lucene.search.Query;
32 import org.apache.lucene.search.TermQuery;
33
34
35 /**
36  * The criterion bean.
37  * <p>
38  * This object defines a <code>Bean</code> for holding a query criterion.<br/>
39  * The idea is to abstract the process of searching into a Bean to be manipulated by CForms.<br/>
40  * This Bean is designed to be persistable.
41  * </p>
42  *
43  */

44 public class SimpleLuceneCriterionBean implements SimpleLuceneCriterion, Cloneable JavaDoc, Serializable JavaDoc {
45
46     /**
47      * The Bean's ID.
48      */

49     protected Long JavaDoc id;
50
51     /**
52      * The Bean's index field to seach in.
53      */

54     protected String JavaDoc field;
55
56     /**
57      * The Bean's match value.
58      */

59     protected String JavaDoc match;
60
61     /**
62      * The Bean's search term.
63      */

64     protected String JavaDoc term;
65     
66     /**
67      * Default constructor.
68      */

69     public SimpleLuceneCriterionBean() {
70     }
71
72
73     /**
74      * Utility constructor.
75      *
76      * @param match the kind of match to use
77      * @param field the field to search
78      * @param term the terms to search for
79      */

80     public SimpleLuceneCriterionBean(String JavaDoc field, String JavaDoc match, String JavaDoc term) {
81         this.field = field;
82         this.match = match;
83         this.term = term;
84     }
85
86     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
87         SimpleLuceneCriterionBean criterion = (SimpleLuceneCriterionBean)super.clone();
88         return criterion;
89     }
90
91     /**
92      * Gets the <code>org.apache.lucene.search.Query</code> from the Criterion
93      * <p>
94      * The analyzer specifies which <code>org.apache.lucene.analysis.Analyzer</code> to use for this search.
95      * </p>
96      *
97      * @param analyzer The <code>org.apache.lucene.analysis.Analyzer</code> to use to extract the Terms from this Criterion
98      */

99     public Query getQuery (Analyzer analyzer) {
100         String JavaDoc f = this.field;
101         Query query = null;
102         if (ANY_FIELD.equals(this.field)) f = LuceneXMLIndexer.BODY_FIELD;
103         // extract Terms from the query string
104
TokenStream tokens = analyzer.tokenStream(f, new StringReader JavaDoc (this.term));
105     Vector JavaDoc words = new Vector JavaDoc();
106     Token token;
107     while (true) {
108       try {
109         token = tokens.next();
110       } catch (IOException JavaDoc e) {
111         token = null;
112       }
113       if (token == null) break;
114       words.addElement(token.termText ());
115     }
116     try {
117       tokens.close();
118     } catch (IOException JavaDoc e) {} // ignore
119

120         // assemble the different matches
121

122         if (ANY_MATCH.equals(this.match)) {
123             if (words.size() > 1) {
124                 query = new BooleanQuery();
125                 for (int i = 0; i < words.size(); i++) {
126                     ((BooleanQuery)query).add(new TermQuery(new Term(f, (String JavaDoc)words.elementAt(i))), false, false);
127                 }
128             } else if (words.size() == 1) {
129                 query = new TermQuery(new Term(f, (String JavaDoc)words.elementAt(0)));
130             }
131         }
132         
133         if (ALL_MATCH.equals(this.match)) {
134             if (words.size() > 1) {
135                 query = new BooleanQuery();
136                 for (int i = 0; i < words.size(); i++) {
137                     ((BooleanQuery)query).add(new TermQuery(new Term (f, (String JavaDoc)words.elementAt(i))), true, false);
138                 }
139             } else if (words.size() == 1) {
140                 query = new TermQuery(new Term(f, (String JavaDoc)words.elementAt(0)));
141             }
142         }
143         
144         if (NOT_MATCH.equals(this.match)) {
145             if (words.size() > 1) {
146                 query = new BooleanQuery();
147                 for (int i = 0; i < words.size(); i++) {
148                     ((BooleanQuery)query).add(new TermQuery(new Term(f, (String JavaDoc)words.elementAt(i))), true, true);
149                 }
150             } else if (words.size() == 1) {
151                 query = new TermQuery(new Term(f, (String JavaDoc)words.elementAt(0)));
152             }
153         }
154         
155         if (LIKE_MATCH.equals(this.match)) {
156             if (words.size() > 1) {
157                 query = new BooleanQuery();
158                 for (int i = 0; i < words.size(); i++) {
159                     ((BooleanQuery)query).add(new FuzzyQuery(new Term(f, (String JavaDoc)words.elementAt(i))), false, false);
160                 }
161             } else if (words.size() == 1) {
162                 query = new FuzzyQuery(new Term(f, (String JavaDoc)words.elementAt(0)));
163             }
164         }
165         
166         if (PHRASE_MATCH.equals (this.match)) {
167             if (words.size() > 1) {
168                 query = new PhraseQuery();
169                 ((PhraseQuery)query).setSlop(0);
170                 for (int i = 0; i < words.size(); i++) {
171                     ((PhraseQuery)query).add(new Term(f, (String JavaDoc)words.elementAt(i)));
172                 }
173             } else if (words.size() == 1) {
174                 query = new TermQuery(new Term(f, (String JavaDoc)words.elementAt(0)));
175             }
176         }
177         return query;
178     }
179     
180     /**
181      * Gets the prohibited status from the Criterion
182      */

183     public boolean isProhibited () {
184         if (NOT_MATCH.equals(this.match)) return true;
185         return false;
186     }
187     
188     
189     // Bean
190

191     /**
192      * Gets the Bean's ID
193      *
194      * @return the <code>Long</code> ID of the Bean.
195      */

196     public Long JavaDoc getId() {
197         return this.id;
198     }
199     
200     /**
201      * Sets the Bean's ID
202      *
203      * @param id the <code>Long</code> ID of the Bean.
204      */

205     public void setId(Long JavaDoc id) {
206         this.id = id;
207     }
208     
209     /**
210      * Gets the Bean's field
211      *
212      * @return the <code>String</code> field of the Bean.
213      */

214     public String JavaDoc getField() {
215         return this.field;
216     }
217     
218     /**
219      * Sets the Bean's field.<br/>
220      * ie. which field would you like this Criterion to search in.
221      *
222      * @param field the <code>String</code> field of the Bean.
223      */

224     public void setField(String JavaDoc field) {
225         this.field = field;
226     }
227     
228     /**
229      * Gets the Bean's match
230      *
231      * @return the <code>String</code> match of the Bean.
232      */

233     public String JavaDoc getMatch() {
234         return this.match;
235     }
236     
237     /**
238      * Sets the Bean's match.<br/>
239      * ie. what kind of match do you want performed by this Criterion.
240      *
241      * @param match the <code>String</code> match of the Bean.
242      */

243     public void setMatch(String JavaDoc match) {
244         this.match = match;
245     }
246     
247     /**
248      * Gets the Bean's term
249      *
250      * @return the <code>String</code> term of the Bean.
251      */

252     public String JavaDoc getTerm() {
253         return this.term;
254     }
255     
256     /**
257      * Sets the Bean's term.<br/>
258      * ie. the string of search terms for this <code>Criterion</code>.
259      *
260      * @param term the <code>String</code> term of the Bean.
261      */

262     public void setTerm(String JavaDoc term) {
263         this.term = term;
264     }
265
266 }
267
Popular Tags