Example usage for org.w3c.dom Document getImplementation

List of usage examples for org.w3c.dom Document getImplementation

Introduction

In this page you can find the example usage for org.w3c.dom Document getImplementation.

Prototype

public DOMImplementation getImplementation();

Source Link

Document

The DOMImplementation object that handles this document.

Usage

From source file:edu.kit.dama.mdm.content.util.DublinCoreHelper.java

/**
 * Create the Dublin Core document./*ww w.  j  av a  2s  .c  o m*/
 *
 * @param theObject The object to create the DC information for.
 * @param pCreator A custom creator stored as author/publisher in Dublin
 * Core. If not provided, the object's uploader is used if available.
 * @param out The output stream to which the DC document is written.
 *
 * @throws ParserConfigurationException If creating the Dublin Core document
 * failed.
 */
public static void writeDublinCoreDocument(DigitalObject theObject, UserData pCreator, OutputStream out)
        throws ParserConfigurationException {
    Document doc = createDublinCoreDocument(theObject, pCreator);
    DOMImplementation impl = doc.getImplementation();
    DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");

    LSOutput lso = implLS.createLSOutput();
    lso.setByteStream(out);
    LSSerializer writer = implLS.createLSSerializer();
    writer.write(doc, lso);
}

From source file:no.kantega.commons.util.XMLHelper.java

public static String getString(Document doc) throws SystemException {
    DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
    LSSerializer lsSerializer = domImplementation.createLSSerializer();
    return lsSerializer.writeToString(doc);
}

From source file:Main.java

public static boolean writeDocumentToStream(Document doc, OutputStream os) {
    // Check if DOM Load and Save is supported

    if (!((doc.getFeature("Core", "3.0") != null) && (doc.getFeature("LS", "3.0") != null)))
        throw new RuntimeException("DOM Load and Save unsupported");

    // Grab the available implementation

    DOMImplementationLS DOMiLS = (DOMImplementationLS) (doc.getImplementation()).getFeature("LS", "3.0");

    // Create LS output destination

    LSOutput lso = DOMiLS.createLSOutput();
    lso.setByteStream(os);/*from  w  ww.  j a v a 2s  . c o m*/

    // Create a LS serializer
    // and tell it to make the output 'pretty'

    LSSerializer serializer = DOMiLS.createLSSerializer();
    serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);

    // Serialize the xml to your output stream

    return serializer.write(doc, lso);
}

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 . java  2  s  .co 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:Main.java

public static String toNormalizedXML(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from  w w w. ja v  a 2  s  .co  m*/
    dbf.setCoalescing(true);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(is);
    document.normalizeDocument();
    document.getDocumentElement().setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", //$NON-NLS-1$
            "xsi:schemaLocation", //$NON-NLS-1$
            "http://abc4trust.eu/wp2/abcschemav1.0 ../../../../../../../../../abc4trust-xml/src/main/resources/xsd/schema.xsd"); //$NON-NLS-1$
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    String xml = serializer.writeToString(document);

    return trim(xml);
}

From source file:Main.java

public static void writeDocument(Document doc, Writer out) throws IOException {
    // happy to replace this code w/ the non-deprecated code, but I couldn't get the transformer 
    // approach to work. 
    //    OutputFormat format = new OutputFormat(doc);
    //    format.setIndenting(true);
    //    format.setIndent(2);
    //    XMLSerializer serializer = new XMLSerializer(out, format);
    //    serializer.serialize(doc);

    DOMImplementationLS impl = (DOMImplementationLS) doc.getImplementation();
    LSSerializer writer = impl.createLSSerializer();
    DOMConfiguration config = writer.getDomConfig();

    if (config.canSetParameter("format-pretty-print", Boolean.TRUE)) {
        config.setParameter("format-pretty-print", Boolean.TRUE);
    }/*from ww w  . j av  a 2  s .  com*/

    // what a crappy way to force the stream to be UTF-8.  yuck!
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    LSOutput output = impl.createLSOutput();
    output.setEncoding("UTF-8");
    output.setByteStream(baos);

    writer.write(doc, output);

    out.write(baos.toString());
    out.flush();
}

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 {//w w w  .j a  va2 s  . 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:edu.wfu.inotado.helper.MarshalHelper.java

/**
 * Pretty-Prints the XML//  ww w  .  j a  va  2  s .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:com.marklogic.client.impl.CombinedQueryBuilderImpl.java

private CombinedQueryDefinitionImpl parseCombinedQuery(RawCombinedQueryDefinition qdef) {
    DOMHandle handle = new DOMHandle();
    HandleAccessor.receiveContent(handle, HandleAccessor.contentAsString(qdef.getHandle()));
    Document combinedQueryXml = handle.get();
    DOMImplementationLS domImplementation = (DOMImplementationLS) combinedQueryXml.getImplementation();
    LSSerializer lsSerializer = domImplementation.createLSSerializer();
    lsSerializer.getDomConfig().setParameter("xml-declaration", false);

    NodeList nl = combinedQueryXml.getElementsByTagNameNS("http://marklogic.com/appservices/search", "options");
    Node n = nl.item(0);/*from  w  w  w .  j a v  a 2 s . co  m*/
    String options = null;
    StringHandle optionsHandle = null;
    if (n != null) {
        options = lsSerializer.writeToString(n);
        optionsHandle = new StringHandle(options).withFormat(Format.XML);
    }

    //TODO this could be more than one string...
    nl = combinedQueryXml.getElementsByTagNameNS("http://marklogic.com/appservices/search", "qtext");
    n = nl.item(0);
    String qtext = null;
    if (n != null) {
        qtext = lsSerializer.writeToString(n);
    }

    nl = combinedQueryXml.getElementsByTagNameNS("http://marklogic.com/appservices/search", "sparql");
    n = nl.item(0);
    String sparql = null;
    if (n != null) {
        sparql = lsSerializer.writeToString(n);
    }

    nl = combinedQueryXml.getElementsByTagNameNS("http://marklogic.com/appservices/search", "query");
    n = nl.item(0);
    String query = null;
    if (n != null) {
        query = lsSerializer.writeToString(nl.item(0));
    }
    StringHandle structuredQueryHandle = new StringHandle().with(query).withFormat(Format.XML);
    RawStructuredQueryDefinition structuredQueryDefinition = new RawQueryDefinitionImpl.Structured(
            structuredQueryHandle);
    return new CombinedQueryDefinitionImpl(structuredQueryDefinition, optionsHandle, qtext, sparql);
}

From source file:org.coronastreet.gpxconverter.Converter.java

private void dumpNode(Element e) {
    Document document = e.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    String str = serializer.writeToString(e);
    log("XML::: " + str);
}