001package org.galaxyproject.gxformat2.v19_09.utils; 002 003import java.io.UnsupportedEncodingException; 004import java.net.URI; 005import java.net.URISyntaxException; 006import java.nio.charset.StandardCharsets; 007 008public class Uris { 009 010 // Emulate Python's urlsplit. 011 public static class UriSplit { 012 String scheme; 013 String netloc; 014 String path; 015 String query; 016 String fragment; 017 018 public UriSplit(String scheme, String netloc, String path, String query, String fragment) { 019 this.scheme = scheme; 020 this.netloc = netloc; 021 this.path = path; 022 this.query = query; 023 this.fragment = fragment; 024 } 025 026 public String toString() { 027 return String.format( 028 "UriSplit[%s,%s,%s,%s,%s]", 029 this.scheme, this.netloc, this.path, this.query, this.fragment); 030 } 031 } 032 033 public static String fileUri(final String path) { 034 return fileUri(path, false); 035 } 036 037 public static String fileUri(final String path, final boolean splitFrag) { 038 if (path.equals("file://")) { 039 return path; 040 } 041 String frag; 042 String urlPath; 043 if (splitFrag) { 044 final String[] pathsp = path.split("#", 2); 045 // is quoting this? 046 urlPath = Uris.quote(pathsp[0]); 047 if (pathsp.length == 2) { 048 frag = "#" + Uris.quote(pathsp[1]); 049 } else { 050 frag = ""; 051 urlPath = Uris.quote(path); 052 } 053 } else { 054 urlPath = Uris.quote(path); 055 frag = ""; 056 } 057 if (urlPath.startsWith("//")) { 058 return "file:" + urlPath + frag; 059 } else { 060 return "file://" + urlPath + frag; 061 } 062 } 063 064 public static UriSplit split(final String uriString) { 065 try { 066 final URI uri = new URI(uriString); 067 return new Uris.UriSplit( 068 uri.getScheme(), uri.getAuthority(), uri.getPath(), uri.getQuery(), uri.getFragment()); 069 } catch (URISyntaxException e) { 070 return new Uris.UriSplit(null, null, uriString, null, null); 071 } 072 } 073 074 public static String unsplit( 075 final String scheme, 076 final String netloc, 077 final String path, 078 final String query, 079 final String fragment) { 080 try { 081 return new URI(scheme, netloc, path, query, fragment).toString(); 082 } catch (URISyntaxException e) { 083 if (scheme == null && path.startsWith("_:")) { 084 String uri = path; 085 if (fragment != null && fragment.length() > 0) { 086 uri += "#" + fragment; 087 } 088 return fragment; 089 } 090 throw new RuntimeException(e); 091 } 092 } 093 094 public static URI toUri(final String url) { 095 try { 096 return new URI(url); 097 } catch (URISyntaxException e) { 098 throw new RuntimeException(e); 099 } 100 } 101 102 public static String quote(final String uri) { 103 try { 104 return java.net.URLDecoder.decode(uri, StandardCharsets.UTF_8.name()); 105 } catch (UnsupportedEncodingException e) { 106 throw new RuntimeException(e); 107 } 108 } 109 110 public static String unquote(final String uri) { 111 try { 112 return java.net.URLEncoder.encode(uri, StandardCharsets.UTF_8.name()); 113 } catch (UnsupportedEncodingException e) { 114 throw new RuntimeException(e); 115 } 116 } 117}