|
14 | 14 | import io.mola.galimatias.GalimatiasParseException; |
15 | 15 | import io.mola.galimatias.ParseIssue; |
16 | 16 | import io.mola.galimatias.URL; |
| 17 | +import io.mola.galimatias.canonicalize.DecodeUnreservedCanonicalizer; |
17 | 18 |
|
18 | 19 | //FIXME 2022 add unit tests |
19 | 20 | public final class URLUtils |
@@ -79,9 +80,9 @@ else if (urlA.equals(urlB)) |
79 | 80 | * in EPUB (to test for remote resources compared to container URLs). |
80 | 81 | * |
81 | 82 | * @param test |
82 | | - * the URL to test |
| 83 | + * the URL to test |
83 | 84 | * @param local |
84 | | - * the URL it is tested against |
| 85 | + * the URL it is tested against |
85 | 86 | * @return `true` if and only if `test` is remote compared to `local`. |
86 | 87 | */ |
87 | 88 | public static boolean isRemote(URL test, URL local) |
@@ -151,13 +152,33 @@ public static String decode(String string) |
151 | 152 | return percentDecode(string); |
152 | 153 | } |
153 | 154 |
|
| 155 | + public static URL normalize(URL url) |
| 156 | + { |
| 157 | + URL normalized = url; |
| 158 | + if (url != null) |
| 159 | + { |
| 160 | + try |
| 161 | + { |
| 162 | + if (url.isHierarchical() && url.path() != null) |
| 163 | + { |
| 164 | + normalized = url.withPath(URLUtils.encodePath(URLUtils.decode(url.path()))); |
| 165 | + } |
| 166 | + normalized = new DecodeUnreservedCanonicalizer().canonicalize(normalized); |
| 167 | + } catch (GalimatiasParseException unexpected) |
| 168 | + { |
| 169 | + throw new AssertionError(unexpected); |
| 170 | + } |
| 171 | + } |
| 172 | + return normalized; |
| 173 | + } |
| 174 | + |
154 | 175 | /** |
155 | 176 | * Returns the MIME type of a `data:` URL. |
156 | 177 | * |
157 | 178 | * @param url |
158 | | - * a URL, can be `null`. |
| 179 | + * a URL, can be `null`. |
159 | 180 | * @return the MIME type declared in the data URL (can be an empty string), or |
160 | | - * `null` if `url` is not a data URL. |
| 181 | + * `null` if `url` is not a data URL. |
161 | 182 | */ |
162 | 183 | public static String getDataURLType(URL url) |
163 | 184 | { |
|