### Question:
DefaultHtmlSerializer implements HtmlSerializer { public String serialize(Document doc) { try { StringWriter sw = HtmlSerialization.createWriter(doc); if (doc.getDoctype() != null) { HtmlSerialization.outputDocType(doc.getDoctype(), sw); } this.serialize(doc, sw); return sw.toString(); } catch (IOException ioe) { return null; } }  String serialize(Document doc); void serialize(Node n, Appendable output);  }### Answer:
@Test public void testComplicatedSerialize() throws Exception { String txt = "<!DOCTYPE html PUBLIC \"- + " \"http: + "<html xml:lang=\"en\" xmlns=\"http: + "<head><title>Apache Shindig!</title></head>" + "<body class=\"composite\">\n" + " <div id=\"bodyColumn\">hello\n" + " <div id=\"contentBox\"></div> \n" + " <div class=\"clear\"><hr></div> \n" + " </div>\n" + "</body></html>"; NekoSimplifiedHtmlParser parser = new NekoSimplifiedHtmlParser( new ParseModule.DOMImplementationProvider().get()); Document doc = parser.parseDom(txt); DefaultHtmlSerializer serializer = new DefaultHtmlSerializer(); assertEquals("Serialized full document", txt, serializer.serialize(doc)); }
@Test public void testComments() throws Exception { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); doc.appendChild(doc.createElement("ABC")); doc.appendChild(doc.createComment("XYZ")); DefaultHtmlSerializer serializer = new DefaultHtmlSerializer(); assertEquals("Comment is preserved", "<ABC></ABC><!--XYZ-->", serializer.serialize(doc)); }
@Test public void testEntities() throws Exception { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element element = doc.createElement("abc"); element.setAttribute("a", "\\x3e\">"); doc.appendChild(element); DefaultHtmlSerializer serializer = new DefaultHtmlSerializer(); assertEquals("Entities escaped", "<abc a=\"\\x3e&#34;&gt;\"></abc>", serializer.serialize(doc)); }
@Test public void testHrefEntities() throws Exception { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element element = doc.createElement("a"); element.setAttribute("href", "http: doc.appendChild(element); DefaultHtmlSerializer serializer = new DefaultHtmlSerializer(); assertEquals("href entities escaped", "<a href=\"http: serializer.serialize(doc)); }
@Test public void testDataTemplateTags() throws Exception { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element element = doc.createElement("osdata"); element.setAttribute("xmlns:foo", "#foo"); doc.appendChild(element); DefaultHtmlSerializer serializer = new DefaultHtmlSerializer(); assertEquals("OSData normalized", "<script type=\"text/os-data\" xmlns:foo=\"#foo\"></script>", serializer.serialize(doc)); }