001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023
002package io.cui.tools.formatting.template;
003
004import static java.util.Objects.requireNonNull;
005import java.io.Serializable;
006import io.cui.tools.formatting.template.lexer.Lexer;
007import io.cui.tools.formatting.template.lexer.LexerBuilder;
008
009/**
010 * @param <F> at least {@link FormatterSupport}
011 */
012public class Validator<F extends FormatterSupport> implements Serializable {
013    private static final long serialVersionUID = 8358892211840118282L;
014    private final Lexer<F> lexer;
015
016    Validator(final Lexer<F> lexer) {
017        this.lexer = requireNonNull(lexer);
018    }
019
020    /**
021     * @param template must not be null
022     *            if template doesn't fit to el-expression or use tokens
023     *            which are not supported.
024     */
025    public void validate(final String template) {
026        requireNonNull(template, "Template must not be null.");
027        this.lexer.validateTemplate(template);
028    }
029
030    /**
031     * @return an {@link ValidatorBuilder}
032     */
033    public static <E extends FormatterSupport> ValidatorBuilder<E> builder() {
034        return new ValidatorBuilder<>();
035    }
036
037    /**
038     * This method provide only validation of simple expression language with
039     * squared brackets. If some other expression language is used on the
040     * template validation will fail. If there is a need for usage of other
041     * expression language use {@link #validateTemplate(String, Lexer)}
042     *
043     * @param template input which should be validated
044     * @param source source must not be null
045     */
046    public static <E extends FormatterSupport> void validateTemplate(final String template, final E source) {
047        new Validator.ValidatorBuilder<E>().forSource(source).validate(template);
048    }
049
050    /**
051     * This method provide only validation of simple expression language with
052     * squared brackets. If some other expression language is used on the
053     * template validation will fail. If there is a need for usage of other
054     * expression language use {@link #validateTemplate(String, Lexer)}
055     *
056     * @param template input which should be validated
057     * @param source target type must not be null
058     */
059    public static <E extends FormatterSupport> void validateTemplate(final String template, final Class<E> source) {
060        new Validator.ValidatorBuilder<E>().forType(source).validate(template);
061    }
062
063    /**
064     * This method provide validation for template of specific expression
065     * language. Therefore, you need to provide a fitting Lexer.
066     *
067     * @param template input which should be validated
068     * @param lexer {@link Lexer} for usage of specific expression language on
069     *            template
070     */
071    public static <E extends FormatterSupport> void validateTemplate(final String template, final Lexer<E> lexer) {
072        new Validator.ValidatorBuilder<E>().withLexer(lexer).validate(template);
073    }
074
075
076    @SuppressWarnings("javadoc")
077    public static final class ValidatorBuilder<E extends FormatterSupport> {
078        private Lexer<E> lexer;
079
080        public Validator<E> forType(final Class<E> source) {
081            if (null == this.lexer) {
082                this.lexer = useSimpleElWithSquaredBrackets(source);
083            }
084            return new Validator<>(this.lexer);
085        }
086
087        public Validator<E> withLexer(final Lexer<E> lexerInstance) {
088            return new Validator<>(lexerInstance);
089        }
090
091        public Validator<E> forSource(E source) {
092            if (null == this.lexer) {
093                this.lexer = useSimpleElWithSquaredBrackets(source);
094            }
095            return new Validator<>(this.lexer);
096        }
097
098        private Lexer<E> useSimpleElWithSquaredBrackets(final Class<E> source) {
099            return LexerBuilder.useSimpleElWithSquaredBrackets().build(source);
100        }
101
102        private Lexer<E> useSimpleElWithSquaredBrackets(E source) {
103            return LexerBuilder.useSimpleElWithSquaredBrackets().build(source);
104        }
105
106        @java.lang.SuppressWarnings("all")
107        @lombok.Generated
108        public ValidatorBuilder() {
109        }
110    }
111}