Example usage for javax.xml.parsers DocumentBuilderFactory setValidating

List of usage examples for javax.xml.parsers DocumentBuilderFactory setValidating

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory setValidating.

Prototype


public void setValidating(boolean validating) 

Source Link

Document

Specifies that the parser produced by this code will validate documents as they are parsed.

Usage

From source file:org.chiba.xml.xforms.Instance.java

/**
 * Returns a new created instance document.
 * <p/>/*from  w  ww  .  j a va2 s.c  om*/
 * If this instance has an original instance, it will be imported into this new document. Otherwise the new document
 * is left empty.
 *
 * @return a new created instance document.
 */
private Document createInstanceDocument() throws XFormsException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(false);
        Document document = factory.newDocumentBuilder().newDocument();

        if (this.initialInstance != null) {
            document.appendChild(document.importNode(this.initialInstance.cloneNode(true), true));

            if (!this.element.hasAttributeNS(NamespaceCtx.XFORMS_NS, SRC_ATTRIBUTE)) {
                // apply namespaces
                NamespaceCtx.applyNamespaces(this.element, document.getDocumentElement());
            }
        }

        return document;
    } catch (Exception e) {
        throw new XFormsException(e);
    }
}

From source file:org.chiba.xml.xforms.test.ChibaBeanTest.java

/**
 * Sets up the test.//w w  w. ja  va  2 s . c o  m
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    this.form = builder.parse(getClass().getResourceAsStream("ChibaBeanTest.xhtml"));
    this.instance = builder.parse(getClass().getResourceAsStream("ChibaBeanTestInstance.xml"));

    String path = getClass().getResource("ChibaBeanTest.xhtml").getPath();
    this.baseURI = "file://" + path.substring(0, path.lastIndexOf("ChibaBeanTest.xhtml"));

    this.processor = new ChibaBean();
}

From source file:org.chiba.xml.xforms.xpath.test.InstanceFactoryTest.java

/**
 * Sets up the test.//  ww  w . ja v  a 2 s  .co m
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);

    Document testDocument = factory.newDocumentBuilder()
            .parse(getClass().getResourceAsStream("InstanceFactoryTest.xml"));
    Element instanceElement = (Element) testDocument.getElementsByTagNameNS(NamespaceCtx.XFORMS_NS, "instance")
            .item(0);
    InstanceFactory instanceFactory = new InstanceFactory();
    instanceFactory.setNamespaceContext(instanceElement);

    this.document = factory.newDocumentBuilder().newDocument();
    this.context = JXPathContext.newContext(this.document);
    this.context.setFactory(instanceFactory);

    Map namespaces = NamespaceCtx.getAllNamespaces(instanceElement);
    Iterator iterator = namespaces.keySet().iterator();
    while (iterator.hasNext()) {
        String prefix = (String) iterator.next();
        String uri = (String) namespaces.get(prefix);

        this.context.registerNamespace(prefix, uri);
    }
}

From source file:org.chiba.xml.xforms.xpath.test.JXPathTest.java

/**
 * Sets up the test./*from   w  ww  .ja va2  s  . co  m*/
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(getClass().getResourceAsStream("JXPathTest.xhtml"));

    this.context = JXPathContext.newContext(document);
    this.context.registerNamespace("html", "http://www.w3.org/2002/06/xhtml2");
    this.context.registerNamespace("xf", "http://www.w3.org/2002/xforms");
    this.context.registerNamespace("ev", "http://www.w3.org/2001/xml-events");
    this.context.registerNamespace("", "");
    this.context.registerNamespace("my", "http://tempuri.org/my");
}

From source file:org.chiba.xml.xforms.xpath.test.PredicateTest.java

/**
 * Sets up the test./* w ww.j a v a2s.c o m*/
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);

    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(PredicateTest.class.getResourceAsStream("PredicateTest.xml"));
    this.rootContext = JXPathContext.newContext(document);

    this.pointer = this.rootContext.getPointer("/people/new/@name");
    this.relativeContext = this.rootContext.getRelativeContext(this.pointer);
}

