uri.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,924 行 · 第 1/5 页

JAVA
1,924
字号
  *                             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)  *  * @exception 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(                "Cannot set scheme from null string!");    }    if (!isConformantSchemeName(p_scheme)) {      throw new MalformedURIException("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  *  * @exception MalformedURIException if p_userinfo contains invalid  *                                  characters  */  public void setUserinfo(String p_userinfo) throws MalformedURIException {    if (p_userinfo == null) {      m_userinfo = null;      return;    }    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 (!isUserinfoCharacter(testChar)) {          throw new MalformedURIException(                  "Userinfo contains invalid character:"+testChar);        }        index++;      }    }    m_userinfo = p_userinfo;  } /**  * <p>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.</p>  *   * <p>Note: This method overwrites registry based authority if it  * previously existed in this URI.</p>  *  * @param p_host the host for this URI  *  * @exception 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.length() == 0) {      if (p_host != null) {        m_regAuthority = null;      }      m_host = p_host;      m_userinfo = null;      m_port = -1;      return;    }    else if (!isWellFormedAddress(p_host)) {      throw new MalformedURIException("Host is not a well formed address!");    }    m_host = p_host;    m_regAuthority = null;  } /**  * 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  *  * @exception 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(                      "Port cannot be set when host is null!");      }    }    else if (p_port != -1) {      throw new MalformedURIException("Invalid port number!");    }    m_port = p_port;  }    /**   * <p>Sets the registry based authority for this URI.</p>   *    * <p>Note: This method overwrites server based authority   * if it previously existed in this URI.</p>   *    * @param authority the registry based authority for this URI   *    * @exception MalformedURIException it authority is not a   * well formed registry based authority   */  public void setRegBasedAuthority(String authority)     throws MalformedURIException {  	if (authority == null) {  	  m_regAuthority = null;  	  return;  	}	// reg_name = 1*( unreserved | escaped | "$" | "," | 	//            ";" | ":" | "@" | "&" | "=" | "+" )  	else if (authority.length() < 1 ||  	  !isValidRegistryBasedAuthority(authority) ||  	  authority.indexOf('/') != -1) {      throw new MalformedURIException("Registry based authority is not well formed.");       	  	}  	m_regAuthority = authority;  	m_host = null;  	m_userinfo = null;  	m_port = -1;  } /**  * 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)  *  * @exception 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, 0);    }  } /**  * 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  *  * @exception 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(              "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.  *  * @param p_queryString the query string for this URI  *  * @exception MalformedURIException if p_queryString is not null and this  *                                  URI does not conform to the generic  *                                  URI syntax or if the path is null  */  public void setQueryString(String p_queryString) throws MalformedURIException {    if (p_queryString == null) {      m_queryString = null;    }    else if (!isGenericURI()) {      throw new MalformedURIException(              "Query string can only be set for a generic URI!");    }    else if (getPath() == null) {      throw new MalformedURIException(              "Query string cannot be set when path is null!");    }    else if (!isURIString(p_queryString)) {      throw new MalformedURIException(              "Query string contains invalid character!");    }    else {      m_queryString = p_queryString;    }  } /**  * Set the fragment for this URI. A non-null value is valid only  * if this is a URI conforming to the generic URI syntax and  * the path value is not null.  *  * @param p_fragment the fragment for this URI  *  * @exception MalformedURIException if p_fragment is not null and this  *                                  URI does not conform to the generic  *                                  URI syntax or if the path is null  */  public void setFragment(String p_fragment) throws MalformedURIException {    if (p_fragment == null) {      m_fragment = null;    }    else if (!isGenericURI()) {      throw new MalformedURIException(         "Fragment can only be set for a generic URI!");    }    else if (getPath() == null) {      throw new MalformedURIException(              "Fragment cannot be set when path is null!");    }    else if (!isURIString(p_fragment)) {      throw new MalformedURIException(              "Fragment contains invalid character!");    }    else {      m_fragment = p_fragment;    }  } /**  * Determines if the passed-in Object is equivalent to this URI.  *  * @param p_test the Object to test for equality.  *  * @return true if p_test is a URI with all values equal to this  *         URI, false otherwise  */  public boolean equals(Object p_test) {    if (p_test instanceof URI) {      URI testURI = (URI) p_test;      if (((m_scheme == null && testURI.m_scheme == null) ||           (m_scheme != null && testURI.m_scheme != null &&            m_scheme.equals(testURI.m_scheme))) &&          ((m_userinfo == null && testURI.m_userinfo == null) ||           (m_userinfo != null && testURI.m_userinfo != null &&            m_userinfo.equals(testURI.m_userinfo))) &&          ((m_host == null && testURI.m_host == null) ||           (m_host != null && testURI.m_host != null &&            m_host.equals(testURI.m_host))) &&            m_port == testURI.m_port &&          ((m_path == null && testURI.m_path == null) ||           (m_path != null && testURI.m_path != null &&            m_path.equals(testURI.m_path))) &&          ((m_queryString == null && testURI.m_queryString == null) ||           (m_queryString != null && testURI.m_queryString != null &&            m_queryString.equals(testURI.m_queryString))) &&          ((m_fragment == null && testURI.m_fragment == null) ||           (m_fragment != null && testURI.m_fragment != null &&            m_fragment.equals(testURI.m_fragment)))) {        return true;      }    }    return false;  } /**  * Get the URI as a string specification. See RFC 2396 Section 5.2.  *  * @return the URI string specification  */  public String toString() {

⌨️ 快捷键说明

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