From b91eced514871ccfc1462657d437ecadd2d7f197 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 22 Sep 2023 16:08:15 +0200 Subject: Uri: Added `Uri tryUriOrFile(final String uri_s)` for convenience / usability, as well as getScheme(String), isValidScheme(String), isFileScheme(String) and isHttpxScheme(String) --- src/java/com/jogamp/common/net/Uri.java | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'src/java/com/jogamp/common/net') diff --git a/src/java/com/jogamp/common/net/Uri.java b/src/java/com/jogamp/common/net/Uri.java index bca90bf..f7e0554 100644 --- a/src/java/com/jogamp/common/net/Uri.java +++ b/src/java/com/jogamp/common/net/Uri.java @@ -475,6 +475,7 @@ public class Uri { public ASCIIEncoded(final String unicode) { super(encodeToASCIIString(unicode)); } + @Override public boolean isASCII() { return true; } } @@ -1167,6 +1168,26 @@ public class Uri { return valueOf(url.toURI()); } + /** + * Return first successful resulting {@link Uri}. + * Try {@link #cast(String)} first, then {@link #valueOfFilepath(String)} and {@link #valueOf(File)} at last. + * @param uri_s a hopefully usable Uri location + * @return a valid Uri instance or {@code null} + */ + public static Uri tryUriOrFile(final String uri_s) { + try { + return Uri.cast( uri_s ); + } catch(final Throwable t) { } + try { + return valueOfFilepath( uri_s ); + } catch(final Throwable t) { } + try { + final File file = new File(uri_s); + return Uri.valueOf(file); + } catch(final Throwable t) { } + return null; + } + // // All string fields are encoded! // @@ -1235,6 +1256,29 @@ public class Uri { return null != scheme && FILE_SCHEME.equals( scheme.get() ); } + public static boolean isFileScheme(final String uri) { + final String scheme = getScheme(uri); + return scheme.equals(FILE_SCHEME); + } + public static boolean isHttpxScheme(final String uri) { + final String scheme = getScheme(uri); + return scheme.equals(HTTP_SCHEME) || scheme.equals(HTTPS_SCHEME); + } + public static String getScheme(final String uri) { + if( null == uri ) { + return ""; + } + final int pos = uri.indexOf(SCHEME_SEPARATOR); + if (0 > pos ) { + return ""; + } + final String scheme = uri.substring(0, pos); + if( ! isValidScheme( scheme ) ) { + return ""; + } + return scheme; + } + /** * Returns true, if this instance is a {@code jar} {@code scheme}, otherwise false. * @since 2.3.2 @@ -2163,6 +2207,18 @@ public class Uri { fail(uri, "invalid scheme", index+errIdx); } } + public static boolean isValidScheme(final String scheme) { + // first char needs to be an alpha char + final char ch = scheme.charAt(0); + if ( !((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) ) { + return false; + } + final int errIdx = validateAlphaNum(scheme, "+-."); + if( 0 <= errIdx ) { + return false; + } + return true; + } private static void validateSsp(final Encoded uri, final String ssp, final int index) throws URISyntaxException { final int errIdx = validateEncoded(ssp, SSP_LEGAL); -- cgit v1.2.3