All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.thewonderlemming.c4plantuml.syntaxchecker.SyntaxErrorMessageMapper Maven / Gradle / Ivy

The newest version!
package org.thewonderlemming.c4plantuml.syntaxchecker;

import java.util.AbstractMap.SimpleEntry;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Maps ANTLR4 grammars specific errors to readable errors.
 *
 * @author thewonderlemming
 *
 */
public enum SyntaxErrorMessageMapper {

    /**
     * An error message related to the !include instruction.
     */
    EXPECTING_INCLUDE(
        "mismatched input '!' expecting INCLUDE",
        "mismatched input. expecting !include");


    private static final Map LOOKUP;

    private final String displayMessage;

    private final String parserMessage;


    static {

        LOOKUP = Stream
            .of(SyntaxErrorMessageMapper.values())
                .map(value -> new SimpleEntry<>(value.getParserMessage(), value))
                .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
    }


    /**
     * Returns an instance of {@link SyntaxErrorMessageMapper} given a parser message, if found.
     *
     * @param message the parser message.
     * @return the {@link SyntaxErrorMessageMapper} instance if found, empty else.
     */
    public static Optional getMappingForParserMessage(final String message) {
        return Optional.ofNullable(LOOKUP.get(message));
    }

    /**
     * Returns a readable message given a parser message if found.
     *
     * @param message the parser message.
     * @return the readable message if found, or the given message else.
     */
    public static String getMappingForParserMessageOrGetDefault(final String message) {

        final Optional value = getMappingForParserMessage(message);

        if (value.isPresent()) {
            return value.get().getDisplayMessage();
        }

        return message;
    }

    private SyntaxErrorMessageMapper(final String parserMessage, final String displayMessage) {

        this.parserMessage = parserMessage;
        this.displayMessage = displayMessage;
    }

    /**
     * Returns the readable message.
     *
     * @return the readable message.
     */
    public String getDisplayMessage() {
        return displayMessage;
    }

    /**
     * Returns the parser original message.
     *
     * @return the parser original message.
     */
    public String getParserMessage() {
        return parserMessage;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy