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

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

Introduction

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

Prototype

public DOMConfiguration getDomConfig();

Source Link

Document

The DOMConfiguration object used by the LSSerializer when serializing a DOM node.

Usage

From source file:org.adeptnet.auth.saml.SAMLClient.java

private String _createAuthnRequest(final String requestId) throws SAMLException {
    final AuthnRequest request = createAuthnRequest(requestId);

    try {//from   www  . j  av  a2  s. c  o  m
        // samlobject to xml dom object
        final Element elem = Configuration.getMarshallerFactory().getMarshaller(request).marshall(request);

        // and to a string...
        final Document document = elem.getOwnerDocument();
        final DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
        final LSSerializer serializer = domImplLS.createLSSerializer();
        serializer.getDomConfig().setParameter("xml-declaration", false);
        return serializer.writeToString(elem);
    } catch (MarshallingException e) {
        throw new SAMLException(e);
    }
}

From source file:org.apache.juddi.mapping.MappingApiToModel.java

private static String serializeTransformElement(Element xformEl) throws DOMException, LSException {
    Document document = xformEl.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    //        serializer.getDomConfig().setParameter("namespaces", true);
    //        serializer.getDomConfig().setParameter("namespace-declarations", true);
    serializer.getDomConfig().setParameter("canonical-form", false);
    serializer.getDomConfig().setParameter("xml-declaration", false);
    String str = serializer.writeToString(xformEl);
    return str;//www .ja v a2s .  c  om
}

From source file:org.apache.syncope.core.logic.init.CamelRouteLoader.java

private String nodeToString(final Node content, final DOMImplementationLS domImpl) {
    StringWriter writer = new StringWriter();
    try {//from   w w w  .ja  v  a  2s  .  c o  m
        LSSerializer serializer = domImpl.createLSSerializer();
        serializer.getDomConfig().setParameter("xml-declaration", false);
        LSOutput lso = domImpl.createLSOutput();
        lso.setCharacterStream(writer);
        serializer.write(content, lso);
    } catch (Exception e) {
        LOG.debug("While serializing route node", e);
    }
    return writer.toString();
}

From source file:org.ms123.common.importing.XmlImporter.java

private String prettyPrint(Element e) {
    DOMImplementation domImplementation = m_document.getImplementation();
    if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS",
                "3.0");
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
        DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
        if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) {
            lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
            LSOutput lsOutput = domImplementationLS.createLSOutput();
            lsOutput.setEncoding("UTF-8");
            StringWriter stringWriter = new StringWriter();
            lsOutput.setCharacterStream(stringWriter);
            lsSerializer.write(e, lsOutput);
            return stringWriter.toString();
        } else {/*from w  w  w .  java 2s. co  m*/
            throw new RuntimeException("DOMConfiguration 'format-pretty-print' parameter isn't settable.");
        }
    } else {
        throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported.");
    }
}

From source file:org.sakaiproject.tool.assessment.qti.util.XmlStringBuffer.java

/**
 * string value of document/* ww w  . ja v a2 s  . c  om*/
 *
 * @return the string
 */
public final String stringValue() {
    if (log.isDebugEnabled()) {
        log.debug("stringValue()");
    }

    if (document == null) {
        return this.xml.toString();
    } else {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
            DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
            LSSerializer writer = impl.createLSSerializer();
            writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
            LSOutput output = impl.createLSOutput();
            output.setByteStream(out);
            writer.write(document, output);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return out.toString();
    }
}

From source file:org.wso2.carbon.datasource.utils.DataSourceUtils.java

public static String elementToString(Element element) {
    try {/*from   w  ww.  ja  v a 2 s.  co m*/
        if (element == null) {
            /* return an empty string because, the other way around works the same,
            where if we give a empty string as the XML, we get a null element
            from "stringToElement" */
            return "";
        }
        Document document = element.getOwnerDocument();
        DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
        LSSerializer serializer = domImplLS.createLSSerializer();
        //by default its true, so set it to false to get String without xml-declaration
        serializer.getDomConfig().setParameter(XML_DECLARATION, false);
        return serializer.writeToString(element);
    } catch (Exception e) {
        logger.error("Error while converting element to string: " + e.getMessage(), e);
        return null;
    }
}

From source file:org.wso2.carbon.mediation.configadmin.util.XMLPrettyPrinter.java

/**
 * XML Pretty Print method with XML Comments support.
 *
 * @return XML formatted String/*from   ww  w.j  av a 2  s  . c om*/
 */
public String xmlFormatWithComments() {
    String xmlOutput = null;
    Document doc;
    LSSerializer lsSerializer;

    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        doc = documentBuilderFactory.newDocumentBuilder().parse(in);

        DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
        lsSerializer = domImplementation.createLSSerializer();
        lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);

        LSOutput lsOutput = domImplementation.createLSOutput();
        lsOutput.setEncoding(encoding);
        Writer stringWriter = new StringWriter();
        lsOutput.setCharacterStream(stringWriter);
        lsSerializer.write(doc, lsOutput);

        xmlOutput = stringWriter.toString();

    } catch (IOException e) {
        log.error("XML Pretty Printer Error reading data from given InputStream to XML Document ", e);
    } catch (SAXException e) {
        log.error("XML Pretty Printer Error parsing the given InputStream to XML Document", e);
    } catch (Exception e) {
        log.error("XML Pretty Printer failed. ", e);
    }
    return xmlOutput;
}

From source file:org.wso2.carbon.ndatasource.core.utils.DataSourceUtils.java

public static String elementToString(Element element) {
    try {/*from  w ww  . j a  v a2  s . com*/
        if (element == null) {
            /* return an empty string because, the other way around works the same,
            where if we give a empty string as the XML, we get a null element
            from "stringToElement" */
            return "";
        }
        Document document = element.getOwnerDocument();
        DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
        LSSerializer serializer = domImplLS.createLSSerializer();
        //by default its true, so set it to false to get String without xml-declaration
        serializer.getDomConfig().setParameter(XML_DECLARATION, false);
        return serializer.writeToString(element);
    } catch (Exception e) {
        log.error("Error while convering element to string: " + e.getMessage(), e);
        return null;
    }
}

From source file:org.wso2.developerstudio.eclipse.security.project.ui.form.SecurityFormPage.java

/**
 * Gets the updated content of the source
 *
 * @return content//from ww  w .  j ava 2s .com
 * @throws TransformerException
 */
public String getUpdatedContent()
        throws TransformerException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    final LSSerializer writer = impl.createLSSerializer();

    // Set this to true if the output needs to be beautified.
    writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
    // Set this to true if the declaration is needed to be outputted.
    writer.getDomConfig().setParameter("xml-declaration", false);

    return writer.writeToString(policyFileDOMDocument);
}