summaryrefslogtreecommitdiff
path: root/support/arara/source/src/main/kotlin/org/islandoftex/arara/filehandling/DatabaseUtils.kt
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2020-03-05 03:00:59 +0000
committerNorbert Preining <norbert@preining.info>2020-03-05 03:00:59 +0000
commit898048513951b471a492afa23e46112d14bcb236 (patch)
tree8596afc705f55d2d07b324a756f7283ac0e2d21b /support/arara/source/src/main/kotlin/org/islandoftex/arara/filehandling/DatabaseUtils.kt
parent19d25b8009801aa98ea2f46b45c37c257f990491 (diff)
CTAN sync 202003050300
Diffstat (limited to 'support/arara/source/src/main/kotlin/org/islandoftex/arara/filehandling/DatabaseUtils.kt')
-rw-r--r--support/arara/source/src/main/kotlin/org/islandoftex/arara/filehandling/DatabaseUtils.kt97
1 files changed, 97 insertions, 0 deletions
diff --git a/support/arara/source/src/main/kotlin/org/islandoftex/arara/filehandling/DatabaseUtils.kt b/support/arara/source/src/main/kotlin/org/islandoftex/arara/filehandling/DatabaseUtils.kt
new file mode 100644
index 0000000000..f4728c9366
--- /dev/null
+++ b/support/arara/source/src/main/kotlin/org/islandoftex/arara/filehandling/DatabaseUtils.kt
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: BSD-3-Clause
+package org.islandoftex.arara.filehandling
+
+import com.charleskorn.kaml.Yaml
+import java.io.File
+import org.islandoftex.arara.Arara
+import org.islandoftex.arara.configuration.AraraSpec
+import org.islandoftex.arara.localization.LanguageController
+import org.islandoftex.arara.localization.Messages
+import org.islandoftex.arara.model.AraraException
+
+/**
+ * Implements database utilitary methods.
+ *
+ * @author Island of TeX
+ * @version 5.0
+ * @since 4.0
+ */
+object DatabaseUtils {
+ // the application messages obtained from the
+ // language controller
+ private val messages = LanguageController
+
+ /**
+ * Gets the file representing the YAML file (database).
+ *
+ * @throws AraraException Something wrong happened, to be caught in the
+ * higher levels.
+ */
+ private val file: File
+ @Throws(AraraException::class)
+ get() {
+ val reference = Arara.config[AraraSpec.Execution.reference]
+ val name = "${Arara.config[AraraSpec.Execution.databaseName]}.yaml"
+ val path = FileHandlingUtils.getParentCanonicalFile(reference)
+ return path.resolve(name)
+ }
+
+ /**
+ * Loads the YAML file representing the database.
+ *
+ * @return The database object.
+ * @throws AraraException Something wrong happened, to be caught in the
+ * higher levels.
+ */
+ @Throws(AraraException::class)
+ fun load(): Database {
+ return if (!exists()) {
+ Database()
+ } else {
+ file.runCatching {
+ val text = readText()
+ if (!text.startsWith("!database"))
+ throw Exception("Database should start with !database")
+ Yaml.default.parse(Database.serializer(), text)
+ }.getOrElse {
+ it.printStackTrace()
+ throw AraraException(messages.getMessage(Messages
+ .ERROR_LOAD_COULD_NOT_LOAD_XML, file.name), it)
+ }
+ }
+ }
+
+ /**
+ * Saves the database on a YAML file.
+ *
+ * @param database The database object.
+ * @throws AraraException Something wrong happened, to be caught in the
+ * higher levels.
+ */
+ @Throws(AraraException::class)
+ fun save(database: Database) {
+ file.runCatching {
+ val content = "!database\n" +
+ Yaml.default.stringify(Database.serializer(), database)
+ writeText(content)
+ }.getOrElse {
+ throw AraraException(
+ messages.getMessage(
+ Messages.ERROR_SAVE_COULD_NOT_SAVE_XML,
+ file.name
+ ), it)
+ }
+ }
+
+ /**
+ * Checks if the YAML file representing the database exists.
+ *
+ * @return A boolean value indicating if the YAML file exists.
+ * @throws AraraException Something wrong happened, to be caught in the
+ * higher levels.
+ */
+ @Throws(AraraException::class)
+ private fun exists(): Boolean {
+ return file.exists()
+ }
+}