1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
package ${package}.utils;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
public class Uris {
// Emulate Python's urlsplit.
public static class UriSplit {
String scheme;
String netloc;
String path;
String query;
String fragment;
public UriSplit(String scheme, String netloc, String path, String query, String fragment) {
this.scheme = scheme;
this.netloc = netloc;
this.path = path;
this.query = query;
this.fragment = fragment;
}
public String toString() {
return String.format("UriSplit[%s,%s,%s,%s,%s]", this.scheme, this.netloc, this.path, this.query,
this.fragment);
}
}
public static String fileUri(final String path) {
return fileUri(path, false);
}
public static String fileUri(final String path, final boolean splitFrag) {
if (path.equals("file://")) {
return path;
}
String frag;
String urlPath;
if (splitFrag) {
final String[] pathsp = path.split("#", 2);
// is quoting this?
urlPath = Uris.quote(pathsp[0]);
if (pathsp.length == 2) {
frag = "#" + Uris.quote(pathsp[1]);
} else {
frag = "";
urlPath = Uris.quote(path);
}
} else {
urlPath = Uris.quote(path);
frag = "";
}
if (urlPath.startsWith("//")) {
return "file:" + urlPath + frag;
} else {
return "file://" + urlPath + frag;
}
}
public static UriSplit split(final String uriString) {
try {
final URI uri = new URI(uriString);
return new Uris.UriSplit(uri.getScheme(), uri.getAuthority(), uri.getPath(), uri.getQuery(),
uri.getFragment());
} catch (URISyntaxException e) {
return new Uris.UriSplit(null, null, uriString, null, null);
}
}
public static String unsplit(final String scheme, final String netloc, final String path, final String query,
final String fragment) {
try {
return new URI(scheme, netloc, path, query, fragment).toString();
} catch (URISyntaxException e) {
if (scheme == null && path.startsWith("_:")) {
String uri = path;
if (fragment != null && fragment.length() > 0) {
uri += "#" + fragment;
}
return fragment;
}
throw new RuntimeException(e);
}
}
public static URI toUri(final String url) {
try {
return new URI(url);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
public static String quote(final String uri) {
try {
return java.net.URLDecoder.decode(uri, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String unquote(final String uri) {
try {
return java.net.URLEncoder.encode(uri, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String shortname(final String input_id) {
try {
final URI uri = new URI(input_id);
final String fragment = uri.getFragment();
if (fragment != null) {
String[] fragment_elements = fragment.split("/");
return fragment_elements[fragment_elements.length - 1];
} else {
String[] path_elements = uri.getPath().split("/");
return path_elements[path_elements.length - 1];
}
} catch (URISyntaxException e) {
return input_id;
}
}
}
|