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