Java URI from toURI(final String uriString)

Here you can find the source of toURI(final String uriString)

Description

Creates a URI from the input string.

License

Open Source License

Parameter

Parameter Description
uriString the uri to be created

Exception

Parameter Description
URISyntaxException an exception

Declaration

public static URI toURI(final String uriString) throws URISyntaxException 

Method Source Code


//package com.java2s;

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    private static final String URI_DECODE_RESERVED = ";/?:@&=+$,#";

    /**//from  w w w .j av a2 s .  c  o m
     * Creates a <code>URI</code> from the input string.
     * The preferred way to invoke this method is with an URL-encoded string.
     * <p>
     * However, if the string has not been encoded, this method will encode it.
     * It is ambiguous whether a string has been encoded or not, which is why
     * it is preferred to pass in the string pre-encoded.
     * <p>
     * This method is useful when manipulating a URI and you don't know if it is 
     * encoded or not.
     * <p>
     * @param uriString the uri to be created
     * @throws URISyntaxException
     */
    public static URI toURI(final String uriString) throws URISyntaxException {
        URI uri;
        try {
            uri = new URI(uriString);
        } catch (URISyntaxException e) {
            // the uriString was perhaps not encoded.
            // try to percent encode it.
            String encodedURIString = encodeUri(uriString);
            try {
                uri = new URI(encodedURIString);
            } catch (URISyntaxException e1) {
                // encoding the uriString didn't help.
                // this probably means there is something structurally
                // wrong with it.

                // NOTE: throwing the original exception.
                // initing with second Exception.  Not the normal
                // use case for initCause(), but this will at least capture both 
                // stack traces
                if (e.getCause() == null) {
                    e.initCause(e1);
                }
                throw e;
            }
        }
        return uri;
    }

    /**
     * Percent encodes <code>uri</code> leaving slashes and other reserved 
     * characters untouched. This is the same as the Javascript implementation
     * of encodURI.
     */
    public static String encodeUri(String uri) throws URISyntaxException {
        return encode(uri, true);
    }

    /**
     * Code taken from rhino-1.7R1/src/org/mozilla/javascript/NativeGlobal.java 
     * and slightly adapted. It is released under the MPL 1.1 and GPL 2.0.
     */
    private static String encode(String str, boolean fullUri) throws URISyntaxException {
        byte[] utf8buf = null;
        StringBuilder sb = null;

        for (int k = 0, length = str.length(); k != length; ++k) {
            char c = str.charAt(k);
            if (encodeUnescaped(c, fullUri)) {
                if (sb != null) {
                    sb.append(c);
                }
            } else {
                if (sb == null) {
                    sb = new StringBuilder(length + 3);
                    sb.append(str);
                    sb.setLength(k);
                    utf8buf = new byte[6];
                }
                if (0xDC00 <= c && c <= 0xDFFF) {
                    throw new URISyntaxException(str, c + " outside of valid range");
                }
                int value;
                if (c < 0xD800 || 0xDBFF < c) {
                    value = c;
                } else {
                    k++;
                    if (k == length) {
                        throw new URISyntaxException(str, "out of chars");
                    }
                    char c2 = str.charAt(k);
                    if (!(0xDC00 <= c2 && c2 <= 0xDFFF)) {
                        throw new URISyntaxException(str, "outside of valid range");
                    }
                    value = ((c - 0xD800) << 10) + (c2 - 0xDC00) + 0x10000;
                }
                int L = oneUcs4ToUtf8Char(utf8buf, value);
                assert utf8buf != null;
                for (int j = 0; j < L; j++) {
                    int d = 0xff & utf8buf[j];
                    sb.append('%');
                    sb.append(toHexChar(d >>> 4));
                    sb.append(toHexChar(d & 0xf));
                }
            }
        }
        return (sb == null) ? str : sb.toString();
    }

    private static boolean encodeUnescaped(char c, boolean fullUri) {
        if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9')) {
            return true;
        }
        if ("-_.!~*'()".indexOf(c) >= 0)
            return true;
        if (fullUri) {
            return URI_DECODE_RESERVED.indexOf(c) >= 0;
        }
        return false;
    }

    private static int oneUcs4ToUtf8Char(byte[] utf8Buffer, int ucs4Char) {
        int utf8Length = 1;

        //JS_ASSERT(ucs4Char <= 0x7FFFFFFF);
        if ((ucs4Char & ~0x7F) == 0)
            utf8Buffer[0] = (byte) ucs4Char;
        else {
            int i;
            int a = ucs4Char >>> 11;
            utf8Length = 2;
            while (a != 0) {
                a >>>= 5;
                utf8Length++;
            }
            i = utf8Length;
            while (--i > 0) {
                utf8Buffer[i] = (byte) ((ucs4Char & 0x3F) | 0x80);
                ucs4Char >>>= 6;
            }
            utf8Buffer[0] = (byte) (0x100 - (1 << (8 - utf8Length)) + ucs4Char);
        }
        return utf8Length;
    }

    private static char toHexChar(int i) {
        if (i >> 4 != 0)
            throw new RuntimeException();
        return (char) ((i < 10) ? i + '0' : i - 10 + 'a');
    }
}

Related

  1. toURI(File f)
  2. toUri(File folder)
  3. toURI(final java.io.File file, final StringBuilder builder)
  4. toURI(final String location)
  5. toURI(final String path)
  6. toUri(java.io.File file)
  7. toUri(String endpoint, Protocol protocol)
  8. toURI(String filePath)
  9. toUri(String ip, String path)