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:org.mitre.eren.model.ModelManager.java

/**
 * Convert the message into a string and then parse it using the abdera parser.
 * Is there a simpler way to do this?/*from  ww  w. j  a  v  a  2  s.c  o  m*/
 * @param edxl
 * @param outgoing
 */
private void attachMessage(EDXLDistribution edxl, Node outgoing) {

    try {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

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

        LSSerializer writer = impl.createLSSerializer();
        String str = writer.writeToString(outgoing);

        Abdera abdera = new Abdera();
        Parser parser = abdera.getParser();
        Document<Feed> feed_doc = parser.parse(new StringReader(str));
        ExtensibleElement content = feed_doc.getRoot();

        ContentObject co = edxl.addContentObject();
        XmlContent xc = co.setXmlContent();
        EmbeddedXMLContent exc = xc.addEmbeddedXMLContent();

        exc.addExtension(content);
    } catch (ClassCastException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

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 {//  ww w .jav  a  2s  . com
            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.oclc.oai.harvester.verb.HarvesterVerb.java

@Override
public String toString() {
    // Element docEl = getDocument().getDocumentElement();
    // return docEl.toString();
    /*//  ww w  .ja  va2s.c  o m
     * Source input = new DOMSource(getDocument()); StringWriter sw = new
     * StringWriter(); Result output = new StreamResult(sw); try {
     * Transformer idTransformer = xformFactory.newTransformer();
     * idTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
     * "yes"); idTransformer.transform(input, output); return sw.toString();
     * } catch (TransformerException e) { return e.getMessage(); }
     */

    //Serialize DOM
    //        OutputFormat format = new OutputFormat(doc);
    //        // as a String
    //        StringWriter stringOut = new StringWriter();
    //        XMLSerializer serial = new XMLSerializer(stringOut, format);
    //        try {
    //            serial.serialize(doc);
    //        } catch (IOException e) {
    //            e.printStackTrace();
    //        }

    DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
    LSSerializer lsSerializer = domImplementation.createLSSerializer();
    // Display the XML
    return lsSerializer.writeToString(doc);

}

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

/**
 * string value of document//from w ww  . ja va2 s .c  o m
 *
 * @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.sakaiproject.util.StorageUtils.java

/**
 * Write a DOM Document to an xml file.//  ww w .  j  a  va2 s  .  c  o m
 * 
 * @param doc
 *        The DOM Document to write.
 * @param fileName
 *        The complete file name path.
 */
public static void writeDocument(Document doc, String fileName) {
    OutputStream out = null;
    try {
        out = new FileOutputStream(fileName);
        //          get an instance of the DOMImplementation registry
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation impl = builder.getDOMImplementation();

        DOMImplementationLS feature = (DOMImplementationLS) impl.getFeature("LS", "3.0");
        LSSerializer serializer = feature.createLSSerializer();
        LSOutput output = feature.createLSOutput();
        output.setByteStream(out);
        output.setEncoding("UTF-8");
        serializer.write(doc, output);

        out.close();
    } catch (Exception any) {
        M_log.warn("writeDocument: " + any.toString());
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {

            }
        }
    }
}

From source file:org.sakaiproject.util.StorageUtils.java

/**
 * Write a DOM Document to an output stream.
 * /* www.  j  ava2s  .co m*/
 * @param doc
 *        The DOM Document to write.
 * @param out
 *        The output stream.
 */
public static String writeDocumentToString(Document doc) {
    try {

        StringWriter sw = new StringWriter();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation impl = builder.getDOMImplementation();

        DOMImplementationLS feature = (DOMImplementationLS) impl.getFeature("LS", "3.0");
        LSSerializer serializer = feature.createLSSerializer();
        LSOutput output = feature.createLSOutput();
        output.setCharacterStream(sw);
        output.setEncoding("UTF-8");
        serializer.write(doc, output);

        sw.flush();
        return sw.toString();
    } catch (Exception any) {
        M_log.warn("writeDocumentToString: " + any.toString());
        return null;
    }
}

From source file:org.springframework.batch.core.jsr.configuration.xml.JsrBeanDefinitionDocumentReader.java

private String elementToString(Element root) {
    DOMImplementationLS domImplLS = (DOMImplementationLS) root.getOwnerDocument().getImplementation();
    return domImplLS.createLSSerializer().writeToString(root);
}

From source file:org.wso2.appserver.webapp.security.utils.SSOUtils.java

/**
 * Serializes the specified SAML 2.0 based XML content representation to its corresponding actual XML syntax
 * representation./*from   www  .  j  a v  a  2  s. c  o  m*/
 *
 * @param xmlObject the SAML 2.0 based XML content object
 * @return a {@link String} representation of the actual XML representation of the SAML 2.0 based XML content
 * representation
 * @throws SSOException if an error occurs during the marshalling process
 */
public static String marshall(XMLObject xmlObject) throws SSOException {
    try {
        Marshaller marshaller = XMLObjectProviderRegistrySupport.getMarshallerFactory()
                .getMarshaller(xmlObject);
        Element element = null;
        if (marshaller != null) {
            element = marshaller.marshall(xmlObject);
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS implementation = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = implementation.createLSSerializer();
        LSOutput output = implementation.createLSOutput();
        output.setByteStream(byteArrayOutputStream);
        writer.write(element, output);
        return new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
    } catch (ClassNotFoundException | InstantiationException | MarshallingException
            | IllegalAccessException e) {
        throw new SSOException("Error in marshalling SAML 2.0 Assertion", e);
    }
}

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

public static String elementToString(Element element) {
    try {//  w  w w  .  j  av a 2 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) {
        logger.error("Error while converting element to string: " + e.getMessage(), e);
        return null;
    }
}

From source file:org.wso2.carbon.identity.application.authenticator.samlsso.util.SSOUtils.java

/**
 * Serializing a SAML2 object into a String
 *
 * @param xmlObject object that needs to serialized.
 * @return serialized object//from   w w w .  jav a 2 s. c  om
 * @throws SAMLSSOException
 */
public static String marshall(XMLObject xmlObject) throws SAMLSSOException {
    try {

        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

        MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        Element element = marshaller.marshall(xmlObject);

        ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream();
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(byteArrayOutputStrm);
        writer.write(element, output);
        return byteArrayOutputStrm.toString();
    } catch (Exception e) {
        log.error("Error Serializing the SAML Response");
        throw new SAMLSSOException("Error Serializing the SAML Response", e);
    }
}