diff --git a/README.md b/README.md index 0b46ef9..9d50fc7 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ The data can be output in several different different forms: * **csv** - comma separated values format * **html** - HyperText Markup Language (HTML) format * **json** - JavaScript Object Notation (JSON) format +* **yaml** - Yet Another Markup Language (YAML) format Here's an example of the standard **text** output: ``` @@ -128,6 +129,22 @@ Using the **json** format (e.g. `OutputFormat.JSON`), you get the following outp ] ``` +Using the **yaml** format (e.g. `OutputFormat.YAML`), you get the following output: +``` +- - "Column A" + - "Column 2" + - "Column III" +- Column A: "A" + Column 2: "B" + Column III: "c" +- Column A: 1 + Column 2: 1024 + Column III: 65535 +- Column A: "Washington" + Column 2: "Adams" + Column III: "Jefferson" +``` + ### Alignment ASCII **text**, **rich**, and **html** support different alignments. Here's how you would update the above table: ```Java @@ -157,7 +174,7 @@ The **html** output will have the appropriate `style="text-align:xxx"` attribute ``` ### Text options -ASCII **text** and **rich** output can hide the header row, drop the border, draw a rule under data rows, and print a caption under the table. **csv**, **html**, and **json** ignore these settings. A `null` or empty caption is skipped. Any other caption is printed unchanged. +ASCII **text** and **rich** output can hide the header row, drop the border, draw a rule under data rows, and print a caption under the table. **csv**, **html**, **json**, and **yaml** ignore these settings. A `null` or empty caption is skipped. Any other caption is printed unchanged. `PrettyTable::setShowHeader(false)` omits the header. Column widths then come only from the data cells, so the columns can get narrower. A table built with no headers already skips that row. @@ -288,7 +305,7 @@ ASCII **text**, **rich**, and **html** remove redundancy by default. This de-dup | | | F | E | +------+------+------+------+ ``` -By default, the code recognizes the redundant fields and removes them from the rendering of the data. `PrettyTable::setRemoveRedundant(false)` keeps every repeated value for `toString()` and for `formattedString(OutputFormat)`. One call can still override that setting with `formattedString(OutputFormat, boolean)`. **csv** and **json** keep every cell either way. +By default, the code recognizes the redundant fields and removes them from the rendering of the data. `PrettyTable::setRemoveRedundant(false)` keeps every repeated value for `toString()` and for `formattedString(OutputFormat)`. One call can still override that setting with `formattedString(OutputFormat, boolean)`. **csv**, **json**, and **yaml** keep every cell either way. In this case, either of these calls: ```Java diff --git a/pom.xml b/pom.xml index a007e12..fe19ae6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ jar 1.1.0 rickwporter-prettytable - Displays List and Map object data in a variety of forms (e.g. text [ascii], rich [utf-8], csv, html, json) + Displays List and Map object data in a variety of forms (e.g. text [ascii], rich [utf-8], csv, html, json, yaml) https://github.com/rickwporter/prettytable-java @@ -37,6 +37,7 @@ 3.20.0 1.14.1 + 2.18.2 5.14.1 @@ -71,6 +72,13 @@ ${version.apache.csv} + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + ${version.jackson} + + org.junit.jupiter diff --git a/src/main/java/rickwporter/prettytable/OutputFormat.java b/src/main/java/rickwporter/prettytable/OutputFormat.java index 5955c01..15dc2be 100644 --- a/src/main/java/rickwporter/prettytable/OutputFormat.java +++ b/src/main/java/rickwporter/prettytable/OutputFormat.java @@ -6,4 +6,5 @@ public enum OutputFormat { CSV, HTML, JSON, + YAML, } diff --git a/src/main/java/rickwporter/prettytable/PrettyTable.java b/src/main/java/rickwporter/prettytable/PrettyTable.java index efe642c..c3cdce9 100644 --- a/src/main/java/rickwporter/prettytable/PrettyTable.java +++ b/src/main/java/rickwporter/prettytable/PrettyTable.java @@ -206,6 +206,10 @@ String toJson() { return formattedString(OutputFormat.JSON, false); } + String toYaml() { + return formattedString(OutputFormat.YAML, false); + } + public String formattedString(OutputFormat format) { return this.formattedString(format, this.omitRedundant); } @@ -220,6 +224,8 @@ public String formattedString(OutputFormat format, boolean removeRedundant) { renderer = new CsvTableRender(); } else if (format == OutputFormat.JSON) { renderer = new JsonTableRender(); + } else if (format == OutputFormat.YAML) { + renderer = new YamlTableRender(); } else if (format == OutputFormat.HTML) { renderer = new HtmlTableRender(); } else { diff --git a/src/main/java/rickwporter/prettytable/YamlTableRender.java b/src/main/java/rickwporter/prettytable/YamlTableRender.java new file mode 100644 index 0000000..3ceb96a --- /dev/null +++ b/src/main/java/rickwporter/prettytable/YamlTableRender.java @@ -0,0 +1,74 @@ +package rickwporter.prettytable; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; + +class YamlTableRender implements TableRenderInterface { + private static final YAMLMapper MAPPER = yamlMapper(); + + private static YAMLMapper yamlMapper() { + YAMLMapper mapper = new YAMLMapper(); + mapper.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER); + mapper.disable(YAMLGenerator.Feature.SPLIT_LINES); + mapper.enable(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE); + return mapper; + } + + private List toData(PrettyTable table) { + List data = new ArrayList(); + List headers = table.getHeaders(); + if (!headers.isEmpty()) { + data.add(new ArrayList(headers)); + } + for (List row : table.getRows()) { + if (headers.isEmpty()) { + List values = new ArrayList(); + for (Object cell : row) { + values.add(this.cellValue(cell)); + } + data.add(values); + } else { + Map values = new LinkedHashMap(); + for (int column = 0; column < row.size(); column++) { + values.put(headers.get(column), this.cellValue(row.get(column))); + } + data.add(values); + } + } + return data; + } + + private Object cellValue(Object cell) { + if (cell instanceof PrettyTable) { + PrettyTable nested = (PrettyTable) cell; + if (nested.getDefaultOutput() == OutputFormat.YAML) { + return this.toData(nested); + } + return nested.toString(); + } + if (cell instanceof Integer) { + return cell; + } + String text = cell.toString(); + try { + return Integer.valueOf(text); + } catch (NumberFormatException ex) { + return text; + } + } + + @Override + public String render(PrettyTable table, boolean removeRedundant) { + try { + return MAPPER.writeValueAsString(this.toData(table)); + } catch (IOException ex) { + throw new IllegalStateException("Unable to render table as YAML", ex); + } + } +} diff --git a/src/test/java/rickwporter/prettytable/PrettyTableTest.java b/src/test/java/rickwporter/prettytable/PrettyTableTest.java index e4a998f..c9fa111 100644 --- a/src/test/java/rickwporter/prettytable/PrettyTableTest.java +++ b/src/test/java/rickwporter/prettytable/PrettyTableTest.java @@ -18,6 +18,7 @@ public class PrettyTableTest { put(OutputFormat.CSV, "csv"); put(OutputFormat.HTML, "html"); put(OutputFormat.JSON, "json"); + put(OutputFormat.YAML, "yaml"); }}; final String loadFileContent(String filename) { @@ -91,6 +92,19 @@ public void testBasicJson() { Assertions.assertEquals(expected, result); } + @Test + public void testBasicYaml() { + PrettyTable table = createBasicTable(); + String result = table.toYaml(); + String expected = loadFileContent("PrettyTable_basic.yaml"); + Assertions.assertEquals(expected, result); + result = table.formattedString(OutputFormat.YAML); + Assertions.assertEquals(expected, result); + table.setOutputFormat(OutputFormat.YAML); + result = table.toString(); + Assertions.assertEquals(expected, result); + } + @Test public void testBasicText() { PrettyTable table = createBasicTable(); @@ -158,6 +172,7 @@ public void testUnformatted() { Map formats = new HashMap() {{ put(OutputFormat.CSV, "csv"); put(OutputFormat.JSON, "json"); + put(OutputFormat.YAML, "yaml"); }}; PrettyTable table = createFormattedTable(); for (Map.Entry entry: formats.entrySet()) { @@ -283,6 +298,10 @@ public void testNumberAsStringJson() { String expected = loadFileContent("PrettyTable_stringnum.json"); String result = table.formattedString(OutputFormat.JSON); Assertions.assertEquals(expected, result); + + expected = loadFileContent("PrettyTable_stringnum.yaml"); + result = table.formattedString(OutputFormat.YAML); + Assertions.assertEquals(expected, result); } @Test @@ -330,6 +349,17 @@ public void testSort() { Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, () -> table.sortByHeader("Q")); } + @Test + public void testYamlEncoding() { + PrettyTable table = new PrettyTable("note", "value"); + table.addRow("a: b", "say \"hi\""); + table.addRow("line1\nline2", "plain"); + table.addRow("crlf\r\nhere", "tab\there"); + + String expected = loadFileContent("PrettyTable_yaml_encoded.yaml"); + Assertions.assertEquals(expected, table.formattedString(OutputFormat.YAML)); + } + @Test public void testCsvEncoding() { // CSV encoding needs to update/encapsulate special characters @@ -401,6 +431,12 @@ public void testNested() { big.setOutputFormat(OutputFormat.JSON); Assertions.assertEquals(expected, big.toString()); + expected = loadFileContent("PrettyTable_nested_mixed.yaml"); + letters.setOutputFormat(OutputFormat.HTML); + numbers.setOutputFormat(OutputFormat.CSV); + big.setOutputFormat(OutputFormat.YAML); + Assertions.assertEquals(expected, big.toString()); + expected = loadFileContent("PrettyTable_nested_mixed.txt"); letters.setOutputFormat(OutputFormat.JSON); numbers.setOutputFormat(OutputFormat.CSV); @@ -508,10 +544,11 @@ public void testRowDividersOption() { Assertions.assertEquals(lineless, table.formattedString(OutputFormat.TEXT, true)); table.setShowLines(true); - // csv, html, and json ignore the setting + // csv, html, json, and yaml ignore the setting Assertions.assertEquals(loadFileContent("PrettyTable_basic.csv"), table.formattedString(OutputFormat.CSV)); Assertions.assertEquals(loadFileContent("PrettyTable_basic.html"), table.formattedString(OutputFormat.HTML)); Assertions.assertEquals(loadFileContent("PrettyTable_basic.json"), table.formattedString(OutputFormat.JSON)); + Assertions.assertEquals(loadFileContent("PrettyTable_basic.yaml"), table.formattedString(OutputFormat.YAML)); table.setOutputFormat(OutputFormat.HTML); Assertions.assertEquals(loadFileContent("PrettyTable_basic.html"), table.toString()); @@ -565,14 +602,17 @@ public void testRemoveRedundantOption() { Assertions.assertEquals(fullRich, table.formattedString(OutputFormat.RICH, false)); Assertions.assertEquals(fullHtml, table.toHtml(false)); - // CSV and JSON keep every value either way + // CSV, JSON, and YAML keep every value either way String csv = loadFileContent("PrettyTable_duplicate.csv"); String json = loadFileContent("PrettyTable_duplicate.json"); + String yaml = loadFileContent("PrettyTable_duplicate.yaml"); Assertions.assertEquals(csv, table.formattedString(OutputFormat.CSV)); Assertions.assertEquals(json, table.formattedString(OutputFormat.JSON)); + Assertions.assertEquals(yaml, table.formattedString(OutputFormat.YAML)); table.setRemoveRedundant(false); Assertions.assertEquals(csv, table.formattedString(OutputFormat.CSV)); Assertions.assertEquals(json, table.formattedString(OutputFormat.JSON)); + Assertions.assertEquals(yaml, table.formattedString(OutputFormat.YAML)); } @Test diff --git a/src/test/java/rickwporter/prettytable/TableBuilderTest.java b/src/test/java/rickwporter/prettytable/TableBuilderTest.java index b941a5a..4e070fa 100644 --- a/src/test/java/rickwporter/prettytable/TableBuilderTest.java +++ b/src/test/java/rickwporter/prettytable/TableBuilderTest.java @@ -239,6 +239,10 @@ public void testInnerListFormats() { expected = this.loadFileContent("TableBuilder_inner.json"); Assertions.assertEquals(expected, actual); + actual = builder.build(OutputFormat.YAML, this.innerList()).toString(); + expected = this.loadFileContent("TableBuilder_inner.yaml"); + Assertions.assertEquals(expected, actual); + actual = builder.build(OutputFormat.HTML, this.innerList()).toString(); expected = this.loadFileContent("TableBuilder_inner.html"); Assertions.assertEquals(expected, actual); diff --git a/src/test/resources/PrettyTable_basic.yaml b/src/test/resources/PrettyTable_basic.yaml new file mode 100644 index 0000000..4935637 --- /dev/null +++ b/src/test/resources/PrettyTable_basic.yaml @@ -0,0 +1,12 @@ +- - "Column A" + - "Column 2" + - "Column III" +- Column A: "A" + Column 2: "B" + Column III: "c" +- Column A: 1 + Column 2: 1024 + Column III: 65535 +- Column A: "Washington" + Column 2: "Adams" + Column III: "Jefferson" diff --git a/src/test/resources/PrettyTable_duplicate.yaml b/src/test/resources/PrettyTable_duplicate.yaml new file mode 100644 index 0000000..1eae527 --- /dev/null +++ b/src/test/resources/PrettyTable_duplicate.yaml @@ -0,0 +1,20 @@ +- - "Col1" + - "Col2" + - "Col3" + - "Col4" +- Col1: "A" + Col2: "B" + Col3: "C" + Col4: "D" +- Col1: "A" + Col2: "B" + Col3: "C" + Col4: "Z" +- Col1: "A" + Col2: "B" + Col3: "F" + Col4: "E" +- Col1: "A" + Col2: "B" + Col3: "F" + Col4: "E" diff --git a/src/test/resources/PrettyTable_empty.yaml b/src/test/resources/PrettyTable_empty.yaml new file mode 100644 index 0000000..00b8b23 --- /dev/null +++ b/src/test/resources/PrettyTable_empty.yaml @@ -0,0 +1,4 @@ +- - "Col1" + - "Col2" + - "Col3" + - "Col4" diff --git a/src/test/resources/PrettyTable_nested.yaml b/src/test/resources/PrettyTable_nested.yaml new file mode 100644 index 0000000..7cdfa26 --- /dev/null +++ b/src/test/resources/PrettyTable_nested.yaml @@ -0,0 +1,30 @@ +- - "Table name" + - "Data" +- Table name: "Letters" + Data: + - - "A" + - "B" + - "C" + - A: "abc" + B: "def" + C: "ghij" + - A: "klmn" + B: "o" + C: "pqr" + - A: "st" + B: "uvx" + C: "yz" +- Table name: "Numbers" + Data: + - - "First" + - "Second" + - "Third" + - First: 123 + Second: 4567 + Third: 89012 + - First: 3456 + Second: 78 + Third: 901 + - First: 23456 + Second: 7890 + Third: 12 diff --git a/src/test/resources/PrettyTable_nested_mixed.yaml b/src/test/resources/PrettyTable_nested_mixed.yaml new file mode 100644 index 0000000..8967fd6 --- /dev/null +++ b/src/test/resources/PrettyTable_nested_mixed.yaml @@ -0,0 +1,36 @@ +- - "Table name" + - "Data" +- Table name: "Letters" + Data: | + + + + + + + + + + + + + + + + + + + + + + + + + +
ABC
abcdefghij
klmnopqr
stuvxyz
+- Table name: "Numbers" + Data: | + First,Second,Third + 123,4567,89012 + 3456,78,901 + 23456,7890,12 diff --git a/src/test/resources/PrettyTable_no_headers.yaml b/src/test/resources/PrettyTable_no_headers.yaml new file mode 100644 index 0000000..aaea50e --- /dev/null +++ b/src/test/resources/PrettyTable_no_headers.yaml @@ -0,0 +1,9 @@ +- - "A" + - "B" + - "c" +- - 1 + - 1024 + - 65535 +- - "Washington" + - "Adams" + - "Jefferson" diff --git a/src/test/resources/PrettyTable_stringnum.yaml b/src/test/resources/PrettyTable_stringnum.yaml new file mode 100644 index 0000000..9d65862 --- /dev/null +++ b/src/test/resources/PrettyTable_stringnum.yaml @@ -0,0 +1,6 @@ +- - "Col 1" + - "Col 2" + - "3" +- Col 1: "a" + Col 2: 9 + "3": 11 diff --git a/src/test/resources/PrettyTable_yaml_encoded.yaml b/src/test/resources/PrettyTable_yaml_encoded.yaml new file mode 100644 index 0000000..8f0488b --- /dev/null +++ b/src/test/resources/PrettyTable_yaml_encoded.yaml @@ -0,0 +1,10 @@ +- - "note" + - "value" +- note: "a: b" + value: "say \"hi\"" +- note: |- + line1 + line2 + value: "plain" +- note: "crlf\r\nhere" + value: "tab\there" diff --git a/src/test/resources/TableBuilder_inner.yaml b/src/test/resources/TableBuilder_inner.yaml new file mode 100644 index 0000000..94de1c1 --- /dev/null +++ b/src/test/resources/TableBuilder_inner.yaml @@ -0,0 +1,42 @@ +- - "Property" + - "Value" +- Property: "prop 1" + Value: "simple" +- Property: "prOp B" + Value: + - - "Name" + - "Properties" + - Name: "sna" + Properties: + - - "Property" + - "Value" + - Property: "abc" + Value: "def" + - Property: "ghi" + Value: "True" + - Name: "foo" + Properties: + - - "Property" + - "Value" + - Property: "abc" + Value: "def" + - Property: "ghi" + Value: "None" + - Name: "bar" + Properties: + - - "Property" + - "Value" + - Property: "abc" + Value: "def" + - Property: "ghi" + Value: "1.2345" + - Name: "Unknown" + Properties: + - - "Property" + - "Value" + - Property: "abc" + Value: "def" + - Property: "ghi" + Value: "blah" +- Property: "Prop III" + Value: "None"