summaryrefslogtreecommitdiff
path: root/support/texplate/source/main/java/org/islandoftex/texplate/model/Configuration.java
blob: a0f8e8370291cc416cb03b6c75bb55fc901d6096 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// SPDX-License-Identifier: BSD-3-Clause
package org.islandoftex.texplate.model;

import com.moandjiezana.toml.Toml;
import io.vavr.control.Try;
import org.islandoftex.texplate.exceptions.InvalidTemplateException;

import java.nio.file.Path;
import java.util.Map;

/**
 * The configuration model.
 *
 * @version 1.0
 * @since 1.0
 */
public class Configuration {

    // template of the template
    private String template;

    // map of variables for the configuration
    private Map<String, Object> map;

    /**
     * Constructor.
     */
    public Configuration() {
    }

    /**
     * Constructor.
     *
     * @param template The template.
     * @param map      The map.
     */
    public Configuration(String template, Map<String, Object> map) {
        this.template = template;
        this.map = map;
    }

    /**
     * Gets the template.
     *
     * @return The template.
     */
    public String getTemplate() {
        return template;
    }

    /**
     * Sets the template.
     *
     * @param template The template.
     */
    public void setTemplate(String template) {
        this.template = template;
    }

    /**
     * Gets the map.
     *
     * @return The map.
     */
    public Map<String, Object> getMap() {
        return map;
    }

    /**
     * Sets the map.
     *
     * @param map The map.
     */
    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    /**
     * Checks whether the configuration is valid.
     *
     * @return A boolean value indicating whether the configuration is valid.
     */
    private boolean isValid() {
        return !(template == null || map == null);
    }

    /**
     * Reads the configuration from path.
     *
     * @param path The path.
     * @return Configuration.
     * @throws InvalidTemplateException The configuration is invalid.
     */
    private static Configuration readFromPath(Path path)
            throws InvalidTemplateException {

        // the actual configuration
        Configuration configuration;

        // the default message
        String message = "The provided configuration file looks invalid. "
                + "Please make sure the configuration has a valid syntax and "
                + "try again. ";

        try {

            // gets the configuration
            configuration = new Toml().read(path.toFile()).
                    to(Configuration.class);

        } catch (Exception exception) {

            // the configuration
            // seems invalid
            throw new InvalidTemplateException(message + "In this particular "
                    + "scenario, there is a possibility that the configuration "
                    + "file does not follow the TOML specification. Please "
                    + "refer to the user manual for further details and a "
                    + "possible fix. Also, the raised exception message can "
                    + "give us some hints on what happened.", exception);
        }

        // checks whether the
        // configuration is valid
        if (configuration.isValid()) {

            // returns the configuration
            return configuration;
        } else {

            // the configuration
            // is invalid
            throw new InvalidTemplateException(message + "Specifically, some "
                    + "mandatory fields are either absent or empty in the "
                    + "configuration file. It is quite important to strictly "
                    + "follow the configuration specification, as detailed in "
                    + "the user manual, or the tool will not work at all.");
        }
    }

    /**
     * Gets the configuration from a path.
     *
     * @param path The path.
     * @return The configuration.
     */
    public static Try<Configuration> of(Path path) {
        return Try.of(() -> readFromPath(path));
    }
}