add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / xml-query-parser / src / java / org / apache / lucene / xmlparser / builders / SpanOrTermsBuilder.java
1 package org.apache.lucene.xmlparser.builders;
2
3 import java.io.IOException;
4 import java.io.StringReader;
5 import java.util.ArrayList;
6
7 import org.apache.lucene.analysis.Analyzer;
8 import org.apache.lucene.analysis.TokenStream;
9 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
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  * 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 SpanOrTermsBuilder extends SpanBuilderBase
38 {
39     Analyzer analyzer;
40     
41     
42     /**
43      * @param analyzer
44      */
45     public SpanOrTermsBuilder(Analyzer analyzer)
46     {
47         super();
48         this.analyzer = analyzer;
49     }
50         public SpanQuery getSpanQuery(Element e) throws ParserException
51         {
52                 String fieldName=DOMUtils.getAttributeWithInheritanceOrFail(e,"fieldName");
53                 String value=DOMUtils.getNonBlankTextOrFail(e);
54                 
55                 try
56                 {
57                         ArrayList<SpanQuery> clausesList=new ArrayList<SpanQuery>();
58                         TokenStream ts=analyzer.reusableTokenStream(fieldName,new StringReader(value));
59                         CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
60                         
61       ts.reset();
62             while (ts.incrementToken()) {
63                             SpanTermQuery stq=new SpanTermQuery(new Term(fieldName, termAtt.toString()));
64                             clausesList.add(stq);
65                         }
66             ts.end();
67             ts.close();
68                         SpanOrQuery soq=new SpanOrQuery(clausesList.toArray(new SpanQuery[clausesList.size()]));
69                         soq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
70                         return soq;
71                 }
72                 catch(IOException ioe)
73                 {
74                     throw new ParserException("IOException parsing value:"+value);
75                 }
76         }
77
78 }