KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/search/basic/sample/BasicExpressionSample.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
25
26 package org.apache.slide.search.basic.sample;
27
28 import org.apache.slide.search.basic.*;
29
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import org.apache.slide.common.SlideException;
33 import org.apache.slide.search.BadQueryException;
34 import org.apache.slide.search.RequestedResource;
35 import org.apache.slide.search.SearchException;
36 import org.apache.slide.structure.ObjectNode;
37 import org.apache.slide.structure.SubjectNode;
38
39 /**
40  * A very basic sample for a store specific Expression. Depending on the
41  * complexity of the concrete store specific implementation, iut might be
42  * a good idea to have an Expression class for each DAV: expression
43  * (SQLEqExpression, SQLOrExpression, ...)
44  *
45  * @version $Revision: 1.5 $
46  */

47 public class BasicExpressionSample implements IBasicExpression {
48     
49     
50     /** an example for an executable command */
51     String JavaDoc theExecutableCommand;
52     
53     /** backptr to the factory */
54     IBasicExpressionFactory factory;
55     
56     /**
57      * constructor for a compare expression like gt, eq, ...
58      * For your concrete implementation you are free, which parameters have to
59      * be passed, let the factory give you everything you need.
60      */

61     BasicExpressionSample (String JavaDoc command, IBasicExpressionFactory factory){
62         theExecutableCommand = command;
63         this.factory = factory;
64     }
65     
66     /**
67      * contructor for a merge expression
68      */

69     BasicExpressionSample (String JavaDoc mergeOperator,
70                            Collection JavaDoc children,
71                            IBasicExpressionFactory factory)
72         throws BadQueryException
73     {
74         this.factory = factory;
75         Iterator JavaDoc it = children.iterator();
76         BasicExpressionSample firstChild = (BasicExpressionSample)it.next();
77         
78         if (firstChild == null)
79             throw new BadQueryException (mergeOperator + " needs at least one nested element");
80         
81         theExecutableCommand = firstChild.theExecutableCommand;
82         
83         // create the executable command
84
while (it.hasNext()) {
85             BasicExpressionSample exp = (BasicExpressionSample)it.next();
86             theExecutableCommand += " " + mergeOperator + " " + exp.theExecutableCommand;
87         }
88     }
89     
90     /**
91      * fake executer. The executable command is printed and a fake result is created.
92      *
93      * @return an IBasicResultSet
94      *
95      * @throws SearchException
96      *
97      */

98     public IBasicResultSet execute() throws SearchException {
99         
100         IBasicResultSet result = new BasicResultSetImpl (true);
101         
102         // here the miracle happens. The command is executed, and ObjectNodes
103
// are created from all results, that match the query.
104
System.out.println("now execute: " + theExecutableCommand);
105         
106         //
107
// fake one result
108
//
109
ObjectNode node = new SubjectNode("/"); // this will return the root folder
110
RequestedResource resource = null;
111         IBasicQuery query = factory.getQuery();
112         
113         try {
114             resource = new ComparableResourceImpl
115                 (node, query.getSearchToken(), query.getScope(),
116                  factory.getPropertyProvider());
117         }
118         catch (SlideException e) {
119             throw new SearchException (e);
120         }
121         
122         result.add (resource);
123         
124         
125         return result;
126     }
127     
128     public void setFactory (IBasicExpressionFactory factory) {
129         this.factory = factory;
130     }
131     
132     public IBasicExpressionFactory getFactory() {
133         return this.factory;
134     }
135     
136     
137 }
138
139
Popular Tags