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