01: /*
02: * Created on 25-Jan-2006
03: */
04: package org.apache.lucene.xmlparser.builders;
05:
06: import org.apache.lucene.search.BooleanClause;
07: import org.apache.lucene.search.BooleanQuery;
08: import org.apache.lucene.search.Query;
09: import org.apache.lucene.xmlparser.DOMUtils;
10: import org.apache.lucene.xmlparser.ParserException;
11: import org.apache.lucene.xmlparser.QueryBuilder;
12: import org.w3c.dom.Element;
13: import org.w3c.dom.Node;
14: import org.w3c.dom.NodeList;
15:
16: /**
17: * Licensed to the Apache Software Foundation (ASF) under one or more
18: * contributor license agreements. See the NOTICE file distributed with
19: * this work for additional information regarding copyright ownership.
20: * The ASF licenses this file to You under the Apache License, Version 2.0
21: * (the "License"); you may not use this file except in compliance with
22: * the License. You may obtain a copy of the License at
23: *
24: * http://www.apache.org/licenses/LICENSE-2.0
25: *
26: * Unless required by applicable law or agreed to in writing, software
27: * distributed under the License is distributed on an "AS IS" BASIS,
28: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29: * See the License for the specific language governing permissions and
30: * limitations under the License.
31: */
32:
33: /**
34: * @author maharwood
35: */
36: public class BooleanQueryBuilder implements QueryBuilder {
37:
38: private QueryBuilder factory;
39:
40: public BooleanQueryBuilder(QueryBuilder factory) {
41: this .factory = factory;
42: }
43:
44: /* (non-Javadoc)
45: * @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
46: */
47: public Query getQuery(Element e) throws ParserException {
48: BooleanQuery bq = new BooleanQuery(DOMUtils.getAttribute(e,
49: "disableCoord", false));
50: bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e,
51: "minimumNumberShouldMatch", 0));
52: bq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));
53:
54: NodeList nl = e.getChildNodes();
55: for (int i = 0; i < nl.getLength(); i++) {
56: Node node = nl.item(i);
57: if (node.getNodeName().equals("Clause")) {
58: Element clauseElem = (Element) node;
59: BooleanClause.Occur occurs = getOccursValue(clauseElem);
60:
61: Element clauseQuery = DOMUtils
62: .getFirstChildOrFail(clauseElem);
63: Query q = factory.getQuery(clauseQuery);
64: bq.add(new BooleanClause(q, occurs));
65: }
66: }
67:
68: return bq;
69: }
70:
71: static BooleanClause.Occur getOccursValue(Element clauseElem)
72: throws ParserException {
73: String occs = clauseElem.getAttribute("occurs");
74: BooleanClause.Occur occurs = BooleanClause.Occur.SHOULD;
75: if ("must".equalsIgnoreCase(occs)) {
76: occurs = BooleanClause.Occur.MUST;
77: } else {
78: if ("mustNot".equalsIgnoreCase(occs)) {
79: occurs = BooleanClause.Occur.MUST_NOT;
80: } else {
81: if (("should".equalsIgnoreCase(occs))
82: || ("".equals(occs))) {
83: occurs = BooleanClause.Occur.SHOULD;
84: } else {
85: if (occs != null) {
86: throw new ParserException(
87: "Invalid value for \"occurs\" attribute of clause:"
88: + occs);
89: }
90: }
91: }
92: }
93: return occurs;
94:
95: }
96:
97: }
|