Skip to content

Commit

Permalink
thunderpay-lib: xmlloader[UriAccessor]
Browse files Browse the repository at this point in the history
  • Loading branch information
krishpranav committed Dec 16, 2024
1 parent 4aa83da commit 526466a
Showing 1 changed file with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static java.nio.charset.StandardCharsets.UTF_8;

public class UriAccessor {

private static final String URI_SCHEME_FOR_ARCHIVE_FILE = "jar:file";
private static final String URI_SCHEME_FOR_CLASSPATH = "jar";
private static final String URI_SCHEME_FOR_FILE = "file";
Expand All @@ -33,16 +34,51 @@ public static URL toURL(final String uri) throws IOException, URISyntaxException

public static URL toURL(final URI inputURI) throws IOException, URISyntaxException {
final String scheme = inputURI.getScheme();
final URI uri;

final URI uri;
if (scheme == null) {

} else if (scheme.equals(URI_SCHEME_FOR_FILE)) {

uri = new URI(Resources.getResource(inputURI.toString()).toExternalForm());
} else if (scheme.equals(URI_SCHEME_FOR_FILE) &&
!inputURI.getSchemeSpecificPart().startsWith("/")) { // interpret URIs of this form as relative path uris
uri = new File(inputURI.getSchemeSpecificPart()).toURI();
} else {
uri = inputURI;
}

return uri.toURL();
}

public static InputStream accessUri(final String uri) throws IOException, URISyntaxException {
return accessUri(new URI(uri));
}

public static InputStream accessUri(final URI uri) throws IOException, URISyntaxException {
final String scheme = uri.getScheme();
if (URI_SCHEME_FOR_CLASSPATH.equals(scheme)) {
if (uri.toString().startsWith(URI_SCHEME_FOR_ARCHIVE_FILE)) {
return getInputStreamFromJarFile(uri);
} else {
return UriAccessor.class.getResourceAsStream(uri.getPath());
}
}

final URL url = toURL(uri);
return url.openConnection().getInputStream();
}

private static InputStream getInputStreamFromJarFile(final URI uri) throws IOException {
final URL url = uri.toURL();
final JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();

return jarURLConnection.getJarFile().getInputStream(jarURLConnection.getJarEntry());
}

public static String accessUriAsString(final String uri) throws IOException, URISyntaxException {
return accessUriAsString(new URI(uri));
}

public static String accessUriAsString(final URI uri) throws IOException, URISyntaxException {
final InputStream stream = accessUri(uri);
return new Scanner(stream, UTF_8.name()).useDelimiter("\\A").next();
}
}

0 comments on commit 526466a

Please sign in to comment.