summaryrefslogtreecommitdiff
path: root/support/texplate/source/main/java/org/islandoftex/texplate/model/Template.java
diff options
context:
space:
mode:
Diffstat (limited to 'support/texplate/source/main/java/org/islandoftex/texplate/model/Template.java')
-rw-r--r--support/texplate/source/main/java/org/islandoftex/texplate/model/Template.java252
1 files changed, 252 insertions, 0 deletions
diff --git a/support/texplate/source/main/java/org/islandoftex/texplate/model/Template.java b/support/texplate/source/main/java/org/islandoftex/texplate/model/Template.java
new file mode 100644
index 0000000000..ca92e04658
--- /dev/null
+++ b/support/texplate/source/main/java/org/islandoftex/texplate/model/Template.java
@@ -0,0 +1,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));
+ }
+
+}