Example usage for org.w3c.dom.ls DOMImplementationLS createLSSerializer

List of usage examples for org.w3c.dom.ls DOMImplementationLS createLSSerializer

Introduction

In this page you can find the example usage for org.w3c.dom.ls DOMImplementationLS createLSSerializer.

Prototype

public LSSerializer createLSSerializer();

Source Link

Document

Create a new LSSerializer object.

Usage

From source file:no.kantega.commons.util.XMLHelper.java

public static String getString(Document doc) throws SystemException {
    DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
    LSSerializer lsSerializer = domImplementation.createLSSerializer();
    return lsSerializer.writeToString(doc);
}

From source file:Main.java

public static boolean writeDocumentToStream(Document doc, OutputStream os) {
    // Check if DOM Load and Save is supported

    if (!((doc.getFeature("Core", "3.0") != null) && (doc.getFeature("LS", "3.0") != null)))
        throw new RuntimeException("DOM Load and Save unsupported");

    // Grab the available implementation

    DOMImplementationLS DOMiLS = (DOMImplementationLS) (doc.getImplementation()).getFeature("LS", "3.0");

    // Create LS output destination

    LSOutput lso = DOMiLS.createLSOutput();
    lso.setByteStream(os);//from  ww w. ja  v a2s.c  om

    // Create a LS serializer
    // and tell it to make the output 'pretty'

    LSSerializer serializer = DOMiLS.createLSSerializer();
    serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);

    // Serialize the xml to your output stream

    return serializer.write(doc, lso);
}

From source file:edu.kit.dama.mdm.content.util.DublinCoreHelper.java

/**
 * Create the Dublin Core document.//  w  w w .  ja  v  a2  s.  c o m
 *
 * @param theObject The object to create the DC information for.
 * @param pCreator A custom creator stored as author/publisher in Dublin
 * Core. If not provided, the object's uploader is used if available.
 * @param out The output stream to which the DC document is written.
 *
 * @throws ParserConfigurationException If creating the Dublin Core document
 * failed.
 */
public static void writeDublinCoreDocument(DigitalObject theObject, UserData pCreator, OutputStream out)
        throws ParserConfigurationException {
    Document doc = createDublinCoreDocument(theObject, pCreator);
    DOMImplementation impl = doc.getImplementation();
    DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");

    LSOutput lso = implLS.createLSOutput();
    lso.setByteStream(out);
    LSSerializer writer = implLS.createLSSerializer();
    writer.write(doc, lso);
}

From source file:Main.java

/**
 * Normalize and pretty-print XML so that it can be compared using string
 * compare. The following code does the following: - Removes comments -
 * Makes sure attributes are ordered consistently - Trims every element -
 * Pretty print the document/*from  www .j  av a 2s . c o m*/
 *
 * @param xml The XML to be normalized
 * @return The equivalent XML, but now normalized
 */
public static String normalizeXML(String xml) throws Exception {
    // Remove all white space adjoining tags ("trim all elements")
    xml = xml.replaceAll("\\s*<", "<");
    xml = xml.replaceAll(">\\s*", ">");

    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSParser lsParser = domLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

    LSInput input = domLS.createLSInput();
    input.setStringData(xml);
    Document document = lsParser.parse(input);

    LSSerializer lsSerializer = domLS.createLSSerializer();
    lsSerializer.getDomConfig().setParameter("comments", Boolean.FALSE);
    lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
    return lsSerializer.writeToString(document);
}

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  ww w.  j a v a 2s. com
    return domOutput.getCharacterStream().toString();
}

From source file:com.portfolio.data.utils.DomUtils.java

public static String getInnerXml(Node node) {
    DOMImplementationLS lsImpl = (DOMImplementationLS) node.getOwnerDocument().getImplementation()
            .getFeature("LS", "3.0");
    LSSerializer lsSerializer = lsImpl.createLSSerializer();
    NodeList childNodes = node.getChildNodes();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < childNodes.getLength(); i++) {
        sb.append(lsSerializer.writeToString(childNodes.item(i)));
    }/*from   ww w  .  jav a  2s . c  o  m*/
    // TODO Comprendre pourquoi CDATA est mal ferm
    if (sb.toString().startsWith("<![CDATA["))
        sb.append("]]>");
    return DomUtils.filtrerInnerXml(sb.toString());
}

From source file:com.prowidesoftware.swift.io.parser.MxParser.java

/**
 * Parse the business header from an XML Element node
 * @see #parseBusinessHeader()/*from  ww  w  .j ava2s. c om*/
 * @since 7.8
 */
public static BusinessHeader parseBusinessHeader(final org.w3c.dom.Element e) {
    org.w3c.dom.ls.DOMImplementationLS lsImpl = (org.w3c.dom.ls.DOMImplementationLS) e.getOwnerDocument()
            .getImplementation().getFeature("LS", "3.0");
    org.w3c.dom.ls.LSSerializer serializer = lsImpl.createLSSerializer();
    serializer.getDomConfig().setParameter("xml-declaration", false);
    String xml = serializer.writeToString(e);
    MxParser parser = new MxParser(xml);
    return parser.parseBusinessHeader();
}

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * Document Object Model (DOM) Level 3 Load and Save Specification See: http://www.w3.org/TR/2004/REC-DOM-Level-3-LS-20040407/
 *
 * @param xmlNode The node to be serialized.
 * @return//from   w w w .  j a  va 2 s .c  om
 */
public static byte[] serializeNode(final Node xmlNode) {

    try {

        final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        final LSSerializer writer = impl.createLSSerializer();

        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        final LSOutput output = impl.createLSOutput();
        output.setByteStream(buffer);
        writer.write(xmlNode, output);

        final byte[] bytes = buffer.toByteArray();
        return bytes;
    } catch (Exception e) {
        throw new DSSException(e);
    }
}

From source file:eu.semaine.util.XMLTool.java

/**
 * Document type to String format conversion 
 * @param document//  www . j a v a2  s. com
 * @return
 * @throws Exception
 * @throws FileNotFoundException
 */
public static String document2String(Document document) throws SystemConfigurationException {
    LSSerializer serializer;
    DOMImplementationLS domImplLS;
    try {
        DOMImplementation implementation = DOMImplementationRegistry.newInstance()
                .getDOMImplementation("XML 3.0");
        domImplLS = (DOMImplementationLS) implementation.getFeature("LS", "3.0");
        serializer = domImplLS.createLSSerializer();
        DOMConfiguration config = serializer.getDomConfig();
        config.setParameter("format-pretty-print", Boolean.TRUE);
        //config.setParameter("canonical-form", Boolean.TRUE);
    } catch (Exception e) {
        throw new SystemConfigurationException("Problem instantiating XML serializer code", e);
    }
    LSOutput output = domImplLS.createLSOutput();
    output.setEncoding("UTF-8");
    StringWriter buf = new StringWriter();
    output.setCharacterStream(buf);
    serializer.write(document, output);
    return buf.toString();
}

From source file:fr.fastconnect.factory.tibco.bw.codereview.pages.CodeReviewIndexMojo.java

protected String formatHtml(String html) throws MojoExecutionException {
    try {/*  ww w.j ava 2  s.com*/
        InputSource src = new InputSource(new StringReader(html));
        Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src)
                .getDocumentElement();
        Boolean keepDeclaration = Boolean.valueOf(html.startsWith("<?xml"));

        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();

        writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
        writer.getDomConfig().setParameter("xml-declaration", keepDeclaration);

        return writer.writeToString(document);
    } catch (Exception e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}