01: package org.apache.lucene.xmlparser.builders;
02:
03: import java.io.IOException;
04: import java.io.StringReader;
05: import java.util.ArrayList;
06:
07: import org.apache.lucene.analysis.Analyzer;
08: import org.apache.lucene.analysis.Token;
09: import org.apache.lucene.analysis.TokenStream;
10: import org.apache.lucene.index.Term;
11: import org.apache.lucene.search.spans.SpanOrQuery;
12: import org.apache.lucene.search.spans.SpanQuery;
13: import org.apache.lucene.search.spans.SpanTermQuery;
14: import org.apache.lucene.xmlparser.DOMUtils;
15: import org.apache.lucene.xmlparser.ParserException;
16: import org.w3c.dom.Element;
17:
18: /**
19: * Licensed to the Apache Software Foundation (ASF) under one or more
20: * contributor license agreements. See the NOTICE file distributed with
21: * this work for additional information regarding copyright ownership.
22: * The ASF licenses this file to You under the Apache License, Version 2.0
23: * (the "License"); you may not use this file except in compliance with
24: * the License. You may obtain a copy of the License at
25: *
26: * http://www.apache.org/licenses/LICENSE-2.0
27: *
28: * Unless required by applicable law or agreed to in writing, software
29: * distributed under the License is distributed on an "AS IS" BASIS,
30: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31: * See the License for the specific language governing permissions and
32: * limitations under the License.
33: */
34: public class SpanOrTermsBuilder extends SpanBuilderBase {
35: Analyzer analyzer;
36:
37: /**
38: * @param analyzer
39: */
40: public SpanOrTermsBuilder(Analyzer analyzer) {
41: super ();
42: this .analyzer = analyzer;
43: }
44:
45: public SpanQuery getSpanQuery(Element e) throws ParserException {
46: String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(
47: e, "fieldName");
48: String value = DOMUtils.getNonBlankTextOrFail(e);
49:
50: try {
51: ArrayList clausesList = new ArrayList();
52: TokenStream ts = analyzer.tokenStream(fieldName,
53: new StringReader(value));
54: Token token = ts.next();
55: while (token != null) {
56: SpanTermQuery stq = new SpanTermQuery(new Term(
57: fieldName, token.termText()));
58: clausesList.add(stq);
59: token = ts.next();
60: }
61: SpanOrQuery soq = new SpanOrQuery((SpanQuery[]) clausesList
62: .toArray(new SpanQuery[clausesList.size()]));
63: soq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));
64: return soq;
65: } catch (IOException ioe) {
66: throw new ParserException("IOException parsing value:"
67: + value);
68: }
69: }
70:
71: }
|