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:nl.imvertor.common.file.XmlFile.java

public boolean isValid() {
    messages.removeAllElements();/*w  w w  .  j a va  2  s . com*/
    try {
        wfcode = WFCODE_OKAY;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(true);
        factory.setNamespaceAware(true);
        factory.setXIncludeAware(true);
        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                "http://www.w3.org/2001/XMLSchema");

        DocumentBuilder builder = factory.newDocumentBuilder();

        builder.setErrorHandler(this);
        builder.parse(new InputSource(this.getCanonicalPath()));
    } catch (Exception e) {
        wfcode = WFCODE_FATAL;
    }
    return wfcode < WFCODE_ERROR;
}

From source file:no.sesat.search.query.analyser.AnalysisRuleFactory.java

private AnalysisRuleFactory(final Context cxt) throws ParserConfigurationException {

    context = cxt;//from  ww w  . j av  a 2 s. co m
    try {
        INSTANCES_LOCK.writeLock().lock();

        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        final DocumentBuilder builder = factory.newDocumentBuilder();
        loader = context.newDocumentLoader(cxt, ANALYSIS_RULES_XMLFILE, builder);

        INSTANCES.put(context.getSite(), this);
    } finally {
        INSTANCES_LOCK.writeLock().unlock();
    }
}

From source file:org.adeptnet.auth.saml.SAMLClient.java

private DocumentBuilder createDocumentBuilder(boolean validating, boolean disAllowDocTypeDeclarations)
        throws ParserConfigurationException {
    final DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
    dfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
    if (disAllowDocTypeDeclarations) {
        dfactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    }//from ww  w  .  ja v  a2  s.  c  om
    dfactory.setValidating(validating);
    dfactory.setNamespaceAware(true);

    return dfactory.newDocumentBuilder();
}

From source file:org.alfresco.util.XMLUtil.java

public static DocumentBuilder getDocumentBuilder(final boolean namespaceAware, final boolean validating) {
    try {/*  w w w .  j  a v a  2  s.  co m*/
        final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(namespaceAware);
        dbf.setValidating(validating);
        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException pce) {
        LOGGER.error(pce);
        return null;
    }
}

From source file:org.alfresco.web.forms.FormDataFunctions.java

private Document parseXML(InputStream is) throws IOException, SAXException {
    Document result = null;// w ww  .j  a va 2 s  .com
    try {
        DocumentBuilderFactory localDbf = dbf.get();
        if (localDbf == null) {
            localDbf = DocumentBuilderFactory.newInstance();
        }
        localDbf.setNamespaceAware(true);
        localDbf.setValidating(false);
        dbf.set(localDbf);
        DocumentBuilder builder = localDbf.newDocumentBuilder();
        result = builder.parse(is);
    } catch (ParserConfigurationException pce) {
        LOGGER.error(pce);
    }

    return result;
}

From source file:org.ambraproject.service.crossref.CrossRefLookupServiceImpl.java

private Document getArticle(String doi) throws FileStoreException {
    String fsid = fileStoreService.objectIDMapper().doiTofsid(doi, "XML");
    Document doc;//  w  w  w.  j  a  v  a 2 s  .c o  m

    InputStream is = fileStoreService.getFileInStream(fsid);

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(false);

        DocumentBuilder builder = factory.newDocumentBuilder();
        EntityResolver resolver = CachedSource.getResolver(XMLServiceImpl.NLM_DTD_URL);
        builder.setEntityResolver(resolver);

        doc = builder.parse(is);
    } catch (Exception e) {
        log.error("Error parsing the article xml for article " + doi, e);
        return null;
    }

    return doc;
}

From source file:org.apache.beehive.netui.util.config.internal.catalog.CatalogParser.java

public CatalogConfig parse(final String resourcePath, final InputStream inputStream) {
    CatalogConfig catalogConfig = null;// ww w . j a va  2s.c om
    InputStream xmlInputStream = null;
    InputStream xsdInputStream = null;
    try {
        xmlInputStream = inputStream;
        xsdInputStream = getClass().getClassLoader().getResourceAsStream(CONFIG_SCHEMA);

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);
        dbf.setNamespaceAware(true);
        dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
        dbf.setAttribute(JAXP_SCHEMA_SOURCE, xsdInputStream);

        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setErrorHandler(new ErrorHandler() {
            public void warning(SAXParseException exception) {
                LOG.info("Validation warning validating config file \"" + resourcePath
                        + "\" against XML Schema \"" + CONFIG_SCHEMA);
            }

            public void error(SAXParseException exception) {
                throw new RuntimeException("Validation errors occurred parsing the config file \""
                        + resourcePath + "\".  Cause: " + exception, exception);
            }

            public void fatalError(SAXParseException exception) {
                throw new RuntimeException("Validation errors occurred parsing the config file \""
                        + resourcePath + "\".  Cause: " + exception, exception);
            }
        });

        db.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId) {
                if (systemId.endsWith("/catalog-config.xsd")) {
                    InputStream inputStream = getClass().getClassLoader().getResourceAsStream(CONFIG_SCHEMA);
                    return new InputSource(inputStream);
                } else
                    return null;
            }
        });

        Document document = db.parse(xmlInputStream);
        Element catalogElement = document.getDocumentElement();
        catalogConfig = parse(catalogElement);

    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Error occurred parsing the config file \"" + resourcePath + "\"", e);
    } catch (IOException e) {
        throw new RuntimeException("Error occurred parsing the config file \"" + resourcePath + "\"", e);
    } catch (SAXException e) {
        throw new RuntimeException("Error occurred parsing the config file \"" + resourcePath + "\"", e);
    } finally {
        try {
            if (xsdInputStream != null)
                xsdInputStream.close();
        } catch (IOException e) {
        }
    }

    return catalogConfig;
}

