diff --git a/src/main/java/de/rub/nds/scanner/core/report/ColorEncoding.java b/src/main/java/de/rub/nds/scanner/core/report/ColorEncoding.java index 1edd3a5c..b2a0ae19 100644 --- a/src/main/java/de/rub/nds/scanner/core/report/ColorEncoding.java +++ b/src/main/java/de/rub/nds/scanner/core/report/ColorEncoding.java @@ -9,32 +9,33 @@ package de.rub.nds.scanner.core.report; import de.rub.nds.scanner.core.probe.result.TestResult; +import de.rub.nds.scanner.core.report.markup.SemanticMarkup; import java.util.HashMap; /** - * Handles color encoding for test results in terminal output. Maps test results to ANSI colors for - * visual differentiation. + * Handles color encoding for test results in terminal output. Maps test results to Semantic colors + * for visual differentiation. */ public class ColorEncoding { - private final HashMap colorMap; + private final HashMap colorMap; /** * Creates a new ColorEncoding with the specified result-to-color mapping. * - * @param colorMap a HashMap mapping TestResult instances to AnsiColor codes + * @param colorMap a HashMap mapping TestResult instances to SemanticMarkup codes */ - public ColorEncoding(HashMap colorMap) { + public ColorEncoding(HashMap colorMap) { this.colorMap = colorMap; } /** - * Returns the AnsiColor associated with a given test result. + * Returns the SemanticMarkup associated with a given test result. * * @param result the test result - * @return the associated AnsiColor, or null if no mapping exists + * @return the associated SemanticMarkup, or null if no mapping exists */ - public AnsiColor getColor(TestResult result) { + public SemanticMarkup getColor(TestResult result) { return colorMap.get(result); } @@ -47,9 +48,9 @@ public AnsiColor getColor(TestResult result) { * @return the color-encoded text string */ public String encode(TestResult result, String encodedText) { - AnsiColor color = this.getColor(result); - if (color != null && color != AnsiColor.DEFAULT_COLOR) { - return color.getCode() + encodedText + AnsiColor.RESET.getCode(); + SemanticMarkup color = this.getColor(result); + if (color != null && color != SemanticMarkup.NEUTRAL) { + return color.applyAnsi(encodedText); } else { return encodedText; } diff --git a/src/main/java/de/rub/nds/scanner/core/report/PrintingScheme.java b/src/main/java/de/rub/nds/scanner/core/report/PrintingScheme.java index aa8950d2..7364936e 100644 --- a/src/main/java/de/rub/nds/scanner/core/report/PrintingScheme.java +++ b/src/main/java/de/rub/nds/scanner/core/report/PrintingScheme.java @@ -11,6 +11,7 @@ import de.rub.nds.scanner.core.probe.AnalyzedProperty; import de.rub.nds.scanner.core.probe.AnalyzedPropertyCategory; import de.rub.nds.scanner.core.probe.result.TestResult; +import de.rub.nds.scanner.core.report.markup.SemanticMarkup; import java.util.HashMap; /** @@ -139,9 +140,9 @@ public String getEncodedKeyText(ScanReport report, AnalyzedProperty property) { * * @param report the scan report containing the result * @param property the property whose result color should be determined - * @return the ANSI color to use for the result value + * @return the color to use for the result value */ - public AnsiColor getValueColor(ScanReport report, AnalyzedProperty property) { + public SemanticMarkup getValueColor(ScanReport report, AnalyzedProperty property) { TestResult result = report.getResult(property); ColorEncoding colorEncoding = valueColorEncodings.getOrDefault(property, defaultColorEncoding); @@ -154,9 +155,9 @@ public AnsiColor getValueColor(ScanReport report, AnalyzedProperty property) { * * @param report the scan report (currently unused) * @param property the property (currently unused) - * @return the default ANSI color + * @return the default color */ - public AnsiColor getKeyColor(ScanReport report, AnalyzedProperty property) { - return AnsiColor.DEFAULT_COLOR; + public SemanticMarkup getKeyColor(ScanReport report, AnalyzedProperty property) { + return SemanticMarkup.NEUTRAL; } } diff --git a/src/main/java/de/rub/nds/scanner/core/report/ReportCreator.java b/src/main/java/de/rub/nds/scanner/core/report/ReportCreator.java index c4c64c2a..83ad1930 100644 --- a/src/main/java/de/rub/nds/scanner/core/report/ReportCreator.java +++ b/src/main/java/de/rub/nds/scanner/core/report/ReportCreator.java @@ -13,6 +13,8 @@ import de.rub.nds.scanner.core.report.container.KeyValueContainer; import de.rub.nds.scanner.core.report.container.ReportContainer; import de.rub.nds.scanner.core.report.container.TextContainer; +import de.rub.nds.scanner.core.report.markup.Markup; +import de.rub.nds.scanner.core.report.markup.SemanticMarkup; /** * Base class for creating report containers from scan results. Provides utility methods for @@ -47,8 +49,8 @@ public ReportCreator(ScannerDetail detail, PrintingScheme scheme) { protected ReportContainer createKeyValueContainer(AnalyzedProperty property, ReportT report) { String key = printingScheme.getEncodedKeyText(report, property); String value = printingScheme.getEncodedValueText(report, property); - AnsiColor keyColour = printingScheme.getKeyColor(report, property); - AnsiColor valueColour = printingScheme.getValueColor(report, property); + Markup keyColour = printingScheme.getValueColor(report, property); + Markup valueColour = printingScheme.getValueColor(report, property); return new KeyValueContainer(key, keyColour, value, valueColour); } @@ -60,7 +62,7 @@ protected ReportContainer createKeyValueContainer(AnalyzedProperty property, Rep * @return a key-value container with default colors */ protected ReportContainer createDefaultKeyValueContainer(String key, String value) { - return new KeyValueContainer(key, AnsiColor.DEFAULT_COLOR, value, AnsiColor.DEFAULT_COLOR); + return new KeyValueContainer(key, SemanticMarkup.NEUTRAL, value, SemanticMarkup.NEUTRAL); } /** @@ -72,7 +74,7 @@ protected ReportContainer createDefaultKeyValueContainer(String key, String valu */ protected ReportContainer createDefaultKeyHexValueContainer(String key, String value) { return new KeyValueContainer( - key, AnsiColor.DEFAULT_COLOR, "0x" + value, AnsiColor.DEFAULT_COLOR); + key, SemanticMarkup.NEUTRAL, "0x" + value, SemanticMarkup.NEUTRAL); } /** @@ -82,6 +84,6 @@ protected ReportContainer createDefaultKeyHexValueContainer(String key, String v * @return a text container with default color */ protected TextContainer createDefaultTextContainer(String text) { - return new TextContainer(text, AnsiColor.DEFAULT_COLOR); + return new TextContainer(text, SemanticMarkup.NEUTRAL); } } diff --git a/src/main/java/de/rub/nds/scanner/core/report/ReportPrinter.java b/src/main/java/de/rub/nds/scanner/core/report/ReportPrinter.java index f18add46..92dd83c2 100644 --- a/src/main/java/de/rub/nds/scanner/core/report/ReportPrinter.java +++ b/src/main/java/de/rub/nds/scanner/core/report/ReportPrinter.java @@ -10,6 +10,9 @@ import de.rub.nds.scanner.core.config.ScannerDetail; import de.rub.nds.scanner.core.probe.AnalyzedProperty; +import de.rub.nds.scanner.core.report.markup.Markup; +import de.rub.nds.scanner.core.report.markup.MarkupUtil; +import de.rub.nds.scanner.core.report.markup.SemanticMarkup; /** * Abstract base class for generating formatted text representations of scan reports. Provides @@ -72,9 +75,12 @@ protected String getBlackString(String value, String format) { * @return the formatted string with optional green color, "Unknown" if value is null */ protected String getGreenString(String value, String format) { - return (printColorful ? AnsiColor.GREEN.getCode() : AnsiColor.RESET.getCode()) - + String.format(format, value == null ? "Unknown" : value) - + AnsiColor.RESET.getCode(); + String formatted = String.format(format, value == null ? "Unknown" : value); + if (printColorful) { + return SemanticMarkup.RESULT_GOOD.applyAnsi(formatted); + } else { + return formatted; + } } /** @@ -85,9 +91,12 @@ protected String getGreenString(String value, String format) { * @return the formatted string with optional yellow color, "Unknown" if value is null */ protected String getYellowString(String value, String format) { - return (printColorful ? AnsiColor.YELLOW.getCode() : AnsiColor.RESET.getCode()) - + String.format(format, value == null ? "Unknown" : value) - + AnsiColor.RESET.getCode(); + String formatted = String.format(format, value == null ? "Unknown" : value); + if (printColorful) { + return SemanticMarkup.RESULT_MEDIUM.applyAnsi(formatted); + } else { + return formatted; + } } /** @@ -98,9 +107,12 @@ protected String getYellowString(String value, String format) { * @return the formatted string with optional red color, "Unknown" if value is null */ protected String getRedString(String value, String format) { - return (printColorful ? AnsiColor.RED.getCode() : AnsiColor.RESET.getCode()) - + String.format(format, value == null ? "Unknown" : value) - + AnsiColor.RESET.getCode(); + String formatted = String.format(format, value == null ? "Unknown" : value); + if (printColorful) { + return SemanticMarkup.RESULT_BAD.applyAnsi(formatted); + } else { + return formatted; + } } /** @@ -122,13 +134,12 @@ protected StringBuilder prettyAppend(StringBuilder builder, String value) { * @param color the color to apply if colors are enabled * @return the builder for method chaining */ - protected StringBuilder prettyAppend(StringBuilder builder, String value, AnsiColor color) { - if (printColorful) { - builder.append(color.getCode()); - } - builder.append(value); + protected StringBuilder prettyAppend( + StringBuilder builder, String value, SemanticMarkup color) { if (printColorful) { - builder.append(AnsiColor.RESET.getCode()); + color.applyAnsi(builder, value); + } else { + builder.append(value); } builder.append("\n"); return builder; @@ -221,7 +232,7 @@ protected StringBuilder prettyAppend( * @return the builder for method chaining */ protected StringBuilder prettyAppend( - StringBuilder builder, String name, Boolean value, AnsiColor color) { + StringBuilder builder, String name, Boolean value, SemanticMarkup color) { return prettyAppend(builder, name, String.valueOf(value), color); } @@ -235,14 +246,12 @@ protected StringBuilder prettyAppend( * @return the builder for method chaining */ protected StringBuilder prettyAppend( - StringBuilder builder, String name, String value, AnsiColor color) { + StringBuilder builder, String name, String value, SemanticMarkup color) { builder.append(addIndentations(name)).append(": "); if (printColorful) { - builder.append(color.getCode()); - } - builder.append(value); - if (printColorful) { - builder.append(AnsiColor.RESET.getCode()); + color.applyAnsi(builder, value); + } else { + builder.append(value); } builder.append("\n"); return builder; @@ -258,14 +267,13 @@ protected StringBuilder prettyAppend( protected StringBuilder prettyAppendHeading(StringBuilder builder, String value) { depth = 0; - return builder.append( - printColorful - ? AnsiColor.BOLD.getCode() + AnsiColor.BLUE.getCode() - : AnsiColor.RESET.getCode()) - .append("\n------------------------------------------------------------\n") - .append(value) - .append("\n\n") - .append(AnsiColor.RESET.getCode()); + Markup headingMarkup = + printColorful ? SemanticMarkup.REPORT_STRUCTURE_HEADING : MarkupUtil.NONE; + return headingMarkup.applyAnsi( + builder, + "\n------------------------------------------------------------\n", + value, + "\n\n"); } /** @@ -275,7 +283,9 @@ protected StringBuilder prettyAppendHeading(StringBuilder builder, String value) * @param name the property name * @param value the property value * @return the builder for method chaining + * @deprecated No replacement currently planned, if you use this, contact us. */ + @Deprecated(forRemoval = true) protected StringBuilder prettyAppendUnderlined( StringBuilder builder, String name, String value) { return builder.append(addIndentations(name)) @@ -294,7 +304,9 @@ protected StringBuilder prettyAppendUnderlined( * @param name the property name * @param value the boolean value * @return the builder for method chaining + * @deprecated No replacement currently planned, if you use this, contact us. */ + @Deprecated(forRemoval = true) protected StringBuilder prettyAppendUnderlined( StringBuilder builder, String name, boolean value) { return builder.append(addIndentations(name)) @@ -313,7 +325,9 @@ protected StringBuilder prettyAppendUnderlined( * @param name the property name * @param value the long value * @return the builder for method chaining + * @deprecated No replacement currently planned, if you use this, contact us. */ + @Deprecated(forRemoval = true) protected StringBuilder prettyAppendUnderlined(StringBuilder builder, String name, long value) { return builder.append(addIndentations(name)) .append(": ") @@ -333,16 +347,9 @@ protected StringBuilder prettyAppendUnderlined(StringBuilder builder, String nam */ protected StringBuilder prettyAppendSubheading(StringBuilder builder, String name) { depth = 1; - return builder.append("--|") - .append( - printColorful - ? AnsiColor.BOLD.getCode() - + AnsiColor.PURPLE.getCode() - + AnsiColor.UNDERLINE.getCode() - + name - + "\n\n" - + AnsiColor.RESET.getCode() - : name + "\n\n"); + Markup markup = + printColorful ? SemanticMarkup.REPORT_STRUCTURE_SUBHEADINGS : MarkupUtil.NONE; + return markup.applyAnsi(builder, "--|", name, "\n\n"); } /** @@ -354,16 +361,9 @@ protected StringBuilder prettyAppendSubheading(StringBuilder builder, String nam */ protected StringBuilder prettyAppendSubSubheading(StringBuilder builder, String name) { depth = 2; - return builder.append("----|") - .append( - printColorful - ? AnsiColor.BOLD.getCode() - + AnsiColor.PURPLE.getCode() - + AnsiColor.UNDERLINE.getCode() - + name - + "\n\n" - + AnsiColor.RESET.getCode() - : name + "\n\n"); + Markup markup = + printColorful ? SemanticMarkup.REPORT_STRUCTURE_SUBHEADINGS : MarkupUtil.NONE; + return markup.applyAnsi(builder, "----|", name, "\n\n"); } /** @@ -375,16 +375,9 @@ protected StringBuilder prettyAppendSubSubheading(StringBuilder builder, String */ protected StringBuilder prettyAppendSubSubSubheading(StringBuilder builder, String name) { depth = 3; - return builder.append("------|") - .append( - printColorful - ? AnsiColor.BOLD.getCode() - + AnsiColor.PURPLE.getCode() - + AnsiColor.UNDERLINE.getCode() - + name - + "\n\n" - + AnsiColor.RESET.getCode() - : name + "\n\n"); + Markup markup = + printColorful ? SemanticMarkup.REPORT_STRUCTURE_SUBHEADINGS : MarkupUtil.NONE; + return markup.applyAnsi(builder, "------|", name, "\n\n"); } /** diff --git a/src/main/java/de/rub/nds/scanner/core/report/container/HeadlineContainer.java b/src/main/java/de/rub/nds/scanner/core/report/container/HeadlineContainer.java index 36fe6cf9..524f8f2c 100644 --- a/src/main/java/de/rub/nds/scanner/core/report/container/HeadlineContainer.java +++ b/src/main/java/de/rub/nds/scanner/core/report/container/HeadlineContainer.java @@ -10,6 +10,7 @@ import de.rub.nds.scanner.core.config.ScannerDetail; import de.rub.nds.scanner.core.report.AnsiColor; +import de.rub.nds.scanner.core.report.markup.SemanticMarkup; /** * Container for displaying headlines in scanner reports. Supports different depth levels with @@ -53,8 +54,7 @@ public HeadlineContainer(String headline, ScannerDetail detail) { @Override public void print(StringBuilder builder, int depth, boolean useColor) { if (useColor) { - builder.append(AnsiColor.BOLD.getCode()); - builder.append(getColorByDepth(depth).getCode()); + builder.append(getColorByDepth(depth).getAnsiCode()); } if (depth == 0) { addHLine(builder); @@ -68,18 +68,12 @@ public void print(StringBuilder builder, int depth, boolean useColor) { builder.append("\n"); } - private static AnsiColor getColorByDepth(int depth) { + private static SemanticMarkup getColorByDepth(int depth) { switch (depth) { case 0: - return AnsiColor.PURPLE; - case 1: - return AnsiColor.BLUE; - case 2: - return AnsiColor.CYAN; - case 3: - return AnsiColor.WHITE; + return SemanticMarkup.REPORT_STRUCTURE_HEADING; default: - return AnsiColor.YELLOW; + return SemanticMarkup.REPORT_STRUCTURE_SUBHEADINGS; } } diff --git a/src/main/java/de/rub/nds/scanner/core/report/container/KeyValueContainer.java b/src/main/java/de/rub/nds/scanner/core/report/container/KeyValueContainer.java index b0f464d7..c7b32f5e 100644 --- a/src/main/java/de/rub/nds/scanner/core/report/container/KeyValueContainer.java +++ b/src/main/java/de/rub/nds/scanner/core/report/container/KeyValueContainer.java @@ -9,7 +9,7 @@ package de.rub.nds.scanner.core.report.container; import de.rub.nds.scanner.core.config.ScannerDetail; -import de.rub.nds.scanner.core.report.AnsiColor; +import de.rub.nds.scanner.core.report.markup.Markup; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -24,25 +24,25 @@ public class KeyValueContainer extends ReportContainer { private static final int PADDED_KEY_LENGTH = 30; private String key; - private AnsiColor keyColor; + private Markup keyMarkup; private String value; - private AnsiColor valueColor; + private Markup valueMarkup; /** * Creates a new KeyValueContainer with specified colors. * * @param key The key text to display - * @param keyColor The color for the key + * @param keyMarkup The color for the key * @param value The value text to display - * @param valueColor The color for the value + * @param valueMarkup The color for the value */ - public KeyValueContainer(String key, AnsiColor keyColor, String value, AnsiColor valueColor) { + public KeyValueContainer(String key, Markup keyMarkup, String value, Markup valueMarkup) { super(ScannerDetail.NORMAL); this.key = key; - this.keyColor = keyColor; + this.keyMarkup = keyMarkup; this.value = value; - this.valueColor = valueColor; + this.valueMarkup = valueMarkup; } /** @@ -56,9 +56,9 @@ public KeyValueContainer(String key, AnsiColor keyColor, String value, AnsiColor @Override public void print(StringBuilder builder, int depth, boolean useColor) { addDepth(builder, depth); - addColor(builder, keyColor, pad(key, PADDED_KEY_LENGTH), useColor); + addColor(builder, keyMarkup, pad(key, PADDED_KEY_LENGTH), useColor); builder.append(": "); - addColor(builder, valueColor, value, useColor); + addColor(builder, valueMarkup, value, useColor); builder.append("\n"); } @@ -97,21 +97,21 @@ public void setKey(String key) { } /** - * Gets the key color. + * Gets the key markup. * - * @return The ANSI color for the key + * @return The markup used for the key */ - public AnsiColor getKeyColor() { - return keyColor; + public Markup getKeyMarkup() { + return keyMarkup; } /** - * Sets the key color. + * Sets the key markup. * - * @param keyColor The new ANSI color for the key + * @param keyMarkup The new markup for the key */ - public void setKeyColor(AnsiColor keyColor) { - this.keyColor = keyColor; + public void setKeyMarkup(Markup keyMarkup) { + this.keyMarkup = keyMarkup; } /** @@ -133,20 +133,20 @@ public void setValue(String value) { } /** - * Gets the value color. + * Gets the value markup. * - * @return The ANSI color for the value + * @return The markup used for the value */ - public AnsiColor getValueColor() { - return valueColor; + public Markup getValueMarkup() { + return valueMarkup; } /** - * Sets the value color. + * Sets the value markup. * - * @param valueColor The new ANSI color for the value + * @param valueMarkup The new markup for the value */ - public void setValueColor(AnsiColor valueColor) { - this.valueColor = valueColor; + public void setValueMarkup(Markup valueMarkup) { + this.valueMarkup = valueMarkup; } } diff --git a/src/main/java/de/rub/nds/scanner/core/report/container/ReportContainer.java b/src/main/java/de/rub/nds/scanner/core/report/container/ReportContainer.java index d9878edd..f19ad1b8 100644 --- a/src/main/java/de/rub/nds/scanner/core/report/container/ReportContainer.java +++ b/src/main/java/de/rub/nds/scanner/core/report/container/ReportContainer.java @@ -9,7 +9,7 @@ package de.rub.nds.scanner.core.report.container; import de.rub.nds.scanner.core.config.ScannerDetail; -import de.rub.nds.scanner.core.report.AnsiColor; +import de.rub.nds.scanner.core.report.markup.Markup; /** * Abstract base class for all report containers in the scanner framework. Provides common @@ -69,15 +69,15 @@ protected StringBuilder addHeadlineDepth(StringBuilder builder, int depth) { * Adds colored text to the StringBuilder if color is enabled. * * @param builder The StringBuilder to append to - * @param color The ANSI color to apply + * @param markup The ANSI color to apply * @param text The text to colorize * @param useColor Whether to apply color codes * @return The modified StringBuilder for method chaining */ protected StringBuilder addColor( - StringBuilder builder, AnsiColor color, String text, boolean useColor) { + StringBuilder builder, Markup markup, String text, boolean useColor) { if (useColor) { - builder.append(color.getCode()).append(text).append(AnsiColor.RESET.getCode()); + markup.applyAnsi(builder, text); } else { builder.append(text); } diff --git a/src/main/java/de/rub/nds/scanner/core/report/container/TextContainer.java b/src/main/java/de/rub/nds/scanner/core/report/container/TextContainer.java index ee42c066..dd89c084 100644 --- a/src/main/java/de/rub/nds/scanner/core/report/container/TextContainer.java +++ b/src/main/java/de/rub/nds/scanner/core/report/container/TextContainer.java @@ -9,7 +9,7 @@ package de.rub.nds.scanner.core.report.container; import de.rub.nds.scanner.core.config.ScannerDetail; -import de.rub.nds.scanner.core.report.AnsiColor; +import de.rub.nds.scanner.core.report.markup.Markup; /** * Container for displaying simple text in scanner reports. Supports colored text output with @@ -18,31 +18,31 @@ public class TextContainer extends ReportContainer { private final String text; - private final AnsiColor color; + private final Markup markup; /** * Creates a new TextContainer with normal detail level. * * @param text The text to display - * @param color The ANSI color for the text + * @param markup The ANSI color for the text */ - public TextContainer(String text, AnsiColor color) { + public TextContainer(String text, Markup markup) { super(ScannerDetail.NORMAL); this.text = text; - this.color = color; + this.markup = markup; } /** * Creates a new TextContainer with specified detail level. * * @param text The text to display - * @param color The ANSI color for the text + * @param markup The ANSI color for the text * @param detail The detail level for this container */ - public TextContainer(String text, AnsiColor color, ScannerDetail detail) { + public TextContainer(String text, Markup markup, ScannerDetail detail) { super(detail); this.text = text; - this.color = color; + this.markup = markup; } /** @@ -67,7 +67,7 @@ public void print(StringBuilder builder, int depth, boolean useColor) { */ public void println(StringBuilder builder, int depth, boolean useColor) { addDepth(builder, depth); - addColor(builder, color, text, useColor); + addColor(builder, markup, text, useColor); } /** diff --git a/src/main/java/de/rub/nds/scanner/core/report/markup/Markup.java b/src/main/java/de/rub/nds/scanner/core/report/markup/Markup.java new file mode 100644 index 00000000..0b00f721 --- /dev/null +++ b/src/main/java/de/rub/nds/scanner/core/report/markup/Markup.java @@ -0,0 +1,15 @@ +/* + * Scanner Core - A Modular Framework for Probe Definition, Execution, and Result Analysis. + * + * Copyright 2017-2023 Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0.txt + */ +package de.rub.nds.scanner.core.report.markup; + +public interface Markup { + String applyAnsi(String text); + + StringBuilder applyAnsi(StringBuilder builder, String... texts); +} diff --git a/src/main/java/de/rub/nds/scanner/core/report/markup/MarkupUtil.java b/src/main/java/de/rub/nds/scanner/core/report/markup/MarkupUtil.java new file mode 100644 index 00000000..b3301c22 --- /dev/null +++ b/src/main/java/de/rub/nds/scanner/core/report/markup/MarkupUtil.java @@ -0,0 +1,28 @@ +/* + * Scanner Core - A Modular Framework for Probe Definition, Execution, and Result Analysis. + * + * Copyright 2017-2023 Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0.txt + */ +package de.rub.nds.scanner.core.report.markup; + +public class MarkupUtil { + private static class _None implements Markup { + @Override + public String applyAnsi(String text) { + return text; + } + + @Override + public StringBuilder applyAnsi(StringBuilder builder, String... texts) { + for (String text : texts) { + builder.append(text); + } + return builder; + } + } + + public static final Markup NONE = new _None(); +} diff --git a/src/main/java/de/rub/nds/scanner/core/report/markup/SemanticMarkup.java b/src/main/java/de/rub/nds/scanner/core/report/markup/SemanticMarkup.java new file mode 100644 index 00000000..78ea0607 --- /dev/null +++ b/src/main/java/de/rub/nds/scanner/core/report/markup/SemanticMarkup.java @@ -0,0 +1,98 @@ +/* + * Scanner Core - A Modular Framework for Probe Definition, Execution, and Result Analysis. + * + * Copyright 2017-2023 Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH + * + * Licensed under Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0.txt + */ +package de.rub.nds.scanner.core.report.markup; + +import de.rub.nds.scanner.core.report.AnsiColor; + +public enum SemanticMarkup implements Markup { + /** Represents a severely bad result, where the server is vulnerable. */ + RESULT_BAD(AnsiColor.RED), + /** Represents a bad result, where the server is not as secure as it could be. */ + RESULT_MEDIUM(AnsiColor.YELLOW), + /** Represents a result, where the scanner was unsure whether it is good or bad. */ + RESULT_UNSURE(AnsiColor.YELLOW_BACKGROUND), + /** Represents a good result, where the server is secure. */ + RESULT_GOOD(AnsiColor.GREEN), + /** Used for structuring the output of the report, e.g., for headings or separators. */ + REPORT_STRUCTURE_HEADING(AnsiColor.BOLD, AnsiColor.CYAN), + REPORT_STRUCTURE_SUBHEADINGS(AnsiColor.BOLD, AnsiColor.UNDERLINE, AnsiColor.CYAN), + REPORT_STRUCTURE_PARAGRAPH(AnsiColor.BOLD), + REPORT_STRUCTURE_TABLE_HEADING(AnsiColor.BOLD), + REPORT_STRUCTURE_LINK(AnsiColor.UNDERLINE), + /** + * Represents an information that led to the scanner being unable to determine the result + * because the server does not support a certain feature. + */ + SCANNER_INFO_SERVER_DEPENDENT(AnsiColor.BLUE), + /** + * Represents an error that led to the scanner being unable to determine the result because the + * server misbehaved. + */ + SCANNER_ERROR_SERVER_DEPENDENT(AnsiColor.BLUE_BACKGROUND), + /** + * Represents an information that led to the scanner being unable to determine the result + * because of an error during evaluation. + */ + SCANNER_ERROR(AnsiColor.PURPLE), + /** + * Represents an information that led to the scanner being unable to determine the result + * because of a programming error in the scanner. + */ + SCANNER_ERROR_PROGRAMMING(AnsiColor.PURPLE_BACKGROUND), + /** Represents a no markup. */ + NEUTRAL(); + + private final String RESET = AnsiColor.RESET.getCode(); + private final String ansiCode; + + SemanticMarkup(AnsiColor... ansiColor) { + if (ansiColor.length == 0) { + this.ansiCode = null; + } else { + StringBuilder codeBuilder = new StringBuilder(); + for (AnsiColor color : ansiColor) { + codeBuilder.append(color.getCode()); + } + this.ansiCode = codeBuilder.toString(); + } + } + + /** + * Gets the underlying ANSI code(s) associated with this markup. Prefer using the applyAnsi + * methods for applying formatting to text, as they handle reset codes automatically. + * + * @return The ANSI code as a string, or null if no formatting is applied + */ + public String getAnsiCode() { + return ansiCode; + } + + @Override + public String applyAnsi(String text) { + if (ansiCode == null) { + // no need to append a reset code if no color was applied + return text; + } + return ansiCode + text + RESET; + } + + @Override + public StringBuilder applyAnsi(StringBuilder builder, String... texts) { + if (ansiCode != null) { + builder.append(ansiCode); + } + for (String text : texts) { + builder.append(text); + } + if (ansiCode != null) { + builder.append(RESET); + } + return builder; + } +}