KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/BasicQueryScope.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.net.URL 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.content.NodeProperty;
32 import org.apache.slide.search.BadGatewayException;
33 import org.apache.slide.search.BadQueryException;
34 import org.apache.slide.search.QueryScope;
35 import org.jdom.Element;
36 import org.jdom.Namespace;
37
38
39 /**
40  * Holds the scope information supplied with the <FROM> element.
41  *
42  * @version $Revision: 1.13 $
43  */

44 public class BasicQueryScope implements QueryScope {
45     
46     private static Namespace slideNamespace =
47         NodeProperty.NamespaceCache.getNamespace (NodeProperty.SLIDE_NAMESPACE);
48     
49     /** String representing the URI */
50     private String JavaDoc href;
51     
52     /** the depth of this query. */
53     private int depth;
54     
55     /** indicates if this scope is a collection */
56     private boolean isCollection = true;
57     
58     /** holds hrefs, that shall be excluded from search. */
59     private Set JavaDoc excludedScopes = new HashSet JavaDoc ();
60     
61     
62     private Set JavaDoc includeSet = new HashSet JavaDoc ();
63     private Set JavaDoc excludeSet = new HashSet JavaDoc ();
64     
65     /**
66      * Constructs a scope
67      *
68      * @param propertyName the name of the property
69      */

70 // public BasicQueryScope (String href) {
71
// this.href = href;
72
// }
73

74     /**
75      * Constructs a scope
76      *
77      * @param propertyName the name of the property
78      */

79     public BasicQueryScope (String JavaDoc href, int depth, Set JavaDoc includeSet, Set JavaDoc excludeSet) {
80         this.href = href;
81         this.depth = depth;
82         this.includeSet = includeSet;
83         this.excludeSet = excludeSet;
84     }
85     
86     /**
87      * Constructs a scope from e FROM element
88      *
89      * @param propertyName the name of the property
90      */

91     public BasicQueryScope (Element fromElement)
92         throws BadQueryException
93     {
94         this.depth = QueryScope.DEPTH_INFINITY;
95         
96         Namespace namespace = fromElement.getNamespace();
97         String JavaDoc name = fromElement.getName ();
98         
99         if (! (namespace.getURI().equals (NodeProperty.DEFAULT_NAMESPACE)
100                    && name.equals (Literals.FROM)))
101             throw new BadQueryException ("expected DAV:from");
102         
103         Element scope = fromElement.getChild (Literals.SCOPE, namespace);
104         
105         if (scope.getChildren (Literals.HREF, namespace).size() != 1) {
106             throw new BadQueryException ("exactly one href element must be defined");
107         }
108         
109         href = scope.getChildTextTrim (Literals.HREF, namespace);
110         
111         try {
112             URL JavaDoc url = new URL JavaDoc (href);
113             throw new BadGatewayException (href + ": Bad Gateway (no server redirection allowed)");
114         }
115         // must not be a valid URL (server redirection not implemented)
116
catch (java.net.MalformedURLException JavaDoc e) {}
117         
118 // if (!href.endsWith("/"))
119
// href += "/";
120

121         Element depth = scope.getChild (Literals.DEPTH, namespace);
122         if (depth != null) {
123             String JavaDoc d = depth.getTextTrim();
124             if (d.equals ("0"))
125                 this.depth = 0;
126             else if (d.equals ("1"))
127                 this.depth = 1;
128         }
129         
130         Iterator JavaDoc it = scope.getChildren (Literals.EXCLUDE, slideNamespace).iterator();
131         while (it.hasNext()) {
132             Element excluded = (Element)it.next();
133             String JavaDoc href = excluded.getText();
134             excludedScopes.add (href);
135         }
136
137         it = scope.getChildren (Literals.INCLUDE_LASTPATHSEGEMENT, namespace).iterator();
138         while (it.hasNext()) {
139             Element incl = (Element)it.next();
140             String JavaDoc pattern = incl.getText();
141             includeSet.add (pattern);
142         }
143         
144         it = scope.getChildren (Literals.EXCLUDE_LASTPATHSEGEMENT, namespace).iterator();
145         while (it.hasNext()) {
146             Element incl = (Element)it.next();
147             String JavaDoc pattern = incl.getText();
148             excludeSet.add (pattern);
149         }
150         
151     }
152     
153     
154     /**
155      * href accessor
156      *
157      * @return a String
158      *
159      */

160     public String JavaDoc getHref() {
161         return href;
162     }
163     
164     /**
165      * depth accessor
166      *
167      * @return an int
168      *
169      */

170     public int getDepth() {
171         return depth;
172     }
173     
174     
175     /**
176      * checks, if another Object is equal to this SelectedProperty
177      *
178      * @param o an Object
179      *
180      * @return true if equal
181      */

182     public boolean equals (Object JavaDoc o) {
183         
184         if (! (o instanceof QueryScope))
185             return false;
186         
187         QueryScope other = (QueryScope)o;
188         if (!href.equals (other.getHref()))
189             return false;
190         
191         if (! (depth == other.getDepth()))
192             return false;
193         
194         return true;
195     }
196     
197     /**
198      * Debugging purpose
199      *
200      * @return String representation of this SelectedProperty
201      */

202     public String JavaDoc toString () {
203         return href + ", depth = " + depth;
204     }
205     
206     public void setIsCollection(boolean isCollection) {
207         this.isCollection = isCollection;
208         if (isCollection == true && !href.endsWith("/"))
209             href = href + "/";
210     }
211     
212     public boolean isCollection() {
213         return isCollection;
214     }
215     
216     
217     /**
218      * Method getExcludedScopes
219      *
220      * @return a Set
221      *
222      */

223     public Set JavaDoc getExcludedScopes() {
224         return excludedScopes;
225     }
226     
227         /**
228      * Method getIncludeSet
229      *
230      * @return a Set
231      */

232     public Set JavaDoc getIncludeSet() {
233         return includeSet;
234     }
235     
236     /**
237      * Method getExcludeSet
238      *
239      * @return a Set
240      */

241     public Set JavaDoc getExcludeSet() {
242         return excludeSet;
243     }
244     
245
246 }
247
Popular Tags