ResourceUtil.java

  1. package docsite.util;

  2. import java.io.*;
  3. import java.net.*;
  4. import java.nio.charset.StandardCharsets;
  5. import java.nio.file.*;
  6. import java.util.*;
  7. import java.util.stream.*;
  8. import docsite.DocsiteException;

  9. public final class ResourceUtil {

  10.     private ResourceUtil() {   }

  11.     private static final Map<String,List<String>> STATIC_RESOURCES = Map.of(
  12.         "css", List.of(
  13.             "css/common.css",
  14.             "font-awesome-all.css",
  15.             "prism-light.css"
  16.         ),
  17.         "js", List.of(
  18.             "prism.js"
  19.         ),
  20.         "webfonts", List.of(
  21.             "fa-brands-400.eot",
  22.             "fa-brands-400.svg",
  23.             "fa-brands-400.ttf",
  24.             "fa-brands-400.woff",
  25.             "fa-brands-400.woff2",
  26.             "fa-regular-400.eot",
  27.             "fa-regular-400.svg",
  28.             "fa-regular-400.ttf",
  29.             "fa-regular-400.woff",
  30.             "fa-regular-400.woff2",
  31.             "fa-solid-900.eot",
  32.             "fa-solid-900.svg",
  33.             "fa-solid-900.ttf",
  34.             "fa-solid-900.woff",
  35.             "fa-solid-900.woff2"
  36.         )
  37.     );


  38.     public static List<String> getResourceFiles(String path) {
  39.         return STATIC_RESOURCES.get(path);
  40.     }


  41.     public static void copyResourceFolder(String folderName, Path outputFolder) throws IOException {
  42.         Path targetFolder = outputFolder.toAbsolutePath().resolve(folderName);
  43.         for (String file : ResourceUtil.getResourceFiles(folderName)) {
  44.             copyResource(folderName+"/"+file, targetFolder);
  45.         }
  46.     }


  47.     public static boolean existsResource(String resource) {
  48.         return Thread.currentThread().getContextClassLoader().getResource(resource) != null;
  49.     }


  50.     public static void copyResource(String resource, Path outputFolder) throws IOException {
  51.         URL resourceUrl = Thread.currentThread().getContextClassLoader().getResource(resource);
  52.         if (resourceUrl == null) {
  53.             throw new FileNotFoundException(resource);
  54.         }
  55.         copyFromURLToFolder(resourceUrl, outputFolder);
  56.     }


  57.     public static void copyExternalFileWithAnotherName(Path path, Path outputFolder, String newName)
  58.     throws IOException {
  59.         outputFolder = outputFolder.toAbsolutePath();
  60.         Path target = outputFolder.resolve(newName);
  61.         copyFromURL(path.toUri().toURL(), target);
  62.     }


  63.     private static void copyFromURLToFolder(URL url, Path outputFolder) throws IOException {
  64.         outputFolder = outputFolder.toAbsolutePath();
  65.         Path target = outputFolder.resolve(Path.of(url.getFile()).getFileName().toString());
  66.         copyFromURL(url, target);
  67.     }


  68.     private static void copyFromURL(URL url, Path target) throws IOException {
  69.         Files.createDirectories(target.getParent());
  70.         Files.copy(
  71.             url.openStream(),
  72.             target,
  73.             StandardCopyOption.REPLACE_EXISTING
  74.         );
  75.     }



  76.     public static String read(InputStream inputStream) throws IOException {
  77.         try (BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
  78.             return reader.lines().collect(Collectors.joining("\n"));
  79.         }
  80.     }


  81.     public static InputStream open(Path baseDir, String source) throws IOException {
  82.         try {
  83.             return baseDir.resolve(source).toUri().toURL().openStream();
  84.         } catch (MalformedURLException e) {
  85.             return Files.newInputStream(Path.of(source));
  86.         }
  87.     }


  88.     public static boolean existsSource(Path baseDir, String source) {
  89.         if (source == null || source.isBlank()) {
  90.             return false;
  91.         }
  92.         if (Files.exists(baseDir.resolve(source))) {
  93.             return true;
  94.         }
  95.         try (InputStream stream = new URL(source).openStream()) {
  96.             return stream != null;
  97.         } catch (IOException e) {
  98.             return false;
  99.         }
  100.     }


  101.     public static void copyFolder(Path siteFolder, Path outputFolder)  {
  102.         try(Stream<Path> walker = Files.walk(siteFolder)) {
  103.             Files.createDirectories(outputFolder);
  104.             walker.forEach(sourcePath -> {
  105.                 try {
  106.                     Path targetPath = outputFolder.resolve(siteFolder.relativize(sourcePath));
  107.                     Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
  108.                 } catch (IOException e) {
  109.                     throw new DocsiteException(e);
  110.                 }
  111.             });
  112.         } catch (IOException e) {
  113.             throw new DocsiteException(e);
  114.         }
  115.     }


  116.     public static void deleteDirectory(Path path) {
  117.         try(Stream<Path> walker = Files.walk(path)) {
  118.             walker
  119.                 .sorted(Comparator.reverseOrder())
  120.                 .forEach(sourcePath -> {
  121.                     try {
  122.                         Files.delete(sourcePath);
  123.                     } catch (IOException e) {
  124.                         throw new DocsiteException(e);
  125.                     }
  126.                 });
  127.         } catch (IOException e) {
  128.             throw new DocsiteException(e);
  129.         }
  130.     }

  131. }