From 34897eebd22c3de968b64f09bb4a371d96cb0b89 Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Mon, 3 Feb 2020 03:02:01 +0000 Subject: CTAN sync 202002030302 --- .../islandoftex/texplate/util/ValidatorUtils.kt | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 support/texplate/source/main/kotlin/org/islandoftex/texplate/util/ValidatorUtils.kt (limited to 'support/texplate/source/main/kotlin/org/islandoftex/texplate/util/ValidatorUtils.kt') diff --git a/support/texplate/source/main/kotlin/org/islandoftex/texplate/util/ValidatorUtils.kt b/support/texplate/source/main/kotlin/org/islandoftex/texplate/util/ValidatorUtils.kt new file mode 100644 index 0000000000..ef91a012c1 --- /dev/null +++ b/support/texplate/source/main/kotlin/org/islandoftex/texplate/util/ValidatorUtils.kt @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: BSD-3-Clause +package org.islandoftex.texplate.util + +import org.islandoftex.texplate.exceptions.InvalidKeySetException +import org.islandoftex.texplate.model.Template + +/** + * Helper methods for validation. + * + * @version 1.0 + * @since 1.0 + */ +object ValidatorUtils { + /** + * Validates the data map based on the template requirements. + * + * @param template The template. + * @param map The data map. + * @return A boolean value indicating whether the data map is valid. + */ + private fun validateRequirements( + template: Template, + map: Map + ): Boolean { + return template.requirements.isNullOrEmpty() || + template.requirements.containsAll(map.keys) + } + + /** + * Validates the template pattern and the data map and throws an exception + * in case of failure. + * + * @param template The template. + * @param map The data map. + * @return The data map. + * @throws InvalidKeySetException There are invalid keys in the map. + */ + @JvmStatic + @Throws(InvalidKeySetException::class) + fun validate( + template: Template, + map: Map + ): Map { + // for starters, we try to validate the template requirements + return if (validateRequirements(template, map)) { + map + } else { + throw InvalidKeySetException("The provided map does not " + + "contain all the keys required by the chosen " + + "template. Make sure to define such keys and try " + + "again. Check the user manual for further details.") + } + } +} -- cgit v1.2.3