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

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

Introduction

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

Prototype

String PROPERTY

To view the source code for org.w3c.dom.bootstrap DOMImplementationRegistry PROPERTY.

Click Source Link

Document

The system property to specify the DOMImplementationSource class names.

Usage

From source file:DOM3.java

public static void main(String[] argv) {

    if (argv.length == 0) {
        printUsage();/*  ww  w.java 2s  .  co  m*/
        System.exit(1);
    }

    try {

        // get DOM Implementation using DOM Registry
        System.setProperty(DOMImplementationRegistry.PROPERTY,
                "org.apache.xerces.dom.DOMXSImplementationSourceImpl");
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

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

        // create DOMBuilder
        builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

        DOMConfiguration config = builder.getDomConfig();

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

        // create filter
        LSParserFilter filter = new DOM3();

        builder.setFilter(filter);

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

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

        // set schema language
        config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema");
        // config.setParameter("psvi",Boolean.TRUE);
        // config.setParameter("schema-type","http://www.w3.org/TR/REC-xml");

        // set schema location
        config.setParameter("schema-location", "personal.xsd");

        // parse document
        System.out.println("Parsing " + argv[0] + "...");
        Document doc = builder.parseURI(argv[0]);

        // set error handler on the Document
        config = doc.getDomConfig();

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

        // set validation feature
        config.setParameter("validate", Boolean.TRUE);
        config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema");
        // config.setParameter("schema-type","http://www.w3.org/TR/REC-xml");
        config.setParameter("schema-location", "data/personal.xsd");

        // remove comments from the document
        config.setParameter("comments", Boolean.FALSE);

        System.out.println("Normalizing document... ");
        doc.normalizeDocument();

        // create DOMWriter
        LSSerializer domWriter = impl.createLSSerializer();

        System.out.println("Serializing document... ");
        config = domWriter.getDomConfig();
        config.setParameter("xml-declaration", Boolean.FALSE);
        // config.setParameter("validate",errorHandler);

        // serialize document to standard output
        // domWriter.writeNode(System.out, doc);
        LSOutput dOut = impl.createLSOutput();
        dOut.setByteStream(System.out);
        domWriter.write(doc, dOut);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:gov.nih.nci.cacis.common.util.ExtractSchematron.java

/**
 * Main method, expect at least 4 arguments: location of top-level service XSD, location of ISO datatypes XSD
 * (optionally followed by a comma and the location of flavors XSD), and the names of the concrete and abstract
 * schematron schema files to output. Optionally can also provide a 5th argument: a comma-separated list of
 * additional rule files to bring in.//www  . j  a v  a  2 s. c o  m
 * 
 * @param args the command-line arguments
 */
public static void main(String[] args) {
    if (args.length < 4) {
        LOG.warn(USAGE);
        System.exit(-1);
    } else {
        System.setProperty(DOMImplementationRegistry.PROPERTY,
                "org.apache.xerces.dom.DOMXSImplementationSourceImpl");
        try {
            String ruleFiles = null; // NOPMD to remove DD-anomaly warning
            if (args.length > 4) {
                ruleFiles = args[4];
            }
            new ExtractSchematron().extract(args[0], args[1], args[2], args[3], ruleFiles);
            // CHECKSTYLE:OFF it's a main method, so may as well catch the generic exception
        } catch (final Exception e) {
            // CHECKSTYLE:ON
            LOG.error("Error extracting ", e);
        }
    }
}

From source file:Main.java

public static String serializeNode(Node node) throws LSException, IllegalAccessException, DOMException,
        InstantiationException, ClassNotFoundException, ClassCastException {
    System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMImplementationSourceImpl");
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSSerializer writer = impl.createLSSerializer();
    String serializedElement = writer.writeToString(node);
    return serializedElement;
}

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

private static Element convertXmlToElementWorker(String xml) throws Exception {
    Element element = null;/*from   ww w . j av 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

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.ja va  2 s  .com
    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  ww  w.j a va  2  s  .com
    return serializedElement;
}

From source file:it.unibas.spicy.persistence.xml.operators.GenerateXSDNodeTree.java

private XSModel initModel(String fileName) throws Exception {
    System.setProperty(DOMImplementationRegistry.PROPERTY,
            "org.apache.xerces.dom.DOMXSImplementationSourceImpl");
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader");
    XSLoader schemaLoader = impl.createXSLoader(null);
    DOMConfiguration config = schemaLoader.getConfig();
    config.setParameter("validate", Boolean.TRUE);
    return schemaLoader.loadURI(fileName);
}

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 {//from ww w.  j a va 2s. c om
        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);
}