diff --git a/base/src/main/java/de/staropensource/sosengine/base/classes/ShortcodeParserSkeleton.java b/base/src/main/java/de/staropensource/sosengine/base/classes/ShortcodeParserSkeleton.java index a782076..5ad2d52 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/classes/ShortcodeParserSkeleton.java +++ b/base/src/main/java/de/staropensource/sosengine/base/classes/ShortcodeParserSkeleton.java @@ -24,11 +24,10 @@ import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.types.CodePart; import de.staropensource.sosengine.base.types.logging.LogIssuer; import lombok.Getter; -import org.fusesource.jansi.Ansi; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; -import java.util.List; +import java.util.LinkedList; +import java.util.Locale; /** * The base skeleton for implementing a shortcode parser. @@ -71,7 +70,7 @@ public abstract class ShortcodeParserSkeleton { */ @NotNull @Getter - protected final List components; + protected final LinkedList components; /** * Constructs this class. @@ -92,8 +91,8 @@ public abstract class ShortcodeParserSkeleton { * @since v1-alpha1 */ @NotNull - private List<@NotNull String> parse(@NotNull String string) { - List components = new ArrayList<>(); // List of components + protected LinkedList<@NotNull String> parse(@NotNull String string) { + LinkedList components = new LinkedList<>(); // List of components boolean tagActive = false; // Indicates that a tag is being parsed String part = ""; // Current part. May be a tag, may be regular text @@ -109,26 +108,36 @@ public abstract class ShortcodeParserSkeleton { if (part.startsWith("fg:")) { // Print debug message if (EngineConfiguration.getInstance().isDebugShortcodeConverter()) - try { - System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=fg data=" + part.substring(3).toUpperCase() + " enum=" + Ansi.Color.valueOf(part.substring(3).toUpperCase())); - } catch (IllegalArgumentException ignored) { - System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=fg data=" + part.substring(3).toUpperCase() + " enum="); - } + System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=fg data=" + part.substring(3).toUpperCase()); - components.add("COLOR:FOREGROUND:" + part.substring(3).toUpperCase()); + if (isValidColor(part.substring(3).toUpperCase())) + components.add("COLOR:FOREGROUND:" + part.substring(3).toUpperCase()); + else { + // Complain about invalid shortcode + if (EngineConfiguration.getInstance().isErrorShortcodeConverter()) + logger.sarn("Invalid shortcode: " + part); + + // Convert tag regular text + components.add("TEXT:" + "<" + part + ">"); + } } // bg:* else if (part.startsWith("bg:")) { // Print debug message if (EngineConfiguration.getInstance().isDebugShortcodeConverter()) - try { - System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=bg data=" + part.substring(3).toUpperCase() + " enum=" + Ansi.Color.valueOf(part.substring(3).toUpperCase())); - } catch (IllegalArgumentException ignored) { - System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=bg data=" + part.substring(3).toUpperCase() + " enum="); - } + System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=bg data=" + part.substring(3).toUpperCase()); - components.add("COLOR:BACKGROUND:" + part.substring(3).toUpperCase()); + if (isValidColor(part.substring(3).toUpperCase())) + components.add("COLOR:BACKGROUND:" + part.substring(3).toUpperCase()); + else { + // Complain about invalid shortcode + if (EngineConfiguration.getInstance().isErrorShortcodeConverter()) + logger.sarn("Invalid shortcode: " + part); + + // Convert tag regular text + components.add("TEXT:" + "<" + part + ">"); + } } // bold @@ -181,16 +190,6 @@ public abstract class ShortcodeParserSkeleton { components.add("ATTRIBUTE:BLINK"); } - // underline - else if (part.equals("negative")) { - // Print debug message - if (EngineConfiguration.getInstance().isDebugShortcodeConverter()) - System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=negative"); - - // Insert attribute - components.add("ATTRIBUTE:NEGATIVE"); - } - // reset else if (part.equals("reset")) { // Print debug message @@ -251,4 +250,20 @@ public abstract class ShortcodeParserSkeleton { return components; } + + /** + * Checks the supplied color tag and returns if it's valid. + * + * @since v1-alpha2 + */ + private boolean isValidColor(@NotNull String color) { + switch (color.toLowerCase(Locale.ROOT)) { + case "black", "white", "red", "green", "blue", "yellow", "magenta", "cyan" -> { + return true; + } + default -> { + return false; + } + } + } }