KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/UriTokenizer.java,v 1.7 2004/07/28 09:38:08 ib Exp $
3  * $Revision: 1.7 $
4  * $Date: 2004/07/28 09:38:08 $
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.NoSuchElementException JavaDoc;
29
30 /**
31  * Tokenizes a scope.
32  * <p/>
33  * This tokenizer derives from StringTokenizer and tokenizes an Uri.
34  * This object is used for browsing down the main tree from the root
35  * element during retrieval and create operations.
36  * <p/>
37  * Ex : the uri /foo/bar/bar.txt will be tokenized in the following tokens :
38  * <li>/
39  * <li>/foo
40  * <li>/foo/bar
41  * <li>/foo/bar/bar.txt
42  *
43  * @version $Revision: 1.7 $
44  */

45 public final class UriTokenizer extends StringTokenizer JavaDoc {
46     
47     
48     // ----------------------------------------------------------- Constructors
49

50     
51     /**
52      * Constructor.
53      *
54      * @param namespace
55      * @param uri
56      */

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

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

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

104     
105     /**
106      * Vector of all the scopes matched by the associated Uri.
107      */

108     Vector JavaDoc uris;
109     
110     
111     /**
112      * Position in the uri vector.
113      */

114     int pos;
115     
116     
117     /**
118      * Associated namespace.
119      */

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

127     
128     /**
129      * Get the parent uri.
130      *
131      * @return Uri
132      */

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

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

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

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

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

203     public Uri nextUri()
204     throws NoSuchElementException JavaDoc {
205     return namespace.getUri(slideToken, nextToken());
206     }
207     
208     
209     /**
210      * Returns the next Uri as an Uri object.
211      *
212      * @param delim
213      * @exception NoSuchElementException
214      */

215     public String JavaDoc nextToken(String JavaDoc delim)
216     throws NoSuchElementException JavaDoc {
217     return nextToken();
218     }
219     
220 }
221
Popular Tags