KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > core > urls > FreezableMutableURI


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.core.urls;
19
20 import java.net.URI JavaDoc;
21 import java.net.URISyntaxException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * A mutable class for creating URIs that can be set to "frozen" such
27  * that it becomes immutable. After this class is frozen, any calls to
28  * methods to set the data components of the URI will throw
29  */

30 public class FreezableMutableURI extends MutableURI
31 {
32
33     private boolean _frozen = false;
34
35     /**
36      * Constructs a <code>FreezableMutableURI</code>.
37      */

38     public FreezableMutableURI()
39     {
40     }
41
42     /**
43      * Constructs a <code>FreezableMutableURI</code>.
44      *
45      * @param uriString the string to be parsed into a URI
46      * @param encoded Flag indicating whether the string is
47      * already encoded.
48      */

49     public FreezableMutableURI( String JavaDoc uriString, boolean encoded ) throws URISyntaxException JavaDoc
50     {
51         super( uriString, encoded );
52     }
53
54     /**
55      * Constructs a <code>FreezableMutableURI</code>.
56      *
57      * @param scheme the name of the protocol to use
58      * @param userInfo the username and password
59      * @param host the name of the host
60      * @param port the port number on the host
61      * @param path the file on the host
62      * @param query the query part of this URI
63      * @param fragment the fragment part of this URI (internal reference in the URL)
64      */

65     public FreezableMutableURI( String JavaDoc scheme, String JavaDoc userInfo, String JavaDoc host, int port,
66                                 String JavaDoc path, String JavaDoc query, String JavaDoc fragment )
67     {
68         super( scheme, userInfo, host, port, path, query, fragment );
69     }
70
71     /**
72      * Constructs a <code>FreezableMutableURI</code>.
73      *
74      * @param uri the initial value for this mutable URI
75      */

76     public FreezableMutableURI( URI JavaDoc uri )
77     {
78         super( uri );
79     }
80
81     /**
82      * Constructs a <code>FreezableMutableURI</code>.
83      *
84      * <p> This is just a convenience constructor that functions the same as
85      * {@link #FreezableMutableURI(URI)} constructor with
86      * {@link java.net.URL#toURI()} as the argument. </p>
87      *
88      * <p>Note, any URL instance that complies with RFC 2396 can be converted
89      * to a URI. However, some URLs that are not strictly in compliance
90      * can not be converted to a URI. See {@link java.net.URL} </p>
91      *
92      * @param url the initial value for this mutable URI
93      * @exception URISyntaxException if this URL is not formatted strictly
94      * to RFC2396 and cannot be converted to a URI.
95      * @see java.net.URL#toURI()
96      */

97     public FreezableMutableURI( URL JavaDoc url ) throws URISyntaxException JavaDoc
98     {
99         super( url );
100     }
101
102     public final boolean isFrozen()
103     {
104         return _frozen;
105     }
106
107     /**
108      * Sets a flag indicating that the URI is immutable (or not).
109      *
110      * @param frozen flag to indicate if the URI is now immutable or not.
111      */

112     public void setFrozen( boolean frozen )
113     {
114         this._frozen = frozen;
115     }
116
117     private void testFrozen()
118     {
119         if ( _frozen )
120         {
121             throw new IllegalStateException JavaDoc( "Cannot modify the URI data. This instance was set to be immutable." );
122         }
123     }
124
125     /**
126      * Reset the value of the <code>FreezableMutableURI</code>.
127      *
128      * <p> This method can also be used to clear the <code>FreezableMutableURI</code>.
129      *
130      * @param uriString the string to be parsed into a URI
131      * @param encoded Flag indicating whether the string is
132      * already encoded.
133      */

134     
135     public void setURI( String JavaDoc uriString, boolean encoded ) throws URISyntaxException JavaDoc
136     {
137         testFrozen();
138         super.setURI( uriString, encoded );
139     }
140
141     /**
142      * Set the encoding used when adding unencoded parameters.
143      *
144      * @param encoding
145      */

146     
147     public void setEncoding( String JavaDoc encoding )
148     {
149         testFrozen();
150         super.setEncoding( encoding );
151     }
152
153     /**
154      * Sets the protocol/scheme.
155      *
156      * @param scheme protocol/scheme
157      */

158     
159     public void setScheme( String JavaDoc scheme )
160     {
161         testFrozen();
162         super.setScheme( scheme );
163     }
164
165     /**
166      * Sets the userInfo.
167      *
168      * @param userInfo userInfo
169      */

170     
171     public void setUserInfo( String JavaDoc userInfo )
172     {
173         testFrozen();
174         super.setUserInfo( userInfo );
175     }
176
177     /**
178      * Sets the host.
179      *
180      * @param host host
181      */

182     
183     public void setHost( String JavaDoc host )
184     {
185         testFrozen();
186         super.setHost( host );
187     }
188
189     /**
190      * Sets the port.
191      *
192      * @param port port
193      */

194     
195     public void setPort( int port )
196     {
197         testFrozen();
198         super.setPort( port );
199     }
200
201     /**
202      * Sets the path.
203      *
204      * @param path path
205      */

206     
207     public void setPath( String JavaDoc path )
208     {
209         testFrozen();
210         super.setPath( path );
211     }
212
213     /**
214      * Sets (and resets) the query string.
215      * This method assumes that the query is already encoded and
216      * the parameter delimiter is the '&amp;' character.
217      *
218      * @param query Query string
219      */

220     
221     public void setQuery( String JavaDoc query )
222     {
223         testFrozen();
224         super.setQuery( query );
225     }
226
227     /**
228      * Add a parameter for the query string.
229      * <p> If the encoded flag is true then this method assumes that
230      * the name and value do not need encoding or are already encoded
231      * correctly. Otherwise, it translates the name and value with the
232      * character encoding of this URI and adds them to the set of
233      * parameters for the query. If the encoding for this URI has
234      * not been set, then the default encoding used is "UTF-8". </p>
235      * <p> Multiple values for the same parameter can be set by
236      * calling this method multiple times with the same name. </p>
237      *
238      * @param name name
239      * @param value value
240      * @param encoded Flag indicating whether the names and values are
241      * already encoded.
242      */

243     
244     public void addParameter( String JavaDoc name, String JavaDoc value, boolean encoded )
245     {
246         testFrozen();
247         super.addParameter( name, value, encoded );
248     }
249
250     /**
251      * Add a parameter to the query string.
252      * <p> If the encoded flag is true then this method assumes that
253      * the name and value do not need encoding or are already encoded
254      * correctly. Otherwise, it translates the name and value with the
255      * character encoding of this URI and adds them to the set of
256      * parameters for the query. If the encoding for this URI has
257      * not been set, then the default encoding used is "UTF-8". </p>
258      *
259      * @param newParams the map of new parameters to add to the URI
260      * @param encoded Flag indicating whether the names and values are
261      * already encoded.
262      */

263     
264     public void addParameters( Map JavaDoc newParams, boolean encoded )
265     {
266         testFrozen();
267         super.addParameters( newParams, encoded );
268     }
269
270     /**
271      * Removes the given parameter.
272      *
273      * @param name name
274      */

275     
276     public void removeParameter( String JavaDoc name )
277     {
278         testFrozen();
279         super.removeParameter( name );
280     }
281
282     /**
283      * Sets the fragment.
284      *
285      * @param fragment fragment
286      */

287     
288     public void setFragment( String JavaDoc fragment )
289     {
290         testFrozen();
291         super.setFragment( fragment );
292     }
293
294     public boolean equals( Object JavaDoc o )
295     {
296         if ( this == o )
297         {
298             return true;
299         }
300         if ( !( o instanceof FreezableMutableURI ) )
301         {
302             return false;
303         }
304         if ( !super.equals( o ) )
305         {
306             return false;
307         }
308
309         final FreezableMutableURI freezableMutableURI = ( FreezableMutableURI ) o;
310
311         if ( _frozen != freezableMutableURI._frozen )
312         {
313             return false;
314         }
315
316         return true;
317     }
318
319     public int hashCode()
320     {
321         int result = super.hashCode();
322         result = 29 * result + ( _frozen ? 1 : 0 );
323         return result;
324     }
325 }
326
327
Popular Tags