aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/com/jogamp/common/net
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2023-09-22 16:08:15 +0200
committerSven Gothel <[email protected]>2023-09-22 16:08:15 +0200
commitb91eced514871ccfc1462657d437ecadd2d7f197 (patch)
tree7caa42c125aa72125e6dbefdad3b237cc586b79a /src/java/com/jogamp/common/net
parentfdda2f12a5aa6f931f693b15e1c3cd7dab030882 (diff)
Uri: Added `Uri tryUriOrFile(final String uri_s)` for convenience / usability, as well as getScheme(String), isValidScheme(String), isFileScheme(String) and isHttpxScheme(String)
Diffstat (limited to 'src/java/com/jogamp/common/net')
-rw-r--r--src/java/com/jogamp/common/net/Uri.java56
1 files changed, 56 insertions, 0 deletions
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);