From source file:org.chiba.xml.xforms.xpath.XPathCompiler.java

/**
 * __UNDOCUMENTED__/*from ww  w  .  j  a  va2s. co  m*/
 *
 * @param xpath __UNDOCUMENTED__
 * @return __UNDOCUMENTED__
 * @throws XFormsException __UNDOCUMENTED__
 */
public Document compile(String xpath) throws XFormsException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(false);

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.newDocument();
        Element root = document.createElement("xpath");
        root.setAttribute("expression", xpath);
        document.appendChild(root);

        Element result = (Element) Parser.parseExpression(xpath, new DOMCompiler(document));
        root.appendChild(result);

        return document;
    } catch (Exception e) {
        throw new XFormsException(e);
    }
}

From source file:org.chiba.xml.xpath.impl.JXPathDOMFactoryTest.java

/**
 * Sets up the test./*from w  w w.j  ava 2s . com*/
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);

    Document testDocument = factory.newDocumentBuilder()
            .parse(getClass().getResourceAsStream("JXPathDOMFactoryTest.xhtml"));
    Element instanceElement = (Element) testDocument
            .getElementsByTagNameNS(NamespaceConstants.XFORMS_NS, "instance").item(0);
    JXPathDOMFactory jxpathFactory = new JXPathDOMFactory();
    jxpathFactory.setNamespaceContext(instanceElement);

    this.document = factory.newDocumentBuilder().newDocument();
    this.context = JXPathContext.newContext(this.document);
    this.context.setFactory(jxpathFactory);

    Map namespaces = NamespaceResolver.getAllNamespaces(instanceElement);
    Iterator iterator = namespaces.keySet().iterator();
    while (iterator.hasNext()) {
        String prefix = (String) iterator.next();
        String uri = (String) namespaces.get(prefix);

        this.context.registerNamespace(prefix, uri);
    }
}

From source file:org.chiba.xml.xpath.impl.JXPathTest.java

/**
 * Sets up the test./*from  www  . java  2 s  .  c o  m*/
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(getClass().getResourceAsStream("JXPathTest.xhtml"));

    this.context = JXPathContext.newContext(document);
    this.context.registerNamespace("html", "http://www.w3.org/1999/xhtml");
    this.context.registerNamespace("xf", "http://www.w3.org/2002/xforms");
    this.context.registerNamespace("ev", "http://www.w3.org/2001/xml-events");
    this.context.registerNamespace("", "");
    this.context.registerNamespace("my", "http://tempuri.org/my");
}

From source file:org.codehaus.enunciate.modules.BasicAppModule.java

/**
 * Loads the node model for merging xml.
 *
 * @param inputStream The input stream of the xml.
 * @return The node model.// ww w .  j a v a 2s.  c o m
 */
protected Document loadMergeXml(InputStream inputStream) throws EnunciateException {
    Document doc;
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(false); //no namespace for the merging...
        builderFactory.setValidating(false);
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        builder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                //we don't want to validate or parse external dtds...
                return new InputSource(new StringReader(""));
            }
        });
        doc = builder.parse(inputStream);
    } catch (Exception e) {
        throw new EnunciateException("Error parsing web.xml file for merging", e);
    }
    return doc;
}

From source file:org.codehaus.enunciate.modules.docs.DocumentationDeploymentModule.java

private NodeModel loadNodeModel(File xml) throws EnunciateException {
    Document doc;/*from w  ww  .  j  a va  2  s .  c o m*/
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(false);
        builderFactory.setValidating(false);
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        builder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                //we don't want to validate or parse external dtds...
                return new InputSource(new StringReader(""));
            }
        });
        doc = builder.parse(new FileInputStream(xml));
    } catch (Exception e) {
        throw new EnunciateException("Error parsing " + xml, e);
    }

    NodeModel.simplify(doc);
    return NodeModel.wrap(doc.getDocumentElement());
}