summaryrefslogtreecommitdiff
path: root/support/texplate/source/main/java/org/islandoftex/texplate/util/PathUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'support/texplate/source/main/java/org/islandoftex/texplate/util/PathUtils.java')
-rw-r--r--support/texplate/source/main/java/org/islandoftex/texplate/util/PathUtils.java123
1 files changed, 123 insertions, 0 deletions
diff --git a/support/texplate/source/main/java/org/islandoftex/texplate/util/PathUtils.java b/support/texplate/source/main/java/org/islandoftex/texplate/util/PathUtils.java
new file mode 100644
index 0000000000..26e101b9ad
--- /dev/null
+++ b/support/texplate/source/main/java/org/islandoftex/texplate/util/PathUtils.java
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: BSD-3-Clause
+package org.islandoftex.texplate.util;
+
+import io.vavr.control.Try;
+import java.io.FileNotFoundException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import org.islandoftex.texplate.Main;
+
+/**
+ * Helper methods for path handling.
+ *
+ * @version 1.0
+ * @since 1.0
+ */
+public class PathUtils {
+
+ // the templates folder
+ private static final String TEMPLATES_FOLDER = "templates";
+
+ // the user application folder
+ private static final String USER_APPLICATION_FOLDER = ".texplate";
+
+ /**
+ * Gets the application path.
+ *
+ * @return The application path.
+ */
+ private static Path getApplicationPath() {
+ return Try.of(() -> Paths.get(Main.class.getProtectionDomain().
+ getCodeSource().getLocation().toURI().getPath()).resolve("..").
+ normalize()).getOrElse(Paths.get("."));
+ }
+
+ /**
+ * Gets the default template path.
+ *
+ * @return The default template path.
+ */
+ private static Path getDefaultTemplatePath() {
+ return getApplicationPath().resolve(TEMPLATES_FOLDER);
+ }
+
+ /**
+ * Gets the user template path.
+ *
+ * @return The user template path.
+ */
+ private static Path getUserTemplatePath() {
+ return Try.of(() -> Paths.get(System.getProperty("user.home"),
+ USER_APPLICATION_FOLDER, TEMPLATES_FOLDER)).
+ getOrElse(Paths.get("."));
+ }
+
+ /**
+ * Searchs all paths looking for the provided template.
+ *
+ * @param name The name to be associated to a template file.
+ * @return The corresponding template file.
+ * @throws FileNotFoundException The template file could not be found.
+ */
+ private static Path searchTemplatePath(String name)
+ throws FileNotFoundException {
+
+ // the file has to be a TOML format,
+ // so we add the extension
+ name = name.concat(".toml");
+
+ // the first reference is based on the
+ // user template path resolved with the
+ // file name
+ Path reference = getUserTemplatePath().resolve(name);
+
+ // if the file actually exists,
+ // the search is done!
+ if (Files.exists(reference)) {
+
+ // return the template
+ // file reference
+ return reference;
+
+ } else {
+
+ // the reference was not found in the
+ // user location, so let us try the
+ // system counterpart
+ reference = getDefaultTemplatePath().resolve(name);
+
+ // if the file actually exists,
+ // the search is done!
+ if (Files.exists(reference)) {
+
+ // return the template
+ // file reference
+ return reference;
+
+ } else {
+
+ // the file reference could not be
+ // found, so an exception is thrown
+ throw new FileNotFoundException("I am sorry, but the template "
+ + "file '" + name + "' could not be found in the "
+ + "default template locations (system and user). Make "
+ + "sure the reference is correct and try again. For "
+ + "reference, these are the paths I searched: '"
+ + getUserTemplatePath() + "' and '"
+ + getDefaultTemplatePath() + "' (in this order).");
+ }
+ }
+ }
+
+ /**
+ * Gets the template path based on the provided template name.
+ *
+ * @param name The template name.
+ * @return The template path, enclosed in a Try object.
+ */
+ public static Try<Path> getTemplatePath(String name) {
+ return Try.of(() -> searchTemplatePath(name));
+ }
+
+}