summaryrefslogtreecommitdiff
path: root/support/texplate/source/main/java/org/islandoftex/texplate/util/PathUtils.java
blob: 26e101b9ad04e053aff9de25e03168c73370d66b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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));
    }

}