KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > search > basic > BasicExpressionFactory


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/BasicExpressionFactory.java,v 1.13 2004/07/28 09:35:02 ib Exp $
3  * $Revision: 1.13 $
4  * $Date: 2004/07/28 09:35:02 $
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;
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.InvalidQueryException;
31 import org.apache.slide.search.PropertyProvider;
32 import org.apache.slide.search.basic.expression.AndExpression;
33 import org.apache.slide.search.basic.expression.ContainsExpression;
34 import org.apache.slide.search.basic.expression.EQExpression;
35 import org.apache.slide.search.basic.expression.EmptyExpression;
36 import org.apache.slide.search.basic.expression.GTEExpression;
37 import org.apache.slide.search.basic.expression.GTExpression;
38 import org.apache.slide.search.basic.expression.GenericBasicExpression;
39 import org.apache.slide.search.basic.expression.IsCollectionExpression;
40 import org.apache.slide.search.basic.expression.IsDefinedExpression;
41 import org.apache.slide.search.basic.expression.IsPrincipalExpression;
42 import org.apache.slide.search.basic.expression.LTEExpression;
43 import org.apache.slide.search.basic.expression.LTExpression;
44 import org.apache.slide.search.basic.expression.NotContainsExpression;
45 import org.apache.slide.search.basic.expression.NotEQExpression;
46 import org.apache.slide.search.basic.expression.NotGTEExpression;
47 import org.apache.slide.search.basic.expression.NotGTExpression;
48 import org.apache.slide.search.basic.expression.NotIsCollectionExpression;
49 import org.apache.slide.search.basic.expression.NotIsDefinedExpression;
50 import org.apache.slide.search.basic.expression.NotIsPrincipalExpression;
51 import org.apache.slide.search.basic.expression.NotLTEExpression;
52 import org.apache.slide.search.basic.expression.NotLTExpression;
53 import org.apache.slide.search.basic.expression.NotPropContainsExpression;
54 import org.apache.slide.search.basic.expression.OrExpression;
55 import org.apache.slide.search.basic.expression.PropContainsExpression;
56 import org.jdom.Element;
57
58 /**
59  * BasicExpressionFactory.java
60  *
61  */

