Example usage for org.w3c.dom.ls LSSerializer setNewLine

List of usage examples for org.w3c.dom.ls LSSerializer setNewLine

Introduction

In this page you can find the example usage for org.w3c.dom.ls LSSerializer setNewLine.

Prototype

public void setNewLine(String newLine);

Source Link

Document

The end-of-line sequence of characters to be used in the XML being written out.

Usage

From source file:Main.java

public static final String prettyPrint(final Document aNode) throws Exception {
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

    DOMImplementationLS impls = (DOMImplementationLS) registry.getDOMImplementation("LS");

    // Prepare the output
    LSOutput domOutput = impls.createLSOutput();
    domOutput.setEncoding(java.nio.charset.Charset.defaultCharset().name());
    StringWriter writer = new StringWriter();
    domOutput.setCharacterStream(writer);
    LSSerializer domWriter = impls.createLSSerializer();
    DOMConfiguration domConfig = domWriter.getDomConfig();
    domConfig.setParameter("format-pretty-print", true);
    domConfig.setParameter("element-content-whitespace", true);
    domWriter.setNewLine("\r\n");
    domConfig.setParameter("cdata-sections", Boolean.TRUE);
    // And finaly, write
    domWriter.write(aNode, domOutput);//from w ww .ja  va 2s. c  o  m
    return domOutput.getCharacterStream().toString();
}

From source file:com.xpn.xwiki.pdf.impl.PdfExportImpl.java

/**
 * Cleans up an HTML document, turning it into valid XHTML.
 * //from www . ja v  a  2  s . co m
 * @param input the source HTML to process
 * @return the cleaned up source
 */
private String convertToStrictXHtml(String input) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Cleaning HTML: " + input);
    }

    try {
        // First step, Tidy the document
        StringWriter tidyOutput = new StringWriter(input.length());
        this.tidy.parse(new StringReader(input), tidyOutput);

        // Tidy can't solve duplicate IDs, so it needs to be done manually
        DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
        docBuilder.setEntityResolver(Utils.getComponent(EntityResolver.class));
        String tidied = tidyOutput.toString().trim();
        if (StringUtils.isEmpty(tidied)) {
            tidied = input.trim();
        }
        Document doc = docBuilder.parse(new InputSource(new StringReader(tidied)));
        List<String> seenIDs = new ArrayList<String>();
        this.cleanIDs(doc.getDocumentElement(), seenIDs);

        // Write back the fixed document to a String
        LSOutput output = lsImpl.createLSOutput();
        StringWriter result = new StringWriter();
        output.setCharacterStream(result);
        LSSerializer serializer = lsImpl.createLSSerializer();
        serializer.setNewLine("\n");
        output.setEncoding(doc.getXmlEncoding());
        serializer.write(doc, output);
        return result.toString();
    } catch (Exception ex) {
        LOGGER.warn("Failed to tidy document for export: " + ex.getMessage(), ex);
        return input;
    }
}