KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > search > basic > expression > MergeExpression


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/expression/MergeExpression.java,v 1.6 2004/07/28 09:34:50 ib Exp $
3  * $Revision: 1.6 $
4  * $Date: 2004/07/28 09:34:50 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.search.basic.expression;
25
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.apache.slide.search.InvalidQueryException;
30 import org.apache.slide.search.SearchException;
31 import org.apache.slide.search.basic.IBasicExpression;
32 import org.apache.slide.search.basic.IBasicResultSet;
33 import org.jdom.Element;
34
35 /**
36  * Abstract base class for merge expressions (AND, OR).
37  *
38  * @version $Revision: 1.6 $
39  */

40 public abstract class MergeExpression extends GenericBasicExpression {
41     
42     
43     /** all nested expressions */
44     private Collection JavaDoc expressionsToMerge = null;
45     
46     /**
47      * Creates a merge expression according to Element e
48      *
49      * @param e jdom element, that describes the expression
50      * @param expressionsToMerge a Collection of IBasicExpressions to merge.
51      */

52     MergeExpression (Element e, Collection JavaDoc expressionsToMerge) throws InvalidQueryException {
53         super (e);
54         this.expressionsToMerge = expressionsToMerge;
55         if (expressionsToMerge.size() == 0) {
56             throw new InvalidQueryException(getMustHaveMergeExpressionsMessage(e.getName()));
57         }
58     }
59         
60     /**
61      * Executes the expression.
62      *
63      * @return a Set of RequestedResource objects
64      *
65      * @throws SearchException
66      */

67     public IBasicResultSet execute () throws SearchException {
68         
69         Iterator JavaDoc iterator = expressionsToMerge.iterator();
70         if (iterator.hasNext()) {
71             resultSet = ((IBasicExpression)iterator.next()).execute();
72         }
73         while (iterator.hasNext()) {
74             IBasicExpression expression = (IBasicExpression)iterator.next();
75             merge(expression.execute());
76         }
77         return resultSet;
78     }
79     
80     
81     /**
82      * Merges the given <code>set</code> into the result Set of this expression.
83      *
84      * @param set the Set to merge.
85      */

86     protected abstract void merge (IBasicResultSet set);
87     
88     
89     /**
90      * String representation for debugging purposes.
91      *
92      * @return this expression as String
93      */

94     protected String JavaDoc toString (String JavaDoc op) {
95         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
96         
97         Iterator JavaDoc it = expressionsToMerge.iterator();
98         
99         while (it.hasNext()) {
100             sb.append (((IBasicExpression) it.next()).toString());
101             if (it.hasNext())
102                 sb.append (" ").append (op).append (" ");
103         }
104         return sb.toString();
105     }
106     
107     /**
108      * Returns the message of the InvalidQueryException that is thrown by the
109      * constructor the <code>expressionsToMerge</code> set is empty.
110      *
111      * @param operationName the name of the operation (e.g. <code>and</code>)
112      *
113      * @return the message of the InvalidQueryException.
114      */

115     private static String JavaDoc getMustHaveMergeExpressionsMessage(String JavaDoc operationName) {
116         return "<" + operationName + "> must have at least on nested expression.";
117     }
118 }
119
120
121
Popular Tags