protected static URI encodeURI()

in ti/phase2/jars/core/src/java/org/apache/ti/core/urls/MutableURI.java [905:996]


    protected static URI encodeURI(String original) throws URISyntaxException {
        if (original == null) {
            throw new IllegalArgumentException("URI-Reference required");
        }

        String scheme = null;
        String authority = null;
        String path = null;
        String query = null;
        String fragment = null;
        String tmp = original.trim();
        int length = tmp.length();
        int from = 0;

        // The test flag whether the URI is started from the path component.
        boolean isStartedFromPath = false;
        int atColon = tmp.indexOf(':');
        int atSlash = tmp.indexOf('/');

        if (atColon < 0 || (atSlash >= 0 && atSlash < atColon)) {
            isStartedFromPath = true;
        }

        int at = indexFirstOf(tmp, isStartedFromPath ? "/?#" : ":/?#", from);

        if (at == -1) {
            at = 0;
        }

        // Parse the scheme.
        if (at < length && tmp.charAt(at) == ':') {
            scheme = tmp.substring(0, at).toLowerCase();
            from = ++at;
        }

        // Parse the authority component.
        if (0 <= at && at < length) {
            if (tmp.charAt(at) == '/') {
                if (at + 2 < length && tmp.charAt(at + 1) == '/') {
                    // the temporary index to start the search from
                    int next = indexFirstOf(tmp, "/?#", at + 2);
                    if (next == -1) {
                        next = (tmp.substring(at + 2).length() == 0) ? at + 2 : tmp.length();
                    }
                    authority = tmp.substring(at + 2, next);
                    from = at = next;
                }
            } else if (scheme != null && tmp.indexOf('/', at + 1) < 0) {
                int next = tmp.indexOf('#', at);
                if (next == -1) {
                    next = length;
                }
                String ssp = tmp.substring(at, next);
                if (next != length) {
                    fragment = tmp.substring(next + 1);
                }
                return new URI(scheme, ssp, fragment);
            }
        }

        // Parse the path component.
        if (from < length) {
            int next = indexFirstOf(tmp, "?#", from);
            if (next == -1) {
                next = length;
            }
            path = tmp.substring(from, next);
            at = next;
        }

        // Parse the query component.
        if (0 <= at && at + 1 < length && tmp.charAt(at) == '?') {
            int next = tmp.indexOf('#', at + 1);
            if (next == -1) {
                next = length;
            }
            query = tmp.substring(at + 1, next);
            at = next;
        }

        // Parse the fragment component.
        if (0 <= at && at + 1 <= length && tmp.charAt(at) == '#') {
            if (at + 1 == length) { // empty fragment
                fragment = "";
            } else {
                fragment = tmp.substring(at + 1);
            }
        }

        // Use java.net.URI to encode components and return.
        return new URI(scheme, authority, path, query, fragment);
    }