Example usage for org.w3c.dom.bootstrap DOMImplementationRegistry getDOMImplementation

List of usage examples for org.w3c.dom.bootstrap DOMImplementationRegistry getDOMImplementation

Introduction

In this page you can find the example usage for org.w3c.dom.bootstrap DOMImplementationRegistry getDOMImplementation.

Prototype

public DOMImplementation getDOMImplementation(final String features) 

Source Link

Document

Return the first implementation that has the desired features, or null if none is found.

Usage

From source file:Main.java

public static String serializeNode(Node node) throws LSException, IllegalAccessException, DOMException,
        InstantiationException, ClassNotFoundException, ClassCastException {
    String serializedElement = null;
    if (node != null) {
        System.setProperty(DOMImplementationRegistry.PROPERTY,
                "org.apache.xerces.dom.DOMImplementationSourceImpl");
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        serializedElement = writer.writeToString(node);
    }/*  w  ww . j  a va 2  s . c  o m*/
    return serializedElement;
}

From source file:Main.java

public static String serializeElement(Element element) throws LSException, IllegalAccessException, DOMException,
        InstantiationException, ClassNotFoundException, ClassCastException {
    String serializedElement = null;
    if (element != null) {
        System.setProperty(DOMImplementationRegistry.PROPERTY,
                "org.apache.xerces.dom.DOMImplementationSourceImpl");
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        serializedElement = writer.writeToString(element);
    }/*from  www .jav a 2 s .com*/
    return serializedElement;
}

From source file:Main.java

private static Element convertXmlToElementWorker(String xml) throws Exception {
    Element element = null;/* w ww .  ja v  a  2  s  .c o m*/

    System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMImplementationSourceImpl");
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS,
            "http://www.w3.org/2001/XMLSchema");
    LSInput lsInput = impl.createLSInput();
    lsInput.setStringData(xml);
    Document doc = parser.parse(lsInput);
    if (doc != null) {
        element = doc.getDocumentElement();
    }
    return element;
}

From source file:Main.java

static public final String toString(Node node, boolean declaration) {
    try {//from  w  w w  .j  ava 2  s  . c  om
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");

        LSSerializer writer = impl.createLSSerializer();
        writer.getDomConfig().setParameter("xml-declaration", declaration);
        return writer.writeToString(node);
    } catch (Exception e) {
        return e.toString();
    }
}

From source file:Main.java

/**
 * Formats an XML string for pretty printing. Requires Java 1.6.
 * /*  w w w  . ja v  a 2  s.  c  o m*/
 * Taken from http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java
 * 
 * @param xml
 * @return pretty-print formatted XML
 */
public static String prettyPrintXml(String xml) {
    try {
        final InputSource src = new InputSource(new StringReader(xml));
        final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src)
                .getDocumentElement();
        final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));

        //May need this: System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");

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

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

        return writer.writeToString(document);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void writeNode(Node node, OutputStream os, boolean formatted)
        throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry
            .getDOMImplementation("LS");

    LSOutput lsOutput = domImplementationLS.createLSOutput();
    lsOutput.setEncoding("UTF-8");
    lsOutput.setByteStream(os);//from  w ww  . j  a  va 2  s .c o m

    LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
    if (formatted)
        setLsSeriliserToFormatted(lsSerializer);
    lsSerializer.write(node, lsOutput);
}

From source file:com.axway.ebxml.XmlUtil.java

/**
 * Write the specified <code>Document</code> to the specified <code>OutputStream</code>
 * @param document the <code>Document</code> to write out
 * @param out     the <code>OutputStream</code> to write to
 * @throws java.io.IOException/* ww  w. j av  a  2 s .  c  om*/
 */
public static void writeTo(Document document, OutputStream out) throws KeyInfoWriterException {
    // Write the signed message to the provided OutputStream. If the provided
    // OutputStream is null then write the message to System.out
    if (document == null)
        throw new IllegalArgumentException("document cannot be null");

    if (out == null) {
        logger.debug("Writing document to System.out");
        out = System.out;
    }

    try {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(out);
        writer.write(document, output);
    } catch (ClassNotFoundException e) {
        throw new KeyInfoWriterException("Unexpected error serializing document to XML", e);
    } catch (InstantiationException e) {
        throw new KeyInfoWriterException("Unexpected error serializing document to XML", e);
    } catch (IllegalAccessException e) {
        throw new KeyInfoWriterException("Unexpected error serializing document to XML", e);
    }
}

From source file:Main.java

private static void serializeXML(Element doc, Writer writer, boolean addXmlDeclaration) throws IOException {
    try {//  w  w  w  . java 2s . c o m
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer serializer = impl.createLSSerializer();
        DOMConfiguration config = serializer.getDomConfig();
        config.setParameter("xml-declaration", addXmlDeclaration);
        // config.setParameter("format-pretty-print", true);
        // config.setParameter("normalize-characters", true);
        LSOutput out = impl.createLSOutput();
        out.setCharacterStream(writer);
        serializer.write(doc, out);
    } catch (Throwable e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.lostinsoftware.xsdparser.XSDParser.java

private static XSDElement parseXSD(InputStream inputStream, List<String> elements, boolean allElements)
        throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException,
        ClassCastException {//from ww w. j  a  v  a 2s  . c  om

    XSDElement mainElement = null;

    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

    XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader");

    XSLoader schemaLoader = impl.createXSLoader(null);

    DOMConfiguration config = schemaLoader.getConfig();

    // create Error Handler
    DOMErrorHandler errorHandler = new XSDParser();

    // set error handler
    config.setParameter("error-handler", errorHandler);

    // set validation feature
    config.setParameter("validate", Boolean.TRUE);

    // parse document
    LSInput input = new DOMInputImpl();
    input.setByteStream(inputStream);
    XSModel model = schemaLoader.load(input);
    if (model != null) {
        // Main element
        XSElementDeclaration element = model.getElementDeclaration(elements.get(0), null);
        mainElement = new XSDElement();
        mainElement.setName(elements.get(0));
        mainElement.setXsDeclaration(element);
        processElement(mainElement, elements, allElements);
    }
    return mainElement;
}

From source file:XMLUtils.java

public static InputStream getInputStream(Document doc) throws Exception {
    DOMImplementationLS impl = null;
    DOMImplementation docImpl = doc.getImplementation();
    // Try to get the DOMImplementation from doc first before
    // defaulting to the sun implementation.
    if (docImpl != null && docImpl.hasFeature("LS", "3.0")) {
        impl = (DOMImplementationLS) docImpl.getFeature("LS", "3.0");
    } else {/*w  w  w.j a  v a2 s  . c o m*/
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        if (impl == null) {
            System.setProperty(DOMImplementationRegistry.PROPERTY,
                    "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
            registry = DOMImplementationRegistry.newInstance();
            impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        }
    }
    LSOutput output = impl.createLSOutput();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    output.setByteStream(byteArrayOutputStream);
    LSSerializer writer = impl.createLSSerializer();
    writer.write(doc, output);
    byte[] buf = byteArrayOutputStream.toByteArray();
    return new ByteArrayInputStream(buf);
}