Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Use ProtocolResolver to determine if local file

AuthorDaveJarvis <email>
Date2020-09-12 12:44:35 GMT-0700
Commit5ded87bd9cfd1079ef799e160abefdf2c2fc4331
Parente7091b2
Delta27 lines added, 6 lines removed, 21-line increase
src/main/java/com/scrivenvar/util/ProtocolResolver.java
*/
public class ProtocolResolver {
- public static final String SCHEME_HTTP = "http";
+ private static final String SCHEME_HTTP = "http";
+ private static final String SCHEME_FILE = "file";
/**
* Answers {@code true} if the given protocol is either HTTP or HTTPS.
*
- * @param protocol The protocol to compare against the web protocol.
+ * @param protocol The protocol to compare against the web URI scheme.
* @return {@code true} the protocol is either HTTP or HTTPS.
*/
- public static boolean isHttp( String protocol ) {
- protocol = protocol == null ? "" : protocol;
- return protocol.toLowerCase().startsWith( SCHEME_HTTP );
+ public static boolean isHttp( final String protocol ) {
+ return sanitize( protocol ).startsWith( SCHEME_HTTP );
+ }
+
+ /**
+ * Answers {@code true} if the given protocol is for a local file.
+ *
+ * @param protocol The protocol to compare against the file URI scheme.
+ * @return {@code true} the protocol is for a local file reference.
+ */
+ public static boolean isFile( String protocol ) {
+ return sanitize( protocol ).startsWith( SCHEME_FILE );
}
/**
* Returns the protocol for a given URI or filename.
*
* @param resource Determine the protocol for this URI or filename.
- * @return The protocol for the given source.
+ * @return The protocol for the given resource.
*/
public static String getProtocol( final String resource ) {
return result;
+ }
+
+ /**
+ * Returns an empty string if the given string to sanitize is {@code null},
+ * otherwise the given string in lowercase.
+ *
+ * @param s The string to sanitize, may be {@code null}.
+ * @return A non-{@code null} string.
+ */
+ private static String sanitize( final String s ) {
+ return s == null ? "" : s.toLowerCase();
}
}