From source file:org.apache.cocoon.xml.dom.DOMUtil.java

/**
 * Create a new empty DOM document./*  w ww.jav  a  2s . co  m*/
 */
public static Document createDocument() throws ProcessingException {
    try {
        DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
        documentFactory.setNamespaceAware(true);
        documentFactory.setValidating(false);
        DocumentBuilder docBuilder = documentFactory.newDocumentBuilder();
        return docBuilder.newDocument();
    } catch (ParserConfigurationException pce) {
        throw new ProcessingException("Creating document failed.", pce);
    }
}

From source file:org.apache.fop.render.svg.SVGDocumentHandler.java

/** {@inheritDoc} */
public void startDocument() throws IFException {
    super.startDocument();
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    builderFactory.setValidating(false);
    try {/* ww w  . j  a  v a 2s. co m*/
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        this.reusedParts = builder.newDocument();
    } catch (ParserConfigurationException e) {
        throw new IFException("Error while setting up a DOM for SVG generation", e);
    }

    try {
        TransformerHandler toDOMHandler = tFactory.newTransformerHandler();
        toDOMHandler.setResult(new DOMResult(this.reusedParts));
        this.handler = decorate(toDOMHandler);
        this.handler.startDocument();
    } catch (SAXException se) {
        throw new IFException("SAX error in startDocument()", se);
    } catch (TransformerConfigurationException e) {
        throw new IFException("Error while setting up a TransformerHandler for SVG generation", e);
    }
}

From source file:org.apache.geronimo.console.databasemanager.wizard.DatabasePoolPortlet.java

private ResourceAdapterParams loadConfigPropertiesByAbstractName(PortletRequest request, String rarPath,
        String abstractName) {//w  ww .  j av  a2 s .  co m
    ResourceAdapterModule module = (ResourceAdapterModule) PortletManager.getManagedBean(request,
            new AbstractName(URI.create(abstractName)));
    String dd = module.getDeploymentDescriptor();
    DocumentBuilderFactory factory = XmlUtil.newDocumentBuilderFactory();
    factory.setValidating(false);
    factory.setNamespaceAware(true);
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        final StringReader reader = new StringReader(dd);
        Document doc = builder.parse(new InputSource(reader));
        reader.close();
        Element elem = doc.getDocumentElement(); // connector
        String displayName = getFirstText(elem.getElementsByTagName("display-name"));
        String description = getFirstText(elem.getElementsByTagName("description"));
        elem = (Element) elem.getElementsByTagName("resourceadapter").item(0);
        elem = (Element) elem.getElementsByTagName("outbound-resourceadapter").item(0);
        NodeList defs = elem.getElementsByTagName("connection-definition");
        List<ConfigParam> all = new ArrayList<ConfigParam>();
        for (int i = 0; i < defs.getLength(); i++) {
            final Element def = (Element) defs.item(i);
            String iface = getFirstText(def.getElementsByTagName("connectionfactory-interface")).trim();
            if (iface.equals("javax.sql.DataSource")) {
                NodeList configs = def.getElementsByTagName("config-property");
                for (int j = 0; j < configs.getLength(); j++) {
                    Element config = (Element) configs.item(j);
                    String name = getFirstText(config.getElementsByTagName("config-property-name")).trim();
                    String type = getFirstText(config.getElementsByTagName("config-property-type")).trim();
                    String test = getFirstText(config.getElementsByTagName("config-property-value"));
                    String value = test == null ? null : test.trim();
                    test = getFirstText(config.getElementsByTagName("description"));
                    String desc = test == null ? null : test.trim();
                    all.add(new ConfigParam(name, type, desc, value));
                }
            }
        }
        return new ResourceAdapterParams(displayName, description, rarPath,
                all.toArray(new ConfigParam[all.size()]));
    } catch (Exception e) {
        log.error("Unable to read resource adapter DD", e);
        return null;
    }
}