KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/QueryTree.java,v 1.4 2004/07/28 09:35:01 ib Exp $
3  * $Revision: 1.4 $
4  * $Date: 2004/07/28 09:35:01 $
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.ArrayList JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 import org.apache.slide.common.Scope;
33 import org.apache.slide.search.InvalidScopeException;
34
35 /**
36  * Represents the scopes for all stores, that are within the scope of one query.
37  *
38  * @version $Revision: 1.4 $
39  *
40  **/

41 public class QueryTree {
42     
43     private TokenizedScope topNode;
44     
45     private List JavaDoc allQueryTreeNodes = new ArrayList JavaDoc ();
46     
47     
48     // QueryTree (Enumeration stores, Scope scope)
49
// throws InvalidScopeException
50
// {
51
// this (stores, scope, new Scope [0]);
52
// }
53

54     /**
55      * Constructs a query tree
56      *
57      * @param stores all stores, that are within this namespace
58      * @param scope the scope of this query
59      * @param excluded a list of scopes, that shall be excluded from search
60      */

61     QueryTree (Enumeration JavaDoc stores, Scope scope, Scope [] excluded)
62         throws InvalidScopeException
63     {
64         // System.out.println (scope);
65
topNode = new TokenizedScope (scope);
66         
67         while (stores.hasMoreElements()) {
68             Scope configuredStore = (Scope)stores.nextElement();
69             TokenizedScope tConfStore = new TokenizedScope (configuredStore);
70             if (tConfStore.isChildOf (topNode) && tConfStore.isNotExcluded (excluded) ) {
71                 allQueryTreeNodes.add (configuredStore);
72             }
73         }
74         allQueryTreeNodes.add (scope);
75         
76     }
77     
78     /**
79      * Checks, if the indicated scope has children within this QueryTree.
80      *
81      * @param scopeToBeChecked the Scope to be checked
82      *
83      * @return a boolean
84      *
85      */

86     public boolean hasChildren (Scope scopeToBeChecked) {
87         boolean result = false;
88         TokenizedScope tScopeToBeChecked = new TokenizedScope (scopeToBeChecked);
89         Iterator JavaDoc it = allQueryTreeNodes.iterator();
90         while (it.hasNext()) {
91             TokenizedScope ts = new TokenizedScope ((Scope)it.next());
92             if (ts.isChildOf (tScopeToBeChecked))
93                 return true;
94         }
95         
96         return false;
97     }
98     
99     /**
100      * calculates the depth of scope within this QueryTree (relative to the
101      * topLevel of tree)
102      *
103      * @param scope a Scope
104      *
105      * @return an int
106      *
107      */

108     public int relativeDepth (Scope scope) {
109         TokenizedScope tScope = new TokenizedScope (scope);
110         boolean contains = allQueryTreeNodes.contains (tScope);
111         
112         return tScope.depth - topNode.depth;
113     }
114     
115     
116     /**
117      * Retrieves an iterator of all scopes in this Tree
118      *
119      * @return an Iterator of Scope objects
120      *
121      */

122     public Iterator JavaDoc iterator () {
123         return allQueryTreeNodes.iterator();
124     }
125     
126     
127     /**
128      * Helper class to handle the scopes
129      *
130      * @version $Revision: 1.4 $
131      *
132      **/

133     class TokenizedScope {
134         private int depth;
135         Scope scope;
136         
137         TokenizedScope (Scope scope) {
138             this.scope = scope;
139             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (scope.toString(), "/");
140             int noOfTokens = st.countTokens();
141             depth = noOfTokens;
142         }
143         
144         /**
145          * Method equals
146          *
147          * @param o an Object
148          *
149          * @return a boolean
150          *
151          */

152         public boolean equals (Object JavaDoc o) {
153             return scope.equals(o);
154         }
155         
156         /**
157          * Method toString
158          *
159          * @return a String
160          *
161          */

162         public String JavaDoc toString () {
163             return scope.toString();
164         }
165         
166         /**
167          * Method isChildOf
168          *
169          * @param tScope a TokenizedScope
170          *
171          * @return a boolean
172          *
173          */

174         public boolean isChildOf (TokenizedScope tScope) {
175             if (depth > tScope.depth && toString().startsWith(tScope.toString())) {
176                 return true;
177             }
178             else {
179                 return false;
180             }
181         }
182         
183         /**
184          * Method isNotExcluded
185          *
186          * @param excluded a Scope[]
187          *
188          * @return a boolean
189          *
190          */

191         public boolean isNotExcluded (Scope []excluded) {
192             for (int i = 0; i < excluded.length; i++) {
193                 if (this.toString().startsWith(excluded [i].toString())) {
194                     return false;
195                 }
196             }
197             return true;
198         }
199     }
200 }
201
202
203
Popular Tags