/** * Retrieves the current content for this object in String form. * If content has been retrieved in parse tree form and has * been edited, the String form is computed from the parse tree by * rendering it. It is <b>strongly</b> encouraged to avoid switching * between retrieval of parse tree (through {@code getParseTree}), * with subsequent edits and retrieval of String contents to avoid * repeated serialization and deserialization. * As a final fallback, if content has been set as bytes, interprets * them as a UTF8 String. * @return Renderable/active content. */ public String getContent() { if (content == null) { if (contentSource != null) { content = contentSource.getResponseAsString(); // Clear on first use contentSource = null; } else if (document != null) { content = HtmlSerialization.serialize(document); } else if (contentBytes != null) { Charset useEncoding = contentEncoding != null ? contentEncoding : Charsets.UTF_8; content = useEncoding.decode(ByteBuffer.wrap(contentBytes)).toString(); } } return content; }
/** * Retrieves the current content for this object in String form. * If content has been retrieved in parse tree form and has * been edited, the String form is computed from the parse tree by * rendering it. It is <b>strongly</b> encouraged to avoid switching * between retrieval of parse tree (through {@code getParseTree}), * with subsequent edits and retrieval of String contents to avoid * repeated serialization and deserialization. * As a final fallback, if content has been set as bytes, interprets * them as a UTF8 String. * @return Renderable/active content. */ public String getContent() { if (content == null) { if (contentSource != null) { content = contentSource.getResponseAsString(); // Clear on first use contentSource = null; } else if (document != null) { content = HtmlSerialization.serialize(document); } else if (contentBytes != null) { Charset useEncoding = contentEncoding != null ? contentEncoding : Charsets.UTF_8; content = useEncoding.decode(ByteBuffer.wrap(contentBytes)).toString(); } } return content; }
@Test public void testSocialTemplateSerialization() { String content = HtmlSerialization.serialize(document); assertTrue("Empty elements not preserved as XML inside template", content.contains("<img/>")); }
protected void parseAndCompareBalanced(String content, String expected, GadgetHtmlParser parser) throws Exception { Document document = parser.parseDom(content); expected = expected.replace(EOL, "\n"); String serialized = HtmlSerialization.serialize(document); assertHtmlEquals(expected, serialized); }
protected void parseAndCompareBalanced(String content, String expected, GadgetHtmlParser parser) throws Exception { Document document = parser.parseDom(content); expected = expected.replace(EOL, "\n"); String serialized = HtmlSerialization.serialize(document); assertHtmlEquals(expected, serialized); }
protected void parseAndCompareBalanced(String content, String expected, GadgetHtmlParser parser) throws Exception { Document document = parser.parseDom(content); expected = expected.replace(EOL, "\n"); String serialized = HtmlSerialization.serialize(document); assertHtmlEquals(expected, serialized); }
@Test public void testSocialTemplateSerialization() { String content = HtmlSerialization.serialize(document); assertTrue("Empty elements not preserved as XML inside template", content.contains("<img/>")); }
@Test public void testSocialTemplateSerialization() { String content = HtmlSerialization.serialize(document); assertTrue("Empty elements not preserved as XML inside template", content.contains("<img/>")); }
@Test public void testInvalid() throws Exception { String content = "<html><div id=\"div_super\" class=\"div_super\" valign:\"middle\"></div></html>"; Document doc = parser.parseDom(content); // Returns a bare Document with error text in it. Node body = doc.getElementsByTagName("body").item(0); assertTrue(body.getTextContent().contains("INVALID_CHARACTER_ERR")); assertTrue(body.getTextContent().contains( "Around ...<div id=\"div_super\" class=\"div_super\"...")); // Verify Serialization: assertTrue(HtmlSerialization.serialize(doc).contains("INVALID_CHARACTER_ERR")); }
@Test public void testInvalid() throws Exception { String content = "<html><div id=\"div_super\" class=\"div_super\" valign:\"middle\"></div></html>"; Document doc = parser.parseDom(content); // Returns a bare Document with error text in it. Node body = doc.getElementsByTagName("body").item(0); assertTrue(body.getTextContent().contains("INVALID_CHARACTER_ERR")); assertTrue(body.getTextContent().contains( "Around ...<div id=\"div_super\" class=\"div_super\"...")); // Verify Serialization: assertTrue(HtmlSerialization.serialize(doc).contains("INVALID_CHARACTER_ERR")); }
@Test public void testInvalid() throws Exception { String content = "<html><div id=\"div_super\" class=\"div_super\" valign:\"middle\"></div></html>"; Document doc = parser.parseDom(content); // Returns a bare Document with error text in it. Node body = doc.getElementsByTagName("body").item(0); assertTrue(body.getTextContent().contains("INVALID_CHARACTER_ERR")); assertTrue(body.getTextContent().contains( "Around ...<div id=\"div_super\" class=\"div_super\"...")); // Verify Serialization: assertTrue(HtmlSerialization.serialize(doc).contains("INVALID_CHARACTER_ERR")); }
private void timeParseDomSerialize(GadgetHtmlParser parser) throws GadgetException { org.w3c.dom.Document document = parser.parseDom(content); try { long parseStart = System.currentTimeMillis(); for (int i = 0; i < numRuns; ++i) { HtmlSerialization.serialize(document); } long parseMillis = System.currentTimeMillis() - parseStart; output("Serializing [" + parseMillis + " ms total: " + ((double) parseMillis) / numRuns + "ms/run]"); } catch (Exception e) { throw new GadgetException(GadgetException.Code.HTML_PARSE_ERROR, e); } try { // Create an "identity" transformer - copies input to output Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.METHOD, "html"); long parseStart = System.currentTimeMillis(); for (int i = 0; i < numRuns; ++i) { StringWriter sw = new StringWriter((content.length() * 11) / 10); t.transform(new DOMSource(document), new StreamResult(sw)); sw.toString(); } long parseMillis = System.currentTimeMillis() - parseStart; output("Serializing DOM Transformer [" + parseMillis + " ms total: " + ((double) parseMillis) / numRuns + "ms/run]"); } catch (Exception e) { throw new GadgetException(GadgetException.Code.HTML_PARSE_ERROR, e); } }
long parseStart = System.currentTimeMillis(); for (int i = 0; i < numRuns; ++i) { HtmlSerialization.serialize(document);
protected byte[] getRawContentBytes() { if (contentBytes == null) { if (contentSource != null) { try { setContentBytesState(IOUtils.toByteArray(contentSource.getResponse()), contentSource.getEncodingCharset()); contentSource = null; } catch (IOException e) { // Doesn't occur; responseBytes wrapped as a ByteArrayInputStream. } } else if (content != null) { // If retrieving a String here, we've already converted to UTF8. // Be sure to reflect this when setting bytes. // In the case of HttpResponseBuilder, this re-sets charset in Content-Type // to UTF-8 rather than whatever it was before. We do this to standardize // on UTF-8 for all String handling. setContentBytesState(CharsetUtil.getUtf8Bytes(content), Charsets.UTF_8); } else if (document != null) { setContentBytesState( CharsetUtil.getUtf8Bytes(HtmlSerialization.serialize(document)), Charsets.UTF_8); } } return contentBytes; }
protected byte[] getRawContentBytes() { if (contentBytes == null) { if (contentSource != null) { try { setContentBytesState(IOUtils.toByteArray(contentSource.getResponse()), contentSource.getEncodingCharset()); contentSource = null; } catch (IOException e) { // Doesn't occur; responseBytes wrapped as a ByteArrayInputStream. } } else if (content != null) { // If retrieving a String here, we've already converted to UTF8. // Be sure to reflect this when setting bytes. // In the case of HttpResponseBuilder, this re-sets charset in Content-Type // to UTF-8 rather than whatever it was before. We do this to standardize // on UTF-8 for all String handling. setContentBytesState(CharsetUtil.getUtf8Bytes(content), Charsets.UTF_8); } else if (document != null) { setContentBytesState( CharsetUtil.getUtf8Bytes(HtmlSerialization.serialize(document)), Charsets.UTF_8); } } return contentBytes; }
protected byte[] getRawContentBytes() { if (contentBytes == null) { if (contentSource != null) { try { setContentBytesState(IOUtils.toByteArray(contentSource.getResponse()), contentSource.getEncodingCharset()); contentSource = null; } catch (IOException e) { // Doesn't occur; responseBytes wrapped as a ByteArrayInputStream. } } else if (content != null) { // If retrieving a String here, we've already converted to UTF8. // Be sure to reflect this when setting bytes. // In the case of HttpResponseBuilder, this re-sets charset in Content-Type // to UTF-8 rather than whatever it was before. We do this to standardize // on UTF-8 for all String handling. setContentBytesState(CharsetUtil.getUtf8Bytes(content), Charsets.UTF_8); } else if (document != null) { setContentBytesState( CharsetUtil.getUtf8Bytes(HtmlSerialization.serialize(document)), Charsets.UTF_8); } } return contentBytes; }
@Test public void testSerializeHtml() throws Exception { String markup = "<!DOCTYPE html>\n" + "<html><head><title>Apache Shindig!</title></head>" + "<body>" + "<div xmlns:osx=\"http://ns.opensocial.org/2008/extensions\">" + "<osx:NavigateToApp>\n" + "<img border=\"0\" src=\"foo.gif\">\n" + "</osx:NavigateToApp>\n" + "</div>" + "</body></html>"; for(GadgetHtmlParser parser : parsers) { Document doc = parser.parseDom(markup); String result = HtmlSerialization.serialize(doc); assertEquals(markup, result); } } }
@Test public void testSerializeHtml() throws Exception { String markup = "<!DOCTYPE html>\n" + "<html><head><title>Apache Shindig!</title></head>" + "<body>" + "<div xmlns:osx=\"http://ns.opensocial.org/2008/extensions\">" + "<osx:NavigateToApp>\n" + "<img border=\"0\" src=\"foo.gif\">\n" + "</osx:NavigateToApp>\n" + "</div>" + "</body></html>"; for(GadgetHtmlParser parser : parsers) { Document doc = parser.parseDom(markup); String result = HtmlSerialization.serialize(doc); assertEquals(markup, result); } } }
@Test @Ignore("Caja parses OS script tags but does not serialize them to their original form") public void testSerialize() throws Exception { String markup = "<!DOCTYPE html>\n" + "<html><head><title>Apache Shindig!</title></head>" + "<body>" + "<script type=\"text/os-data\" xmlns:os=\"http://ns.opensocial.org/2008/markup\">" + " <os:PeopleRequest groupId=\"@friends\" key=\"friends\" userId=\"@viewer\"></os:PeopleRequest>\n" + "</script>" + "<script require=\"friends\" type=\"text/os-template\">\n" + " <ul><li repeat=\"${friends}\">\n" + " <span id=\"id${Context.Index}\">${Cur.name.givenName}</span>\n" + " </li></ul>" + "</script>" + "</body></html>"; for(GadgetHtmlParser parser : parsers) { Document doc = parser.parseDom(markup); String result = HtmlSerialization.serialize(doc); assertEquals(markup, result); } }
@Test @Ignore("Caja parses OS script tags but does not serialize them to their original form") public void testSerialize() throws Exception { String markup = "<!DOCTYPE html>\n" + "<html><head><title>Apache Shindig!</title></head>" + "<body>" + "<script type=\"text/os-data\" xmlns:os=\"http://ns.opensocial.org/2008/markup\">" + " <os:PeopleRequest groupId=\"@friends\" key=\"friends\" userId=\"@viewer\"></os:PeopleRequest>\n" + "</script>" + "<script require=\"friends\" type=\"text/os-template\">\n" + " <ul><li repeat=\"${friends}\">\n" + " <span id=\"id${Context.Index}\">${Cur.name.givenName}</span>\n" + " </li></ul>" + "</script>" + "</body></html>"; for(GadgetHtmlParser parser : parsers) { Document doc = parser.parseDom(markup); String result = HtmlSerialization.serialize(doc); assertEquals(markup, result); } }