All Downloads are FREE. Search and download functionalities are using the official Maven repository.

wrm.libsass.Lookups Maven / Gradle / Ivy

There is a newer version: 0.3.1
Show newest version
package wrm.libsass;

import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.Optional;

class Lookups {
  static Optional findLocalFile(URI base, URI uri) {
    String pathname = base.resolve(uri).toString();
    File file = new File(pathname);
    return file.exists()
        ? Optional.of(Lookup.Result.of(file))
        : Optional.empty();
  }

  static Optional findResource(URI uri) {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL url = classLoader.getResource(uri.toString());
    return Optional.ofNullable(url).flatMap(Lookups::toResult);
  }

  private static Optional toResult(URL url) {
    switch (url.getProtocol()) {
      case "file":
        return Optional.of(Lookup.Result.of(url.toString(), url));
      case "jar":
        try {
          JarURLConnection jarUrlConnection = (JarURLConnection) url.openConnection();
          String name = jarUrlConnection.getEntryName();
          return Optional.of(Lookup.Result.of(name, url));
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      default:
        return Optional.empty();
    }
  }

  static Optional findWebJarResource(URI uri, WebJarTranslator translator) {
    return translator.translate(uri).flatMap(Lookups::findResource);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy