uri.java

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

JAVA
1,673
字号
        if (testChar == '%')        {          if (index + 2 >= end ||!isHex(p_uriSpec.charAt(index + 1))                  ||!isHex(p_uriSpec.charAt(index + 2)))          {            throw new MalformedURIException(              "Fragment contains invalid escape sequence!");          }        }        else if (!isReservedCharacter(testChar)                 &&!isUnreservedCharacter(testChar))        {          throw new MalformedURIException(            "Fragment contains invalid character:" + testChar);        }        index++;      }      m_fragment = p_uriSpec.substring(start, index);    }  }  /**   * Get the scheme for this URI.   *   * @return the scheme for this URI   */  public String getScheme()  {    return m_scheme;  }  /**   * Get the scheme-specific part for this URI (everything following the   * scheme and the first colon). See RFC 2396 Section 5.2 for spec.   *   * @return the scheme-specific part for this URI   */  public String getSchemeSpecificPart()  {    StringBuffer schemespec = new StringBuffer();    if (m_userinfo != null || m_host != null || m_port != -1)    {      schemespec.append("//");    }    if (m_userinfo != null)    {      schemespec.append(m_userinfo);      schemespec.append('@');    }    if (m_host != null)    {      schemespec.append(m_host);    }    if (m_port != -1)    {      schemespec.append(':');      schemespec.append(m_port);    }    if (m_path != null)    {      schemespec.append((m_path));    }    if (m_queryString != null)    {      schemespec.append('?');      schemespec.append(m_queryString);    }    if (m_fragment != null)    {      schemespec.append('#');      schemespec.append(m_fragment);    }    return schemespec.toString();  }  /**   * Get the userinfo for this URI.   *   * @return the userinfo for this URI (null if not specified).   */  public String getUserinfo()  {    return m_userinfo;  }  /**   * Get the host for this URI.   *   * @return the host for this URI (null if not specified).   */  public String getHost()  {    return m_host;  }  /**   * Get the port for this URI.   *   * @return the port for this URI (-1 if not specified).   */  public int getPort()  {    return m_port;  }  /**   * Get the path for this URI (optionally with the query string and   * fragment).   *   * @param p_includeQueryString if true (and query string is not null),   *                             then a "?" followed by the query string   *                             will be appended   * @param p_includeFragment if true (and fragment is not null),   *                             then a "#" followed by the fragment   *                             will be appended   *   * @return the path for this URI possibly including the query string   *         and fragment   */  public String getPath(boolean p_includeQueryString,                        boolean p_includeFragment)  {    StringBuffer pathString = new StringBuffer(m_path);    if (p_includeQueryString && m_queryString != null)    {      pathString.append('?');      pathString.append(m_queryString);    }    if (p_includeFragment && m_fragment != null)    {      pathString.append('#');      pathString.append(m_fragment);    }    return pathString.toString();  }  /**   * Get the path for this URI. Note that the value returned is the path   * only and does not include the query string or fragment.   *   * @return the path for this URI.   */  public String getPath()  {    return m_path;  }  /**   * Get the query string for this URI.   *   * @return the query string for this URI. Null is returned if there   *         was no "?" in the URI spec, empty string if there was a   *         "?" but no query string following it.   */  public String getQueryString()  {    return m_queryString;  }  /**   * Get the fragment for this URI.   *   * @return the fragment for this URI. Null is returned if there   *         was no "#" in the URI spec, empty string if there was a   *         "#" but no fragment following it.   */  public String getFragment()  {    return m_fragment;  }  /**   * Set the scheme for this URI. The scheme is converted to lowercase   * before it is set.   *   * @param p_scheme the scheme for this URI (cannot be null)   *   * @throws MalformedURIException if p_scheme is not a conformant   *                                  scheme name   */  public void setScheme(String p_scheme) throws MalformedURIException  {    if (p_scheme == null)    {      throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_SCHEME_FROM_NULL_STRING, null)); //"Cannot set scheme from null string!");    }    if (!isConformantSchemeName(p_scheme))    {      throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_SCHEME_NOT_CONFORMANT, null)); //"The scheme is not conformant.");    }    m_scheme = p_scheme.toLowerCase();  }  /**   * Set the userinfo for this URI. If a non-null value is passed in and   * the host value is null, then an exception is thrown.   *   * @param p_userinfo the userinfo for this URI   *   * @throws MalformedURIException if p_userinfo contains invalid   *                                  characters   */  public void setUserinfo(String p_userinfo) throws MalformedURIException  {    if (p_userinfo == null)    {      m_userinfo = null;    }    else    {      if (m_host == null)      {        throw new MalformedURIException(          "Userinfo cannot be set when host is null!");      }      // userinfo can contain alphanumerics, mark characters, escaped      // and ';',':','&','=','+','$',','      int index = 0;      int end = p_userinfo.length();      char testChar = '\0';      while (index < end)      {        testChar = p_userinfo.charAt(index);        if (testChar == '%')        {          if (index + 2 >= end ||!isHex(p_userinfo.charAt(index + 1))                  ||!isHex(p_userinfo.charAt(index + 2)))          {            throw new MalformedURIException(              "Userinfo contains invalid escape sequence!");          }        }        else if (!isUnreservedCharacter(testChar)                 && USERINFO_CHARACTERS.indexOf(testChar) == -1)        {          throw new MalformedURIException(            "Userinfo contains invalid character:" + testChar);        }        index++;      }    }    m_userinfo = p_userinfo;  }  /**   * Set the host for this URI. If null is passed in, the userinfo   * field is also set to null and the port is set to -1.   *   * @param p_host the host for this URI   *   * @throws MalformedURIException if p_host is not a valid IP   *                                  address or DNS hostname.   */  public void setHost(String p_host) throws MalformedURIException  {    if (p_host == null || p_host.trim().length() == 0)    {      m_host = p_host;      m_userinfo = null;      m_port = -1;    }    else if (!isWellFormedAddress(p_host))    {      throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_HOST_ADDRESS_NOT_WELLFORMED, null)); //"Host is not a well formed address!");    }    m_host = p_host;  }  /**   * Set the port for this URI. -1 is used to indicate that the port is   * not specified, otherwise valid port numbers are  between 0 and 65535.   * If a valid port number is passed in and the host field is null,   * an exception is thrown.   *   * @param p_port the port number for this URI   *   * @throws MalformedURIException if p_port is not -1 and not a   *                                  valid port number   */  public void setPort(int p_port) throws MalformedURIException  {    if (p_port >= 0 && p_port <= 65535)    {      if (m_host == null)      {        throw new MalformedURIException(          XMLMessages.createXMLMessage(XMLErrorResources.ER_PORT_WHEN_HOST_NULL, null)); //"Port cannot be set when host is null!");      }    }    else if (p_port != -1)    {      throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_INVALID_PORT, null)); //"Invalid port number!");    }    m_port = p_port;  }  /**   * Set the path for this URI. If the supplied path is null, then the   * query string and fragment are set to null as well. If the supplied   * path includes a query string and/or fragment, these fields will be   * parsed and set as well. Note that, for URIs following the "generic   * URI" syntax, the path specified should start with a slash.   * For URIs that do not follow the generic URI syntax, this method   * sets the scheme-specific part.   *   * @param p_path the path for this URI (may be null)   *   * @throws MalformedURIException if p_path contains invalid   *                                  characters   */  public void setPath(String p_path) throws MalformedURIException  {    if (p_path == null)    {      m_path = null;      m_queryString = null;      m_fragment = null;    }    else    {      initializePath(p_path);    }  }  /**   * Append to the end of the path of this URI. If the current path does   * not end in a slash and the path to be appended does not begin with   * a slash, a slash will be appended to the current path before the   * new segment is added. Also, if the current path ends in a slash   * and the new segment begins with a slash, the extra slash will be   * removed before the new segment is appended.   *   * @param p_addToPath the new segment to be added to the current path   *   * @throws MalformedURIException if p_addToPath contains syntax   *                                  errors   */  public void appendPath(String p_addToPath) throws MalformedURIException  {    if (p_addToPath == null || p_addToPath.trim().length() == 0)    {      return;    }    if (!isURIString(p_addToPath))    {      throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_PATH_INVALID_CHAR, new Object[]{p_addToPath})); //"Path contains invalid character!");    }    if (m_path == null || m_path.trim().length() == 0)    {      if (p_addToPath.startsWith("/"))      {        m_path = p_addToPath;      }      else      {        m_path = "/" + p_addToPath;      }    }    else if (m_path.endsWith("/"))    {      if (p_addToPath.startsWith("/"))      {        m_path = m_path.concat(p_addToPath.substring(1));      }      else      {        m_path = m_path.concat(p_addToPath);      }    }    else    {      if (p_addToPath.startsWith("/"))      {        m_path = m_path.concat(p_addToPath);      }      else      {        m_path = m_path.concat("/" + p_addToPath);      }    }  }  /**   * Set the query string for this URI. A non-null value is valid only   * if this is an URI conforming to the generic URI syntax and   * the path value is not null.

⌨️ 快捷键说明

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