62 public class BasicExpressionFactory implements IBasicExpressionFactory {
63     
64     // public static final String EXPRESSION_FACTORY = "ExpressionFactory";
65

66     /**
67      * The pool of resources in the given scope.
68      */

69     protected ComparableResourcesPool requestedResourcePool = null;
70     
71     /**
72      * The IBasicQuery to use.
73      */

74     protected IBasicQuery query = null;
75     
76     /**
77      * The PropertyProvider to use.
78      */

79     protected PropertyProvider propertyProvider = null;
80     
81     
82     
83     /**
84      * Creates a BasicExpressionFactory.
85      */

86     public BasicExpressionFactory() {
87         this(null);
88     }
89     
90     /**
91      * Creates a BasicExpressionFactory with the given
92      * <code>requestedResourcePool</code> to use.
93      *
94      * @param requestedResourcePool the RequestedResourcePool to use.
95      */

96     public BasicExpressionFactory(ComparableResourcesPool requestedResourcePool) {
97         this.requestedResourcePool = requestedResourcePool;
98     }
99     
100     /**
101      * Initializes the factory. Is called exactly once and before any call
102      * to crateExpression ()
103      *
104      * @param query the IBasicQuery.
105      * @param propertyProvider the PropertyProvider to use (may be
106      * <code>null</code>).
107      *
108      * @throws BadQueryException
109      */

110     public void init (IBasicQuery query, PropertyProvider propertyProvider) throws BadQueryException {
111         this.query = query;
112         this.propertyProvider = propertyProvider;
113     }
114     
115     /**
116      * Returns the IBasicQuery to use (set in method {@link #init init()}).
117      *
118      * @return the IBasicQuery to use.
119      */

120     public IBasicQuery getQuery() {
121         return query;
122     }
123     
124     /**
125      * Returns the PropertyProvider to use (set in method {@link #init init()}).
126      *
127      * @return the PropertyProvider to use.
128      */

129     public PropertyProvider getPropertyProvider() {
130         return propertyProvider;
131     }
132     
133     
134     /**
135      * Creates a MergeExpression for the given element (AND, OR). The given children
136      * are the expressions to merge.
137      *
138      * @param name the name of the Element describing the merge expression.
139      * @param namespace the namespace of the Element describing the merge expression.
140      * @param expressionsToMerge the expressions to merge.
141      *
142      * @return an IBasicExpression
143      *
144      * @throws BadQueryException
145      */

146     public IBasicExpression createMergeExpression (String JavaDoc name, String JavaDoc namespace, Collection JavaDoc expressionsToMerge) throws BadQueryException {
147         
148         IBasicExpression result = null;
149         if (name == null) {
150             result = new EmptyExpression(getRequestedResourcePool());
151         }
152         else {
153             if (NamespaceCache.DEFAULT_URI.equals(namespace)) {
154                 if (name.equals (Literals.AND))
155                     result = new AndExpression (new Element(name, NamespaceCache.getNamespace(namespace)),
156                                                 expressionsToMerge);
157                     
158                 else if (name.equals (Literals.OR))
159                     result = new OrExpression (new Element(name, NamespaceCache.getNamespace(namespace)),
160                                                expressionsToMerge);
161             }
162             if (result == null) {
163                 throw new InvalidQueryException
164                     ("operator <" + NamespaceCache.DEFAULT_URI + ":" + name + "> is an unprocessable entity");
165                 
166             }
167         }
168         if (result != null) result.setFactory(this);
169         return result;
170     }
171     
172     /**
173      * Creates a (non-merge) expression (compare...) for the given Element.
174      *
175      * @param element an Element describing the expression.
176      *
177      * @return an IBasicExpression
178      *
179      * @throws BadQueryException
180      *
181      */

182     public IBasicExpression createExpression (Element element) throws BadQueryException {
183         
184         IBasicExpression result = null;
185         if (element == null) {
186             result = new EmptyExpression(getRequestedResourcePool());
187         }
188         else {
189             String JavaDoc namespace = element.getNamespace().getURI();
190             String JavaDoc name = element.getName();
191             if (namespace.equals (NamespaceCache.DEFAULT_URI)) {
192                 result = createDAVExpression(element);
193             }
194             else if (namespace.equals (NamespaceCache.SLIDE_URI)) {
195                 result = createNonDAVExpression(element, namespace);
196             }
197             else {
198                 throw new InvalidQueryException
199                     ("operator <" + namespace + ":" + name + "> is an unprocessable entity");
200                 
201             }
202         }
203         
204         if (result != null) result.setFactory(this);
205         return result;
206     }
207     
208     
209     private GenericBasicExpression createDAVExpression
210         (Element e) throws BadQueryException
211     {
212         String JavaDoc name = e.getName();
213         GenericBasicExpression result = null;
214         
215         
216         if (name.equals (Literals.GT))
217             result = new GTExpression (e, getRequestedResourcePool());
218             
219         else if (name.equals (Literals.NOT_GT))
220             result = new NotGTExpression (e, getRequestedResourcePool());
221             
222         else if (name.equals (Literals.GTE))
223             result = new GTEExpression (e, getRequestedResourcePool());
224             
225         else if (name.equals (Literals.NOT_GTE))
226             result = new NotGTEExpression (e, getRequestedResourcePool());
227             
228         else if (name.equals (Literals.LT))
229             result = new LTExpression (e, getRequestedResourcePool());
230             
231         else if (name.equals (Literals.NOT_LT))
232             result = new NotLTExpression (e, getRequestedResourcePool());
233             
234         else if (name.equals (Literals.LTE))
235             result = new LTEExpression (e, getRequestedResourcePool());
236             
237         else if (name.equals (Literals.NOT_LTE))
238             result = new NotLTEExpression (e, getRequestedResourcePool());
239             
240         else if (name.equals (Literals.EQ))
241             result = new EQExpression (e, getRequestedResourcePool());
242             
243         else if (name.equals (Literals.NOT_EQ))
244             result = new NotEQExpression (e, getRequestedResourcePool());
245             
246         else if (name.equals (Literals.CONTAINS))
247             result = new ContainsExpression (e, getRequestedResourcePool());
248             
249         else if (name.equals (Literals.NOT_CONTAINS))
250             result = new NotContainsExpression (e, getRequestedResourcePool());
251             
252         else if (name.equals (Literals.ISCOLLECTION))
253             result = new IsCollectionExpression (e, getRequestedResourcePool());
254             
255         else if (name.equals (Literals.NOT_ISCOLLECTION))
256             result = new NotIsCollectionExpression (e, getRequestedResourcePool());
257             
258         else if (name.equals (Literals.ISDEFINED))
259             result = new IsDefinedExpression (e, getRequestedResourcePool());
260             
261         else if (name.equals (Literals.NOT_ISDEFINED))
262             result = new NotIsDefinedExpression (e, getRequestedResourcePool());
263             
264         else
265             throw new InvalidQueryException
266                 ("operator <" + NamespaceCache.DEFAULT_URI + ":" + name + "> is an unprocessable entity");
267         
268         return result;
269     }
270     
271     private GenericBasicExpression createNonDAVExpression
272         (Element e, String JavaDoc namespace) throws BadQueryException
273     {
274         
275         String JavaDoc name = e.getName();
276         GenericBasicExpression result = null;
277         
278         if (namespace.equals (NamespaceCache.SLIDE_URI)
279             && name.equals (Literals.ISPRINCIPAL))
280             
281             result = new IsPrincipalExpression (e, getRequestedResourcePool());
282             
283         else if (namespace.equals (NamespaceCache.SLIDE_URI)
284                  && name.equals (Literals.NOT_ISPRINCIPAL))
285             
286             result = new NotIsPrincipalExpression (e, getRequestedResourcePool());
287             
288         else if (namespace.equals (NamespaceCache.SLIDE_URI)
289                  && name.equals (Literals.PROPCONTAINS))
290             
291             result = new PropContainsExpression (e, getRequestedResourcePool());
292             
293         else if (namespace.equals (NamespaceCache.SLIDE_URI)
294                  && name.equals (Literals.NOT_PROPCONTAINS))
295             
296             result = new NotPropContainsExpression (e, getRequestedResourcePool());
297             
298         else
299             throw new InvalidQueryException
300                 ("operator <" + namespace + ":" + name + "> is an unprocessable entity");
301         
302         return result;
303     }
304     
305     
306     /**
307      * Returns the RequestedResourcesPool to use for the query. The pool will
308      * be created lazily, means the first time it is requested. If creation fails,
309      * a SearchException is thrown.
310      *
311      * @return the RequestedResourcesPool to use for the query.
312      *
313      * @throws SerachException if creation of the pool failed.
314      */

315     protected ComparableResourcesPool getRequestedResourcePool() throws BadQueryException {
316         
317         if (requestedResourcePool == null) {
318             requestedResourcePool =
319                 new ComparableResourcesPoolImpl(query.getSearchToken(), query.getScope(), getPropertyProvider());
320         }
321         return requestedResourcePool;
322     }
323 }
324
325
Popular Tags