HtmlWriter implements DataWriter<HtmlWriteOptions> { public void write(Table table, HtmlWriteOptions options) throws IOException { ElementCreator elements = options.elementCreator(); Element html = elements.create("table"); html.appendChild(header(table.columns(), elements)); Element tbody = elements.create("tbody"); html.appendChild(tbody); for (int row = 0; row < table.rowCount(); row++) { tbody.appendChild(row(row, table, elements, options)); } try (Writer writer = options.destination().createWriter()) { writer.write(html.toString()); } }  static void register(WriterRegistry registry); void write(Table table, HtmlWriteOptions options); @Override void write(Table table, Destination dest);  }
@Test public void basic() { String output = table.write().toString("html"); assertEquals( "<table>" + LINE_END + " <thead>" + LINE_END + " <tr>" + LINE_END + " <th>v</th>" + LINE_END + " <th>v2</th>" + LINE_END + " </tr>" + LINE_END + " </thead>" + LINE_END + " <tbody>" + LINE_END + " <tr>" + LINE_END + " <td>1</td>" + LINE_END + " <td>1</td>" + LINE_END + " </tr>" + LINE_END + " <tr>" + LINE_END + " <td>2</td>" + LINE_END + " <td>2</td>" + LINE_END + " </tr>" + LINE_END + " <tr>" + LINE_END + " <td></td>" + LINE_END + " <td></td>" + LINE_END + " </tr>" + LINE_END + " </tbody>" + LINE_END + "</table>", output); }
@Test public void alternatingRows() throws IOException { OutputStream baos = new ByteArrayOutputStream(); ElementCreator elementCreator = (elementName, column, row) -> { Element element = new Element(elementName); if (elementName.equals("tr") && row != null) { return element.addClass(row % 2 == 0 ? "even" : "odd"); } return element; }; table .write() .usingOptions(HtmlWriteOptions.builder(baos).elementCreator(elementCreator).build()); String output = baos.toString(); assertEquals( "<table>" + LINE_END + " <thead>" + LINE_END + " <tr>" + LINE_END + " <th>v</th>" + LINE_END + " <th>v2</th>" + LINE_END + " </tr>" + LINE_END + " </thead>" + LINE_END + " <tbody>" + LINE_END + " <tr class=\"even\">" + LINE_END + " <td>1</td>" + LINE_END + " <td>1</td>" + LINE_END + " </tr>" + LINE_END + " <tr class=\"odd\">" + LINE_END + " <td>2</td>" + LINE_END + " <td>2</td>" + LINE_END + " </tr>" + LINE_END + " <tr class=\"even\">" + LINE_END + " <td></td>" + LINE_END + " <td></td>" + LINE_END + " </tr>" + LINE_END + " </tbody>" + LINE_END + "</table>", output); }
@Test public void noEscape() throws IOException { String[] data = {"<p>foo</p>"}; Table table = Table.create("t", StringColumn.create("data", data)); OutputStream baos = new ByteArrayOutputStream(); table.write().usingOptions(HtmlWriteOptions.builder(baos).escapeText(false).build()); String output = baos.toString(); assertEquals( "<table>" + LINE_END + " <thead>" + LINE_END + " <tr>" + LINE_END + " <th>data</th>" + LINE_END + " </tr>" + LINE_END + " </thead>" + LINE_END + " <tbody>" + LINE_END + " <tr>" + LINE_END + " <td><p>foo</p></td>" + LINE_END + " </tr>" + LINE_END + " </tbody>" + LINE_END + "</table>", output); }
@Test public void escape() throws IOException { String[] data = {"<p>foo</p>"}; Table table = Table.create("t", StringColumn.create("data", data)); OutputStream baos = new ByteArrayOutputStream(); table.write().usingOptions(HtmlWriteOptions.builder(baos).build()); String output = baos.toString(); assertEquals( "<table>" + LINE_END + " <thead>" + LINE_END + " <tr>" + LINE_END + " <th>data</th>" + LINE_END + " </tr>" + LINE_END + " </thead>" + LINE_END + " <tbody>" + LINE_END + " <tr>" + LINE_END + " <td>&lt;p&gt;foo&lt;/p&gt;</td>" + LINE_END + " </tr>" + LINE_END + " </tbody>" + LINE_END + "</table>", output); }