Java URI Create createURI(String path)

Here you can find the source of createURI(String path)

Description

Creates a URI from a path, the path can be relative or absolute, '\' and '/' are normalised.

License

Open Source License

Parameter

Parameter Description
path the path to create the URI for.

Return

a new URI.

Declaration

public static URI createURI(String path) 

Method Source Code


//package com.java2s;

import java.io.UnsupportedEncodingException;
import java.net.URI;

public class Main {
    /**//from  w w  w .  j a v a2s . c  o  m
     * Creates a URI from a path, the path can be relative or absolute, '\' and '/' are normalised.
     *
     * @param path the path to create the URI for.
     * @return a new URI.
     */
    public static URI createURI(String path) {
        path = path.replace('\\', '/');
        return URI.create(encodeURI(path));
    }

    private static String encodeURI(String url) {
        StringBuffer uri = new StringBuffer(url.length());
        int length = url.length();
        for (int i = 0; i < length; i++) {
            char c = url.charAt(i);

            switch (c) {
            case '!':
            case '#':
            case '$':
            case '%':
            case '&':
            case '\'':
            case '(':
            case ')':
            case '*':
            case '+':
            case ',':
            case '-':
            case '.':
            case '/':
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case ':':
            case ';':
            case '=':
            case '?':
            case '@':
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F':
            case 'G':
            case 'H':
            case 'I':
            case 'J':
            case 'K':
            case 'L':
            case 'M':
            case 'N':
            case 'O':
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
            case 'T':
            case 'U':
            case 'V':
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
            case '[':
            case ']':
            case '_':
            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f':
            case 'g':
            case 'h':
            case 'i':
            case 'j':
            case 'k':
            case 'l':
            case 'm':
            case 'n':
            case 'o':
            case 'p':
            case 'q':
            case 'r':
            case 's':
            case 't':
            case 'u':
            case 'v':
            case 'w':
            case 'x':
            case 'y':
            case 'z':
            case '~':
                uri.append(c);
                break;
            default:
                StringBuffer result = new StringBuffer(3);
                String s = String.valueOf(c);
                try {
                    byte[] data = s.getBytes("UTF8");
                    for (int j = 0; j < data.length; j++) {
                        result.append('%');
                        String hex = Integer.toHexString(data[j]);
                        result.append(hex.substring(hex.length() - 2));
                    }
                    uri.append(result.toString());
                } catch (UnsupportedEncodingException ex) {
                    // should never happen
                }
            }
        }

        return uri.toString();
    }

    /**
     * Creates a String from a URI, the URI can be relative or absolute, the URI is decoded.
     *
     * TODO Why can't I just return uri.toString()???
     *
     * @param uri the URI to return the string representation for.
     * @return a string representation of the URI.
     */
    public static String toString(URI uri) {
        StringBuffer buffer = new StringBuffer();
        if (uri.getScheme() != null) {
            buffer.append(uri.getScheme());
            buffer.append(":");
        }
        buffer.append(uri.getSchemeSpecificPart());
        if (uri.getFragment() != null) {
            buffer.append("#");
            buffer.append(uri.getFragment());
        }
        return buffer.toString();
    }
}

Related

  1. createUri(String base)
  2. createURI(String extension)
  3. createURI(String filename)
  4. createURI(String hostname, int port)
  5. createUri(String name, Kind kind)
  6. createURI(String path)
  7. createURI(String scheme, String host, String path)
  8. createURI(String spec)
  9. createUri(String sUri, String defaultSchema, int deafultPort)