summaryrefslogtreecommitdiff
path: root/support/arara/source/src/main/kotlin/org/islandoftex/arara/ruleset/RuleUtils.kt
blob: e823e4e598f26b911d25c632d3fe49b34e2034d0 (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
// SPDX-License-Identifier: BSD-3-Clause
package org.islandoftex.arara.ruleset

import com.charleskorn.kaml.Yaml
import java.io.File
import org.islandoftex.arara.localization.LanguageController
import org.islandoftex.arara.localization.Messages
import org.islandoftex.arara.model.AraraException
import org.islandoftex.arara.utils.CommonUtils

/**
 * Implements rule utilitary methods.
 *
 * @author Island of TeX
 * @version 5.0
 * @since 4.0
 */
object RuleUtils {
    // the application messages obtained from the
    // language controller
    private val messages = LanguageController

    /**
     * Parses the provided file, checks the identifier and returns a rule
     * representation.
     *
     * @param file The rule file.
     * @param identifier The directive identifier.
     * @return The rule object.
     * @throws AraraException Something wrong happened, to be caught in the
     * higher levels.
     */
    @Throws(AraraException::class)
    fun parseRule(file: File, identifier: String): Rule {
        val rule = file.runCatching {
            val text = readText()
            if (!text.startsWith("!config"))
                throw Exception("Rule should start with !config")
            Yaml.default.parse(Rule.serializer(), text)
        }.getOrElse {
            throw AraraException(
                    CommonUtils.ruleErrorHeader + messages.getMessage(
                            Messages.ERROR_PARSERULE_GENERIC_ERROR
                    ), it)
        }

        validateHeader(rule, identifier)
        validateBody(rule)
        return rule
    }

    /**
     * Validates the rule header according to the directive identifier.
     *
     * @param rule The rule object.
     * @param identifier The directive identifier.
     * @throws AraraException Something wrong happened, to be caught in the
     * higher levels.
     */
    @Throws(AraraException::class)
    @Suppress("ThrowsCount")
    private fun validateHeader(rule: Rule, identifier: String) {
        if (rule.identifier != Rule.INVALID_RULE_IDENTIFIER) {
            if (rule.identifier != identifier) {
                throw AraraException(CommonUtils.ruleErrorHeader +
                        messages.getMessage(
                                Messages.ERROR_VALIDATEHEADER_WRONG_IDENTIFIER,
                                rule.identifier,
                                identifier))
            }
        } else {
            throw AraraException(CommonUtils.ruleErrorHeader +
                    messages.getMessage(Messages.ERROR_VALIDATEHEADER_NULL_ID))
        }
        if (rule.name == Rule.INVALID_RULE_NAME) {
            throw AraraException(
                    CommonUtils.ruleErrorHeader + messages.getMessage(
                            Messages.ERROR_VALIDATEHEADER_NULL_NAME
                    )
            )
        }
    }

    /**
     * Validates the rule body.
     *
     * @param rule The rule object.
     * @throws AraraException Something wrong happened, to be caught in the
     * higher levels.
     */
    @Throws(AraraException::class)
    @Suppress("ThrowsCount")
    private fun validateBody(rule: Rule) {
        if (rule.commands.any { it.command == null }) {
            throw AraraException(CommonUtils.ruleErrorHeader +
                    messages.getMessage(
                            Messages.ERROR_VALIDATEBODY_NULL_COMMAND))
        }

        val arguments = mutableListOf<String>()
        for (argument in rule.arguments) {
            if (argument.identifier != null) {
                if (argument.flag != null || argument.default != null) {
                    arguments.add(argument.identifier!!)
                } else {
                    throw AraraException(
                            CommonUtils.ruleErrorHeader + messages.getMessage(
                                    Messages.ERROR_VALIDATEBODY_MISSING_KEYS
                            )
                    )
                }
            } else {
                throw AraraException(
                        CommonUtils.ruleErrorHeader + messages.getMessage(
                                Messages.ERROR_VALIDATEBODY_NULL_ARGUMENT_ID
                        )
                )
            }
        }

        arguments.intersect(listOf("files", "reference")).forEach {
            throw AraraException(
                    CommonUtils.ruleErrorHeader + messages.getMessage(
                            Messages.ERROR_VALIDATEBODY_ARGUMENT_ID_IS_RESERVED,
                            it
                    )
            )
        }

        val expected = arguments.size
        val found = arguments.toSet().size
        if (expected != found) {
            throw AraraException(
                    CommonUtils.ruleErrorHeader + messages.getMessage(
                            Messages.ERROR_VALIDATEBODY_DUPLICATE_ARGUMENT_IDENTIFIERS
                    )
            )
        }
    }
}