FormattedText implements TextProcessor { @Override public String getSimpleHtml(String rawText) { rawText = ReferenceTagger.markRefs(rawText); Document jsoupDoc = Jsoup.parse("<body>" + rawText + "</body>", "", Parser.xmlParser()); jsoupDoc.outputSettings().prettyPrint(false).charset(StandardCharsets.UTF_16); Elements figEls = jsoupDoc.select("a.figref"); for (int i = 1; i <= figEls.size(); i++) { Element element = figEls.get(i - 1); element.attr("id", "FR-" + Strings.padStart(String.valueOf(i), 4, '0')); } Elements headerEls = jsoupDoc.select("PAC"); for (int i = 1; i <= headerEls.size(); i++) { Element element = headerEls.get(i - 1); element.attr("id", "H-" + Strings.padStart(String.valueOf(i), 4, '0')); element.tagName("h2"); } jsoupDoc.select("PAR").tagName("p"); for (int j = 0; j < 4; j++) { jsoupDoc.select("PA" + j).tagName("p").attr("level", String.valueOf(j)); } jsoupDoc.select("PAL").tagName("p"); Elements parEls = jsoupDoc.select("p"); for (int i = 1; i <= parEls.size(); i++) { Element element = parEls.get(i - 1); element.attr("id", "P-" + Strings.padStart(String.valueOf(i), 4, '0')); } Elements tblEls = jsoupDoc.select("TBL"); for (int i = 1; i <= tblEls.size(); i++) { Element tbl = tblEls.get(i - 1); tbl.attr("id", "P-" + Strings.padStart(String.valueOf(i), 4, '0')); tbl.tagName("pre"); tbl.attr("class", "freetext-table"); Element par = jsoupDoc.createElement("p"); tbl.replaceWith(par); par.appendChild(tbl); } String textStr = jsoupDoc.outerHtml(); textStr = textStr.replaceAll("\\\\n", "\n"); Whitelist whitelist = Whitelist.none(); whitelist.addTags(HTML_WHITELIST); whitelist.addAttributes(":all", HTML_WHITELIST_ATTRIB); OutputSettings outSettings = new Document.OutputSettings(); outSettings.charset(Charsets.UTF_16); outSettings.prettyPrint(false); Cleaner cleaner = new Cleaner(whitelist); Document clean = cleaner.clean(jsoupDoc); clean.outputSettings(outSettings); return clean.body().html(); }  @Override String getPlainText(String rawText, FreetextConfig textConfig); @Override String getSimpleHtml(String rawText); @Override List<String> getParagraphText(String rawText);  }
@Test public void SimpleHtml() { FormattedText format = new FormattedText(); StringBuilder stb = new StringBuilder(); stb.append("<PAC>SECTION TITLE</PAC>\n"); stb.append("<PAR>Paragraph text, referenceing FIG. 1 is a side elevational view</PAR>\n"); stb.append("<PAR>More text now referenceing FIG. 2B is a top view</PAR>\n"); StringBuilder expectStb = new StringBuilder(); expectStb.append("<h2 id=\"H-0001\">SECTION TITLE</h2>\n"); expectStb.append("<p id=\"P-0001\">Paragraph text, referenceing <a class=\"figref\" idref=\"FIG-1\" id=\"FR-0001\">FIG-1</a> is a side elevational view</p>\n"); expectStb.append("<p id=\"P-0002\">More text now referenceing <a class=\"figref\" idref=\"FIG-2B\" id=\"FR-0002\">FIG-2B</a> is a top view</p>\n"); String expect = expectStb.toString(); String actual = format.getSimpleHtml(stb.toString()); assertEquals(expect, actual); }