src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/util/JaxmURI.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.messaging.saaj.util;
ohair@286 27
ohair@286 28 // Imported from: org.apache.xerces.util
ohair@286 29 // Needed to work around differences in JDK1.2 and 1.3 and deal with userInfo
ohair@286 30
ohair@286 31 import java.io.IOException;
ohair@286 32 import java.io.Serializable;
ohair@286 33
ohair@286 34
ohair@286 35 /**********************************************************************
ohair@286 36 * A class to represent a Uniform Resource Identifier (URI). This class
ohair@286 37 * is designed to handle the parsing of URIs and provide access to
ohair@286 38 * the various components (scheme, host, port, userinfo, path, query
ohair@286 39 * string and fragment) that may constitute a URI.
ohair@286 40 * <p>
ohair@286 41 * Parsing of a URI specification is done according to the URI
ohair@286 42 * syntax described in RFC 2396
ohair@286 43 * <http://www.ietf.org/rfc/rfc2396.txt?number=2396>. Every URI consists
ohair@286 44 * of a scheme, followed by a colon (':'), followed by a scheme-specific
ohair@286 45 * part. For URIs that follow the "generic URI" syntax, the scheme-
ohair@286 46 * specific part begins with two slashes ("//") and may be followed
ohair@286 47 * by an authority segment (comprised of user information, host, and
ohair@286 48 * port), path segment, query segment and fragment. Note that RFC 2396
ohair@286 49 * no longer specifies the use of the parameters segment and excludes
ohair@286 50 * the "user:password" syntax as part of the authority segment. If
ohair@286 51 * "user:password" appears in a URI, the entire user/password string
ohair@286 52 * is stored as userinfo.
ohair@286 53 * <p>
ohair@286 54 * For URIs that do not follow the "generic URI" syntax (e.g. mailto),
ohair@286 55 * the entire scheme-specific part is treated as the "path" portion
ohair@286 56 * of the URI.
ohair@286 57 * <p>
ohair@286 58 * Note that, unlike the java.net.URL class, this class does not provide
ohair@286 59 * any built-in network access functionality nor does it provide any
ohair@286 60 * scheme-specific functionality (for example, it does not know a
ohair@286 61 * default port for a specific scheme). Rather, it only knows the
ohair@286 62 * grammar and basic set of operations that can be applied to a URI.
ohair@286 63 *
ohair@286 64 * @version
ohair@286 65 *
ohair@286 66 **********************************************************************/
ohair@286 67 public class JaxmURI implements Serializable {
ohair@286 68
ohair@286 69 /*******************************************************************
ohair@286 70 * MalformedURIExceptions are thrown in the process of building a URI
ohair@286 71 * or setting fields on a URI when an operation would result in an
ohair@286 72 * invalid URI specification.
ohair@286 73 *
ohair@286 74 ********************************************************************/
ohair@286 75 public static class MalformedURIException extends IOException {
ohair@286 76
ohair@286 77 /******************************************************************
ohair@286 78 * Constructs a <code>MalformedURIException</code> with no specified
ohair@286 79 * detail message.
ohair@286 80 ******************************************************************/
ohair@286 81 public MalformedURIException() {
ohair@286 82 super();
ohair@286 83 }
ohair@286 84
ohair@286 85 /*****************************************************************
ohair@286 86 * Constructs a <code>MalformedURIException</code> with the
ohair@286 87 * specified detail message.
ohair@286 88 *
ohair@286 89 * @param p_msg the detail message.
ohair@286 90 ******************************************************************/
ohair@286 91 public MalformedURIException(String p_msg) {
ohair@286 92 super(p_msg);
ohair@286 93 }
ohair@286 94 }
ohair@286 95
ohair@286 96 /** reserved characters */
ohair@286 97 private static final String RESERVED_CHARACTERS = ";/?:@&=+$,";
ohair@286 98
ohair@286 99 /** URI punctuation mark characters - these, combined with
ohair@286 100 alphanumerics, constitute the "unreserved" characters */
ohair@286 101 private static final String MARK_CHARACTERS = "-_.!~*'() ";
ohair@286 102
ohair@286 103 /** scheme can be composed of alphanumerics and these characters */
ohair@286 104 private static final String SCHEME_CHARACTERS = "+-.";
ohair@286 105
ohair@286 106 /** userinfo can be composed of unreserved, escaped and these
ohair@286 107 characters */
ohair@286 108 private static final String USERINFO_CHARACTERS = ";:&=+$,";
ohair@286 109
ohair@286 110 /** Stores the scheme (usually the protocol) for this URI. */
ohair@286 111 private String m_scheme = null;
ohair@286 112
ohair@286 113 /** If specified, stores the userinfo for this URI; otherwise null */
ohair@286 114 private String m_userinfo = null;
ohair@286 115
ohair@286 116 /** If specified, stores the host for this URI; otherwise null */
ohair@286 117 private String m_host = null;
ohair@286 118
ohair@286 119 /** If specified, stores the port for this URI; otherwise -1 */
ohair@286 120 private int m_port = -1;
ohair@286 121
ohair@286 122 /** If specified, stores the path for this URI; otherwise null */
ohair@286 123 private String m_path = null;
ohair@286 124
ohair@286 125 /** If specified, stores the query string for this URI; otherwise
ohair@286 126 null. */
ohair@286 127 private String m_queryString = null;
ohair@286 128
ohair@286 129 /** If specified, stores the fragment for this URI; otherwise null */
ohair@286 130 private String m_fragment = null;
ohair@286 131
ohair@286 132 private static boolean DEBUG = false;
ohair@286 133
ohair@286 134 /**
ohair@286 135 * Construct a new and uninitialized URI.
ohair@286 136 */
ohair@286 137 public JaxmURI() {
ohair@286 138 }
ohair@286 139
ohair@286 140 /**
ohair@286 141 * Construct a new URI from another URI. All fields for this URI are
ohair@286 142 * set equal to the fields of the URI passed in.
ohair@286 143 *
ohair@286 144 * @param p_other the URI to copy (cannot be null)
ohair@286 145 */
ohair@286 146 public JaxmURI(JaxmURI p_other) {
ohair@286 147 initialize(p_other);
ohair@286 148 }
ohair@286 149
ohair@286 150 /**
ohair@286 151 * Construct a new URI from a URI specification string. If the
ohair@286 152 * specification follows the "generic URI" syntax, (two slashes
ohair@286 153 * following the first colon), the specification will be parsed
ohair@286 154 * accordingly - setting the scheme, userinfo, host,port, path, query
ohair@286 155 * string and fragment fields as necessary. If the specification does
ohair@286 156 * not follow the "generic URI" syntax, the specification is parsed
ohair@286 157 * into a scheme and scheme-specific part (stored as the path) only.
ohair@286 158 *
ohair@286 159 * @param p_uriSpec the URI specification string (cannot be null or
ohair@286 160 * empty)
ohair@286 161 *
ohair@286 162 * @exception MalformedURIException if p_uriSpec violates any syntax
ohair@286 163 * rules
ohair@286 164 */
ohair@286 165 public JaxmURI(String p_uriSpec) throws MalformedURIException {
ohair@286 166 this((JaxmURI)null, p_uriSpec);
ohair@286 167 }
ohair@286 168
ohair@286 169 /**
ohair@286 170 * Construct a new URI from a base URI and a URI specification string.
ohair@286 171 * The URI specification string may be a relative URI.
ohair@286 172 *
ohair@286 173 * @param p_base the base URI (cannot be null if p_uriSpec is null or
ohair@286 174 * empty)
ohair@286 175 * @param p_uriSpec the URI specification string (cannot be null or
ohair@286 176 * empty if p_base is null)
ohair@286 177 *
ohair@286 178 * @exception MalformedURIException if p_uriSpec violates any syntax
ohair@286 179 * rules
ohair@286 180 */
ohair@286 181 public JaxmURI(JaxmURI p_base, String p_uriSpec) throws MalformedURIException {
ohair@286 182 initialize(p_base, p_uriSpec);
ohair@286 183 }
ohair@286 184
ohair@286 185 /**
ohair@286 186 * Construct a new URI that does not follow the generic URI syntax.
ohair@286 187 * Only the scheme and scheme-specific part (stored as the path) are
ohair@286 188 * initialized.
ohair@286 189 *
ohair@286 190 * @param p_scheme the URI scheme (cannot be null or empty)
ohair@286 191 * @param p_schemeSpecificPart the scheme-specific part (cannot be
ohair@286 192 * null or empty)
ohair@286 193 *
ohair@286 194 * @exception MalformedURIException if p_scheme violates any
ohair@286 195 * syntax rules
ohair@286 196 */
ohair@286 197 public JaxmURI(String p_scheme, String p_schemeSpecificPart)
ohair@286 198 throws MalformedURIException {
ohair@286 199 if (p_scheme == null || p_scheme.trim().length() == 0) {
ohair@286 200 throw new MalformedURIException(
ohair@286 201 "Cannot construct URI with null/empty scheme!");
ohair@286 202 }
ohair@286 203 if (p_schemeSpecificPart == null ||
ohair@286 204 p_schemeSpecificPart.trim().length() == 0) {
ohair@286 205 throw new MalformedURIException(
ohair@286 206 "Cannot construct URI with null/empty scheme-specific part!");
ohair@286 207 }
ohair@286 208 setScheme(p_scheme);
ohair@286 209 setPath(p_schemeSpecificPart);
ohair@286 210 }
ohair@286 211
ohair@286 212 /**
ohair@286 213 * Construct a new URI that follows the generic URI syntax from its
ohair@286 214 * component parts. Each component is validated for syntax and some
ohair@286 215 * basic semantic checks are performed as well. See the individual
ohair@286 216 * setter methods for specifics.
ohair@286 217 *
ohair@286 218 * @param p_scheme the URI scheme (cannot be null or empty)
ohair@286 219 * @param p_host the hostname or IPv4 address for the URI
ohair@286 220 * @param p_path the URI path - if the path contains '?' or '#',
ohair@286 221 * then the query string and/or fragment will be
ohair@286 222 * set from the path; however, if the query and
ohair@286 223 * fragment are specified both in the path and as
ohair@286 224 * separate parameters, an exception is thrown
ohair@286 225 * @param p_queryString the URI query string (cannot be specified
ohair@286 226 * if path is null)
ohair@286 227 * @param p_fragment the URI fragment (cannot be specified if path
ohair@286 228 * is null)
ohair@286 229 *
ohair@286 230 * @exception MalformedURIException if any of the parameters violates
ohair@286 231 * syntax rules or semantic rules
ohair@286 232 */
ohair@286 233 public JaxmURI(String p_scheme, String p_host, String p_path,
ohair@286 234 String p_queryString, String p_fragment)
ohair@286 235 throws MalformedURIException {
ohair@286 236 this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment);
ohair@286 237 }
ohair@286 238
ohair@286 239 /**
ohair@286 240 * Construct a new URI that follows the generic URI syntax from its
ohair@286 241 * component parts. Each component is validated for syntax and some
ohair@286 242 * basic semantic checks are performed as well. See the individual
ohair@286 243 * setter methods for specifics.
ohair@286 244 *
ohair@286 245 * @param p_scheme the URI scheme (cannot be null or empty)
ohair@286 246 * @param p_userinfo the URI userinfo (cannot be specified if host
ohair@286 247 * is null)
ohair@286 248 * @param p_host the hostname or IPv4 address for the URI
ohair@286 249 * @param p_port the URI port (may be -1 for "unspecified"; cannot
ohair@286 250 * be specified if host is null)
ohair@286 251 * @param p_path the URI path - if the path contains '?' or '#',
ohair@286 252 * then the query string and/or fragment will be
ohair@286 253 * set from the path; however, if the query and
ohair@286 254 * fragment are specified both in the path and as
ohair@286 255 * separate parameters, an exception is thrown
ohair@286 256 * @param p_queryString the URI query string (cannot be specified
ohair@286 257 * if path is null)
ohair@286 258 * @param p_fragment the URI fragment (cannot be specified if path
ohair@286 259 * is null)
ohair@286 260 *
ohair@286 261 * @exception MalformedURIException if any of the parameters violates
ohair@286 262 * syntax rules or semantic rules
ohair@286 263 */
ohair@286 264 public JaxmURI(String p_scheme, String p_userinfo,
ohair@286 265 String p_host, int p_port, String p_path,
ohair@286 266 String p_queryString, String p_fragment)
ohair@286 267 throws MalformedURIException {
ohair@286 268 if (p_scheme == null || p_scheme.trim().length() == 0) {
ohair@286 269 throw new MalformedURIException("Scheme is required!");
ohair@286 270 }
ohair@286 271
ohair@286 272 if (p_host == null) {
ohair@286 273 if (p_userinfo != null) {
ohair@286 274 throw new MalformedURIException(
ohair@286 275 "Userinfo may not be specified if host is not specified!");
ohair@286 276 }
ohair@286 277 if (p_port != -1) {
ohair@286 278 throw new MalformedURIException(
ohair@286 279 "Port may not be specified if host is not specified!");
ohair@286 280 }
ohair@286 281 }
ohair@286 282
ohair@286 283 if (p_path != null) {
ohair@286 284 if (p_path.indexOf('?') != -1 && p_queryString != null) {
ohair@286 285 throw new MalformedURIException(
ohair@286 286 "Query string cannot be specified in path and query string!");
ohair@286 287 }
ohair@286 288
ohair@286 289 if (p_path.indexOf('#') != -1 && p_fragment != null) {
ohair@286 290 throw new MalformedURIException(
ohair@286 291 "Fragment cannot be specified in both the path and fragment!");
ohair@286 292 }
ohair@286 293 }
ohair@286 294
ohair@286 295 setScheme(p_scheme);
ohair@286 296 setHost(p_host);
ohair@286 297 setPort(p_port);
ohair@286 298 setUserinfo(p_userinfo);
ohair@286 299 setPath(p_path);
ohair@286 300 setQueryString(p_queryString);
ohair@286 301 setFragment(p_fragment);
ohair@286 302 }
ohair@286 303
ohair@286 304 /**
ohair@286 305 * Initialize all fields of this URI from another URI.
ohair@286 306 *
ohair@286 307 * @param p_other the URI to copy (cannot be null)
ohair@286 308 */
ohair@286 309 private void initialize(JaxmURI p_other) {
ohair@286 310 m_scheme = p_other.getScheme();
ohair@286 311 m_userinfo = p_other.getUserinfo();
ohair@286 312 m_host = p_other.getHost();
ohair@286 313 m_port = p_other.getPort();
ohair@286 314 m_path = p_other.getPath();
ohair@286 315 m_queryString = p_other.getQueryString();
ohair@286 316 m_fragment = p_other.getFragment();
ohair@286 317 }
ohair@286 318
ohair@286 319 /**
ohair@286 320 * Initializes this URI from a base URI and a URI specification string.
ohair@286 321 * See RFC 2396 Section 4 and Appendix B for specifications on parsing
ohair@286 322 * the URI and Section 5 for specifications on resolving relative URIs
ohair@286 323 * and relative paths.
ohair@286 324 *
ohair@286 325 * @param p_base the base URI (may be null if p_uriSpec is an absolute
ohair@286 326 * URI)
ohair@286 327 * @param p_uriSpec the URI spec string which may be an absolute or
ohair@286 328 * relative URI (can only be null/empty if p_base
ohair@286 329 * is not null)
ohair@286 330 *
ohair@286 331 * @exception MalformedURIException if p_base is null and p_uriSpec
ohair@286 332 * is not an absolute URI or if
ohair@286 333 * p_uriSpec violates syntax rules
ohair@286 334 */
ohair@286 335 private void initialize(JaxmURI p_base, String p_uriSpec)
ohair@286 336 throws MalformedURIException {
ohair@286 337 if (p_base == null &&
ohair@286 338 (p_uriSpec == null || p_uriSpec.trim().length() == 0)) {
ohair@286 339 throw new MalformedURIException(
ohair@286 340 "Cannot initialize URI with empty parameters.");
ohair@286 341 }
ohair@286 342
ohair@286 343 // just make a copy of the base if spec is empty
ohair@286 344 if (p_uriSpec == null || p_uriSpec.trim().length() == 0) {
ohair@286 345 initialize(p_base);
ohair@286 346 return;
ohair@286 347 }
ohair@286 348
ohair@286 349 String uriSpec = p_uriSpec.trim();
ohair@286 350 int uriSpecLen = uriSpec.length();
ohair@286 351 int index = 0;
ohair@286 352
ohair@286 353 // Check for scheme, which must be before `/'. Also handle names with
ohair@286 354 // DOS drive letters ('D:'), so 1-character schemes are not allowed.
ohair@286 355 int colonIdx = uriSpec.indexOf(':');
ohair@286 356 int slashIdx = uriSpec.indexOf('/');
ohair@286 357 if ((colonIdx < 2) || (colonIdx > slashIdx && slashIdx != -1)) {
ohair@286 358 int fragmentIdx = uriSpec.indexOf('#');
ohair@286 359 // A standalone base is a valid URI according to spec
ohair@286 360 if (p_base == null && fragmentIdx != 0 ) {
ohair@286 361 throw new MalformedURIException("No scheme found in URI.");
ohair@286 362 }
ohair@286 363 }
ohair@286 364 else {
ohair@286 365 initializeScheme(uriSpec);
ohair@286 366 index = m_scheme.length()+1;
ohair@286 367 }
ohair@286 368
ohair@286 369 // two slashes means generic URI syntax, so we get the authority
ohair@286 370 if (((index+1) < uriSpecLen) &&
ohair@286 371 (uriSpec.substring(index).startsWith("//"))) {
ohair@286 372 index += 2;
ohair@286 373 int startPos = index;
ohair@286 374
ohair@286 375 // get authority - everything up to path, query or fragment
ohair@286 376 char testChar = '\0';
ohair@286 377 while (index < uriSpecLen) {
ohair@286 378 testChar = uriSpec.charAt(index);
ohair@286 379 if (testChar == '/' || testChar == '?' || testChar == '#') {
ohair@286 380 break;
ohair@286 381 }
ohair@286 382 index++;
ohair@286 383 }
ohair@286 384
ohair@286 385 // if we found authority, parse it out, otherwise we set the
ohair@286 386 // host to empty string
ohair@286 387 if (index > startPos) {
ohair@286 388 initializeAuthority(uriSpec.substring(startPos, index));
ohair@286 389 }
ohair@286 390 else {
ohair@286 391 m_host = "";
ohair@286 392 }
ohair@286 393 }
ohair@286 394
ohair@286 395 initializePath(uriSpec.substring(index));
ohair@286 396
ohair@286 397 // Resolve relative URI to base URI - see RFC 2396 Section 5.2
ohair@286 398 // In some cases, it might make more sense to throw an exception
ohair@286 399 // (when scheme is specified is the string spec and the base URI
ohair@286 400 // is also specified, for example), but we're just following the
ohair@286 401 // RFC specifications
ohair@286 402 if (p_base != null) {
ohair@286 403
ohair@286 404 // check to see if this is the current doc - RFC 2396 5.2 #2
ohair@286 405 // note that this is slightly different from the RFC spec in that
ohair@286 406 // we don't include the check for query string being null
ohair@286 407 // - this handles cases where the urispec is just a query
ohair@286 408 // string or a fragment (e.g. "?y" or "#s") -
ohair@286 409 // see <http://www.ics.uci.edu/~fielding/url/test1.html> which
ohair@286 410 // identified this as a bug in the RFC
ohair@286 411 if (m_path.length() == 0 && m_scheme == null &&
ohair@286 412 m_host == null) {
ohair@286 413 m_scheme = p_base.getScheme();
ohair@286 414 m_userinfo = p_base.getUserinfo();
ohair@286 415 m_host = p_base.getHost();
ohair@286 416 m_port = p_base.getPort();
ohair@286 417 m_path = p_base.getPath();
ohair@286 418
ohair@286 419 if (m_queryString == null) {
ohair@286 420 m_queryString = p_base.getQueryString();
ohair@286 421 }
ohair@286 422 return;
ohair@286 423 }
ohair@286 424
ohair@286 425 // check for scheme - RFC 2396 5.2 #3
ohair@286 426 // if we found a scheme, it means absolute URI, so we're done
ohair@286 427 if (m_scheme == null) {
ohair@286 428 m_scheme = p_base.getScheme();
ohair@286 429 }
ohair@286 430 else {
ohair@286 431 return;
ohair@286 432 }
ohair@286 433
ohair@286 434 // check for authority - RFC 2396 5.2 #4
ohair@286 435 // if we found a host, then we've got a network path, so we're done
ohair@286 436 if (m_host == null) {
ohair@286 437 m_userinfo = p_base.getUserinfo();
ohair@286 438 m_host = p_base.getHost();
ohair@286 439 m_port = p_base.getPort();
ohair@286 440 }
ohair@286 441 else {
ohair@286 442 return;
ohair@286 443 }
ohair@286 444
ohair@286 445 // check for absolute path - RFC 2396 5.2 #5
ohair@286 446 if (m_path.length() > 0 &&
ohair@286 447 m_path.startsWith("/")) {
ohair@286 448 return;
ohair@286 449 }
ohair@286 450
ohair@286 451 // if we get to this point, we need to resolve relative path
ohair@286 452 // RFC 2396 5.2 #6
alanb@368 453 String path = "";
ohair@286 454 String basePath = p_base.getPath();
ohair@286 455
ohair@286 456 // 6a - get all but the last segment of the base URI path
ohair@286 457 if (basePath != null) {
ohair@286 458 int lastSlash = basePath.lastIndexOf('/');
ohair@286 459 if (lastSlash != -1) {
ohair@286 460 path = basePath.substring(0, lastSlash+1);
ohair@286 461 }
ohair@286 462 }
ohair@286 463
ohair@286 464 // 6b - append the relative URI path
ohair@286 465 path = path.concat(m_path);
ohair@286 466
ohair@286 467 // 6c - remove all "./" where "." is a complete path segment
ohair@286 468 index = -1;
ohair@286 469 while ((index = path.indexOf("/./")) != -1) {
ohair@286 470 path = path.substring(0, index+1).concat(path.substring(index+3));
ohair@286 471 }
ohair@286 472
ohair@286 473 // 6d - remove "." if path ends with "." as a complete path segment
ohair@286 474 if (path.endsWith("/.")) {
ohair@286 475 path = path.substring(0, path.length()-1);
ohair@286 476 }
ohair@286 477
ohair@286 478 // 6e - remove all "<segment>/../" where "<segment>" is a complete
ohair@286 479 // path segment not equal to ".."
ohair@286 480 index = 1;
ohair@286 481 int segIndex = -1;
ohair@286 482 String tempString = null;
ohair@286 483
ohair@286 484 while ((index = path.indexOf("/../", index)) > 0) {
ohair@286 485 tempString = path.substring(0, path.indexOf("/../"));
ohair@286 486 segIndex = tempString.lastIndexOf('/');
ohair@286 487 if (segIndex != -1) {
ohair@286 488 if (!tempString.substring(segIndex++).equals("..")) {
ohair@286 489 path = path.substring(0, segIndex).concat(path.substring(index+4));
ohair@286 490 }
ohair@286 491 else
ohair@286 492 index += 4;
ohair@286 493 }
ohair@286 494 else
ohair@286 495 index += 4;
ohair@286 496 }
ohair@286 497
ohair@286 498 // 6f - remove ending "<segment>/.." where "<segment>" is a
ohair@286 499 // complete path segment
ohair@286 500 if (path.endsWith("/..")) {
ohair@286 501 tempString = path.substring(0, path.length()-3);
ohair@286 502 segIndex = tempString.lastIndexOf('/');
ohair@286 503 if (segIndex != -1) {
ohair@286 504 path = path.substring(0, segIndex+1);
ohair@286 505 }
ohair@286 506 }
ohair@286 507 m_path = path;
ohair@286 508 }
ohair@286 509 }
ohair@286 510
ohair@286 511 /**
ohair@286 512 * Initialize the scheme for this URI from a URI string spec.
ohair@286 513 *
ohair@286 514 * @param p_uriSpec the URI specification (cannot be null)
ohair@286 515 *
ohair@286 516 * @exception MalformedURIException if URI does not have a conformant
ohair@286 517 * scheme
ohair@286 518 */
ohair@286 519 private void initializeScheme(String p_uriSpec)
ohair@286 520 throws MalformedURIException {
ohair@286 521 int uriSpecLen = p_uriSpec.length();
ohair@286 522 int index = 0;
ohair@286 523 String scheme = null;
ohair@286 524 char testChar = '\0';
ohair@286 525
ohair@286 526 while (index < uriSpecLen) {
ohair@286 527 testChar = p_uriSpec.charAt(index);
ohair@286 528 if (testChar == ':' || testChar == '/' ||
ohair@286 529 testChar == '?' || testChar == '#') {
ohair@286 530 break;
ohair@286 531 }
ohair@286 532 index++;
ohair@286 533 }
ohair@286 534 scheme = p_uriSpec.substring(0, index);
ohair@286 535
ohair@286 536 if (scheme.length() == 0) {
ohair@286 537 throw new MalformedURIException("No scheme found in URI.");
ohair@286 538 }
ohair@286 539 else {
ohair@286 540 setScheme(scheme);
ohair@286 541 }
ohair@286 542 }
ohair@286 543
ohair@286 544 /**
ohair@286 545 * Initialize the authority (userinfo, host and port) for this
ohair@286 546 * URI from a URI string spec.
ohair@286 547 *
ohair@286 548 * @param p_uriSpec the URI specification (cannot be null)
ohair@286 549 *
ohair@286 550 * @exception MalformedURIException if p_uriSpec violates syntax rules
ohair@286 551 */
ohair@286 552 private void initializeAuthority(String p_uriSpec)
ohair@286 553 throws MalformedURIException {
ohair@286 554 int index = 0;
ohair@286 555 int start = 0;
ohair@286 556 int end = p_uriSpec.length();
ohair@286 557 char testChar = '\0';
ohair@286 558 String userinfo = null;
ohair@286 559
ohair@286 560 // userinfo is everything up @
ohair@286 561 if (p_uriSpec.indexOf('@', start) != -1) {
ohair@286 562 while (index < end) {
ohair@286 563 testChar = p_uriSpec.charAt(index);
ohair@286 564 if (testChar == '@') {
ohair@286 565 break;
ohair@286 566 }
ohair@286 567 index++;
ohair@286 568 }
ohair@286 569 userinfo = p_uriSpec.substring(start, index);
ohair@286 570 index++;
ohair@286 571 }
ohair@286 572
ohair@286 573 // host is everything up to ':'
ohair@286 574 String host = null;
ohair@286 575 start = index;
ohair@286 576 while (index < end) {
ohair@286 577 testChar = p_uriSpec.charAt(index);
ohair@286 578 if (testChar == ':') {
ohair@286 579 break;
ohair@286 580 }
ohair@286 581 index++;
ohair@286 582 }
ohair@286 583 host = p_uriSpec.substring(start, index);
ohair@286 584 int port = -1;
ohair@286 585 if (host.length() > 0) {
ohair@286 586 // port
ohair@286 587 if (testChar == ':') {
ohair@286 588 index++;
ohair@286 589 start = index;
ohair@286 590 while (index < end) {
ohair@286 591 index++;
ohair@286 592 }
ohair@286 593 String portStr = p_uriSpec.substring(start, index);
ohair@286 594 if (portStr.length() > 0) {
ohair@286 595 for (int i = 0; i < portStr.length(); i++) {
ohair@286 596 if (!isDigit(portStr.charAt(i))) {
ohair@286 597 throw new MalformedURIException(
ohair@286 598 portStr +
ohair@286 599 " is invalid. Port should only contain digits!");
ohair@286 600 }
ohair@286 601 }
ohair@286 602 try {
ohair@286 603 port = Integer.parseInt(portStr);
ohair@286 604 }
ohair@286 605 catch (NumberFormatException nfe) {
ohair@286 606 // can't happen
ohair@286 607 }
ohair@286 608 }
ohair@286 609 }
ohair@286 610 }
ohair@286 611 setHost(host);
ohair@286 612 setPort(port);
ohair@286 613 setUserinfo(userinfo);
ohair@286 614 }
ohair@286 615
ohair@286 616 /**
ohair@286 617 * Initialize the path for this URI from a URI string spec.
ohair@286 618 *
ohair@286 619 * @param p_uriSpec the URI specification (cannot be null)
ohair@286 620 *
ohair@286 621 * @exception MalformedURIException if p_uriSpec violates syntax rules
ohair@286 622 */
ohair@286 623 private void initializePath(String p_uriSpec)
ohair@286 624 throws MalformedURIException {
ohair@286 625 if (p_uriSpec == null) {
ohair@286 626 throw new MalformedURIException(
ohair@286 627 "Cannot initialize path from null string!");
ohair@286 628 }
ohair@286 629
ohair@286 630 int index = 0;
ohair@286 631 int start = 0;
ohair@286 632 int end = p_uriSpec.length();
ohair@286 633 char testChar = '\0';
ohair@286 634
ohair@286 635 // path - everything up to query string or fragment
ohair@286 636 while (index < end) {
ohair@286 637 testChar = p_uriSpec.charAt(index);
ohair@286 638 if (testChar == '?' || testChar == '#') {
ohair@286 639 break;
ohair@286 640 }
ohair@286 641 // check for valid escape sequence
ohair@286 642 if (testChar == '%') {
ohair@286 643 if (index+2 >= end ||
ohair@286 644 !isHex(p_uriSpec.charAt(index+1)) ||
ohair@286 645 !isHex(p_uriSpec.charAt(index+2))) {
ohair@286 646 throw new MalformedURIException(
ohair@286 647 "Path contains invalid escape sequence!");
ohair@286 648 }
ohair@286 649 }
ohair@286 650 else if (!isReservedCharacter(testChar) &&
ohair@286 651 !isUnreservedCharacter(testChar)) {
ohair@286 652 throw new MalformedURIException(
ohair@286 653 "Path contains invalid character: " + testChar);
ohair@286 654 }
ohair@286 655 index++;
ohair@286 656 }
ohair@286 657 m_path = p_uriSpec.substring(start, index);
ohair@286 658
ohair@286 659 // query - starts with ? and up to fragment or end
ohair@286 660 if (testChar == '?') {
ohair@286 661 index++;
ohair@286 662 start = index;
ohair@286 663 while (index < end) {
ohair@286 664 testChar = p_uriSpec.charAt(index);
ohair@286 665 if (testChar == '#') {
ohair@286 666 break;
ohair@286 667 }
ohair@286 668 if (testChar == '%') {
ohair@286 669 if (index+2 >= end ||
ohair@286 670 !isHex(p_uriSpec.charAt(index+1)) ||
ohair@286 671 !isHex(p_uriSpec.charAt(index+2))) {
ohair@286 672 throw new MalformedURIException(
ohair@286 673 "Query string contains invalid escape sequence!");
ohair@286 674 }
ohair@286 675 }
ohair@286 676 else if (!isReservedCharacter(testChar) &&
ohair@286 677 !isUnreservedCharacter(testChar)) {
ohair@286 678 throw new MalformedURIException(
ohair@286 679 "Query string contains invalid character:" + testChar);
ohair@286 680 }
ohair@286 681 index++;
ohair@286 682 }
ohair@286 683 m_queryString = p_uriSpec.substring(start, index);
ohair@286 684 }
ohair@286 685
ohair@286 686 // fragment - starts with #
ohair@286 687 if (testChar == '#') {
ohair@286 688 index++;
ohair@286 689 start = index;
ohair@286 690 while (index < end) {
ohair@286 691 testChar = p_uriSpec.charAt(index);
ohair@286 692
ohair@286 693 if (testChar == '%') {
ohair@286 694 if (index+2 >= end ||
ohair@286 695 !isHex(p_uriSpec.charAt(index+1)) ||
ohair@286 696 !isHex(p_uriSpec.charAt(index+2))) {
ohair@286 697 throw new MalformedURIException(
ohair@286 698 "Fragment contains invalid escape sequence!");
ohair@286 699 }
ohair@286 700 }
ohair@286 701 else if (!isReservedCharacter(testChar) &&
ohair@286 702 !isUnreservedCharacter(testChar)) {
ohair@286 703 throw new MalformedURIException(
ohair@286 704 "Fragment contains invalid character:"+testChar);
ohair@286 705 }
ohair@286 706 index++;
ohair@286 707 }
ohair@286 708 m_fragment = p_uriSpec.substring(start, index);
ohair@286 709 }
ohair@286 710 }
ohair@286 711
ohair@286 712 /**
ohair@286 713 * Get the scheme for this URI.
ohair@286 714 *
ohair@286 715 * @return the scheme for this URI
ohair@286 716 */
ohair@286 717 public String getScheme() {
ohair@286 718 return m_scheme;
ohair@286 719 }
ohair@286 720
ohair@286 721 /**
ohair@286 722 * Get the scheme-specific part for this URI (everything following the
ohair@286 723 * scheme and the first colon). See RFC 2396 Section 5.2 for spec.
ohair@286 724 *
ohair@286 725 * @return the scheme-specific part for this URI
ohair@286 726 */
ohair@286 727 public String getSchemeSpecificPart() {
ohair@286 728 StringBuffer schemespec = new StringBuffer();
ohair@286 729
ohair@286 730 if (m_userinfo != null || m_host != null || m_port != -1) {
ohair@286 731 schemespec.append("//");
ohair@286 732 }
ohair@286 733
ohair@286 734 if (m_userinfo != null) {
ohair@286 735 schemespec.append(m_userinfo);
ohair@286 736 schemespec.append('@');
ohair@286 737 }
ohair@286 738
ohair@286 739 if (m_host != null) {
ohair@286 740 schemespec.append(m_host);
ohair@286 741 }
ohair@286 742
ohair@286 743 if (m_port != -1) {
ohair@286 744 schemespec.append(':');
ohair@286 745 schemespec.append(m_port);
ohair@286 746 }
ohair@286 747
ohair@286 748 if (m_path != null) {
ohair@286 749 schemespec.append((m_path));
ohair@286 750 }
ohair@286 751
ohair@286 752 if (m_queryString != null) {
ohair@286 753 schemespec.append('?');
ohair@286 754 schemespec.append(m_queryString);
ohair@286 755 }
ohair@286 756
ohair@286 757 if (m_fragment != null) {
ohair@286 758 schemespec.append('#');
ohair@286 759 schemespec.append(m_fragment);
ohair@286 760 }
ohair@286 761
ohair@286 762 return schemespec.toString();
ohair@286 763 }
ohair@286 764
ohair@286 765 /**
ohair@286 766 * Get the userinfo for this URI.
ohair@286 767 *
ohair@286 768 * @return the userinfo for this URI (null if not specified).
ohair@286 769 */
ohair@286 770 public String getUserinfo() {
ohair@286 771 return m_userinfo;
ohair@286 772 }
ohair@286 773
ohair@286 774 /**
ohair@286 775 * Get the host for this URI.
ohair@286 776 *
ohair@286 777 * @return the host for this URI (null if not specified).
ohair@286 778 */
ohair@286 779 public String getHost() {
ohair@286 780 return m_host;
ohair@286 781 }
ohair@286 782
ohair@286 783 /**
ohair@286 784 * Get the port for this URI.
ohair@286 785 *
ohair@286 786 * @return the port for this URI (-1 if not specified).
ohair@286 787 */
ohair@286 788 public int getPort() {
ohair@286 789 return m_port;
ohair@286 790 }
ohair@286 791
ohair@286 792 /**
ohair@286 793 * Get the path for this URI (optionally with the query string and
ohair@286 794 * fragment).
ohair@286 795 *
ohair@286 796 * @param p_includeQueryString if true (and query string is not null),
ohair@286 797 * then a "?" followed by the query string
ohair@286 798 * will be appended
ohair@286 799 * @param p_includeFragment if true (and fragment is not null),
ohair@286 800 * then a "#" followed by the fragment
ohair@286 801 * will be appended
ohair@286 802 *
ohair@286 803 * @return the path for this URI possibly including the query string
ohair@286 804 * and fragment
ohair@286 805 */
ohair@286 806 public String getPath(boolean p_includeQueryString,
ohair@286 807 boolean p_includeFragment) {
ohair@286 808 StringBuffer pathString = new StringBuffer(m_path);
ohair@286 809
ohair@286 810 if (p_includeQueryString && m_queryString != null) {
ohair@286 811 pathString.append('?');
ohair@286 812 pathString.append(m_queryString);
ohair@286 813 }
ohair@286 814
ohair@286 815 if (p_includeFragment && m_fragment != null) {
ohair@286 816 pathString.append('#');
ohair@286 817 pathString.append(m_fragment);
ohair@286 818 }
ohair@286 819 return pathString.toString();
ohair@286 820 }
ohair@286 821
ohair@286 822 /**
ohair@286 823 * Get the path for this URI. Note that the value returned is the path
ohair@286 824 * only and does not include the query string or fragment.
ohair@286 825 *
ohair@286 826 * @return the path for this URI.
ohair@286 827 */
ohair@286 828 public String getPath() {
ohair@286 829 return m_path;
ohair@286 830 }
ohair@286 831
ohair@286 832 /**
ohair@286 833 * Get the query string for this URI.
ohair@286 834 *
ohair@286 835 * @return the query string for this URI. Null is returned if there
ohair@286 836 * was no "?" in the URI spec, empty string if there was a
ohair@286 837 * "?" but no query string following it.
ohair@286 838 */
ohair@286 839 public String getQueryString() {
ohair@286 840 return m_queryString;
ohair@286 841 }
ohair@286 842
ohair@286 843 /**
ohair@286 844 * Get the fragment for this URI.
ohair@286 845 *
ohair@286 846 * @return the fragment for this URI. Null is returned if there
ohair@286 847 * was no "#" in the URI spec, empty string if there was a
ohair@286 848 * "#" but no fragment following it.
ohair@286 849 */
ohair@286 850 public String getFragment() {
ohair@286 851 return m_fragment;
ohair@286 852 }
ohair@286 853
ohair@286 854 /**
ohair@286 855 * Set the scheme for this URI. The scheme is converted to lowercase
ohair@286 856 * before it is set.
ohair@286 857 *
ohair@286 858 * @param p_scheme the scheme for this URI (cannot be null)
ohair@286 859 *
ohair@286 860 * @exception MalformedURIException if p_scheme is not a conformant
ohair@286 861 * scheme name
ohair@286 862 */
ohair@286 863 public void setScheme(String p_scheme) throws MalformedURIException {
ohair@286 864 if (p_scheme == null) {
ohair@286 865 throw new MalformedURIException(
ohair@286 866 "Cannot set scheme from null string!");
ohair@286 867 }
ohair@286 868 if (!isConformantSchemeName(p_scheme)) {
ohair@286 869 throw new MalformedURIException("The scheme is not conformant.");
ohair@286 870 }
ohair@286 871
ohair@286 872 m_scheme = p_scheme.toLowerCase();
ohair@286 873 }
ohair@286 874
ohair@286 875 /**
ohair@286 876 * Set the userinfo for this URI. If a non-null value is passed in and
ohair@286 877 * the host value is null, then an exception is thrown.
ohair@286 878 *
ohair@286 879 * @param p_userinfo the userinfo for this URI
ohair@286 880 *
ohair@286 881 * @exception MalformedURIException if p_userinfo contains invalid
ohair@286 882 * characters
ohair@286 883 */
ohair@286 884 public void setUserinfo(String p_userinfo) throws MalformedURIException {
ohair@286 885 if (p_userinfo == null) {
ohair@286 886 m_userinfo = null;
ohair@286 887 }
ohair@286 888 else {
ohair@286 889 if (m_host == null) {
ohair@286 890 throw new MalformedURIException(
ohair@286 891 "Userinfo cannot be set when host is null!");
ohair@286 892 }
ohair@286 893
ohair@286 894 // userinfo can contain alphanumerics, mark characters, escaped
ohair@286 895 // and ';',':','&','=','+','$',','
ohair@286 896 int index = 0;
ohair@286 897 int end = p_userinfo.length();
ohair@286 898 char testChar = '\0';
ohair@286 899 while (index < end) {
ohair@286 900 testChar = p_userinfo.charAt(index);
ohair@286 901 if (testChar == '%') {
ohair@286 902 if (index+2 >= end ||
ohair@286 903 !isHex(p_userinfo.charAt(index+1)) ||
ohair@286 904 !isHex(p_userinfo.charAt(index+2))) {
ohair@286 905 throw new MalformedURIException(
ohair@286 906 "Userinfo contains invalid escape sequence!");
ohair@286 907 }
ohair@286 908 }
ohair@286 909 else if (!isUnreservedCharacter(testChar) &&
ohair@286 910 USERINFO_CHARACTERS.indexOf(testChar) == -1) {
ohair@286 911 throw new MalformedURIException(
ohair@286 912 "Userinfo contains invalid character:"+testChar);
ohair@286 913 }
ohair@286 914 index++;
ohair@286 915 }
ohair@286 916 }
ohair@286 917 m_userinfo = p_userinfo;
ohair@286 918 }
ohair@286 919
ohair@286 920 /**
ohair@286 921 * Set the host for this URI. If null is passed in, the userinfo
ohair@286 922 * field is also set to null and the port is set to -1.
ohair@286 923 *
ohair@286 924 * @param p_host the host for this URI
ohair@286 925 *
ohair@286 926 * @exception MalformedURIException if p_host is not a valid IP
ohair@286 927 * address or DNS hostname.
ohair@286 928 */
ohair@286 929 public void setHost(String p_host) throws MalformedURIException {
ohair@286 930 if (p_host == null || p_host.trim().length() == 0) {
ohair@286 931 m_host = p_host;
ohair@286 932 m_userinfo = null;
ohair@286 933 m_port = -1;
ohair@286 934 }
ohair@286 935 else if (!isWellFormedAddress(p_host)) {
ohair@286 936 throw new MalformedURIException("Host is not a well formed address!");
ohair@286 937 }
ohair@286 938 m_host = p_host;
ohair@286 939 }
ohair@286 940
ohair@286 941 /**
ohair@286 942 * Set the port for this URI. -1 is used to indicate that the port is
ohair@286 943 * not specified, otherwise valid port numbers are between 0 and 65535.
ohair@286 944 * If a valid port number is passed in and the host field is null,
ohair@286 945 * an exception is thrown.
ohair@286 946 *
ohair@286 947 * @param p_port the port number for this URI
ohair@286 948 *
ohair@286 949 * @exception MalformedURIException if p_port is not -1 and not a
ohair@286 950 * valid port number
ohair@286 951 */
ohair@286 952 public void setPort(int p_port) throws MalformedURIException {
ohair@286 953 if (p_port >= 0 && p_port <= 65535) {
ohair@286 954 if (m_host == null) {
ohair@286 955 throw new MalformedURIException(
ohair@286 956 "Port cannot be set when host is null!");
ohair@286 957 }
ohair@286 958 }
ohair@286 959 else if (p_port != -1) {
ohair@286 960 throw new MalformedURIException("Invalid port number!");
ohair@286 961 }
ohair@286 962 m_port = p_port;
ohair@286 963 }
ohair@286 964
ohair@286 965 /**
ohair@286 966 * Set the path for this URI. If the supplied path is null, then the
ohair@286 967 * query string and fragment are set to null as well. If the supplied
ohair@286 968 * path includes a query string and/or fragment, these fields will be
ohair@286 969 * parsed and set as well. Note that, for URIs following the "generic
ohair@286 970 * URI" syntax, the path specified should start with a slash.
ohair@286 971 * For URIs that do not follow the generic URI syntax, this method
ohair@286 972 * sets the scheme-specific part.
ohair@286 973 *
ohair@286 974 * @param p_path the path for this URI (may be null)
ohair@286 975 *
ohair@286 976 * @exception MalformedURIException if p_path contains invalid
ohair@286 977 * characters
ohair@286 978 */
ohair@286 979 public void setPath(String p_path) throws MalformedURIException {
ohair@286 980 if (p_path == null) {
ohair@286 981 m_path = null;
ohair@286 982 m_queryString = null;
ohair@286 983 m_fragment = null;
ohair@286 984 }
ohair@286 985 else {
ohair@286 986 initializePath(p_path);
ohair@286 987 }
ohair@286 988 }
ohair@286 989
ohair@286 990 /**
ohair@286 991 * Append to the end of the path of this URI. If the current path does
ohair@286 992 * not end in a slash and the path to be appended does not begin with
ohair@286 993 * a slash, a slash will be appended to the current path before the
ohair@286 994 * new segment is added. Also, if the current path ends in a slash
ohair@286 995 * and the new segment begins with a slash, the extra slash will be
ohair@286 996 * removed before the new segment is appended.
ohair@286 997 *
ohair@286 998 * @param p_addToPath the new segment to be added to the current path
ohair@286 999 *
ohair@286 1000 * @exception MalformedURIException if p_addToPath contains syntax
ohair@286 1001 * errors
ohair@286 1002 */
ohair@286 1003 public void appendPath(String p_addToPath)
ohair@286 1004 throws MalformedURIException {
ohair@286 1005 if (p_addToPath == null || p_addToPath.trim().length() == 0) {
ohair@286 1006 return;
ohair@286 1007 }
ohair@286 1008
ohair@286 1009 if (!isURIString(p_addToPath)) {
ohair@286 1010 throw new MalformedURIException(
ohair@286 1011 "Path contains invalid character!");
ohair@286 1012 }
ohair@286 1013
ohair@286 1014 if (m_path == null || m_path.trim().length() == 0) {
ohair@286 1015 if (p_addToPath.startsWith("/")) {
ohair@286 1016 m_path = p_addToPath;
ohair@286 1017 }
ohair@286 1018 else {
ohair@286 1019 m_path = "/" + p_addToPath;
ohair@286 1020 }
ohair@286 1021 }
ohair@286 1022 else if (m_path.endsWith("/")) {
ohair@286 1023 if (p_addToPath.startsWith("/")) {
ohair@286 1024 m_path = m_path.concat(p_addToPath.substring(1));
ohair@286 1025 }
ohair@286 1026 else {
ohair@286 1027 m_path = m_path.concat(p_addToPath);
ohair@286 1028 }
ohair@286 1029 }
ohair@286 1030 else {
ohair@286 1031 if (p_addToPath.startsWith("/")) {
ohair@286 1032 m_path = m_path.concat(p_addToPath);
ohair@286 1033 }
ohair@286 1034 else {
ohair@286 1035 m_path = m_path.concat("/" + p_addToPath);
ohair@286 1036 }
ohair@286 1037 }
ohair@286 1038 }
ohair@286 1039
ohair@286 1040 /**
ohair@286 1041 * Set the query string for this URI. A non-null value is valid only
ohair@286 1042 * if this is an URI conforming to the generic URI syntax and
ohair@286 1043 * the path value is not null.
ohair@286 1044 *
ohair@286 1045 * @param p_queryString the query string for this URI
ohair@286 1046 *
ohair@286 1047 * @exception MalformedURIException if p_queryString is not null and this
ohair@286 1048 * URI does not conform to the generic
ohair@286 1049 * URI syntax or if the path is null
ohair@286 1050 */
ohair@286 1051 public void setQueryString(String p_queryString) throws MalformedURIException {
ohair@286 1052 if (p_queryString == null) {
ohair@286 1053 m_queryString = null;
ohair@286 1054 }
ohair@286 1055 else if (!isGenericURI()) {
ohair@286 1056 throw new MalformedURIException(
ohair@286 1057 "Query string can only be set for a generic URI!");
ohair@286 1058 }
ohair@286 1059 else if (getPath() == null) {
ohair@286 1060 throw new MalformedURIException(
ohair@286 1061 "Query string cannot be set when path is null!");
ohair@286 1062 }
ohair@286 1063 else if (!isURIString(p_queryString)) {
ohair@286 1064 throw new MalformedURIException(
ohair@286 1065 "Query string contains invalid character!");
ohair@286 1066 }
ohair@286 1067 else {
ohair@286 1068 m_queryString = p_queryString;
ohair@286 1069 }
ohair@286 1070 }
ohair@286 1071
ohair@286 1072 /**
ohair@286 1073 * Set the fragment for this URI. A non-null value is valid only
ohair@286 1074 * if this is a URI conforming to the generic URI syntax and
ohair@286 1075 * the path value is not null.
ohair@286 1076 *
ohair@286 1077 * @param p_fragment the fragment for this URI
ohair@286 1078 *
ohair@286 1079 * @exception MalformedURIException if p_fragment is not null and this
ohair@286 1080 * URI does not conform to the generic
ohair@286 1081 * URI syntax or if the path is null
ohair@286 1082 */
ohair@286 1083 public void setFragment(String p_fragment) throws MalformedURIException {
ohair@286 1084 if (p_fragment == null) {
ohair@286 1085 m_fragment = null;
ohair@286 1086 }
ohair@286 1087 else if (!isGenericURI()) {
ohair@286 1088 throw new MalformedURIException(
ohair@286 1089 "Fragment can only be set for a generic URI!");
ohair@286 1090 }
ohair@286 1091 else if (getPath() == null) {
ohair@286 1092 throw new MalformedURIException(
ohair@286 1093 "Fragment cannot be set when path is null!");
ohair@286 1094 }
ohair@286 1095 else if (!isURIString(p_fragment)) {
ohair@286 1096 throw new MalformedURIException(
ohair@286 1097 "Fragment contains invalid character!");
ohair@286 1098 }
ohair@286 1099 else {
ohair@286 1100 m_fragment = p_fragment;
ohair@286 1101 }
ohair@286 1102 }
ohair@286 1103
ohair@286 1104 /**
ohair@286 1105 * Determines if the passed-in Object is equivalent to this URI.
ohair@286 1106 *
ohair@286 1107 * @param p_test the Object to test for equality.
ohair@286 1108 *
ohair@286 1109 * @return true if p_test is a URI with all values equal to this
ohair@286 1110 * URI, false otherwise
ohair@286 1111 */
ohair@286 1112 public boolean equals(Object p_test) {
ohair@286 1113 if (p_test instanceof JaxmURI) {
ohair@286 1114 JaxmURI testURI = (JaxmURI) p_test;
ohair@286 1115 if (((m_scheme == null && testURI.m_scheme == null) ||
ohair@286 1116 (m_scheme != null && testURI.m_scheme != null &&
ohair@286 1117 m_scheme.equals(testURI.m_scheme))) &&
ohair@286 1118 ((m_userinfo == null && testURI.m_userinfo == null) ||
ohair@286 1119 (m_userinfo != null && testURI.m_userinfo != null &&
ohair@286 1120 m_userinfo.equals(testURI.m_userinfo))) &&
ohair@286 1121 ((m_host == null && testURI.m_host == null) ||
ohair@286 1122 (m_host != null && testURI.m_host != null &&
ohair@286 1123 m_host.equals(testURI.m_host))) &&
ohair@286 1124 m_port == testURI.m_port &&
ohair@286 1125 ((m_path == null && testURI.m_path == null) ||
ohair@286 1126 (m_path != null && testURI.m_path != null &&
ohair@286 1127 m_path.equals(testURI.m_path))) &&
ohair@286 1128 ((m_queryString == null && testURI.m_queryString == null) ||
ohair@286 1129 (m_queryString != null && testURI.m_queryString != null &&
ohair@286 1130 m_queryString.equals(testURI.m_queryString))) &&
ohair@286 1131 ((m_fragment == null && testURI.m_fragment == null) ||
ohair@286 1132 (m_fragment != null && testURI.m_fragment != null &&
ohair@286 1133 m_fragment.equals(testURI.m_fragment)))) {
ohair@286 1134 return true;
ohair@286 1135 }
ohair@286 1136 }
ohair@286 1137 return false;
ohair@286 1138 }
ohair@286 1139
alanb@368 1140 public int hashCode() {
alanb@368 1141 // No members safe to use, just default to a constant.
alanb@368 1142 return 153214;
alanb@368 1143 }
alanb@368 1144
ohair@286 1145 /**
ohair@286 1146 * Get the URI as a string specification. See RFC 2396 Section 5.2.
ohair@286 1147 *
ohair@286 1148 * @return the URI string specification
ohair@286 1149 */
ohair@286 1150 public String toString() {
ohair@286 1151 StringBuffer uriSpecString = new StringBuffer();
ohair@286 1152
ohair@286 1153 if (m_scheme != null) {
ohair@286 1154 uriSpecString.append(m_scheme);
ohair@286 1155 uriSpecString.append(':');
ohair@286 1156 }
ohair@286 1157 uriSpecString.append(getSchemeSpecificPart());
ohair@286 1158 return uriSpecString.toString();
ohair@286 1159 }
ohair@286 1160
ohair@286 1161 /**
ohair@286 1162 * Get the indicator as to whether this URI uses the "generic URI"
ohair@286 1163 * syntax.
ohair@286 1164 *
ohair@286 1165 * @return true if this URI uses the "generic URI" syntax, false
ohair@286 1166 * otherwise
ohair@286 1167 */
ohair@286 1168 public boolean isGenericURI() {
ohair@286 1169 // presence of the host (whether valid or empty) means
ohair@286 1170 // double-slashes which means generic uri
ohair@286 1171 return (m_host != null);
ohair@286 1172 }
ohair@286 1173
ohair@286 1174 /**
ohair@286 1175 * Determine whether a scheme conforms to the rules for a scheme name.
ohair@286 1176 * A scheme is conformant if it starts with an alphanumeric, and
ohair@286 1177 * contains only alphanumerics, '+','-' and '.'.
ohair@286 1178 *
ohair@286 1179 * @return true if the scheme is conformant, false otherwise
ohair@286 1180 */
ohair@286 1181 public static boolean isConformantSchemeName(String p_scheme) {
ohair@286 1182 if (p_scheme == null || p_scheme.trim().length() == 0) {
ohair@286 1183 return false;
ohair@286 1184 }
ohair@286 1185
ohair@286 1186 if (!isAlpha(p_scheme.charAt(0))) {
ohair@286 1187 return false;
ohair@286 1188 }
ohair@286 1189
ohair@286 1190 char testChar;
ohair@286 1191 for (int i = 1; i < p_scheme.length(); i++) {
ohair@286 1192 testChar = p_scheme.charAt(i);
ohair@286 1193 if (!isAlphanum(testChar) &&
ohair@286 1194 SCHEME_CHARACTERS.indexOf(testChar) == -1) {
ohair@286 1195 return false;
ohair@286 1196 }
ohair@286 1197 }
ohair@286 1198
ohair@286 1199 return true;
ohair@286 1200 }
ohair@286 1201
ohair@286 1202 /**
ohair@286 1203 * Determine whether a string is syntactically capable of representing
ohair@286 1204 * a valid IPv4 address or the domain name of a network host. A valid
ohair@286 1205 * IPv4 address consists of four decimal digit groups separated by a
ohair@286 1206 * '.'. A hostname consists of domain labels (each of which must
ohair@286 1207 * begin and end with an alphanumeric but may contain '-') separated
ohair@286 1208 & by a '.'. See RFC 2396 Section 3.2.2.
ohair@286 1209 *
ohair@286 1210 * @return true if the string is a syntactically valid IPv4 address
ohair@286 1211 * or hostname
ohair@286 1212 */
ohair@286 1213 public static boolean isWellFormedAddress(String p_address) {
ohair@286 1214 if (p_address == null) {
ohair@286 1215 return false;
ohair@286 1216 }
ohair@286 1217
ohair@286 1218 String address = p_address.trim();
ohair@286 1219 int addrLength = address.length();
ohair@286 1220 if (addrLength == 0 || addrLength > 255) {
ohair@286 1221 return false;
ohair@286 1222 }
ohair@286 1223
ohair@286 1224 if (address.startsWith(".") || address.startsWith("-")) {
ohair@286 1225 return false;
ohair@286 1226 }
ohair@286 1227
ohair@286 1228 // rightmost domain label starting with digit indicates IP address
ohair@286 1229 // since top level domain label can only start with an alpha
ohair@286 1230 // see RFC 2396 Section 3.2.2
ohair@286 1231 int index = address.lastIndexOf('.');
ohair@286 1232 if (address.endsWith(".")) {
ohair@286 1233 index = address.substring(0, index).lastIndexOf('.');
ohair@286 1234 }
ohair@286 1235
ohair@286 1236 if (index+1 < addrLength && isDigit(p_address.charAt(index+1))) {
ohair@286 1237 char testChar;
ohair@286 1238 int numDots = 0;
ohair@286 1239
ohair@286 1240 // make sure that 1) we see only digits and dot separators, 2) that
ohair@286 1241 // any dot separator is preceded and followed by a digit and
ohair@286 1242 // 3) that we find 3 dots
ohair@286 1243 for (int i = 0; i < addrLength; i++) {
ohair@286 1244 testChar = address.charAt(i);
ohair@286 1245 if (testChar == '.') {
ohair@286 1246 if (!isDigit(address.charAt(i-1)) ||
ohair@286 1247 (i+1 < addrLength && !isDigit(address.charAt(i+1)))) {
ohair@286 1248 return false;
ohair@286 1249 }
ohair@286 1250 numDots++;
ohair@286 1251 }
ohair@286 1252 else if (!isDigit(testChar)) {
ohair@286 1253 return false;
ohair@286 1254 }
ohair@286 1255 }
ohair@286 1256 if (numDots != 3) {
ohair@286 1257 return false;
ohair@286 1258 }
ohair@286 1259 }
ohair@286 1260 else {
ohair@286 1261 // domain labels can contain alphanumerics and '-"
ohair@286 1262 // but must start and end with an alphanumeric
ohair@286 1263 char testChar;
ohair@286 1264
ohair@286 1265 for (int i = 0; i < addrLength; i++) {
ohair@286 1266 testChar = address.charAt(i);
ohair@286 1267 if (testChar == '.') {
ohair@286 1268 if (!isAlphanum(address.charAt(i-1))) {
ohair@286 1269 return false;
ohair@286 1270 }
ohair@286 1271 if (i+1 < addrLength && !isAlphanum(address.charAt(i+1))) {
ohair@286 1272 return false;
ohair@286 1273 }
ohair@286 1274 }
ohair@286 1275 else if (!isAlphanum(testChar) && testChar != '-') {
ohair@286 1276 return false;
ohair@286 1277 }
ohair@286 1278 }
ohair@286 1279 }
ohair@286 1280 return true;
ohair@286 1281 }
ohair@286 1282
ohair@286 1283
ohair@286 1284 /**
ohair@286 1285 * Determine whether a char is a digit.
ohair@286 1286 *
ohair@286 1287 * @return true if the char is betweeen '0' and '9', false otherwise
ohair@286 1288 */
ohair@286 1289 private static boolean isDigit(char p_char) {
ohair@286 1290 return p_char >= '0' && p_char <= '9';
ohair@286 1291 }
ohair@286 1292
ohair@286 1293 /**
ohair@286 1294 * Determine whether a character is a hexadecimal character.
ohair@286 1295 *
ohair@286 1296 * @return true if the char is betweeen '0' and '9', 'a' and 'f'
ohair@286 1297 * or 'A' and 'F', false otherwise
ohair@286 1298 */
ohair@286 1299 private static boolean isHex(char p_char) {
ohair@286 1300 return (isDigit(p_char) ||
ohair@286 1301 (p_char >= 'a' && p_char <= 'f') ||
ohair@286 1302 (p_char >= 'A' && p_char <= 'F'));
ohair@286 1303 }
ohair@286 1304
ohair@286 1305 /**
ohair@286 1306 * Determine whether a char is an alphabetic character: a-z or A-Z
ohair@286 1307 *
ohair@286 1308 * @return true if the char is alphabetic, false otherwise
ohair@286 1309 */
ohair@286 1310 private static boolean isAlpha(char p_char) {
ohair@286 1311 return ((p_char >= 'a' && p_char <= 'z') ||
ohair@286 1312 (p_char >= 'A' && p_char <= 'Z' ));
ohair@286 1313 }
ohair@286 1314
ohair@286 1315 /**
ohair@286 1316 * Determine whether a char is an alphanumeric: 0-9, a-z or A-Z
ohair@286 1317 *
ohair@286 1318 * @return true if the char is alphanumeric, false otherwise
ohair@286 1319 */
ohair@286 1320 private static boolean isAlphanum(char p_char) {
ohair@286 1321 return (isAlpha(p_char) || isDigit(p_char));
ohair@286 1322 }
ohair@286 1323
ohair@286 1324 /**
ohair@286 1325 * Determine whether a character is a reserved character:
ohair@286 1326 * ';', '/', '?', ':', '@', '&', '=', '+', '$' or ','
ohair@286 1327 *
ohair@286 1328 * @return true if the string contains any reserved characters
ohair@286 1329 */
ohair@286 1330 private static boolean isReservedCharacter(char p_char) {
ohair@286 1331 return RESERVED_CHARACTERS.indexOf(p_char) != -1;
ohair@286 1332 }
ohair@286 1333
ohair@286 1334 /**
ohair@286 1335 * Determine whether a char is an unreserved character.
ohair@286 1336 *
ohair@286 1337 * @return true if the char is unreserved, false otherwise
ohair@286 1338 */
ohair@286 1339 private static boolean isUnreservedCharacter(char p_char) {
ohair@286 1340 return (isAlphanum(p_char) ||
ohair@286 1341 MARK_CHARACTERS.indexOf(p_char) != -1);
ohair@286 1342 }
ohair@286 1343
ohair@286 1344 /**
ohair@286 1345 * Determine whether a given string contains only URI characters (also
ohair@286 1346 * called "uric" in RFC 2396). uric consist of all reserved
ohair@286 1347 * characters, unreserved characters and escaped characters.
ohair@286 1348 *
ohair@286 1349 * @return true if the string is comprised of uric, false otherwise
ohair@286 1350 */
ohair@286 1351 private static boolean isURIString(String p_uric) {
ohair@286 1352 if (p_uric == null) {
ohair@286 1353 return false;
ohair@286 1354 }
ohair@286 1355 int end = p_uric.length();
ohair@286 1356 char testChar = '\0';
ohair@286 1357 for (int i = 0; i < end; i++) {
ohair@286 1358 testChar = p_uric.charAt(i);
ohair@286 1359 if (testChar == '%') {
ohair@286 1360 if (i+2 >= end ||
ohair@286 1361 !isHex(p_uric.charAt(i+1)) ||
ohair@286 1362 !isHex(p_uric.charAt(i+2))) {
ohair@286 1363 return false;
ohair@286 1364 }
ohair@286 1365 else {
ohair@286 1366 i += 2;
ohair@286 1367 continue;
ohair@286 1368 }
ohair@286 1369 }
ohair@286 1370 if (isReservedCharacter(testChar) ||
ohair@286 1371 isUnreservedCharacter(testChar)) {
ohair@286 1372 continue;
ohair@286 1373 }
ohair@286 1374 else {
ohair@286 1375 return false;
ohair@286 1376 }
ohair@286 1377 }
ohair@286 1378 return true;
ohair@286 1379 }
ohair@286 1380 }

mercurial