uri.java

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

JAVA
1,619
字号
        if (m_host != null || m_regAuthority != null) {
            schemespec.append("//");

            // Server based authority.
            if (m_host != null) {

                if (m_userinfo != null) {
                    schemespec.append(m_userinfo);
                    schemespec.append('@');
                }

                schemespec.append(m_host);

                if (m_port != -1) {
                    schemespec.append(':');
                    schemespec.append(m_port);
                }
            }
            // Registry based authority.
            else {
                schemespec.append(m_regAuthority);
            }
        }

        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 registry based authority for this URI.
     *
     * @return the registry based authority (null if not specified).
     */
    public String getRegBasedAuthority() {
        return m_regAuthority;
    }

    /**
     * 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(
                    "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
     * @throws 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/>
     * <p>Note: This method overwrites registry based authority if it previously existed in this
     * URI.</p>
     *
     * @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.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
     * @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(
                        "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/>
     * <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
     * @throws 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)
     * @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, 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
     * @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(
                    "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);
            }

⌨️ 快捷键说明

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