add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / xml-query-parser / src / java / org / apache / lucene / xmlparser / builders / TermsFilterBuilder.java
1 package org.apache.lucene.xmlparser.builders;
2
3 import java.io.IOException;
4 import java.io.StringReader;
5
6 import org.apache.lucene.analysis.Analyzer;
7 import org.apache.lucene.analysis.TokenStream;
8 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
9 import org.apache.lucene.index.Term;
10 import org.apache.lucene.search.Filter;
11 import org.apache.lucene.search.TermsFilter;
12 import org.apache.lucene.xmlparser.DOMUtils;
13 import org.apache.lucene.xmlparser.FilterBuilder;
14 import org.apache.lucene.xmlparser.ParserException;
15 import org.w3c.dom.Element;
16
17 /**
18  * Licensed to the Apache Software Foundation (ASF) under one or more
19  * contributor license agreements.  See the NOTICE file distributed with
20  * this work for additional information regarding copyright ownership.
21  * The ASF licenses this file to You under the Apache License, Version 2.0
22  * (the "License"); you may not use this file except in compliance with
23  * the License.  You may obtain a copy of the License at
24  *
25  *     http://www.apache.org/licenses/LICENSE-2.0
26  *
27  * Unless required by applicable law or agreed to in writing, software
28  * distributed under the License is distributed on an "AS IS" BASIS,
29  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30  * See the License for the specific language governing permissions and
31  * limitations under the License.
32  */
33
34 /**
35  * 
36  */
37 public class TermsFilterBuilder implements FilterBuilder
38 {
39         Analyzer analyzer;
40         
41         /**
42          * @param analyzer
43          */
44         public TermsFilterBuilder(Analyzer analyzer)
45         {
46                 this.analyzer = analyzer;
47         }
48         
49         /*
50          * (non-Javadoc)
51          * 
52          * @see org.apache.lucene.xmlparser.FilterBuilder#process(org.w3c.dom.Element)
53          */
54         public Filter getFilter(Element e) throws ParserException
55         {
56                 TermsFilter tf = new TermsFilter();
57                 String text = DOMUtils.getNonBlankTextOrFail(e);
58                 String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
59     
60                 try
61                 {
62             TokenStream ts = analyzer.reusableTokenStream(fieldName, new StringReader(text));
63             CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
64                         Term term = null;
65       ts.reset();
66               while (ts.incrementToken()) {
67                                 if (term == null)
68                                 {
69                                         term = new Term(fieldName, termAtt.toString());
70                                 } else
71                                 {
72 //                                       create from previous to save fieldName.intern overhead
73                                         term = term.createTerm(termAtt.toString()); 
74                                 }
75                                 tf.addTerm(term);
76                         }
77             ts.end();
78             ts.close();
79                 } 
80                 catch (IOException ioe)
81                 {
82                         throw new RuntimeException("Error constructing terms from index:"
83                                         + ioe);
84                 }
85                 return tf;
86         }
87 }