001: package org.apache.lucene.queryParser.surround.query;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.util.List;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023:
024: public abstract class ComposedQuery extends SrndQuery {
025:
026: public ComposedQuery(List qs, boolean operatorInfix, String opName) {
027: recompose(qs);
028: this .operatorInfix = operatorInfix;
029: this .opName = opName;
030: }
031:
032: protected void recompose(List queries) {
033: if (queries.size() < 2)
034: throw new AssertionError("Too few subqueries");
035: this .queries = queries;
036: }
037:
038: private String opName;
039:
040: public String getOperatorName() {
041: return opName;
042: }
043:
044: private List queries;
045:
046: public Iterator getSubQueriesIterator() {
047: return queries.listIterator();
048: }
049:
050: public int getNrSubQueries() {
051: return queries.size();
052: }
053:
054: public SrndQuery getSubQuery(int qn) {
055: return (SrndQuery) queries.get(qn);
056: }
057:
058: private boolean operatorInfix;
059:
060: public boolean isOperatorInfix() {
061: return operatorInfix;
062: } /* else prefix operator */
063:
064: public List makeLuceneSubQueriesField(String fn,
065: BasicQueryFactory qf) {
066: ArrayList luceneSubQueries = new ArrayList();
067: Iterator sqi = getSubQueriesIterator();
068: while (sqi.hasNext()) {
069: luceneSubQueries.add(((SrndQuery) sqi.next())
070: .makeLuceneQueryField(fn, qf));
071: }
072: return luceneSubQueries;
073: }
074:
075: public String toString() {
076: StringBuffer r = new StringBuffer();
077: if (isOperatorInfix()) {
078: infixToString(r);
079: } else {
080: prefixToString(r);
081: }
082: weightToString(r);
083: return r.toString();
084: }
085:
086: /* Override for different spacing */
087: protected String getPrefixSeparator() {
088: return ", ";
089: }
090:
091: protected String getBracketOpen() {
092: return "(";
093: }
094:
095: protected String getBracketClose() {
096: return ")";
097: }
098:
099: protected void infixToString(StringBuffer r) {
100: /* Brackets are possibly redundant in the result. */
101: Iterator sqi = getSubQueriesIterator();
102: r.append(getBracketOpen());
103: if (sqi.hasNext()) {
104: r.append(sqi.next().toString());
105: while (sqi.hasNext()) {
106: r.append(" ");
107: r.append(getOperatorName()); /* infix operator */
108: r.append(" ");
109: r.append(sqi.next().toString());
110: }
111: }
112: r.append(getBracketClose());
113: }
114:
115: protected void prefixToString(StringBuffer r) {
116: Iterator sqi = getSubQueriesIterator();
117: r.append(getOperatorName()); /* prefix operator */
118: r.append(getBracketOpen());
119: if (sqi.hasNext()) {
120: r.append(sqi.next().toString());
121: while (sqi.hasNext()) {
122: r.append(getPrefixSeparator());
123: r.append(sqi.next().toString());
124: }
125: }
126: r.append(getBracketClose());
127: }
128:
129: public boolean isFieldsSubQueryAcceptable() {
130: /* at least one subquery should be acceptable */
131: Iterator sqi = getSubQueriesIterator();
132: while (sqi.hasNext()) {
133: if (((SrndQuery) sqi.next()).isFieldsSubQueryAcceptable()) {
134: return true;
135: }
136: }
137: return false;
138: }
139: }
|