aoqi@0: /*
aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.xml.internal.messaging.saaj.util;
aoqi@0:
aoqi@0: // Imported from: org.apache.xerces.util
aoqi@0: // Needed to work around differences in JDK1.2 and 1.3 and deal with userInfo
aoqi@0:
aoqi@0: import java.io.IOException;
aoqi@0: import java.io.Serializable;
aoqi@0:
aoqi@0:
aoqi@0: /**********************************************************************
aoqi@0: * A class to represent a Uniform Resource Identifier (URI). This class
aoqi@0: * is designed to handle the parsing of URIs and provide access to
aoqi@0: * the various components (scheme, host, port, userinfo, path, query
aoqi@0: * string and fragment) that may constitute a URI.
aoqi@0: *
aoqi@0: * Parsing of a URI specification is done according to the URI
aoqi@0: * syntax described in RFC 2396
aoqi@0: * . Every URI consists
aoqi@0: * of a scheme, followed by a colon (':'), followed by a scheme-specific
aoqi@0: * part. For URIs that follow the "generic URI" syntax, the scheme-
aoqi@0: * specific part begins with two slashes ("//") and may be followed
aoqi@0: * by an authority segment (comprised of user information, host, and
aoqi@0: * port), path segment, query segment and fragment. Note that RFC 2396
aoqi@0: * no longer specifies the use of the parameters segment and excludes
aoqi@0: * the "user:password" syntax as part of the authority segment. If
aoqi@0: * "user:password" appears in a URI, the entire user/password string
aoqi@0: * is stored as userinfo.
aoqi@0: *
aoqi@0: * For URIs that do not follow the "generic URI" syntax (e.g. mailto),
aoqi@0: * the entire scheme-specific part is treated as the "path" portion
aoqi@0: * of the URI.
aoqi@0: *
aoqi@0: * Note that, unlike the java.net.URL class, this class does not provide
aoqi@0: * any built-in network access functionality nor does it provide any
aoqi@0: * scheme-specific functionality (for example, it does not know a
aoqi@0: * default port for a specific scheme). Rather, it only knows the
aoqi@0: * grammar and basic set of operations that can be applied to a URI.
aoqi@0: *
aoqi@0: * @version
aoqi@0: *
aoqi@0: **********************************************************************/
aoqi@0: public class JaxmURI implements Serializable {
aoqi@0:
aoqi@0: /*******************************************************************
aoqi@0: * MalformedURIExceptions are thrown in the process of building a URI
aoqi@0: * or setting fields on a URI when an operation would result in an
aoqi@0: * invalid URI specification.
aoqi@0: *
aoqi@0: ********************************************************************/
aoqi@0: public static class MalformedURIException extends IOException {
aoqi@0:
aoqi@0: /******************************************************************
aoqi@0: * Constructs a MalformedURIException
with no specified
aoqi@0: * detail message.
aoqi@0: ******************************************************************/
aoqi@0: public MalformedURIException() {
aoqi@0: super();
aoqi@0: }
aoqi@0:
aoqi@0: /*****************************************************************
aoqi@0: * Constructs a MalformedURIException
with the
aoqi@0: * specified detail message.
aoqi@0: *
aoqi@0: * @param p_msg the detail message.
aoqi@0: ******************************************************************/
aoqi@0: public MalformedURIException(String p_msg) {
aoqi@0: super(p_msg);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** reserved characters */
aoqi@0: private static final String RESERVED_CHARACTERS = ";/?:@&=+$,";
aoqi@0:
aoqi@0: /** URI punctuation mark characters - these, combined with
aoqi@0: alphanumerics, constitute the "unreserved" characters */
aoqi@0: private static final String MARK_CHARACTERS = "-_.!~*'() ";
aoqi@0:
aoqi@0: /** scheme can be composed of alphanumerics and these characters */
aoqi@0: private static final String SCHEME_CHARACTERS = "+-.";
aoqi@0:
aoqi@0: /** userinfo can be composed of unreserved, escaped and these
aoqi@0: characters */
aoqi@0: private static final String USERINFO_CHARACTERS = ";:&=+$,";
aoqi@0:
aoqi@0: /** Stores the scheme (usually the protocol) for this URI. */
aoqi@0: private String m_scheme = null;
aoqi@0:
aoqi@0: /** If specified, stores the userinfo for this URI; otherwise null */
aoqi@0: private String m_userinfo = null;
aoqi@0:
aoqi@0: /** If specified, stores the host for this URI; otherwise null */
aoqi@0: private String m_host = null;
aoqi@0:
aoqi@0: /** If specified, stores the port for this URI; otherwise -1 */
aoqi@0: private int m_port = -1;
aoqi@0:
aoqi@0: /** If specified, stores the path for this URI; otherwise null */
aoqi@0: private String m_path = null;
aoqi@0:
aoqi@0: /** If specified, stores the query string for this URI; otherwise
aoqi@0: null. */
aoqi@0: private String m_queryString = null;
aoqi@0:
aoqi@0: /** If specified, stores the fragment for this URI; otherwise null */
aoqi@0: private String m_fragment = null;
aoqi@0:
aoqi@0: private static boolean DEBUG = false;
aoqi@0:
aoqi@0: /**
aoqi@0: * Construct a new and uninitialized URI.
aoqi@0: */
aoqi@0: public JaxmURI() {
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Construct a new URI from another URI. All fields for this URI are
aoqi@0: * set equal to the fields of the URI passed in.
aoqi@0: *
aoqi@0: * @param p_other the URI to copy (cannot be null)
aoqi@0: */
aoqi@0: public JaxmURI(JaxmURI p_other) {
aoqi@0: initialize(p_other);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Construct a new URI from a URI specification string. If the
aoqi@0: * specification follows the "generic URI" syntax, (two slashes
aoqi@0: * following the first colon), the specification will be parsed
aoqi@0: * accordingly - setting the scheme, userinfo, host,port, path, query
aoqi@0: * string and fragment fields as necessary. If the specification does
aoqi@0: * not follow the "generic URI" syntax, the specification is parsed
aoqi@0: * into a scheme and scheme-specific part (stored as the path) only.
aoqi@0: *
aoqi@0: * @param p_uriSpec the URI specification string (cannot be null or
aoqi@0: * empty)
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_uriSpec violates any syntax
aoqi@0: * rules
aoqi@0: */
aoqi@0: public JaxmURI(String p_uriSpec) throws MalformedURIException {
aoqi@0: this((JaxmURI)null, p_uriSpec);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Construct a new URI from a base URI and a URI specification string.
aoqi@0: * The URI specification string may be a relative URI.
aoqi@0: *
aoqi@0: * @param p_base the base URI (cannot be null if p_uriSpec is null or
aoqi@0: * empty)
aoqi@0: * @param p_uriSpec the URI specification string (cannot be null or
aoqi@0: * empty if p_base is null)
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_uriSpec violates any syntax
aoqi@0: * rules
aoqi@0: */
aoqi@0: public JaxmURI(JaxmURI p_base, String p_uriSpec) throws MalformedURIException {
aoqi@0: initialize(p_base, p_uriSpec);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Construct a new URI that does not follow the generic URI syntax.
aoqi@0: * Only the scheme and scheme-specific part (stored as the path) are
aoqi@0: * initialized.
aoqi@0: *
aoqi@0: * @param p_scheme the URI scheme (cannot be null or empty)
aoqi@0: * @param p_schemeSpecificPart the scheme-specific part (cannot be
aoqi@0: * null or empty)
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_scheme violates any
aoqi@0: * syntax rules
aoqi@0: */
aoqi@0: public JaxmURI(String p_scheme, String p_schemeSpecificPart)
aoqi@0: throws MalformedURIException {
aoqi@0: if (p_scheme == null || p_scheme.trim().length() == 0) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Cannot construct URI with null/empty scheme!");
aoqi@0: }
aoqi@0: if (p_schemeSpecificPart == null ||
aoqi@0: p_schemeSpecificPart.trim().length() == 0) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Cannot construct URI with null/empty scheme-specific part!");
aoqi@0: }
aoqi@0: setScheme(p_scheme);
aoqi@0: setPath(p_schemeSpecificPart);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Construct a new URI that follows the generic URI syntax from its
aoqi@0: * component parts. Each component is validated for syntax and some
aoqi@0: * basic semantic checks are performed as well. See the individual
aoqi@0: * setter methods for specifics.
aoqi@0: *
aoqi@0: * @param p_scheme the URI scheme (cannot be null or empty)
aoqi@0: * @param p_host the hostname or IPv4 address for the URI
aoqi@0: * @param p_path the URI path - if the path contains '?' or '#',
aoqi@0: * then the query string and/or fragment will be
aoqi@0: * set from the path; however, if the query and
aoqi@0: * fragment are specified both in the path and as
aoqi@0: * separate parameters, an exception is thrown
aoqi@0: * @param p_queryString the URI query string (cannot be specified
aoqi@0: * if path is null)
aoqi@0: * @param p_fragment the URI fragment (cannot be specified if path
aoqi@0: * is null)
aoqi@0: *
aoqi@0: * @exception MalformedURIException if any of the parameters violates
aoqi@0: * syntax rules or semantic rules
aoqi@0: */
aoqi@0: public JaxmURI(String p_scheme, String p_host, String p_path,
aoqi@0: String p_queryString, String p_fragment)
aoqi@0: throws MalformedURIException {
aoqi@0: this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Construct a new URI that follows the generic URI syntax from its
aoqi@0: * component parts. Each component is validated for syntax and some
aoqi@0: * basic semantic checks are performed as well. See the individual
aoqi@0: * setter methods for specifics.
aoqi@0: *
aoqi@0: * @param p_scheme the URI scheme (cannot be null or empty)
aoqi@0: * @param p_userinfo the URI userinfo (cannot be specified if host
aoqi@0: * is null)
aoqi@0: * @param p_host the hostname or IPv4 address for the URI
aoqi@0: * @param p_port the URI port (may be -1 for "unspecified"; cannot
aoqi@0: * be specified if host is null)
aoqi@0: * @param p_path the URI path - if the path contains '?' or '#',
aoqi@0: * then the query string and/or fragment will be
aoqi@0: * set from the path; however, if the query and
aoqi@0: * fragment are specified both in the path and as
aoqi@0: * separate parameters, an exception is thrown
aoqi@0: * @param p_queryString the URI query string (cannot be specified
aoqi@0: * if path is null)
aoqi@0: * @param p_fragment the URI fragment (cannot be specified if path
aoqi@0: * is null)
aoqi@0: *
aoqi@0: * @exception MalformedURIException if any of the parameters violates
aoqi@0: * syntax rules or semantic rules
aoqi@0: */
aoqi@0: public JaxmURI(String p_scheme, String p_userinfo,
aoqi@0: String p_host, int p_port, String p_path,
aoqi@0: String p_queryString, String p_fragment)
aoqi@0: throws MalformedURIException {
aoqi@0: if (p_scheme == null || p_scheme.trim().length() == 0) {
aoqi@0: throw new MalformedURIException("Scheme is required!");
aoqi@0: }
aoqi@0:
aoqi@0: if (p_host == null) {
aoqi@0: if (p_userinfo != null) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Userinfo may not be specified if host is not specified!");
aoqi@0: }
aoqi@0: if (p_port != -1) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Port may not be specified if host is not specified!");
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: if (p_path != null) {
aoqi@0: if (p_path.indexOf('?') != -1 && p_queryString != null) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Query string cannot be specified in path and query string!");
aoqi@0: }
aoqi@0:
aoqi@0: if (p_path.indexOf('#') != -1 && p_fragment != null) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Fragment cannot be specified in both the path and fragment!");
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: setScheme(p_scheme);
aoqi@0: setHost(p_host);
aoqi@0: setPort(p_port);
aoqi@0: setUserinfo(p_userinfo);
aoqi@0: setPath(p_path);
aoqi@0: setQueryString(p_queryString);
aoqi@0: setFragment(p_fragment);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Initialize all fields of this URI from another URI.
aoqi@0: *
aoqi@0: * @param p_other the URI to copy (cannot be null)
aoqi@0: */
aoqi@0: private void initialize(JaxmURI p_other) {
aoqi@0: m_scheme = p_other.getScheme();
aoqi@0: m_userinfo = p_other.getUserinfo();
aoqi@0: m_host = p_other.getHost();
aoqi@0: m_port = p_other.getPort();
aoqi@0: m_path = p_other.getPath();
aoqi@0: m_queryString = p_other.getQueryString();
aoqi@0: m_fragment = p_other.getFragment();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Initializes this URI from a base URI and a URI specification string.
aoqi@0: * See RFC 2396 Section 4 and Appendix B for specifications on parsing
aoqi@0: * the URI and Section 5 for specifications on resolving relative URIs
aoqi@0: * and relative paths.
aoqi@0: *
aoqi@0: * @param p_base the base URI (may be null if p_uriSpec is an absolute
aoqi@0: * URI)
aoqi@0: * @param p_uriSpec the URI spec string which may be an absolute or
aoqi@0: * relative URI (can only be null/empty if p_base
aoqi@0: * is not null)
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_base is null and p_uriSpec
aoqi@0: * is not an absolute URI or if
aoqi@0: * p_uriSpec violates syntax rules
aoqi@0: */
aoqi@0: private void initialize(JaxmURI p_base, String p_uriSpec)
aoqi@0: throws MalformedURIException {
aoqi@0: if (p_base == null &&
aoqi@0: (p_uriSpec == null || p_uriSpec.trim().length() == 0)) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Cannot initialize URI with empty parameters.");
aoqi@0: }
aoqi@0:
aoqi@0: // just make a copy of the base if spec is empty
aoqi@0: if (p_uriSpec == null || p_uriSpec.trim().length() == 0) {
aoqi@0: initialize(p_base);
aoqi@0: return;
aoqi@0: }
aoqi@0:
aoqi@0: String uriSpec = p_uriSpec.trim();
aoqi@0: int uriSpecLen = uriSpec.length();
aoqi@0: int index = 0;
aoqi@0:
aoqi@0: // Check for scheme, which must be before `/'. Also handle names with
aoqi@0: // DOS drive letters ('D:'), so 1-character schemes are not allowed.
aoqi@0: int colonIdx = uriSpec.indexOf(':');
aoqi@0: int slashIdx = uriSpec.indexOf('/');
aoqi@0: if ((colonIdx < 2) || (colonIdx > slashIdx && slashIdx != -1)) {
aoqi@0: int fragmentIdx = uriSpec.indexOf('#');
aoqi@0: // A standalone base is a valid URI according to spec
aoqi@0: if (p_base == null && fragmentIdx != 0 ) {
aoqi@0: throw new MalformedURIException("No scheme found in URI.");
aoqi@0: }
aoqi@0: }
aoqi@0: else {
aoqi@0: initializeScheme(uriSpec);
aoqi@0: index = m_scheme.length()+1;
aoqi@0: }
aoqi@0:
aoqi@0: // two slashes means generic URI syntax, so we get the authority
aoqi@0: if (((index+1) < uriSpecLen) &&
aoqi@0: (uriSpec.substring(index).startsWith("//"))) {
aoqi@0: index += 2;
aoqi@0: int startPos = index;
aoqi@0:
aoqi@0: // get authority - everything up to path, query or fragment
aoqi@0: char testChar = '\0';
aoqi@0: while (index < uriSpecLen) {
aoqi@0: testChar = uriSpec.charAt(index);
aoqi@0: if (testChar == '/' || testChar == '?' || testChar == '#') {
aoqi@0: break;
aoqi@0: }
aoqi@0: index++;
aoqi@0: }
aoqi@0:
aoqi@0: // if we found authority, parse it out, otherwise we set the
aoqi@0: // host to empty string
aoqi@0: if (index > startPos) {
aoqi@0: initializeAuthority(uriSpec.substring(startPos, index));
aoqi@0: }
aoqi@0: else {
aoqi@0: m_host = "";
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: initializePath(uriSpec.substring(index));
aoqi@0:
aoqi@0: // Resolve relative URI to base URI - see RFC 2396 Section 5.2
aoqi@0: // In some cases, it might make more sense to throw an exception
aoqi@0: // (when scheme is specified is the string spec and the base URI
aoqi@0: // is also specified, for example), but we're just following the
aoqi@0: // RFC specifications
aoqi@0: if (p_base != null) {
aoqi@0:
aoqi@0: // check to see if this is the current doc - RFC 2396 5.2 #2
aoqi@0: // note that this is slightly different from the RFC spec in that
aoqi@0: // we don't include the check for query string being null
aoqi@0: // - this handles cases where the urispec is just a query
aoqi@0: // string or a fragment (e.g. "?y" or "#s") -
aoqi@0: // see which
aoqi@0: // identified this as a bug in the RFC
aoqi@0: if (m_path.length() == 0 && m_scheme == null &&
aoqi@0: m_host == null) {
aoqi@0: m_scheme = p_base.getScheme();
aoqi@0: m_userinfo = p_base.getUserinfo();
aoqi@0: m_host = p_base.getHost();
aoqi@0: m_port = p_base.getPort();
aoqi@0: m_path = p_base.getPath();
aoqi@0:
aoqi@0: if (m_queryString == null) {
aoqi@0: m_queryString = p_base.getQueryString();
aoqi@0: }
aoqi@0: return;
aoqi@0: }
aoqi@0:
aoqi@0: // check for scheme - RFC 2396 5.2 #3
aoqi@0: // if we found a scheme, it means absolute URI, so we're done
aoqi@0: if (m_scheme == null) {
aoqi@0: m_scheme = p_base.getScheme();
aoqi@0: }
aoqi@0: else {
aoqi@0: return;
aoqi@0: }
aoqi@0:
aoqi@0: // check for authority - RFC 2396 5.2 #4
aoqi@0: // if we found a host, then we've got a network path, so we're done
aoqi@0: if (m_host == null) {
aoqi@0: m_userinfo = p_base.getUserinfo();
aoqi@0: m_host = p_base.getHost();
aoqi@0: m_port = p_base.getPort();
aoqi@0: }
aoqi@0: else {
aoqi@0: return;
aoqi@0: }
aoqi@0:
aoqi@0: // check for absolute path - RFC 2396 5.2 #5
aoqi@0: if (m_path.length() > 0 &&
aoqi@0: m_path.startsWith("/")) {
aoqi@0: return;
aoqi@0: }
aoqi@0:
aoqi@0: // if we get to this point, we need to resolve relative path
aoqi@0: // RFC 2396 5.2 #6
aoqi@0: String path = "";
aoqi@0: String basePath = p_base.getPath();
aoqi@0:
aoqi@0: // 6a - get all but the last segment of the base URI path
aoqi@0: if (basePath != null) {
aoqi@0: int lastSlash = basePath.lastIndexOf('/');
aoqi@0: if (lastSlash != -1) {
aoqi@0: path = basePath.substring(0, lastSlash+1);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: // 6b - append the relative URI path
aoqi@0: path = path.concat(m_path);
aoqi@0:
aoqi@0: // 6c - remove all "./" where "." is a complete path segment
aoqi@0: index = -1;
aoqi@0: while ((index = path.indexOf("/./")) != -1) {
aoqi@0: path = path.substring(0, index+1).concat(path.substring(index+3));
aoqi@0: }
aoqi@0:
aoqi@0: // 6d - remove "." if path ends with "." as a complete path segment
aoqi@0: if (path.endsWith("/.")) {
aoqi@0: path = path.substring(0, path.length()-1);
aoqi@0: }
aoqi@0:
aoqi@0: // 6e - remove all "/../" where "" is a complete
aoqi@0: // path segment not equal to ".."
aoqi@0: index = 1;
aoqi@0: int segIndex = -1;
aoqi@0: String tempString = null;
aoqi@0:
aoqi@0: while ((index = path.indexOf("/../", index)) > 0) {
aoqi@0: tempString = path.substring(0, path.indexOf("/../"));
aoqi@0: segIndex = tempString.lastIndexOf('/');
aoqi@0: if (segIndex != -1) {
aoqi@0: if (!tempString.substring(segIndex++).equals("..")) {
aoqi@0: path = path.substring(0, segIndex).concat(path.substring(index+4));
aoqi@0: }
aoqi@0: else
aoqi@0: index += 4;
aoqi@0: }
aoqi@0: else
aoqi@0: index += 4;
aoqi@0: }
aoqi@0:
aoqi@0: // 6f - remove ending "/.." where "" is a
aoqi@0: // complete path segment
aoqi@0: if (path.endsWith("/..")) {
aoqi@0: tempString = path.substring(0, path.length()-3);
aoqi@0: segIndex = tempString.lastIndexOf('/');
aoqi@0: if (segIndex != -1) {
aoqi@0: path = path.substring(0, segIndex+1);
aoqi@0: }
aoqi@0: }
aoqi@0: m_path = path;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Initialize the scheme for this URI from a URI string spec.
aoqi@0: *
aoqi@0: * @param p_uriSpec the URI specification (cannot be null)
aoqi@0: *
aoqi@0: * @exception MalformedURIException if URI does not have a conformant
aoqi@0: * scheme
aoqi@0: */
aoqi@0: private void initializeScheme(String p_uriSpec)
aoqi@0: throws MalformedURIException {
aoqi@0: int uriSpecLen = p_uriSpec.length();
aoqi@0: int index = 0;
aoqi@0: String scheme = null;
aoqi@0: char testChar = '\0';
aoqi@0:
aoqi@0: while (index < uriSpecLen) {
aoqi@0: testChar = p_uriSpec.charAt(index);
aoqi@0: if (testChar == ':' || testChar == '/' ||
aoqi@0: testChar == '?' || testChar == '#') {
aoqi@0: break;
aoqi@0: }
aoqi@0: index++;
aoqi@0: }
aoqi@0: scheme = p_uriSpec.substring(0, index);
aoqi@0:
aoqi@0: if (scheme.length() == 0) {
aoqi@0: throw new MalformedURIException("No scheme found in URI.");
aoqi@0: }
aoqi@0: else {
aoqi@0: setScheme(scheme);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Initialize the authority (userinfo, host and port) for this
aoqi@0: * URI from a URI string spec.
aoqi@0: *
aoqi@0: * @param p_uriSpec the URI specification (cannot be null)
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_uriSpec violates syntax rules
aoqi@0: */
aoqi@0: private void initializeAuthority(String p_uriSpec)
aoqi@0: throws MalformedURIException {
aoqi@0: int index = 0;
aoqi@0: int start = 0;
aoqi@0: int end = p_uriSpec.length();
aoqi@0: char testChar = '\0';
aoqi@0: String userinfo = null;
aoqi@0:
aoqi@0: // userinfo is everything up @
aoqi@0: if (p_uriSpec.indexOf('@', start) != -1) {
aoqi@0: while (index < end) {
aoqi@0: testChar = p_uriSpec.charAt(index);
aoqi@0: if (testChar == '@') {
aoqi@0: break;
aoqi@0: }
aoqi@0: index++;
aoqi@0: }
aoqi@0: userinfo = p_uriSpec.substring(start, index);
aoqi@0: index++;
aoqi@0: }
aoqi@0:
aoqi@0: // host is everything up to ':'
aoqi@0: String host = null;
aoqi@0: start = index;
aoqi@0: while (index < end) {
aoqi@0: testChar = p_uriSpec.charAt(index);
aoqi@0: if (testChar == ':') {
aoqi@0: break;
aoqi@0: }
aoqi@0: index++;
aoqi@0: }
aoqi@0: host = p_uriSpec.substring(start, index);
aoqi@0: int port = -1;
aoqi@0: if (host.length() > 0) {
aoqi@0: // port
aoqi@0: if (testChar == ':') {
aoqi@0: index++;
aoqi@0: start = index;
aoqi@0: while (index < end) {
aoqi@0: index++;
aoqi@0: }
aoqi@0: String portStr = p_uriSpec.substring(start, index);
aoqi@0: if (portStr.length() > 0) {
aoqi@0: for (int i = 0; i < portStr.length(); i++) {
aoqi@0: if (!isDigit(portStr.charAt(i))) {
aoqi@0: throw new MalformedURIException(
aoqi@0: portStr +
aoqi@0: " is invalid. Port should only contain digits!");
aoqi@0: }
aoqi@0: }
aoqi@0: try {
aoqi@0: port = Integer.parseInt(portStr);
aoqi@0: }
aoqi@0: catch (NumberFormatException nfe) {
aoqi@0: // can't happen
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0: setHost(host);
aoqi@0: setPort(port);
aoqi@0: setUserinfo(userinfo);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Initialize the path for this URI from a URI string spec.
aoqi@0: *
aoqi@0: * @param p_uriSpec the URI specification (cannot be null)
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_uriSpec violates syntax rules
aoqi@0: */
aoqi@0: private void initializePath(String p_uriSpec)
aoqi@0: throws MalformedURIException {
aoqi@0: if (p_uriSpec == null) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Cannot initialize path from null string!");
aoqi@0: }
aoqi@0:
aoqi@0: int index = 0;
aoqi@0: int start = 0;
aoqi@0: int end = p_uriSpec.length();
aoqi@0: char testChar = '\0';
aoqi@0:
aoqi@0: // path - everything up to query string or fragment
aoqi@0: while (index < end) {
aoqi@0: testChar = p_uriSpec.charAt(index);
aoqi@0: if (testChar == '?' || testChar == '#') {
aoqi@0: break;
aoqi@0: }
aoqi@0: // check for valid escape sequence
aoqi@0: if (testChar == '%') {
aoqi@0: if (index+2 >= end ||
aoqi@0: !isHex(p_uriSpec.charAt(index+1)) ||
aoqi@0: !isHex(p_uriSpec.charAt(index+2))) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Path contains invalid escape sequence!");
aoqi@0: }
aoqi@0: }
aoqi@0: else if (!isReservedCharacter(testChar) &&
aoqi@0: !isUnreservedCharacter(testChar)) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Path contains invalid character: " + testChar);
aoqi@0: }
aoqi@0: index++;
aoqi@0: }
aoqi@0: m_path = p_uriSpec.substring(start, index);
aoqi@0:
aoqi@0: // query - starts with ? and up to fragment or end
aoqi@0: if (testChar == '?') {
aoqi@0: index++;
aoqi@0: start = index;
aoqi@0: while (index < end) {
aoqi@0: testChar = p_uriSpec.charAt(index);
aoqi@0: if (testChar == '#') {
aoqi@0: break;
aoqi@0: }
aoqi@0: if (testChar == '%') {
aoqi@0: if (index+2 >= end ||
aoqi@0: !isHex(p_uriSpec.charAt(index+1)) ||
aoqi@0: !isHex(p_uriSpec.charAt(index+2))) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Query string contains invalid escape sequence!");
aoqi@0: }
aoqi@0: }
aoqi@0: else if (!isReservedCharacter(testChar) &&
aoqi@0: !isUnreservedCharacter(testChar)) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Query string contains invalid character:" + testChar);
aoqi@0: }
aoqi@0: index++;
aoqi@0: }
aoqi@0: m_queryString = p_uriSpec.substring(start, index);
aoqi@0: }
aoqi@0:
aoqi@0: // fragment - starts with #
aoqi@0: if (testChar == '#') {
aoqi@0: index++;
aoqi@0: start = index;
aoqi@0: while (index < end) {
aoqi@0: testChar = p_uriSpec.charAt(index);
aoqi@0:
aoqi@0: if (testChar == '%') {
aoqi@0: if (index+2 >= end ||
aoqi@0: !isHex(p_uriSpec.charAt(index+1)) ||
aoqi@0: !isHex(p_uriSpec.charAt(index+2))) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Fragment contains invalid escape sequence!");
aoqi@0: }
aoqi@0: }
aoqi@0: else if (!isReservedCharacter(testChar) &&
aoqi@0: !isUnreservedCharacter(testChar)) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Fragment contains invalid character:"+testChar);
aoqi@0: }
aoqi@0: index++;
aoqi@0: }
aoqi@0: m_fragment = p_uriSpec.substring(start, index);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the scheme for this URI.
aoqi@0: *
aoqi@0: * @return the scheme for this URI
aoqi@0: */
aoqi@0: public String getScheme() {
aoqi@0: return m_scheme;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the scheme-specific part for this URI (everything following the
aoqi@0: * scheme and the first colon). See RFC 2396 Section 5.2 for spec.
aoqi@0: *
aoqi@0: * @return the scheme-specific part for this URI
aoqi@0: */
aoqi@0: public String getSchemeSpecificPart() {
aoqi@0: StringBuffer schemespec = new StringBuffer();
aoqi@0:
aoqi@0: if (m_userinfo != null || m_host != null || m_port != -1) {
aoqi@0: schemespec.append("//");
aoqi@0: }
aoqi@0:
aoqi@0: if (m_userinfo != null) {
aoqi@0: schemespec.append(m_userinfo);
aoqi@0: schemespec.append('@');
aoqi@0: }
aoqi@0:
aoqi@0: if (m_host != null) {
aoqi@0: schemespec.append(m_host);
aoqi@0: }
aoqi@0:
aoqi@0: if (m_port != -1) {
aoqi@0: schemespec.append(':');
aoqi@0: schemespec.append(m_port);
aoqi@0: }
aoqi@0:
aoqi@0: if (m_path != null) {
aoqi@0: schemespec.append((m_path));
aoqi@0: }
aoqi@0:
aoqi@0: if (m_queryString != null) {
aoqi@0: schemespec.append('?');
aoqi@0: schemespec.append(m_queryString);
aoqi@0: }
aoqi@0:
aoqi@0: if (m_fragment != null) {
aoqi@0: schemespec.append('#');
aoqi@0: schemespec.append(m_fragment);
aoqi@0: }
aoqi@0:
aoqi@0: return schemespec.toString();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the userinfo for this URI.
aoqi@0: *
aoqi@0: * @return the userinfo for this URI (null if not specified).
aoqi@0: */
aoqi@0: public String getUserinfo() {
aoqi@0: return m_userinfo;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the host for this URI.
aoqi@0: *
aoqi@0: * @return the host for this URI (null if not specified).
aoqi@0: */
aoqi@0: public String getHost() {
aoqi@0: return m_host;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the port for this URI.
aoqi@0: *
aoqi@0: * @return the port for this URI (-1 if not specified).
aoqi@0: */
aoqi@0: public int getPort() {
aoqi@0: return m_port;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the path for this URI (optionally with the query string and
aoqi@0: * fragment).
aoqi@0: *
aoqi@0: * @param p_includeQueryString if true (and query string is not null),
aoqi@0: * then a "?" followed by the query string
aoqi@0: * will be appended
aoqi@0: * @param p_includeFragment if true (and fragment is not null),
aoqi@0: * then a "#" followed by the fragment
aoqi@0: * will be appended
aoqi@0: *
aoqi@0: * @return the path for this URI possibly including the query string
aoqi@0: * and fragment
aoqi@0: */
aoqi@0: public String getPath(boolean p_includeQueryString,
aoqi@0: boolean p_includeFragment) {
aoqi@0: StringBuffer pathString = new StringBuffer(m_path);
aoqi@0:
aoqi@0: if (p_includeQueryString && m_queryString != null) {
aoqi@0: pathString.append('?');
aoqi@0: pathString.append(m_queryString);
aoqi@0: }
aoqi@0:
aoqi@0: if (p_includeFragment && m_fragment != null) {
aoqi@0: pathString.append('#');
aoqi@0: pathString.append(m_fragment);
aoqi@0: }
aoqi@0: return pathString.toString();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the path for this URI. Note that the value returned is the path
aoqi@0: * only and does not include the query string or fragment.
aoqi@0: *
aoqi@0: * @return the path for this URI.
aoqi@0: */
aoqi@0: public String getPath() {
aoqi@0: return m_path;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the query string for this URI.
aoqi@0: *
aoqi@0: * @return the query string for this URI. Null is returned if there
aoqi@0: * was no "?" in the URI spec, empty string if there was a
aoqi@0: * "?" but no query string following it.
aoqi@0: */
aoqi@0: public String getQueryString() {
aoqi@0: return m_queryString;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the fragment for this URI.
aoqi@0: *
aoqi@0: * @return the fragment for this URI. Null is returned if there
aoqi@0: * was no "#" in the URI spec, empty string if there was a
aoqi@0: * "#" but no fragment following it.
aoqi@0: */
aoqi@0: public String getFragment() {
aoqi@0: return m_fragment;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the scheme for this URI. The scheme is converted to lowercase
aoqi@0: * before it is set.
aoqi@0: *
aoqi@0: * @param p_scheme the scheme for this URI (cannot be null)
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_scheme is not a conformant
aoqi@0: * scheme name
aoqi@0: */
aoqi@0: public void setScheme(String p_scheme) throws MalformedURIException {
aoqi@0: if (p_scheme == null) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Cannot set scheme from null string!");
aoqi@0: }
aoqi@0: if (!isConformantSchemeName(p_scheme)) {
aoqi@0: throw new MalformedURIException("The scheme is not conformant.");
aoqi@0: }
aoqi@0:
aoqi@0: m_scheme = p_scheme.toLowerCase();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the userinfo for this URI. If a non-null value is passed in and
aoqi@0: * the host value is null, then an exception is thrown.
aoqi@0: *
aoqi@0: * @param p_userinfo the userinfo for this URI
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_userinfo contains invalid
aoqi@0: * characters
aoqi@0: */
aoqi@0: public void setUserinfo(String p_userinfo) throws MalformedURIException {
aoqi@0: if (p_userinfo == null) {
aoqi@0: m_userinfo = null;
aoqi@0: }
aoqi@0: else {
aoqi@0: if (m_host == null) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Userinfo cannot be set when host is null!");
aoqi@0: }
aoqi@0:
aoqi@0: // userinfo can contain alphanumerics, mark characters, escaped
aoqi@0: // and ';',':','&','=','+','$',','
aoqi@0: int index = 0;
aoqi@0: int end = p_userinfo.length();
aoqi@0: char testChar = '\0';
aoqi@0: while (index < end) {
aoqi@0: testChar = p_userinfo.charAt(index);
aoqi@0: if (testChar == '%') {
aoqi@0: if (index+2 >= end ||
aoqi@0: !isHex(p_userinfo.charAt(index+1)) ||
aoqi@0: !isHex(p_userinfo.charAt(index+2))) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Userinfo contains invalid escape sequence!");
aoqi@0: }
aoqi@0: }
aoqi@0: else if (!isUnreservedCharacter(testChar) &&
aoqi@0: USERINFO_CHARACTERS.indexOf(testChar) == -1) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Userinfo contains invalid character:"+testChar);
aoqi@0: }
aoqi@0: index++;
aoqi@0: }
aoqi@0: }
aoqi@0: m_userinfo = p_userinfo;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the host for this URI. If null is passed in, the userinfo
aoqi@0: * field is also set to null and the port is set to -1.
aoqi@0: *
aoqi@0: * @param p_host the host for this URI
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_host is not a valid IP
aoqi@0: * address or DNS hostname.
aoqi@0: */
aoqi@0: public void setHost(String p_host) throws MalformedURIException {
aoqi@0: if (p_host == null || p_host.trim().length() == 0) {
aoqi@0: m_host = p_host;
aoqi@0: m_userinfo = null;
aoqi@0: m_port = -1;
aoqi@0: }
aoqi@0: else if (!isWellFormedAddress(p_host)) {
aoqi@0: throw new MalformedURIException("Host is not a well formed address!");
aoqi@0: }
aoqi@0: m_host = p_host;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the port for this URI. -1 is used to indicate that the port is
aoqi@0: * not specified, otherwise valid port numbers are between 0 and 65535.
aoqi@0: * If a valid port number is passed in and the host field is null,
aoqi@0: * an exception is thrown.
aoqi@0: *
aoqi@0: * @param p_port the port number for this URI
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_port is not -1 and not a
aoqi@0: * valid port number
aoqi@0: */
aoqi@0: public void setPort(int p_port) throws MalformedURIException {
aoqi@0: if (p_port >= 0 && p_port <= 65535) {
aoqi@0: if (m_host == null) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Port cannot be set when host is null!");
aoqi@0: }
aoqi@0: }
aoqi@0: else if (p_port != -1) {
aoqi@0: throw new MalformedURIException("Invalid port number!");
aoqi@0: }
aoqi@0: m_port = p_port;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the path for this URI. If the supplied path is null, then the
aoqi@0: * query string and fragment are set to null as well. If the supplied
aoqi@0: * path includes a query string and/or fragment, these fields will be
aoqi@0: * parsed and set as well. Note that, for URIs following the "generic
aoqi@0: * URI" syntax, the path specified should start with a slash.
aoqi@0: * For URIs that do not follow the generic URI syntax, this method
aoqi@0: * sets the scheme-specific part.
aoqi@0: *
aoqi@0: * @param p_path the path for this URI (may be null)
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_path contains invalid
aoqi@0: * characters
aoqi@0: */
aoqi@0: public void setPath(String p_path) throws MalformedURIException {
aoqi@0: if (p_path == null) {
aoqi@0: m_path = null;
aoqi@0: m_queryString = null;
aoqi@0: m_fragment = null;
aoqi@0: }
aoqi@0: else {
aoqi@0: initializePath(p_path);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Append to the end of the path of this URI. If the current path does
aoqi@0: * not end in a slash and the path to be appended does not begin with
aoqi@0: * a slash, a slash will be appended to the current path before the
aoqi@0: * new segment is added. Also, if the current path ends in a slash
aoqi@0: * and the new segment begins with a slash, the extra slash will be
aoqi@0: * removed before the new segment is appended.
aoqi@0: *
aoqi@0: * @param p_addToPath the new segment to be added to the current path
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_addToPath contains syntax
aoqi@0: * errors
aoqi@0: */
aoqi@0: public void appendPath(String p_addToPath)
aoqi@0: throws MalformedURIException {
aoqi@0: if (p_addToPath == null || p_addToPath.trim().length() == 0) {
aoqi@0: return;
aoqi@0: }
aoqi@0:
aoqi@0: if (!isURIString(p_addToPath)) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Path contains invalid character!");
aoqi@0: }
aoqi@0:
aoqi@0: if (m_path == null || m_path.trim().length() == 0) {
aoqi@0: if (p_addToPath.startsWith("/")) {
aoqi@0: m_path = p_addToPath;
aoqi@0: }
aoqi@0: else {
aoqi@0: m_path = "/" + p_addToPath;
aoqi@0: }
aoqi@0: }
aoqi@0: else if (m_path.endsWith("/")) {
aoqi@0: if (p_addToPath.startsWith("/")) {
aoqi@0: m_path = m_path.concat(p_addToPath.substring(1));
aoqi@0: }
aoqi@0: else {
aoqi@0: m_path = m_path.concat(p_addToPath);
aoqi@0: }
aoqi@0: }
aoqi@0: else {
aoqi@0: if (p_addToPath.startsWith("/")) {
aoqi@0: m_path = m_path.concat(p_addToPath);
aoqi@0: }
aoqi@0: else {
aoqi@0: m_path = m_path.concat("/" + p_addToPath);
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the query string for this URI. A non-null value is valid only
aoqi@0: * if this is an URI conforming to the generic URI syntax and
aoqi@0: * the path value is not null.
aoqi@0: *
aoqi@0: * @param p_queryString the query string for this URI
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_queryString is not null and this
aoqi@0: * URI does not conform to the generic
aoqi@0: * URI syntax or if the path is null
aoqi@0: */
aoqi@0: public void setQueryString(String p_queryString) throws MalformedURIException {
aoqi@0: if (p_queryString == null) {
aoqi@0: m_queryString = null;
aoqi@0: }
aoqi@0: else if (!isGenericURI()) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Query string can only be set for a generic URI!");
aoqi@0: }
aoqi@0: else if (getPath() == null) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Query string cannot be set when path is null!");
aoqi@0: }
aoqi@0: else if (!isURIString(p_queryString)) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Query string contains invalid character!");
aoqi@0: }
aoqi@0: else {
aoqi@0: m_queryString = p_queryString;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the fragment for this URI. A non-null value is valid only
aoqi@0: * if this is a URI conforming to the generic URI syntax and
aoqi@0: * the path value is not null.
aoqi@0: *
aoqi@0: * @param p_fragment the fragment for this URI
aoqi@0: *
aoqi@0: * @exception MalformedURIException if p_fragment is not null and this
aoqi@0: * URI does not conform to the generic
aoqi@0: * URI syntax or if the path is null
aoqi@0: */
aoqi@0: public void setFragment(String p_fragment) throws MalformedURIException {
aoqi@0: if (p_fragment == null) {
aoqi@0: m_fragment = null;
aoqi@0: }
aoqi@0: else if (!isGenericURI()) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Fragment can only be set for a generic URI!");
aoqi@0: }
aoqi@0: else if (getPath() == null) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Fragment cannot be set when path is null!");
aoqi@0: }
aoqi@0: else if (!isURIString(p_fragment)) {
aoqi@0: throw new MalformedURIException(
aoqi@0: "Fragment contains invalid character!");
aoqi@0: }
aoqi@0: else {
aoqi@0: m_fragment = p_fragment;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Determines if the passed-in Object is equivalent to this URI.
aoqi@0: *
aoqi@0: * @param p_test the Object to test for equality.
aoqi@0: *
aoqi@0: * @return true if p_test is a URI with all values equal to this
aoqi@0: * URI, false otherwise
aoqi@0: */
aoqi@0: public boolean equals(Object p_test) {
aoqi@0: if (p_test instanceof JaxmURI) {
aoqi@0: JaxmURI testURI = (JaxmURI) p_test;
aoqi@0: if (((m_scheme == null && testURI.m_scheme == null) ||
aoqi@0: (m_scheme != null && testURI.m_scheme != null &&
aoqi@0: m_scheme.equals(testURI.m_scheme))) &&
aoqi@0: ((m_userinfo == null && testURI.m_userinfo == null) ||
aoqi@0: (m_userinfo != null && testURI.m_userinfo != null &&
aoqi@0: m_userinfo.equals(testURI.m_userinfo))) &&
aoqi@0: ((m_host == null && testURI.m_host == null) ||
aoqi@0: (m_host != null && testURI.m_host != null &&
aoqi@0: m_host.equals(testURI.m_host))) &&
aoqi@0: m_port == testURI.m_port &&
aoqi@0: ((m_path == null && testURI.m_path == null) ||
aoqi@0: (m_path != null && testURI.m_path != null &&
aoqi@0: m_path.equals(testURI.m_path))) &&
aoqi@0: ((m_queryString == null && testURI.m_queryString == null) ||
aoqi@0: (m_queryString != null && testURI.m_queryString != null &&
aoqi@0: m_queryString.equals(testURI.m_queryString))) &&
aoqi@0: ((m_fragment == null && testURI.m_fragment == null) ||
aoqi@0: (m_fragment != null && testURI.m_fragment != null &&
aoqi@0: m_fragment.equals(testURI.m_fragment)))) {
aoqi@0: return true;
aoqi@0: }
aoqi@0: }
aoqi@0: return false;
aoqi@0: }
aoqi@0:
aoqi@0: public int hashCode() {
aoqi@0: // No members safe to use, just default to a constant.
aoqi@0: return 153214;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the URI as a string specification. See RFC 2396 Section 5.2.
aoqi@0: *
aoqi@0: * @return the URI string specification
aoqi@0: */
aoqi@0: public String toString() {
aoqi@0: StringBuffer uriSpecString = new StringBuffer();
aoqi@0:
aoqi@0: if (m_scheme != null) {
aoqi@0: uriSpecString.append(m_scheme);
aoqi@0: uriSpecString.append(':');
aoqi@0: }
aoqi@0: uriSpecString.append(getSchemeSpecificPart());
aoqi@0: return uriSpecString.toString();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the indicator as to whether this URI uses the "generic URI"
aoqi@0: * syntax.
aoqi@0: *
aoqi@0: * @return true if this URI uses the "generic URI" syntax, false
aoqi@0: * otherwise
aoqi@0: */
aoqi@0: public boolean isGenericURI() {
aoqi@0: // presence of the host (whether valid or empty) means
aoqi@0: // double-slashes which means generic uri
aoqi@0: return (m_host != null);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Determine whether a scheme conforms to the rules for a scheme name.
aoqi@0: * A scheme is conformant if it starts with an alphanumeric, and
aoqi@0: * contains only alphanumerics, '+','-' and '.'.
aoqi@0: *
aoqi@0: * @return true if the scheme is conformant, false otherwise
aoqi@0: */
aoqi@0: public static boolean isConformantSchemeName(String p_scheme) {
aoqi@0: if (p_scheme == null || p_scheme.trim().length() == 0) {
aoqi@0: return false;
aoqi@0: }
aoqi@0:
aoqi@0: if (!isAlpha(p_scheme.charAt(0))) {
aoqi@0: return false;
aoqi@0: }
aoqi@0:
aoqi@0: char testChar;
aoqi@0: for (int i = 1; i < p_scheme.length(); i++) {
aoqi@0: testChar = p_scheme.charAt(i);
aoqi@0: if (!isAlphanum(testChar) &&
aoqi@0: SCHEME_CHARACTERS.indexOf(testChar) == -1) {
aoqi@0: return false;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: return true;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Determine whether a string is syntactically capable of representing
aoqi@0: * a valid IPv4 address or the domain name of a network host. A valid
aoqi@0: * IPv4 address consists of four decimal digit groups separated by a
aoqi@0: * '.'. A hostname consists of domain labels (each of which must
aoqi@0: * begin and end with an alphanumeric but may contain '-') separated
aoqi@0: & by a '.'. See RFC 2396 Section 3.2.2.
aoqi@0: *
aoqi@0: * @return true if the string is a syntactically valid IPv4 address
aoqi@0: * or hostname
aoqi@0: */
aoqi@0: public static boolean isWellFormedAddress(String p_address) {
aoqi@0: if (p_address == null) {
aoqi@0: return false;
aoqi@0: }
aoqi@0:
aoqi@0: String address = p_address.trim();
aoqi@0: int addrLength = address.length();
aoqi@0: if (addrLength == 0 || addrLength > 255) {
aoqi@0: return false;
aoqi@0: }
aoqi@0:
aoqi@0: if (address.startsWith(".") || address.startsWith("-")) {
aoqi@0: return false;
aoqi@0: }
aoqi@0:
aoqi@0: // rightmost domain label starting with digit indicates IP address
aoqi@0: // since top level domain label can only start with an alpha
aoqi@0: // see RFC 2396 Section 3.2.2
aoqi@0: int index = address.lastIndexOf('.');
aoqi@0: if (address.endsWith(".")) {
aoqi@0: index = address.substring(0, index).lastIndexOf('.');
aoqi@0: }
aoqi@0:
aoqi@0: if (index+1 < addrLength && isDigit(p_address.charAt(index+1))) {
aoqi@0: char testChar;
aoqi@0: int numDots = 0;
aoqi@0:
aoqi@0: // make sure that 1) we see only digits and dot separators, 2) that
aoqi@0: // any dot separator is preceded and followed by a digit and
aoqi@0: // 3) that we find 3 dots
aoqi@0: for (int i = 0; i < addrLength; i++) {
aoqi@0: testChar = address.charAt(i);
aoqi@0: if (testChar == '.') {
aoqi@0: if (!isDigit(address.charAt(i-1)) ||
aoqi@0: (i+1 < addrLength && !isDigit(address.charAt(i+1)))) {
aoqi@0: return false;
aoqi@0: }
aoqi@0: numDots++;
aoqi@0: }
aoqi@0: else if (!isDigit(testChar)) {
aoqi@0: return false;
aoqi@0: }
aoqi@0: }
aoqi@0: if (numDots != 3) {
aoqi@0: return false;
aoqi@0: }
aoqi@0: }
aoqi@0: else {
aoqi@0: // domain labels can contain alphanumerics and '-"
aoqi@0: // but must start and end with an alphanumeric
aoqi@0: char testChar;
aoqi@0:
aoqi@0: for (int i = 0; i < addrLength; i++) {
aoqi@0: testChar = address.charAt(i);
aoqi@0: if (testChar == '.') {
aoqi@0: if (!isAlphanum(address.charAt(i-1))) {
aoqi@0: return false;
aoqi@0: }
aoqi@0: if (i+1 < addrLength && !isAlphanum(address.charAt(i+1))) {
aoqi@0: return false;
aoqi@0: }
aoqi@0: }
aoqi@0: else if (!isAlphanum(testChar) && testChar != '-') {
aoqi@0: return false;
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0: return true;
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0: /**
aoqi@0: * Determine whether a char is a digit.
aoqi@0: *
aoqi@0: * @return true if the char is betweeen '0' and '9', false otherwise
aoqi@0: */
aoqi@0: private static boolean isDigit(char p_char) {
aoqi@0: return p_char >= '0' && p_char <= '9';
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Determine whether a character is a hexadecimal character.
aoqi@0: *
aoqi@0: * @return true if the char is betweeen '0' and '9', 'a' and 'f'
aoqi@0: * or 'A' and 'F', false otherwise
aoqi@0: */
aoqi@0: private static boolean isHex(char p_char) {
aoqi@0: return (isDigit(p_char) ||
aoqi@0: (p_char >= 'a' && p_char <= 'f') ||
aoqi@0: (p_char >= 'A' && p_char <= 'F'));
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Determine whether a char is an alphabetic character: a-z or A-Z
aoqi@0: *
aoqi@0: * @return true if the char is alphabetic, false otherwise
aoqi@0: */
aoqi@0: private static boolean isAlpha(char p_char) {
aoqi@0: return ((p_char >= 'a' && p_char <= 'z') ||
aoqi@0: (p_char >= 'A' && p_char <= 'Z' ));
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Determine whether a char is an alphanumeric: 0-9, a-z or A-Z
aoqi@0: *
aoqi@0: * @return true if the char is alphanumeric, false otherwise
aoqi@0: */
aoqi@0: private static boolean isAlphanum(char p_char) {
aoqi@0: return (isAlpha(p_char) || isDigit(p_char));
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Determine whether a character is a reserved character:
aoqi@0: * ';', '/', '?', ':', '@', '&', '=', '+', '$' or ','
aoqi@0: *
aoqi@0: * @return true if the string contains any reserved characters
aoqi@0: */
aoqi@0: private static boolean isReservedCharacter(char p_char) {
aoqi@0: return RESERVED_CHARACTERS.indexOf(p_char) != -1;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Determine whether a char is an unreserved character.
aoqi@0: *
aoqi@0: * @return true if the char is unreserved, false otherwise
aoqi@0: */
aoqi@0: private static boolean isUnreservedCharacter(char p_char) {
aoqi@0: return (isAlphanum(p_char) ||
aoqi@0: MARK_CHARACTERS.indexOf(p_char) != -1);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Determine whether a given string contains only URI characters (also
aoqi@0: * called "uric" in RFC 2396). uric consist of all reserved
aoqi@0: * characters, unreserved characters and escaped characters.
aoqi@0: *
aoqi@0: * @return true if the string is comprised of uric, false otherwise
aoqi@0: */
aoqi@0: private static boolean isURIString(String p_uric) {
aoqi@0: if (p_uric == null) {
aoqi@0: return false;
aoqi@0: }
aoqi@0: int end = p_uric.length();
aoqi@0: char testChar = '\0';
aoqi@0: for (int i = 0; i < end; i++) {
aoqi@0: testChar = p_uric.charAt(i);
aoqi@0: if (testChar == '%') {
aoqi@0: if (i+2 >= end ||
aoqi@0: !isHex(p_uric.charAt(i+1)) ||
aoqi@0: !isHex(p_uric.charAt(i+2))) {
aoqi@0: return false;
aoqi@0: }
aoqi@0: else {
aoqi@0: i += 2;
aoqi@0: continue;
aoqi@0: }
aoqi@0: }
aoqi@0: if (isReservedCharacter(testChar) ||
aoqi@0: isUnreservedCharacter(testChar)) {
aoqi@0: continue;
aoqi@0: }
aoqi@0: else {
aoqi@0: return false;
aoqi@0: }
aoqi@0: }
aoqi@0: return true;
aoqi@0: }
aoqi@0: }