KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > common > ScopeTokenizer


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/ScopeTokenizer.java,v 1.7 2004/07/28 09:38:16 ib Exp $
3  * $Revision: 1.7 $
4  * $Date: 2004/07/28 09:38:16 $
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.common;
25
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.Vector JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30
31 /**
32  * Tokenizes a scope.
33  * <p/>
34  * This tokenizer derives from StringTokenizer and tokenizes a scope in sub scopes.
35  * This object is used for Scope matching in the registry. The API of this object
36  * is the same as the StringTokenizer API.
37  * <p/>
38  * Ex : the scope /foo/bar/bar.txt will be tokenized in the following tokens :
39  * <li>/foo/bar/bar.txt
40  * <li>/foo/bar
41  * <li>/foo
42  * <li>/
43  *
44  * @see StringTokenizer
45  * @version $Revision: 1.7 $
46  */

47 public final class ScopeTokenizer extends StringTokenizer JavaDoc {
48     
49     
50     // ----------------------------------------------------------- Constructors
51

52     
53     /**
54      * Constructor.
55      *
56      * @param uri Uri which needs to be tokenized in Scopes.
57      */

58     public ScopeTokenizer(SlideToken slideToken, Namespace namespace, String JavaDoc uri) {
59         super(uri, "/");
60         
61         // We then build the scopes.
62
scopes = new Vector JavaDoc();
63         
64         // Initializing namespace
65
this.slideToken = slideToken;
66         this.namespace = namespace;
67         
68         String JavaDoc courScope = new String JavaDoc();
69         
70         scopes.insertElementAt("/", 0);
71         
72         // We parse the token list and build the scope String objects.
73
while(super.hasMoreTokens()) {
74             courScope = courScope + "/" + super.nextToken();
75             scopes.insertElementAt(courScope, 0);
76         }
77         
78         pos = 0;
79     }
80     
81     
82     /**
83      * Constructor.
84      *
85      * @param namespace
86      * @param uri
87      */

88     public ScopeTokenizer(Namespace namespace, String JavaDoc uri) {
89         this(null, namespace, uri);
90     }
91     
92     
93     /**
94      * Constructor.
95      *
96      * @param namespace
97      * @param scope
98      */

99     public ScopeTokenizer(Namespace namespace, Scope scope) {
100         this(null, namespace, scope.toString());
101     }
102     
103     
104     // ----------------------------------------------------- Instance Variables
105

106     
107     /**
108      * Tokens container.
109      */

110     Vector JavaDoc scopes;
111     
112     
113     /**
114      * Cursor position in the token container.
115      */

116     int pos;
117     
118     
119     /**
120      * Namespace.
121      */

122     Namespace namespace;
123     SlideToken slideToken;
124     
125     
126     // --------------------------------------------------------- Public Methods
127

128     
129     /**
130      * Get the parent Uri of the top level scope.
131      *
132      * @return Uri Parent Uri
133      */

134     public Uri getParentUri() {
135         if (scopes.size() <= 1) {
136             return null;
137         } else {
138             return namespace.getUri(slideToken, (String JavaDoc) scopes.elementAt(1));
139         }
140     }
141     
142     
143     /**
144      * True if the ScopeTokenizer contains additional scopes.
145      *
146      * @return boolean
147      */

148     public boolean hasMoreElements() {
149         return scopes.size() > pos;
150     }
151     
152     
153     /**
154      * True if the ScopeTokenizer contains additional scopes.
155      *
156      * @return boolean
157      */

158     public boolean hasMoreTokens() {
159         return hasMoreElements();
160     }
161     
162     
163     /**
164      * Returns the next Scope as an Object.
165      *
166      * @return Object
167      * @exception NoSuchElementException
168      */

169     public Object JavaDoc nextElement()
170     throws NoSuchElementException JavaDoc {
171     Object JavaDoc obj = null;
172     try {
173         if (!hasMoreElements()) {
174         throw new NoSuchElementException JavaDoc();
175         }
176         obj = scopes.elementAt(pos);
177         pos = pos + 1;
178     } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
179         // Should NEVER happen.
180
e.printStackTrace();
181     }
182     return obj;
183     }
184     
185     
186     /**
187      * Returns the next Scope as a String object.
188      *
189      * @return String
190      * @exception NoSuchElementException
191      */

192     public String JavaDoc nextToken()
193     throws NoSuchElementException JavaDoc {
194     return (String JavaDoc) nextElement();
195     }
196     
197     
198     /**
199      * Returns the next Scope as a Scope object.
200      *
201      * @return Scope
202      * @exception NoSuchElementException
203      */

204     public Scope nextScope()
205     throws NoSuchElementException JavaDoc {
206     return new Scope(nextToken());
207     }
208     
209     
210     /**
211      * Returns the next Scope as a String object.
212      *
213      * @param delim
214      * @exception NoSuchElementException
215      */

216     public String JavaDoc nextToken(String JavaDoc delim)
217     throws NoSuchElementException JavaDoc {
218     return nextToken();
219     }
220     
221     
222     /**
223      * Returns the scopes.
224      *
225      * @return Enumeration
226      */

227     public Enumeration JavaDoc elements() {
228     return scopes.elements();
229     }
230     
231     
232     // -------------------------------------------------------- Package Methods
233

234     
235     /**
236      * Get the parsed uri.
237      *
238      * @return String Uri
239      */

240     String JavaDoc getUri() {
241         if (scopes.size() < 1) {
242             return null;
243         } else {
244             return (String JavaDoc) scopes.elementAt(0);
245         }
246     }
247     
248 }
249
Popular Tags