uri.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,619 行 · 第 1/5 页

JAVA
1,619
字号
        }
    }

    /**
     * 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
     * @throws 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 (this.m_path == 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
     * @throws 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;
    }

    /**
     * Returns a hash-code value for this URI.  The hash code is based upon all of the URI's
     * components, and satisfies the general contract of the {@link Object#hashCode()
     * Object.hashCode} method. </p>
     *
     * @return A hash-code value for this URI
     */
    public int hashCode() {
        return toString().hashCode();
    }

    /**
     * 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);
    }

    /**
     * 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/>
     * <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 first = address.charAt(i - 2);
                char second = address.charAt(i - 1);
                if (!(first < '2' ||
                        (first == '2' &&
                                (second < '5' ||
                                        (second == '5' && testChar <= '5'))))) {
                    return false;
                }
            }
        }
        return (numDots == 3);
    }

    /**
     * <p>Determines whether a string is an IPv6 reference as defined by RFC 2732, where IPv6address
     * is defined in RFC 2373. The IPv6 address is parsed according to Section 2.2 of RFC 2373, with
     * the additional constraint that the address be composed of 128 bits of information.</p>
     * <p/>
     * <p><code>IPv6reference = "[" IPv6address "]"</code></p>
     * <p/>
     * <p>Note: The BNF expressed in RFC 2373 Appendix B does not accurately describe section 2.2, and
     * was in fact removed from RFC 3513, the successor of RFC 2373.</p>
     *
     * @return true if the string is a syntactically valid IPv6 reference
     */
    public static boolean isWellFormedIPv6Reference(String address) {

        int addrLength = address.length();
        int index = 1;
        int end = addrLength - 1;

        // Check if string is a potential match for IPv6reference.
        if (!(addrLength > 2 && address.charAt(0) == '['
                && address.charAt(end) == ']')) {
            return false;
        }

        // Counter for the number of 16-bit sections read in the address.
        int [] counter = new int[1];

        // Scan hex sequence before possible '::' or IPv4 address.
        index = scan

⌨️ 快捷键说明

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