001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.core.urls;
020:
021: import java.net.URI;
022: import java.net.URISyntaxException;
023: import java.net.URL;
024: import java.util.Map;
025:
026: /**
027: * A mutable class for creating URIs that can be set to "frozen" such
028: * that it becomes immutable. After this class is frozen, any calls to
029: * methods to set the data components of the URI will throw
030: */
031: public class FreezableMutableURI extends MutableURI {
032:
033: private boolean _frozen = false;
034:
035: /**
036: * Constructs a <code>FreezableMutableURI</code>.
037: */
038: public FreezableMutableURI() {
039: }
040:
041: /**
042: * Constructs a <code>FreezableMutableURI</code>.
043: *
044: * @param uriString the string to be parsed into a URI
045: * @param encoded Flag indicating whether the string is
046: * already encoded.
047: */
048: public FreezableMutableURI(String uriString, boolean encoded)
049: throws URISyntaxException {
050: super (uriString, encoded);
051: }
052:
053: /**
054: * Constructs a <code>FreezableMutableURI</code>.
055: *
056: * @param scheme the name of the protocol to use
057: * @param userInfo the username and password
058: * @param host the name of the host
059: * @param port the port number on the host
060: * @param path the file on the host
061: * @param query the query part of this URI
062: * @param fragment the fragment part of this URI (internal reference in the URL)
063: */
064: public FreezableMutableURI(String scheme, String userInfo,
065: String host, int port, String path, String query,
066: String fragment) {
067: super (scheme, userInfo, host, port, path, query, fragment);
068: }
069:
070: /**
071: * Constructs a <code>FreezableMutableURI</code>.
072: *
073: * @param uri the initial value for this mutable URI
074: */
075: public FreezableMutableURI(URI uri) {
076: super (uri);
077: }
078:
079: /**
080: * Constructs a <code>FreezableMutableURI</code>.
081: *
082: * <p> This is just a convenience constructor that functions the same as
083: * {@link #FreezableMutableURI(URI)} constructor with
084: * {@link java.net.URL#toURI()} as the argument. </p>
085: *
086: * <p>Note, any URL instance that complies with RFC 2396 can be converted
087: * to a URI. However, some URLs that are not strictly in compliance
088: * can not be converted to a URI. See {@link java.net.URL} </p>
089: *
090: * @param url the initial value for this mutable URI
091: * @exception URISyntaxException if this URL is not formatted strictly
092: * to RFC2396 and cannot be converted to a URI.
093: * @see java.net.URL#toURI()
094: */
095: public FreezableMutableURI(URL url) throws URISyntaxException {
096: super (url);
097: }
098:
099: public final boolean isFrozen() {
100: return _frozen;
101: }
102:
103: /**
104: * Sets a flag indicating that the URI is immutable (or not).
105: *
106: * @param frozen flag to indicate if the URI is now immutable or not.
107: */
108: public void setFrozen(boolean frozen) {
109: this ._frozen = frozen;
110: }
111:
112: private void testFrozen() {
113: if (_frozen) {
114: throw new IllegalStateException(
115: "Cannot modify the URI data. This instance was set to be immutable.");
116: }
117: }
118:
119: /**
120: * Reset the value of the <code>FreezableMutableURI</code>.
121: *
122: * <p> This method can also be used to clear the <code>FreezableMutableURI</code>.
123: *
124: * @param uriString the string to be parsed into a URI
125: * @param encoded Flag indicating whether the string is
126: * already encoded.
127: */
128:
129: public void setURI(String uriString, boolean encoded)
130: throws URISyntaxException {
131: testFrozen();
132: super .setURI(uriString, encoded);
133: }
134:
135: /**
136: * Set the encoding used when adding unencoded parameters.
137: *
138: * @param encoding
139: */
140:
141: public void setEncoding(String encoding) {
142: testFrozen();
143: super .setEncoding(encoding);
144: }
145:
146: /**
147: * Sets the protocol/scheme.
148: *
149: * @param scheme protocol/scheme
150: */
151:
152: public void setScheme(String scheme) {
153: testFrozen();
154: super .setScheme(scheme);
155: }
156:
157: /**
158: * Sets the userInfo.
159: *
160: * @param userInfo userInfo
161: */
162:
163: public void setUserInfo(String userInfo) {
164: testFrozen();
165: super .setUserInfo(userInfo);
166: }
167:
168: /**
169: * Sets the host.
170: *
171: * @param host host
172: */
173:
174: public void setHost(String host) {
175: testFrozen();
176: super .setHost(host);
177: }
178:
179: /**
180: * Sets the port.
181: *
182: * @param port port
183: */
184:
185: public void setPort(int port) {
186: testFrozen();
187: super .setPort(port);
188: }
189:
190: /**
191: * Sets the path.
192: *
193: * @param path path
194: */
195:
196: public void setPath(String path) {
197: testFrozen();
198: super .setPath(path);
199: }
200:
201: /**
202: * Sets (and resets) the query string.
203: * This method assumes that the query is already encoded and
204: * the parameter delimiter is the '&' character.
205: *
206: * @param query Query string
207: */
208:
209: public void setQuery(String query) {
210: testFrozen();
211: super .setQuery(query);
212: }
213:
214: /**
215: * Add a parameter for the query string.
216: * <p> If the encoded flag is true then this method assumes that
217: * the name and value do not need encoding or are already encoded
218: * correctly. Otherwise, it translates the name and value with the
219: * character encoding of this URI and adds them to the set of
220: * parameters for the query. If the encoding for this URI has
221: * not been set, then the default encoding used is "UTF-8". </p>
222: * <p> Multiple values for the same parameter can be set by
223: * calling this method multiple times with the same name. </p>
224: *
225: * @param name name
226: * @param value value
227: * @param encoded Flag indicating whether the names and values are
228: * already encoded.
229: */
230:
231: public void addParameter(String name, String value, boolean encoded) {
232: testFrozen();
233: super .addParameter(name, value, encoded);
234: }
235:
236: /**
237: * Add a parameter to the query string.
238: * <p> If the encoded flag is true then this method assumes that
239: * the name and value do not need encoding or are already encoded
240: * correctly. Otherwise, it translates the name and value with the
241: * character encoding of this URI and adds them to the set of
242: * parameters for the query. If the encoding for this URI has
243: * not been set, then the default encoding used is "UTF-8". </p>
244: *
245: * @param newParams the map of new parameters to add to the URI
246: * @param encoded Flag indicating whether the names and values are
247: * already encoded.
248: */
249:
250: public void addParameters(Map newParams, boolean encoded) {
251: testFrozen();
252: super .addParameters(newParams, encoded);
253: }
254:
255: /**
256: * Removes the given parameter.
257: *
258: * @param name name
259: */
260:
261: public void removeParameter(String name) {
262: testFrozen();
263: super .removeParameter(name);
264: }
265:
266: /**
267: * Sets the fragment.
268: *
269: * @param fragment fragment
270: */
271:
272: public void setFragment(String fragment) {
273: testFrozen();
274: super .setFragment(fragment);
275: }
276:
277: /**
278: * Sets the URI to be opaque using the given scheme and
279: * schemeSpecificPart.
280: * <p> From {@link URI}: "A URI is opaque if, and only
281: * if, it is absolute and its scheme-specific part does not begin with
282: * a slash character ('/'). An opaque URI has a scheme, a
283: * scheme-specific part, and possibly a fragment; all other components
284: * are undefined." </p>
285: *
286: * @param scheme the scheme component of this URI
287: * @param schemeSpecificPart the scheme-specific part of this URI
288: */
289: public void setOpaque(String scheme, String schemeSpecificPart) {
290: testFrozen();
291: super .setOpaque(scheme, schemeSpecificPart);
292: }
293:
294: public boolean equals(Object o) {
295: if (this == o) {
296: return true;
297: }
298: if (!(o instanceof FreezableMutableURI)) {
299: return false;
300: }
301: if (!super .equals(o)) {
302: return false;
303: }
304:
305: final FreezableMutableURI freezableMutableURI = (FreezableMutableURI) o;
306:
307: if (_frozen != freezableMutableURI._frozen) {
308: return false;
309: }
310:
311: return true;
312: }
313:
314: public int hashCode() {
315: int result = super .hashCode();
316: result = 29 * result + (_frozen ? 1 : 0);
317: return result;
318: }
319: }
|