Converts a URI to a directory path. See https://docs.basex.org/wiki/Repository#URI_Rewriting for details. @param uri namespace URI @return converted path
(final String uri)
| 353 | * @return converted path |
| 354 | */ |
| 355 | public static String uri2path(final String uri) { |
| 356 | String path = uri; |
| 357 | try { |
| 358 | final URI u = new URI(uri); |
| 359 | final TokenBuilder tb = new TokenBuilder(); |
| 360 | if(u.isOpaque()) { |
| 361 | tb.add(u.getScheme()).add('/').add(u.getSchemeSpecificPart().replace(':', '/')); |
| 362 | } else { |
| 363 | final String auth = u.getAuthority(); |
| 364 | if(auth != null) { |
| 365 | // reverse authority, replace dots by slashes. example: basex.org → org/basex |
| 366 | final String[] comp = split(auth, '.'); |
| 367 | for(int c = comp.length - 1; c >= 0; c--) tb.add('/').add(comp[c]); |
| 368 | } |
| 369 | // add remaining path |
| 370 | final String p = u.getPath(); |
| 371 | tb.add(p == null || p.isEmpty() ? "/" : p.replace('.', '/')); |
| 372 | } |
| 373 | path = tb.toString(); |
| 374 | } catch(final URISyntaxException ex) { |
| 375 | Util.debug(ex); |
| 376 | } |
| 377 | |
| 378 | // replace special characters with dashes; remove multiple slashes |
| 379 | path = path.replaceAll("[^\\w.-/]+", "-").replaceAll("//+", "/"); |
| 380 | // add "index" string |
| 381 | if(endsWith(path, '/')) path += "index"; |
| 382 | // remove heading slash |
| 383 | if(startsWith(path, '/')) path = path.substring(1); |
| 384 | return path; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Canonicalizes a string: heuristic CP437/UTF-8 mojibake fix followed by Unicode normalization. |