KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/ComparableResourcesPoolImpl.java,v 1.4 2004/07/28 09:35:02 ib Exp $
3  * $Revision: 1.4 $
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.Enumeration JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import org.apache.slide.common.SlideException;
32 import org.apache.slide.common.SlideToken;
33 import org.apache.slide.content.Content;
34 import org.apache.slide.search.BadQueryException;
35 import org.apache.slide.search.InvalidScopeException;
36 import org.apache.slide.search.PropertyProvider;
37 import org.apache.slide.search.QueryScope;
38 import org.apache.slide.search.SearchToken;
39 import org.apache.slide.search.SlideUri;
40 import org.apache.slide.structure.ObjectNode;
41 import org.apache.slide.structure.Structure;
42 import org.apache.slide.structure.StructureException;
43
44 /**
45  * Represents the pool of all resources out of which the query result is
46  * computed.
47  *
48  * @version $Revision: 1.4 $
49  */

50 public class ComparableResourcesPoolImpl implements ComparableResourcesPool {
51     
52     /** */
53     private Structure structure;
54     private Content contentHelper;
55     private SlideToken slideToken;
56     private QueryScope scope;
57     private SearchToken searchToken;
58     
59     private int scopeDepth;
60     private int maxSlideDepth;
61     private boolean partialResult = false;
62     private Set JavaDoc pool;
63     private SlideUri slideContext;
64     
65     /**
66      * The PropertyProvider to use (may be <code>null</code>).
67      */

68     protected PropertyProvider propertyProvider = null;
69     
70     /**
71      * Constructs a RequestedResourcesPool
72      *
73      * @param searchToken the searchToken
74      * @param scope the scope of the query
75      * @param propertyProvider the PropertyProvider to use (may be
76      * <code>null</code>).
77      *
78      * @throws BadQueryException
79      */

80     public ComparableResourcesPoolImpl (SearchToken searchToken,
81                                        QueryScope scope,
82                                        PropertyProvider propertyProvider)
83         throws BadQueryException
84     {
85         this.structure = searchToken.getStructureHelper();
86         this.slideToken = searchToken.getSlideToken();
87         this.scope = scope;
88         this.propertyProvider = propertyProvider;
89         this.contentHelper = searchToken.getContentHelper();
90         this.slideContext = searchToken.getSlideContext();
91         this.searchToken = searchToken;
92         
93         scopeDepth = scope.getDepth ();
94         maxSlideDepth = searchToken.getMaxDepth();
95         
96         createPool ();
97     }
98     
99     /**
100      * Method resourceIterator
101      *
102      * @return an Iterator
103      *
104      */

105     public Iterator JavaDoc resourceIterator() {
106         return pool.iterator();
107     }
108     
109     
110     /**
111      * Method getPool
112      *
113      * @return a Set
114      *
115      */

116     public Set JavaDoc getPool() {
117         return pool;
118     }
119     
120     
121     /**
122      * Method createPool
123      *
124      * @throws BadQueryException
125      *
126      */

127     private void createPool () throws BadQueryException {
128         pool = new HashSet JavaDoc ();
129         
130         String JavaDoc resourceUri = searchToken.getSlideContext().getSlidePath (scope.getHref());
131         
132         // Get the object from Data.
133
ObjectNode resource = null;
134         
135         try {
136             resource = structure.retrieve (slideToken, resourceUri);
137             parseOneObject (resource, 0);
138         }
139         catch (StructureException e) {
140             throw new InvalidScopeException
141                 ("scope " + resourceUri + " is invalid");
142         }
143         catch (Exception JavaDoc e) {
144             throw new BadQueryException (e.getMessage());
145         }
146     }
147     
148     /**
149      * Method parseOneObject. Called recursively, result set truncation is done
150      * here.
151      *
152      * @param object an ObjectNode
153      * @param currentDepth an int
154      *
155      * @throws SlideException
156      *
157      */

158     private void parseOneObject (ObjectNode object, int currentDepth)
159         throws SlideException
160     {
161         if (currentDepth > scopeDepth)
162             return;
163         
164         if (currentDepth > maxSlideDepth) {
165             partialResult = true;
166             return;
167         }
168         
169         Enumeration JavaDoc children = null;
170         children = structure.getChildren (slideToken, object);
171         
172         while (children.hasMoreElements()) {
173             ObjectNode cur = (ObjectNode)children.nextElement();
174             parseOneObject (cur, currentDepth + 1);
175         }
176         
177         ComparableResource item =
178             new ComparableResourceImpl (object, searchToken, scope, propertyProvider);
179         
180         pool.add (item);
181     }
182     
183     /**
184      * Indicates if the server truncated the result set.
185      *
186      * @return a boolean
187      *
188      */

189     public boolean partialResult() {
190         return partialResult;
191     }
192
193     /**
194      * Returns the scope of this ResourcePool.
195      *
196      * @return the scope of this ResourcePool.
197      */

198     public QueryScope getScope() {
199         return scope;
200     }
201
202 }
203
Popular Tags