KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > search > basic > sample > BasicExpressionFactorySample


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/search/basic/sample/BasicExpressionFactorySample.java,v 1.5 2004/07/28 09:34:22 ib Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/07/28 09:34:22 $
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.sample;
25
26 import java.util.Collection JavaDoc;
27
28 import org.apache.slide.content.NodeProperty.NamespaceCache;
29 import org.apache.slide.search.BadQueryException;
30 import org.apache.slide.search.PropertyProvider;
31 import org.apache.slide.search.basic.IBasicExpression;
32 import org.apache.slide.search.basic.IBasicExpressionFactory;
33 import org.apache.slide.search.basic.IBasicQuery;
34 import org.jdom.Element;
35
36 /**
37  * This factory creates executable BasicExpressions. An instance is created for
38  * each SEARCH request.
39  *
40  * @version $Revision: 1.5 $
41  */

42 public class BasicExpressionFactorySample implements IBasicExpressionFactory {
43     
44     
45     private IBasicQuery query;
46     protected PropertyProvider propertyProvider;
47     
48     /**
49      * called for merge expressions (or, and).
50      *
51      * @param mergeOperator and, or
52      * @param namespace the namespace of this expression
53      * @param expressionsToMerge all expressions, that shall be merged
54      *
55      * @return an IBasicExpression
56      *
57      * @throws BadQueryException
58      *
59      */

60     public IBasicExpression createMergeExpression (String JavaDoc mergeOperator,
61                                                    String JavaDoc namespace,
62                                                    Collection JavaDoc expressionsToMerge)
63         throws BadQueryException
64     {
65         // you might want to check for the namespace
66
BasicExpressionSample result = null;
67         try {
68             result = new BasicExpressionSample (mergeOperator, expressionsToMerge, this);
69         }
70         
71         // if one of the expressions is for example a generic expression,
72
// a ClassCastException is thrown, merge is not possible, so return null.
73
catch (ClassCastException JavaDoc e) {
74             System.out.println("one of the the expressions is not an ExpressionSample");
75         }
76         
77         return result;
78     }
79     
80     /**
81      * Called by the expression compiler for each leave expression.
82      *
83      * @param element an Element discribing the expression
84      *
85      * @return an IBasicExpression
86      *
87      * @throws BadQueryException
88      *
89      */

90     public IBasicExpression createExpression (Element element)
91         throws BadQueryException
92     {
93         BasicExpressionSample result = null;
94         
95         if (element == null) {
96             result = new BasicExpressionSample ("(no WHERE specified)", this);
97         }
98         else {
99             String JavaDoc namespace = element.getNamespace().getURI();
100             if (namespace.equals (NamespaceCache.DEFAULT_URI))
101                 result = createDAVExpression (element);
102             
103             // allow store specific extensions
104
// else if (namespace.equals (MyNamespace))
105
// result = createMyExpression (element);
106
}
107         
108         return result;
109     }
110     
111     
112     /**
113      * Called, when the expression is in the default (DAV:) namespace.
114      *
115      *
116      * @param e an Element
117      *
118      * @return a BasicExpressionTemplate
119      *
120      */

121     private BasicExpressionSample createDAVExpression (Element e) {
122         String JavaDoc name = e.getName();
123         BasicExpressionSample result = null;
124         
125         if (name.equals ("eq")) {
126             String JavaDoc prop = propName (e);
127             String JavaDoc literal = e.getChild ("literal", e.getNamespace()).getText();
128             result = new BasicExpressionSample ("(" + prop + " equals " + literal + ")", this);
129         }
130             
131         else if (name.equals ("lt")) {
132             String JavaDoc prop = propName (e);
133             String JavaDoc literal = e.getChildText ("literal", e.getNamespace());
134             
135             result = new BasicExpressionSample ("(" + prop + " lower_than " + literal + ")", this);
136         }
137         // ...
138

139         return result;
140     }
141         
142     /**
143      * called by BasicExpressionCompiler after construction.
144      *
145      * @param query the associated BasicQuery
146      * @param propertyProvider the PropertyProvider for this expression.
147      *
148      * @throws BadQueryException
149      *
150      */

151     public void init(IBasicQuery query, PropertyProvider propertyProvider)
152         throws BadQueryException
153     {
154         this.query = (IBasicQuery) query;
155         this.propertyProvider = propertyProvider;
156     }
157     
158     /**
159      * Method getPropertyProvider
160      *
161      * @return the PropertyProvider
162      *
163      */

164     public PropertyProvider getPropertyProvider() {
165         return propertyProvider;
166     }
167     
168     /**
169      * Method getQuery
170      *
171      * @return the IBasicQuery
172      *
173      */

174     public IBasicQuery getQuery() {
175         return query;
176     }
177     
178     
179     private String JavaDoc propName (Element e) {
180         Element propElem = e.getChild ("prop", e.getNamespace());
181         Element el = (Element) propElem.getChildren().get(0);
182         return el.getName();
183     }
184 }
185
186
Popular Tags