1 package org.apache.lucene.xmlparser.builders;
3 import java.io.IOException;
4 import java.io.StringReader;
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;
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
25 * http://www.apache.org/licenses/LICENSE-2.0
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.
37 public class TermsFilterBuilder implements FilterBuilder
44 public TermsFilterBuilder(Analyzer analyzer)
46 this.analyzer = analyzer;
52 * @see org.apache.lucene.xmlparser.FilterBuilder#process(org.w3c.dom.Element)
54 public Filter getFilter(Element e) throws ParserException
56 TermsFilter tf = new TermsFilter();
57 String text = DOMUtils.getNonBlankTextOrFail(e);
58 String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
62 TokenStream ts = analyzer.reusableTokenStream(fieldName, new StringReader(text));
63 CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
66 while (ts.incrementToken()) {
69 term = new Term(fieldName, termAtt.toString());
72 // create from previous to save fieldName.intern overhead
73 term = term.createTerm(termAtt.toString());
80 catch (IOException ioe)
82 throw new RuntimeException("Error constructing terms from index:"