uri.java

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

JAVA
1,914
字号
  	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() {    StringBuffer uriSpecString = new StringBuffer();    if (m_scheme != null) {      uriSpecString.append(m_scheme);      uriSpecString.append(':');    }    uriSpecString.append(getSchemeSpecificPart());    return uriSpecString.toString();  } /**  * Get the indicator as to whether this URI uses the "generic URI"  * syntax.  *  * @return true if this URI uses the "generic URI" syntax, false  *         otherwise  */  public boolean isGenericURI() {    // presence of the host (whether valid or empty) means    // double-slashes which means generic uri    return (m_host != null);  }    /**   * Returns whether this URI represents an absolute URI.   *   * @return true if this URI represents an absolute URI, false   *         otherwise   */  public boolean isAbsoluteURI() {      // presence of the scheme means absolute uri      return (m_scheme != null);  } /**  * Determine whether a scheme conforms to the rules for a scheme name.  * A scheme is conformant if it starts with an alphanumeric, and  * contains only alphanumerics, '+','-' and '.'.  *  * @return true if the scheme is conformant, false otherwise  */  public static boolean isConformantSchemeName(String p_scheme) {    if (p_scheme == null || p_scheme.trim().length() == 0) {      return false;    }    if (!isAlpha(p_scheme.charAt(0))) {      return false;    }    char testChar;    int schemeLength = p_scheme.length();    for (int i = 1; i < schemeLength; ++i) {      testChar = p_scheme.charAt(i);      if (!isSchemeCharacter(testChar)) {        return false;      }    }    return true;  } /**  * Determine whether a string is syntactically capable of representing  * a valid IPv4 address, IPv6 reference or the domain name of a network host.   * A valid IPv4 address consists of four decimal digit groups separated by a  * '.'. Each group must consist of one to three digits. See RFC 2732 Section 3,  * and RFC 2373 Section 2.2, for the definition of IPv6 references. A hostname   * consists of domain labels (each of which must begin and end with an alphanumeric   * but may contain '-') separated & by a '.'. See RFC 2396 Section 3.2.2.  *  * @return true if the string is a syntactically valid IPv4 address,   * IPv6 reference or hostname  */  public static boolean isWellFormedAddress(String address) {    if (address == null) {      return false;    }    int addrLength = address.length();    if (addrLength == 0) {      return false;    }        // Check if the host is a valid IPv6reference.    if (address.startsWith("[")) {      return isWellFormedIPv6Reference(address);    }    // Cannot start with a '.', '-', or end with a '-'.    if (address.startsWith(".") ||         address.startsWith("-") ||         address.endsWith("-")) {      return false;    }    // rightmost domain label starting with digit indicates IP address    // since top level domain label can only start with an alpha    // see RFC 2396 Section 3.2.2    int index = address.lastIndexOf('.');    if (address.endsWith(".")) {      index = address.substring(0, index).lastIndexOf('.');    }    if (index+1 < addrLength && isDigit(address.charAt(index+1))) {      return isWellFormedIPv4Address(address);    }    else {      // hostname      = *( domainlabel "." ) toplabel [ "." ]      // domainlabel   = alphanum | alphanum *( alphanum | "-" ) alphanum      // toplabel      = alpha | alpha *( alphanum | "-" ) alphanum            // RFC 2396 states that hostnames take the form described in       // RFC 1034 (Section 3) and RFC 1123 (Section 2.1). According      // to RFC 1034, hostnames are limited to 255 characters.      if (addrLength > 255) {      	return false;      }            // domain labels can contain alphanumerics and '-"      // but must start and end with an alphanumeric      char testChar;      int labelCharCount = 0;      for (int i = 0; i < addrLength; i++) {        testChar = address.charAt(i);        if (testChar == '.') {          if (!isAlphanum(address.charAt(i-1))) {            return false;          }          if (i+1 < addrLength && !isAlphanum(address.charAt(i+1))) {            return false;          }          labelCharCount = 0;        }        else if (!isAlphanum(testChar) && testChar != '-') {          return false;        }        // RFC 1034: Labels must be 63 characters or less.        else if (++labelCharCount > 63) {          return false;        }      }    }    return true;  }    /**   * <p>Determines whether a string is an IPv4 address as defined by    * RFC 2373, and under the further constraint that it must be a 32-bit   * address. Though not expressed in the grammar, in order to satisfy    * the 32-bit address constraint, each segment of the address cannot    * be greater than 255 (8 bits of information).</p>   *   * <p><code>IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT</code></p>   *   * @return true if the string is a syntactically valid IPv4 address   */  public static boolean isWellFormedIPv4Address(String address) {            int addrLength = address.length();      char testChar;      int numDots = 0;      int numDigits = 0;      // make sure that 1) we see only digits and dot separators, 2) that      // any dot separator is preceded and followed by a digit and      // 3) that we find 3 dots      //      // RFC 2732 amended RFC 2396 by replacing the definition       // of IPv4address with the one defined by RFC 2373. - mrglavas      //      // IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT      //      // One to three digits must be in each segment.      for (int i = 0; i < addrLength; i++) {        testChar = address.charAt(i);        if (testChar == '.') {          if ((i > 0 && !isDigit(address.charAt(i-1))) ||               (i+1 < addrLength && !isDigit(address.charAt(i+1)))) {            return false;          }          numDigits = 0;          if (++numDots > 3) {            return false;          }        }        else if (!isDigit(testChar)) {          return false;        }        // Check that that there are no more than three digits        // in this segment.        else if (++numDigits > 3) {          return false;        }        // Check that this segment is not greater than 255.        else if (numDigits == 3) {          char firs

⌨️ 快捷键说明

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