/** * 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; }
/** {@inheritDoc} */ 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; } }
/** * Create a writer sized to the original length of the document * @param doc * @return */ public static StringWriter createWriter(Document doc) { int originalLength = getOriginalLength(doc); if (originalLength == -1) { return new StringWriter(8192); } else { // Typically rewriting makes a document larger return new StringWriter((originalLength * 11) / 10); } }
HtmlSerialization.attach(document, serializerProvider.get(), source); return document; } catch (NullPointerException e) { HtmlSerialization.attach(document, serializerProvider.get(), source); HtmlSerialization.copySerializer(document, copy); return copy;
/** * Create a text node with proper escaping. */ protected final void appendTextNode(Node parent, String text) { if (text == null || "".equals(text)) { return; } try { StringBuilder sb = new StringBuilder(text.length()); HtmlSerialization.printEscapedText(text, sb); parent.appendChild(parent.getOwnerDocument().createTextNode(sb.toString())); } catch (IOException ioe) { throw new RuntimeException(ioe); } } }
/** {@inheritDoc} */ public String serialize(Document doc) { try { StringWriter sw = HtmlSerialization.createWriter(doc); if (doc.getDoctype() != null) { outputDocType(doc.getDoctype(), sw); } this.serialize(doc, sw); return sw.toString(); } catch (IOException ioe) { return null; } }
HtmlSerialization.attach(document, serializer, null); Node html = null; LinkedList<Node> beforeHtml = Lists.newLinkedList();
HtmlSerialization.attach(document, serializerProvider.get(), source); return document; } catch (NullPointerException e) { HtmlSerialization.attach(document, serializerProvider.get(), source); HtmlSerialization.copySerializer(document, copy); return copy;
/** * Create a text node with proper escaping. */ protected final void appendTextNode(Node parent, String text) { if (text == null || "".equals(text)) { return; } try { StringBuilder sb = new StringBuilder(text.length()); HtmlSerialization.printEscapedText(text, sb); parent.appendChild(parent.getOwnerDocument().createTextNode(sb.toString())); } catch (IOException ioe) { throw new RuntimeException(ioe); } } }
/** {@inheritDoc} */ public String serialize(Document doc) { try { StringWriter sw = HtmlSerialization.createWriter(doc); if (doc.getDoctype() != null) { outputDocType(doc.getDoctype(), sw); } this.serialize(doc, sw); return sw.toString(); } catch (IOException ioe) { return null; } }
@Override protected Document parseDomImpl(String source) throws GadgetException { MessageQueue mq = new SimpleMessageQueue(); try { DomParser parser = getDomParser(source, mq); Document doc = parser.parseDocument().getOwnerDocument(); VanillaCajaHtmlSerializer serializer = new VanillaCajaHtmlSerializer(); HtmlSerialization.attach(doc, serializer, null); return doc; } catch (ParseException e) { throw new GadgetException(GadgetException.Code.HTML_PARSE_ERROR, e.getCajaMessage().toString(), HttpResponse.SC_INTERNAL_SERVER_ERROR); } catch (NullPointerException e) { throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e); } }
/** * 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; }
HtmlSerialization.attach(document, serializerProvider.get(), source); return document; } catch (NullPointerException e) { HtmlSerialization.attach(document, serializerProvider.get(), source); HtmlSerialization.copySerializer(document, copy); return copy;
/** * Create a text node with proper escaping. */ protected final void appendTextNode(Node parent, String text) { if (text == null || "".equals(text)) { return; } try { StringBuilder sb = new StringBuilder(text.length()); HtmlSerialization.printEscapedText(text, sb); parent.appendChild(parent.getOwnerDocument().createTextNode(sb.toString())); } catch (IOException ioe) { throw new RuntimeException(ioe); } } }
public String serialize(Document doc) { StringWriter sw = HtmlSerialization.createWriter(doc); return Nodes.render(doc, new RenderContext(new Concatenator(sw, null)).asXml()); } }
public String serialize(Document doc) { try { StringWriter sw = HtmlSerialization.createWriter(doc); if (doc.getDoctype() != null) { HtmlSerialization.outputDocType(doc.getDoctype(), sw); } RenderContext renderContext = new RenderContext(new Concatenator(sw, null)) // More compact but needs charset set correctly. .withAsciiOnly(false) .withMarkupRenderMode(MarkupRenderMode.HTML); // Use render unsafe in order to retain comments in the serialized HTML. // TODO: This function is deprecated. Use a non-deprecated function. Nodes.renderUnsafe(doc, renderContext); return sw.toString(); } catch (IOException e) { return null; } } }
/** * Create a writer sized to the original length of the document * @param doc * @return */ public static StringWriter createWriter(Document doc) { int originalLength = getOriginalLength(doc); if (originalLength == -1) { return new StringWriter(8192); } else { // Typically rewriting makes a document larger return new StringWriter((originalLength * 11) / 10); } }