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

@SuppressWarnings("unchecked")
public synchronized static <T> T getDOMImpl() {
    if (IMPL == null) {
        try {//from   w  w  w  .  j a  v a 2s .  c  o m
            DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
            IMPL = registry.getDOMImplementation("LS 3.0");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return (T) IMPL;
}

From source file:Main.java

/**
 * Serialises the XML node into a string.
 * //from  www. j  ava 2s.c o m
 * @param node the XML node
 * @return the corresponding string
 */
public static String serialise(Node node) {
    try {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS lsImpl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer serializer = lsImpl.createLSSerializer();
        return serializer.writeToString(node);
    } catch (Exception e) {
        log.fine("could not serialise XML node: " + e);
        return "";
    }
}

From source file:Main.java

private static String parseXmlDocToString(Document xmlDoc)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    // Add formatting to the xml document in case we want to save to a file
    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", true); // Set this to true if the declaration is needed to be outputted

    return writer.writeToString(xmlDoc);
}

From source file:Main.java

public static String format(String xml) throws Exception {
    InputSource src = new InputSource(new StringReader(xml));
    Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
    Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));

    System.setProperty(DOMImplementationRegistry.PROPERTY,
            "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");

    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);
}

From source file:Main.java

public static String prettyPrint(Document document) {
    // Pretty-prints a DOM document to XML using DOM Load and Save's
    // LSSerializer.
    // Note that the "format-pretty-print" DOM configuration parameter can
    // only be set in JDK 1.6+.

    DOMImplementationRegistry domImplementationRegistry;
    try {/*from  www .j av a2s.c  o  m*/
        domImplementationRegistry = DOMImplementationRegistry.newInstance();
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | ClassCastException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    DOMImplementation domImplementation = document.getImplementation();
    if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
        /*DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation
          .getFeature("LS", "3.0");*/
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry
                .getDOMImplementation("LS");

        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(document, lsOutput);
            return stringWriter.toString();
        } else {
            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:Main.java

public static String prettyFormat(String input) {
    try {/*from www  .j  a  va  2 s.com*/
        final InputSource src = new InputSource(new StringReader(input));
        final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src)
                .getDocumentElement();
        final Boolean keepDeclaration = Boolean.valueOf(input.startsWith("<?xml"));
        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 String formatXml(String xml) {

    try {// w w  w .jav a 2s  .co  m
        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();

        // 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", keepDeclaration);

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

        System.err.println("Exception thrown");
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static <T extends OutputStream> T lsSerializeDom(Node doc, T byteStream, String encoding)
        throws Exception {
    if (doc == null)
        return byteStream;
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");

    LSOutput lsOutput = impl.createLSOutput();
    lsOutput.setByteStream(byteStream);/*from w w  w  .  j av  a 2 s. c o  m*/
    encoding = encoding == null ? "UTF-8" : encoding;
    lsOutput.setEncoding(encoding);

    impl.createLSSerializer().write(doc, lsOutput);

    return byteStream;
}

From source file:Main.java

/**
 * Save an XML file.//w  w w .j a  v  a 2  s .  c  o  m
 */
public static void saveXML(Document document, File file) {
    try {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        FileOutputStream fos = new FileOutputStream(file);
        LSOutput lso = impl.createLSOutput();
        lso.setByteStream(fos);
        writer.write(document, lso);
        /*
        OutputFormat of = new OutputFormat(document, "ISO-8859-1", true);
        serializer.setOutputFormat(of);
        serializer.setOutputCharStream(new FileWriter(file));
        serializer.serialize(document);
         */
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * @return DOM Implementation Registry//from w  w w  . ja va2  s .  c  om
 * @throws Exception
 */
private static DOMImplementationRegistry getDOMImplementationRegistry() throws Exception {
    if (domImpRegistry == null) {
        domImpRegistry = DOMImplementationRegistry.newInstance();
    }
    return domImpRegistry;
}