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

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

Introduction

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

Prototype

public static DOMImplementationRegistry newInstance()
        throws ClassNotFoundException, InstantiationException, IllegalAccessException, ClassCastException 

Source Link

Document

Obtain a new instance of a DOMImplementationRegistry.

Usage

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 w w  . ja  v a 2 s . c o  m

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

From source file:Main.java

/**
 * /* w  w w .  j av  a2s. c  o  m*/
 * @param xmlNode
 * @return
 */
public static String serializeNode(Node xmlNode) {

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

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

        return new String(buffer.toByteArray());
    } catch (Exception e) {
        /* Serialize node is for debuging only */
        return null;
    }
}

From source file:Main.java

static public final String toString(Node node, boolean declaration) {
    try {/*from ww w .jav  a2 s  . com*/
        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.
 * /*from  w  w  w  .  j av  a2 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: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/*from  www.j a  va2s .  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

/**
 * 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/*  w  ww . j  av a 2 s  .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:javatojs.DomUtil.java

public static Document readDocument(String uri) {
    Document document = null;//from  w  w  w  .jav  a 2  s .  co m
    try {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

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

        LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

        System.out.println("In readDocument uri: " + uri);
        document = builder.parseURI(uri);
        if (document == null) {
            System.out.println("Null doc returned!!!!!!!!");
        }
        System.out.println("Read toc: \n " + document);
    } catch (Exception e) {
        System.out.println("ERROR reading toc: \n ");
        e.printStackTrace();
    }
    return document;
}

From source file:javatojs.DomUtil.java

public static void writeDocument(Document document) {
    try {//ww w .  j  a  v a  2 s  .c o m
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        String documentStr = writer.writeToString(document);
        System.out.println("Serialized document: \n" + documentStr);
    } catch (Exception e) {
        System.out.println("ERROR writing document: \n ");
        e.printStackTrace();
    }
}

From source file:Main.java

private static Element convertXmlToElementWorker(String xml) throws Exception {
    Element element = null;//from  w w  w . j a v a2  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:com.espertech.esper.event.xml.XSDSchemaMapper.java

private static XSModel readSchemaInternal(String schemaResource, String schemaText)
        throws IllegalAccessException, InstantiationException, ClassNotFoundException, ConfigurationException,
        URISyntaxException {/*from   w w w  .  j  a v  a 2 s. c  o  m*/
    LSInputImpl input = null;
    String baseURI = null;
    if (schemaResource != null) {
        URL url = ResourceLoader.resolveClassPathOrURLResource("schema", schemaResource);
        baseURI = url.toURI().toString();
    } else {
        input = new LSInputImpl(schemaText);
    }

    // Uses Xerxes internal classes
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    registry.addSource(new DOMXSImplementationSourceImpl());
    Object xsImplementation = registry.getDOMImplementation("XS-Loader");
    if (xsImplementation == null) {
        throw new ConfigurationException(
                "Failed to retrieve XS-Loader implementation from registry obtained via DOMImplementationRegistry.newInstance, please check that registry.getDOMImplementation(\"XS-Loader\") returns an instance");
    }
    if (!JavaClassHelper.isImplementsInterface(xsImplementation.getClass(), XSImplementation.class)) {
        String message = "The XS-Loader instance returned by the DOM registry class '"
                + xsImplementation.getClass().getName() + "' does not implement the interface '"
                + XSImplementation.class.getName()
                + "'; If you have a another Xerces distribution in your classpath please ensure the classpath order loads the JRE Xerces distribution or set the DOMImplementationRegistry.PROPERTY system property";
        throw new ConfigurationException(message);
    }
    XSImplementation impl = (XSImplementation) xsImplementation;
    XSLoader schemaLoader = impl.createXSLoader(null);
    XSModel xsModel;
    if (input != null) {
        xsModel = schemaLoader.load(input);
    } else {
        xsModel = schemaLoader.loadURI(baseURI);
    }

    if (xsModel == null) {
        throw new ConfigurationException("Failed to read schema via URL '" + schemaResource + '\'');
    }

    return xsModel;
}