uri.java

来自「JAVA 所有包」· Java 代码 · 共 1,653 行 · 第 1/4 页

JAVA
1,653
字号
/* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: URI.java,v 1.1.4.1 2005/09/08 11:03:20 suresh_emailid Exp $ */package com.sun.org.apache.xml.internal.serializer.utils;import java.io.IOException;import java.io.Serializable;/** * A class to represent a Uniform Resource Identifier (URI). This class * is designed to handle the parsing of URIs and provide access to * the various components (scheme, host, port, userinfo, path, query * string and fragment) that may constitute a URI. * <p> * Parsing of a URI specification is done according to the URI * syntax described in RFC 2396 * <http://www.ietf.org/rfc/rfc2396.txt?number=2396>. Every URI consists * of a scheme, followed by a colon (':'), followed by a scheme-specific * part. For URIs that follow the "generic URI" syntax, the scheme- * specific part begins with two slashes ("//") and may be followed * by an authority segment (comprised of user information, host, and * port), path segment, query segment and fragment. Note that RFC 2396 * no longer specifies the use of the parameters segment and excludes * the "user:password" syntax as part of the authority segment. If * "user:password" appears in a URI, the entire user/password string * is stored as userinfo. * <p> * For URIs that do not follow the "generic URI" syntax (e.g. mailto), * the entire scheme-specific part is treated as the "path" portion * of the URI. * <p> * Note that, unlike the java.net.URL class, this class does not provide * any built-in network access functionality nor does it provide any * scheme-specific functionality (for example, it does not know a * default port for a specific scheme). Rather, it only knows the * grammar and basic set of operations that can be applied to a URI. * * This class is a copy of the one in com.sun.org.apache.xml.internal.utils.  * It exists to cut the serializers dependancy on that package. *  * A minor change from the original is that this class no longer implements * Serializable, and the serialVersionUID magic field is dropped, and * the class is no longer "public". *   * @xsl.usage internal */final class URI{  /**   * MalformedURIExceptions are thrown in the process of building a URI   * or setting fields on a URI when an operation would result in an   * invalid URI specification.   *   */  public static class MalformedURIException extends IOException  {    /**     * Constructs a <code>MalformedURIException</code> with no specified     * detail message.     */    public MalformedURIException()    {      super();    }    /**     * Constructs a <code>MalformedURIException</code> with the     * specified detail message.     *     * @param p_msg the detail message.     */    public MalformedURIException(String p_msg)    {      super(p_msg);    }  }  /** reserved characters */  private static final String RESERVED_CHARACTERS = ";/?:@&=+$,";  /**   * URI punctuation mark characters - these, combined with   *   alphanumerics, constitute the "unreserved" characters    */  private static final String MARK_CHARACTERS = "-_.!~*'() ";  /** scheme can be composed of alphanumerics and these characters */  private static final String SCHEME_CHARACTERS = "+-.";  /**   * userinfo can be composed of unreserved, escaped and these   *   characters    */  private static final String USERINFO_CHARACTERS = ";:&=+$,";  /** Stores the scheme (usually the protocol) for this URI.   *  @serial */  private String m_scheme = null;  /** If specified, stores the userinfo for this URI; otherwise null.   *  @serial */  private String m_userinfo = null;  /** If specified, stores the host for this URI; otherwise null.   *  @serial */  private String m_host = null;  /** If specified, stores the port for this URI; otherwise -1.   *  @serial */  private int m_port = -1;  /** If specified, stores the path for this URI; otherwise null.   *  @serial */  private String m_path = null;  /**   * If specified, stores the query string for this URI; otherwise   *   null.    * @serial    */  private String m_queryString = null;  /** If specified, stores the fragment for this URI; otherwise null.   *  @serial */  private String m_fragment = null;  /** Indicate whether in DEBUG mode          */  private static boolean DEBUG = false;  /**   * Construct a new and uninitialized URI.   */  public URI(){}  /**   * Construct a new URI from another URI. All fields for this URI are   * set equal to the fields of the URI passed in.   *   * @param p_other the URI to copy (cannot be null)   */  public URI(URI p_other)  {    initialize(p_other);  }  /**   * Construct a new URI from a URI specification string. If the   * specification follows the "generic URI" syntax, (two slashes   * following the first colon), the specification will be parsed   * accordingly - setting the scheme, userinfo, host,port, path, query   * string and fragment fields as necessary. If the specification does   * not follow the "generic URI" syntax, the specification is parsed   * into a scheme and scheme-specific part (stored as the path) only.   *   * @param p_uriSpec the URI specification string (cannot be null or   *                  empty)   *   * @throws MalformedURIException if p_uriSpec violates any syntax   *                                   rules   */  public URI(String p_uriSpec) throws MalformedURIException  {    this((URI) null, p_uriSpec);  }  /**   * Construct a new URI from a base URI and a URI specification string.   * The URI specification string may be a relative URI.   *   * @param p_base the base URI (cannot be null if p_uriSpec is null or   *               empty)   * @param p_uriSpec the URI specification string (cannot be null or   *                  empty if p_base is null)   *   * @throws MalformedURIException if p_uriSpec violates any syntax   *                                  rules   */  public URI(URI p_base, String p_uriSpec) throws MalformedURIException  {    initialize(p_base, p_uriSpec);  }  /**   * Construct a new URI that does not follow the generic URI syntax.   * Only the scheme and scheme-specific part (stored as the path) are   * initialized.   *   * @param p_scheme the URI scheme (cannot be null or empty)   * @param p_schemeSpecificPart the scheme-specific part (cannot be   *                             null or empty)   *   * @throws MalformedURIException if p_scheme violates any   *                                  syntax rules   */  public URI(String p_scheme, String p_schemeSpecificPart)          throws MalformedURIException  {    if (p_scheme == null || p_scheme.trim().length() == 0)    {      throw new MalformedURIException(        "Cannot construct URI with null/empty scheme!");    }    if (p_schemeSpecificPart == null            || p_schemeSpecificPart.trim().length() == 0)    {      throw new MalformedURIException(        "Cannot construct URI with null/empty scheme-specific part!");    }    setScheme(p_scheme);    setPath(p_schemeSpecificPart);  }  /**   * Construct a new URI that follows the generic URI syntax from its   * component parts. Each component is validated for syntax and some   * basic semantic checks are performed as well.  See the individual   * setter methods for specifics.   *   * @param p_scheme the URI scheme (cannot be null or empty)   * @param p_host the hostname or IPv4 address for the URI   * @param p_path the URI path - if the path contains '?' or '#',   *               then the query string and/or fragment will be   *               set from the path; however, if the query and   *               fragment are specified both in the path and as   *               separate parameters, an exception is thrown   * @param p_queryString the URI query string (cannot be specified   *                      if path is null)   * @param p_fragment the URI fragment (cannot be specified if path   *                   is null)   *   * @throws MalformedURIException if any of the parameters violates   *                                  syntax rules or semantic rules   */  public URI(String p_scheme, String p_host, String p_path, String p_queryString, String p_fragment)          throws MalformedURIException  {    this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment);  }  /**   * Construct a new URI that follows the generic URI syntax from its   * component parts. Each component is validated for syntax and some   * basic semantic checks are performed as well.  See the individual   * setter methods for specifics.   *   * @param p_scheme the URI scheme (cannot be null or empty)   * @param p_userinfo the URI userinfo (cannot be specified if host   *                   is null)   * @param p_host the hostname or IPv4 address for the URI   * @param p_port the URI port (may be -1 for "unspecified"; cannot   *               be specified if host is null)   * @param p_path the URI path - if the path contains '?' or '#',   *               then the query string and/or fragment will be   *               set from the path; however, if the query and   *               fragment are specified both in the path and as   *               separate parameters, an exception is thrown   * @param p_queryString the URI query string (cannot be specified   *                      if path is null)   * @param p_fragment the URI fragment (cannot be specified if path   *                   is null)   *   * @throws MalformedURIException if any of the parameters violates   *                                  syntax rules or semantic rules   */  public URI(String p_scheme, String p_userinfo, String p_host, int p_port, String p_path, String p_queryString, String p_fragment)          throws MalformedURIException  {    if (p_scheme == null || p_scheme.trim().length() == 0)    {      throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_SCHEME_REQUIRED, null)); //"Scheme is required!");    }    if (p_host == null)    {      if (p_userinfo != null)      {        throw new MalformedURIException(          Utils.messages.createMessage(MsgKey.ER_NO_USERINFO_IF_NO_HOST, null)); //"Userinfo may not be specified if host is not specified!");      }      if (p_port != -1)      {        throw new MalformedURIException(          Utils.messages.createMessage(MsgKey.ER_NO_PORT_IF_NO_HOST, null)); //"Port may not be specified if host is not specified!");      }    }    if (p_path != null)    {      if (p_path.indexOf('?') != -1 && p_queryString != null)      {        throw new MalformedURIException(          Utils.messages.createMessage(MsgKey.ER_NO_QUERY_STRING_IN_PATH, null)); //"Query string cannot be specified in path and query string!");      }      if (p_path.indexOf('#') != -1 && p_fragment != null)      {        throw new MalformedURIException(          Utils.messages.createMessage(MsgKey.ER_NO_FRAGMENT_STRING_IN_PATH, null)); //"Fragment cannot be specified in both the path and fragment!");      }    }    setScheme(p_scheme);    setHost(p_host);    setPort(p_port);    setUserinfo(p_userinfo);    setPath(p_path);    setQueryString(p_queryString);    setFragment(p_fragment);  }  /**   * Initialize all fields of this URI from another URI.   *   * @param p_other the URI to copy (cannot be null)   */  private void initialize(URI p_other)  {    m_scheme = p_other.getScheme();    m_userinfo = p_other.getUserinfo();    m_host = p_other.getHost();    m_port = p_other.getPort();    m_path = p_other.getPath();    m_queryString = p_other.getQueryString();    m_fragment = p_other.getFragment();  }  /**   * Initializes this URI from a base URI and a URI specification string.   * See RFC 2396 Section 4 and Appendix B for specifications on parsing   * the URI and Section 5 for specifications on resolving relative URIs   * and relative paths.   *   * @param p_base the base URI (may be null if p_uriSpec is an absolute   *               URI)   * @param p_uriSpec the URI spec string which may be an absolute or   *                  relative URI (can only be null/empty if p_base   *                  is not null)   *   * @throws MalformedURIException if p_base is null and p_uriSpec   *                                  is not an absolute URI or if   *                                  p_uriSpec violates syntax rules   */  private void initialize(URI p_base, String p_uriSpec)          throws MalformedURIException  {    if (p_base == null            && (p_uriSpec == null || p_uriSpec.trim().length() == 0))    {      throw new MalformedURIException(        Utils.messages.createMessage(MsgKey.ER_CANNOT_INIT_URI_EMPTY_PARMS, null)); //"Cannot initialize URI with empty parameters.");    }    // just make a copy of the base if spec is empty    if (p_uriSpec == null || p_uriSpec.trim().length() == 0)    {      initialize(p_base);      return;    }    String uriSpec = p_uriSpec.trim();    int uriSpecLen = uriSpec.length();    int index = 0;    // check for scheme    int colonIndex = uriSpec.indexOf(':');    if (colonIndex < 0)    {      if (p_base == null)      {        throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_NO_SCHEME_IN_URI, new Object[]{uriSpec})); //"No scheme found in URI: "+uriSpec);      }    }    else    {      initializeScheme(uriSpec);      uriSpec = uriSpec.substring(colonIndex+1);      uriSpecLen = uriSpec.length();    }    // two slashes means generic URI syntax, so we get the authority    if (((index + 1) < uriSpecLen)            && (uriSpec.substring(index).startsWith("//")))    {      index += 2;      int startPos = index;      // get authority - everything up to path, query or fragment      char testChar = '\0';

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?