Example usage for org.w3c.dom DOMImplementation hasFeature

List of usage examples for org.w3c.dom DOMImplementation hasFeature

Introduction

In this page you can find the example usage for org.w3c.dom DOMImplementation hasFeature.

Prototype

public boolean hasFeature(String feature, String version);

Source Link

Document

Test if the DOM implementation implements a specific feature and version, as specified in <a href="http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#DOMFeatures">DOM Features</a>.

Usage

From source file:DOMFeatures.java

public static void main(String args[]) {

    DOMImplementation domImpl = DOMImplementationImpl.getDOMImplementation();

    String features[] = { "Traversal", "HTML", "CSS", "Range", "XML", "Views", "Events" };

    for (int i = 0; i < features.length; i++) {
        System.out.println("Has " + features[i] + " = " + domImpl.hasFeature(features[i], "2.0"));
    }//from ww  w.ja  v a  2  s  .com
}

From source file:Main.java

public static String lsSerializePretty(Document doc) {
    DOMImplementation domImplementation = doc.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(doc, lsOutput);
            return stringWriter.toString();
        } else {/*w w  w  . ja  v a 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:Main.java

public static String prettyPrintWithDOM3LS(Document document) {
    DOMImplementation domImplementation = 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(document, lsOutput);
            return stringWriter.toString();
        } else {/*ww  w .ja va 2s  .  c  o  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:Main.java

public static void writeDocumentToDisk(Document document, File directoryToWriteTo, String fileName) {
    DOMImplementation domImplementation = 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");
            try {
                lsOutput.setByteStream(new FileOutputStream(new File(directoryToWriteTo, fileName)));
            } catch (FileNotFoundException e) {
                throw new IllegalStateException(e);
            }/*  w  w  w .  j a va  2 s  . c  o m*/
            lsSerializer.write(document, lsOutput);
        }
    }

}

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 {//  ww w. ja v  a2 s  .c  om
        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

/**
 * Converts a dom element to a String/*from w  w w.  j a  va 2 s. c om*/
 * 
 * @param node
 * @return the dom as a String
 */
public static String writeDomToString(Element node) {
    DOMImplementation domImplementation = node.getOwnerDocument().getImplementation();
    if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS",
                "3.0");
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();

        LSOutput lsOutput = domImplementationLS.createLSOutput();
        lsOutput.setEncoding("UTF-8");

        StringWriter stringWriter = new StringWriter();
        lsOutput.setCharacterStream(stringWriter);
        lsSerializer.write(node, lsOutput);
        return stringWriter.toString();
    } else {
        throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported.");
    }
}

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 av a 2s . 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);
}

From source file:DOMModuleChecker.java

/**
 * <p>This method does the actual work of checking the provided
 *  DOM implementation for the modules it supports.</p>
 *
 * @throws <code>Exception</code> - generic exception handling.
 *///from  ww  w . j a  va2s .com
public void check() throws Exception {
    DOMImplementation impl = (DOMImplementation) Class.forName(vendorImplementationClass).newInstance();
    for (int i = 0; i < moduleNames.length; i++) {
        if (impl.hasFeature(moduleNames[i], "2.0")) {
            System.out.println("Support for " + moduleNames[i] + " is included in this DOM implementation.");
        } else {
            System.out
                    .println("Support for " + moduleNames[i] + " is not included in this DOM implementation.");
        }
    }
}

From source file:edu.wfu.inotado.helper.MarshalHelper.java

/**
 * Pretty-Prints the XML/*from   w w  w .  ja  v  a 2s . c  o m*/
 * 
 * @param xml
 * @return
 */
@SuppressWarnings("unused")
private String prettyPrintXml(String xml) {
    DocumentBuilder documentBuilder;
    StringWriter stringWriter = new StringWriter();
    try {
        documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputStream inputStream = new ByteArrayInputStream(xml.getBytes("UTF8"));
        Document document = documentBuilder.parse(inputStream);
        DOMImplementation domImplementation = 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");

                lsOutput.setCharacterStream(stringWriter);
                lsSerializer.write(document, lsOutput);
                return stringWriter.toString();
            } else {
                log.warn("DOMConfiguration 'format-pretty-print' parameter isn't settable.");
            }
        } else {
            log.warn("DOM 3.0 LS and/or DOM 2.0 Core not supported.");
        }
    } catch (ParserConfigurationException e) {
        log.error("Unable to parse xml: " + xml, e);
    } catch (UnsupportedEncodingException e) {
        log.error("Encoding unspported for xml: " + xml, e);
    } catch (SAXException e) {
        log.error("SAXException occurred: " + xml, e);
    } catch (IOException e) {
        log.error("IOException occurred:" + xml, e);
    }
    // return the input string if any error occurs
    return xml;
}

From source file:de.mpg.escidoc.services.exportmanager.Export.java

/**
 * Parses <code>itemList</code> XML to <code>org.w3c.dom.Document</code>.
 * /*w ww.ja  va  2  s.  com*/
 * @param itemList
 * @return <code>org.w3c.dom.Document</code>
 * @throws ExportManagerException
 */
private Document parseDocument(String itemList) throws ExportManagerException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder parser;
    try {
        parser = dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new ExportManagerException("Cannot create DocumentBuilder:", e);
    }

    // Check for the traversal module
    DOMImplementation impl = parser.getDOMImplementation();
    if (!impl.hasFeature("traversal", "2.0")) {
        throw new ExportManagerException("A DOM implementation that supports traversal is required.");
    }
    Document doc;
    try {
        doc = parser.parse(new ByteArrayInputStream(itemList.getBytes()));
    } catch (Exception e) {
        throw new ExportManagerException("Cannot parse itemList to w3c document");
    }
    return doc;
}