List of usage examples for javax.xml.parsers DocumentBuilder setErrorHandler
public abstract void setErrorHandler(ErrorHandler eh);
From source file:org.jmingo.parser.xml.dom.ContextDefinitionParser.java
/** * {@inheritDoc}// w w w .j av a2 s . co m */ @Override public ContextDefinition parse(Path path) throws JMingoParserException { LOGGER.debug("parse context configuration: {}", path); ContextDefinition contextDefinition; try (InputStream is = new FileInputStream(path.toFile())) { DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); builder.setErrorHandler(parseErrorHandler); Document document = builder.parse(new InputSource(is)); contextDefinition = new ContextDefinition(); Element element = document.getDocumentElement(); contextDefinition.setQuerySetConfig(parseQuerySetConfigTag(element)); contextDefinition.setMongoConfig(parseMongoTag(element)); parseConvertersTag(contextDefinition, element); contextDefinition.setDefaultConverter(parseDefaultConverterTag(element)); } catch (Exception e) { throw new JMingoParserException(e); } return contextDefinition; }
From source file:org.jmingo.parser.xml.dom.QuerySetParser.java
/** * {@inheritDoc}//from w w w. j a v a2 s .c o m */ @Override public QuerySet parse(Path path) throws JMingoParserException { LOGGER.debug("parse query set: {}", path); QuerySet querySet; try (InputStream is = new FileInputStream(path.toFile())) { String checksum = FileUtils.checksum(path.toFile()); querySet = new QuerySet(); querySet.setChecksum(checksum); DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); builder.setErrorHandler(parseErrorHandler); Document document = builder.parse(new InputSource(is)); Element root = document.getDocumentElement(); parseConfigTag(root, querySet); parseQueryFragments(root, querySet); parseQueries(root, querySet); } catch (Exception e) { throw new JMingoParserException(e); } return querySet; }
From source file:org.kuali.kfs.sys.context.CheckModularization.java
protected Document generateDwrConfigDocument(String fileName) throws Exception { DefaultResourceLoader resourceLoader = new DefaultResourceLoader(ClassLoaderUtils.getDefaultClassLoader()); InputStream in = resourceLoader.getResource(fileName).getInputStream(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true);/*from w w w . ja v a 2 s. c o m*/ DocumentBuilder db = dbf.newDocumentBuilder(); db.setEntityResolver(new DTDEntityResolver()); db.setErrorHandler(new LogErrorHandler()); Document doc = db.parse(in); return doc; }
From source file:org.kuali.rice.core.impl.impex.xml.XmlIngesterServiceImpl.java
private static void validate(final XmlDoc xmlDoc, EntityResolver resolver) throws ParserConfigurationException, IOException, SAXException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true);/*w w w .j a v a2s . c o m*/ dbf.setNamespaceAware(true); dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI); DocumentBuilder db = dbf.newDocumentBuilder(); db.setEntityResolver(resolver); db.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException se) { LOG.warn("Warning parsing xml doc " + xmlDoc, se); addProcessingException(xmlDoc, "Warning parsing xml doc " + xmlDoc, se); } public void error(SAXParseException se) throws SAXException { LOG.error("Error parsing xml doc " + xmlDoc, se); addProcessingException(xmlDoc, "Error parsing xml doc " + xmlDoc, se); throw se; } public void fatalError(SAXParseException se) throws SAXException { LOG.error("Fatal error parsing xml doc " + xmlDoc, se); addProcessingException(xmlDoc, "Fatal error parsing xml doc " + xmlDoc, se); throw se; } }); db.parse(xmlDoc.getStream()); }
From source file:org.kuali.rice.ken.util.Util.java
/** * This method uses DOM to parse the input source of XML. * @param source the input source/* w ww. j a v a2s . c o m*/ * @param validate whether to turn on validation * @param namespaceAware whether to turn on namespace awareness * @return Document the parsed (possibly validated) document * @throws ParserConfigurationException * @throws IOException * @throws SAXException */ public static Document parse(final InputSource source, boolean validate, boolean namespaceAware, EntityResolver entityResolver) throws ParserConfigurationException, IOException, SAXException { // TODO: optimize this DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate); dbf.setNamespaceAware(namespaceAware); dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); DocumentBuilder db = dbf.newDocumentBuilder(); if (entityResolver != null) { db.setEntityResolver(entityResolver); } db.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException se) { LOG.warn("Warning parsing xml doc " + source, se); } public void error(SAXParseException se) throws SAXException { LOG.error("Error parsing xml doc " + source, se); throw se; } public void fatalError(SAXParseException se) throws SAXException { LOG.error("Fatal error parsing xml doc " + source, se); throw se; } }); return db.parse(source); }
From source file:org.kuali.rice.ken.xpath.XPathTest.java
protected Document getDocument(boolean namespaceAware, boolean validate) throws Exception { // TODO: optimize this final InputSource source = getTestXMLInputSource(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate);/*w w w .j a va2 s .co m*/ dbf.setNamespaceAware(namespaceAware); dbf.setAttribute(JAXPConstants.JAXP_SCHEMA_LANGUAGE, JAXPConstants.W3C_XML_SCHEMA); DocumentBuilder db = dbf.newDocumentBuilder(); LOG.info("Setting entityresolver"); db.setEntityResolver(Util.getNotificationEntityResolver(services.getNotificationContentTypeService())); db.setErrorHandler(new SimpleErrorHandler(LOG)); return db.parse(source); }
From source file:org.kuali.rice.kew.xml.DocumentTypeXmlParserTest.java
private boolean validate(String docName) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true);/*from www . ja v a 2 s. c om*/ dbf.setNamespaceAware(true); dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI); DocumentBuilder db = dbf.newDocumentBuilder(); db.setEntityResolver(new org.kuali.rice.core.impl.impex.xml.ClassLoaderEntityResolver()); db.setErrorHandler(new DefaultHandler() { @Override public void error(SAXParseException e) throws SAXException { this.fatalError(e); } @Override public void fatalError(SAXParseException e) throws SAXException { super.fatalError(e); } }); try { db.parse(getClass().getResourceAsStream(docName + ".xml")); return true; } catch (SAXException se) { log.error("Error validating " + docName + ".xml", se); return false; } }
From source file:ORG.oclc.os.SRW.SRUServerTester.java
public Document renderXML(String record) { Document document;/* w w w . j a v a 2 s . co m*/ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //factory.setValidating(true); factory.setNamespaceAware(true); try { DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new org.xml.sax.ErrorHandler() { // ignore fatal errors (an exception is guaranteed) @Override public void fatalError(SAXParseException exception) throws SAXException { } // treat validation errors as fatal @Override public void error(SAXParseException e) throws SAXParseException { throw e; } // dump warnings too @Override public void warning(SAXParseException err) throws SAXParseException { out("** Warning"); out(", line "); out(err.getLineNumber()); out(", uri "); out(err.getSystemId()); out('\n'); out(" "); out(err.getMessage()); out('\n'); } }); document = builder.parse(new InputSource(new StringReader(record))); } catch (java.io.IOException e) { out("</pre><pre class='red'>"); out("test failed: unable to parse record: "); out(e.getMessage()); out('\n'); out(record); out('\n'); out("</pre><pre>"); return null; } catch (javax.xml.parsers.ParserConfigurationException e) { out("</pre><pre class='red'>"); out("test failed: unable to parse record: "); out(e.getMessage()); out('\n'); out(record); out('\n'); out("</pre><pre>"); return null; } catch (org.xml.sax.SAXException e) { out("</pre><pre class='red'>"); out("test failed: unable to parse record: "); out(e.getMessage()); out('\n'); out(record); out('\n'); out("</pre><pre>"); return null; } return document; }
From source file:org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGeneratorTest.java
private void verifyXmlFiles(final Collection<File> xmlFiles) throws Exception { ErrorHandler errorHandler = new ErrorHandler() { @Override/* w w w . ja va 2s. co m*/ public void warning(final SAXParseException exception) throws SAXException { fail("Generated blueprint xml is not well formed " + exception.getMessage()); } @Override public void fatalError(final SAXParseException exception) throws SAXException { fail("Generated blueprint xml is not well formed " + exception.getMessage()); } @Override public void error(final SAXParseException exception) throws SAXException { fail("Generated blueprint xml is not well formed " + exception.getMessage()); } }; for (File file : xmlFiles) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(errorHandler); builder.parse(new InputSource(file.getPath())); } }
From source file:org.openspaces.admin.alert.config.parser.XmlAlertConfigurationParser.java
@Override public AlertConfiguration[] parse() throws AlertConfigurationParserException { List<AlertConfiguration> alertConfigurations = new ArrayList<AlertConfiguration>(); try {//from w ww.ja v a2 s . c o m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = factory.newDocumentBuilder(); docBuilder.setErrorHandler(new SimpleSaxErrorHandler()); Document doc = docBuilder.parse(new InputSource(is)); Element alertsElm = doc.getDocumentElement(); List<Element> list = getChildElementsByTagName(alertsElm, "alert"); for (Element alertElm : list) { String classAttr = alertElm.getAttribute("class"); String enabledAttr = alertElm.getAttribute("enabled"); if (logger.isTraceEnabled()) { logger.trace("alert: class=" + classAttr + " enabled=" + enabledAttr); logger.trace("properties: "); } Map<String, String> properties = new HashMap<String, String>(); List<Element> propertiesElm = getChildElementsByTagName(alertElm, "property"); for (Element property : propertiesElm) { String keyAttr = property.getAttribute("key"); String valueAttr = property.getAttribute("value"); properties.put(keyAttr, valueAttr); if (logger.isTraceEnabled()) { logger.trace("property: key=" + keyAttr + " value=" + valueAttr); } } Class<? extends AlertConfiguration> clazz = Thread.currentThread().getContextClassLoader() .loadClass(classAttr).asSubclass(AlertConfiguration.class); AlertConfiguration alertConfiguration = clazz.newInstance(); alertConfiguration.setEnabled(Boolean.parseBoolean(enabledAttr)); alertConfiguration.setProperties(properties); alertConfigurations.add(alertConfiguration); } } catch (Throwable t) { throw new AlertConfigurationParserException("Failed to parse configuration file", t); } finally { try { is.close(); } catch (IOException e) { // ignore } } return alertConfigurations.toArray(new AlertConfiguration[alertConfigurations.size()]); }