Example usage for org.w3c.dom DOMConfiguration setParameter

List of usage examples for org.w3c.dom DOMConfiguration setParameter

Introduction

In this page you can find the example usage for org.w3c.dom DOMConfiguration setParameter.

Prototype

public void setParameter(String name, Object value) throws DOMException;

Source Link

Document

Set the value of a parameter.

Usage

From source file:DOM3.java

public static void main(String[] argv) {

    if (argv.length == 0) {
        printUsage();/*  ww  w . j  a  va 2  s  .  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:Main.java

/**
 * Set standard DOM configuration.// w  w w. jav  a 2 s .c  om
 *
 * @param document The document to set the configuration for.
 */
public static void setConfigParams(Document document) {
    DOMConfiguration config = document.getDomConfig();
    config.setParameter(DOM_CONFIG_COMMENTS, true);
    config.setParameter(DOM_CONFIG_ELEMENT_CONTENT_WHITESPACE, true);
    if (config.canSetParameter(DOM_CONFIG_CANONICAL_FORM, true)) {
        config.setParameter(DOM_CONFIG_CANONICAL_FORM, true);
    }
}

From source file:Main.java

public static final String prettyPrint(final Document aNode) throws Exception {
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

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

    // Prepare the output
    LSOutput domOutput = impls.createLSOutput();
    domOutput.setEncoding(java.nio.charset.Charset.defaultCharset().name());
    StringWriter writer = new StringWriter();
    domOutput.setCharacterStream(writer);
    LSSerializer domWriter = impls.createLSSerializer();
    DOMConfiguration domConfig = domWriter.getDomConfig();
    domConfig.setParameter("format-pretty-print", true);
    domConfig.setParameter("element-content-whitespace", true);
    domWriter.setNewLine("\r\n");
    domConfig.setParameter("cdata-sections", Boolean.TRUE);
    // And finaly, write
    domWriter.write(aNode, domOutput);//from  w  w  w  . j a va 2s. c  om
    return domOutput.getCharacterStream().toString();
}

From source file:Main.java

private static Document parse(LSInput input, boolean validateIfSchema) {
    DOMImplementationLS impl = getDOMImpl();
    LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
    DOMConfiguration config = parser.getDomConfig();
    config.setParameter("element-content-whitespace", false);
    config.setParameter("namespaces", true);
    config.setParameter("validate-if-schema", validateIfSchema);
    return parser.parse(input);
}

From source file:Main.java

private static void serializeXML(Element doc, Writer writer, boolean addXmlDeclaration) throws IOException {
    try {/*from   www .  j a  va2  s  .  c  o  m*/
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer serializer = impl.createLSSerializer();
        DOMConfiguration config = serializer.getDomConfig();
        config.setParameter("xml-declaration", addXmlDeclaration);
        // config.setParameter("format-pretty-print", true);
        // config.setParameter("normalize-characters", true);
        LSOutput out = impl.createLSOutput();
        out.setCharacterStream(writer);
        serializer.write(doc, out);
    } catch (Throwable e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

public static String serialize(Document document, boolean prettyPrint) {
    DOMImplementationLS impl = getDOMImpl();
    LSSerializer serializer = impl.createLSSerializer();
    // document.normalizeDocument();
    DOMConfiguration config = serializer.getDomConfig();
    if (prettyPrint && config.canSetParameter("format-pretty-print", Boolean.TRUE)) {
        config.setParameter("format-pretty-print", true);
    }//from   ww w .  ja v a2s .c o m
    config.setParameter("xml-declaration", true);
    LSOutput output = impl.createLSOutput();
    output.setEncoding("UTF-8");
    Writer writer = new StringWriter();
    output.setCharacterStream(writer);
    serializer.write(document, output);
    return writer.toString();
}

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 . java  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

/**
 * Obtain a the DOM, level 3, Load/Save serializer {@link LSSerializer} instance from the
 * given {@link DOMImplementationLS} instance.
 * /*from  w w  w  . j ava 2  s.  c om*/
 * <p>
 * The serializer instance will be configured with the parameters passed as the <code>serializerParams</code>
 * argument. It will also be configured with an {@link LSSerializerFilter} that shows all nodes to the filter, 
 * and accepts all nodes shown.
 * </p>
 * 
 * @param domImplLS the DOM Level 3 Load/Save implementation to use
 * @param serializerParams parameters to pass to the {@link DOMConfiguration} of the serializer
 *         instance, obtained via {@link LSSerializer#getDomConfig()}. May be null.
 *         
 * @return a new LSSerializer instance
 */
public static LSSerializer getLSSerializer(DOMImplementationLS domImplLS,
        Map<String, Object> serializerParams) {
    LSSerializer serializer = domImplLS.createLSSerializer();

    serializer.setFilter(new LSSerializerFilter() {

        public short acceptNode(Node arg0) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return SHOW_ALL;
        }
    });

    if (serializerParams != null) {
        DOMConfiguration serializerDOMConfig = serializer.getDomConfig();
        for (String key : serializerParams.keySet()) {
            serializerDOMConfig.setParameter(key, serializerParams.get(key));
        }
    }

    return serializer;
}

From source file:com.lostinsoftware.xsdparser.XSDParser.java

private static XSDElement parseXSD(InputStream inputStream, List<String> elements, boolean allElements)
        throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException,
        ClassCastException {/*w ww .  j a v a  2  s.c  o  m*/

    XSDElement mainElement = null;

    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

    XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader");

    XSLoader schemaLoader = impl.createXSLoader(null);

    DOMConfiguration config = schemaLoader.getConfig();

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

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

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

    // parse document
    LSInput input = new DOMInputImpl();
    input.setByteStream(inputStream);
    XSModel model = schemaLoader.load(input);
    if (model != null) {
        // Main element
        XSElementDeclaration element = model.getElementDeclaration(elements.get(0), null);
        mainElement = new XSDElement();
        mainElement.setName(elements.get(0));
        mainElement.setXsDeclaration(element);
        processElement(mainElement, elements, allElements);
    }
    return mainElement;
}

From source file:eu.semaine.util.XMLTool.java

/**
 * Document type to String format conversion 
 * @param document//from www  . j a v a  2 s  .c o  m
 * @return
 * @throws Exception
 * @throws FileNotFoundException
 */
public static String document2String(Document document) throws SystemConfigurationException {
    LSSerializer serializer;
    DOMImplementationLS domImplLS;
    try {
        DOMImplementation implementation = DOMImplementationRegistry.newInstance()
                .getDOMImplementation("XML 3.0");
        domImplLS = (DOMImplementationLS) implementation.getFeature("LS", "3.0");
        serializer = domImplLS.createLSSerializer();
        DOMConfiguration config = serializer.getDomConfig();
        config.setParameter("format-pretty-print", Boolean.TRUE);
        //config.setParameter("canonical-form", Boolean.TRUE);
    } catch (Exception e) {
        throw new SystemConfigurationException("Problem instantiating XML serializer code", e);
    }
    LSOutput output = domImplLS.createLSOutput();
    output.setEncoding("UTF-8");
    StringWriter buf = new StringWriter();
    output.setCharacterStream(buf);
    serializer.write(document, output);
    return buf.toString();
}