01: package org.apache.lucene.xmlparser.builders;
02:
03: import java.util.ArrayList;
04:
05: import org.apache.lucene.search.spans.SpanNearQuery;
06: import org.apache.lucene.search.spans.SpanQuery;
07: import org.apache.lucene.xmlparser.DOMUtils;
08: import org.apache.lucene.xmlparser.ParserException;
09: import org.w3c.dom.Element;
10: import org.w3c.dom.Node;
11:
12: /**
13: * Licensed to the Apache Software Foundation (ASF) under one or more
14: * contributor license agreements. See the NOTICE file distributed with
15: * this work for additional information regarding copyright ownership.
16: * The ASF licenses this file to You under the Apache License, Version 2.0
17: * (the "License"); you may not use this file except in compliance with
18: * the License. You may obtain a copy of the License at
19: *
20: * http://www.apache.org/licenses/LICENSE-2.0
21: *
22: * Unless required by applicable law or agreed to in writing, software
23: * distributed under the License is distributed on an "AS IS" BASIS,
24: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25: * See the License for the specific language governing permissions and
26: * limitations under the License.
27: */
28: public class SpanNearBuilder extends SpanBuilderBase {
29: SpanQueryBuilder factory;
30:
31: public SpanNearBuilder(SpanQueryBuilder factory) {
32: this .factory = factory;
33: }
34:
35: public SpanQuery getSpanQuery(Element e) throws ParserException {
36: String slopString = DOMUtils.getAttributeOrFail(e, "slop");
37: int slop = Integer.parseInt(slopString);
38: boolean inOrder = DOMUtils.getAttribute(e, "inOrder", false);
39: ArrayList spans = new ArrayList();
40: for (Node kid = e.getFirstChild(); kid != null; kid = kid
41: .getNextSibling()) {
42: if (kid.getNodeType() == Node.ELEMENT_NODE) {
43: spans.add(factory.getSpanQuery((Element) kid));
44: }
45: }
46: SpanQuery[] spanQueries = (SpanQuery[]) spans
47: .toArray(new SpanQuery[spans.size()]);
48: SpanNearQuery snq = new SpanNearQuery(spanQueries, slop,
49: inOrder);
50: return snq;
51: }
52:
53: }
|