summaryrefslogtreecommitdiff
path: root/support/texplate/source/main/java/org/islandoftex/texplate/model/Template.java
blob: ca92e046584a4ec7b4e74350d1cbdac62d408132 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// 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.List;
import java.util.Map;

/**
 * The template model.
 *
 * @version 1.0
 * @since 1.0
 */
public class Template {

    // name of the template
    private String name;

    // description of the template
    private String description;

    // list of authors who wrote the template
    private List<String> authors;

    // list of requirements for the template
    private List<String> requirements;

    // the document to be configured
    private String document;

    // the map handlers
    private Map<String, String> handlers;

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

    /**
     * Constructor.
     *
     * @param name         Name of the template.
     * @param description  Description of the template.
     * @param authors      List of authors of the template.
     * @param requirements List of requirements for the template.
     * @param document     The document to be configured.
     * @param handlers     The optional map handlers.
     */
    public Template(String name, String description, List<String> authors,
                    List<String> requirements, String document,
                    Map<String, String> handlers) {
        this.name = name;
        this.description = description;
        this.authors = authors;
        this.requirements = requirements;
        this.document = document;
        this.handlers = handlers;
    }

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

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

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

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

    /**
     * Gets the template authors.
     *
     * @return The template authors.
     */
    public List<String> getAuthors() {
        return authors;
    }

    /**
     * Sets the template authors.
     *
     * @param authors The template authors.
     */
    public void setAuthors(List<String> authors) {
        this.authors = authors;
    }

    /**
     * Gets the template requirements.
     *
     * @return The template requirements.
     */
    public List<String> getRequirements() {
        return requirements;
    }

    /**
     * Sets the template requirements.
     *
     * @param requirements The template requirements.
     */
    public void setRequirements(List<String> requirements) {
        this.requirements = requirements;
    }

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

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

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

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

    /**
     * Checks whether the template is valid.
     *
     * @return A boolean value indicating whether the template is valid.
     */
    private boolean isValid() {
        return !((name == null) || (description == null)
                || (authors == null) || (requirements == null)
                || (document == null) || name.trim().isEmpty()
                || description.trim().isEmpty() || authors.isEmpty()
                || document.trim().isEmpty());
    }

    /**
     * Reads the template from the provided path.
     *
     * @param path The path to the template file.
     * @return The template object from the provided path.
     * @throws InvalidTemplateException The template is invalid.
     */
    private static Template readFromPath(Path path)
            throws InvalidTemplateException {

        // the actual template
        Template template;

        // the exception message, in case the
        // conversion fails or if there are
        // missing elements from the template
        String message = "The provided template file looks invalid. Please "
                + "make sure the template has a valid syntax and try again. ";

        try {

            // reads the file and converts
            // the TOML format into the object
            template = new Toml().read(path.toFile()).to(Template.class);

        } catch (Exception exception) {

            // throws the new exception and
            // attaches the original cause
            throw new InvalidTemplateException(message + "In this particular "
                    + "scenario, there is a possibility that the template "
                    + "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);
        }

        // the conversion hasn't failed, but we need
        // to check whether the template is valid
        if (template.isValid()) {

            // everything went fine, so
            // simply return the template
            return template;
        } else {

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

    /**
     * Returns the template from the provided path.
     *
     * @param path The path in which the template is retrieved.
     * @return The corresponding template, enclosed in a Try object.
     */
    public static Try<Template> of(Path path) {
        return Try.of(() -> readFromPath(path));
    }

}