List of usage examples for javax.xml.parsers DocumentBuilder setErrorHandler
public abstract void setErrorHandler(ErrorHandler eh);
From source file:org.gudy.azureus2.pluginsimpl.local.utils.xml.simpleparser.SimpleXMLParserDocumentImpl.java
private void createSupport(InputStream input_stream) throws SimpleXMLParserDocumentException { try {/*from www. j a v a2 s. c o m*/ DocumentBuilderFactory dbf = getDBF(); // Step 2: create a DocumentBuilder that satisfies the constraints // specified by the DocumentBuilderFactory DocumentBuilder db = dbf.newDocumentBuilder(); // Set an ErrorHandler before parsing OutputStreamWriter errorWriter = new OutputStreamWriter(System.err); MyErrorHandler error_handler = new MyErrorHandler(new PrintWriter(errorWriter, true)); db.setErrorHandler(error_handler); db.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) { // System.out.println( publicId + ", " + systemId ); // handle bad DTD external refs try { URL url = new URL(systemId); if (source_url != null) { String net = AENetworkClassifier.categoriseAddress(source_url.getHost()); if (net != AENetworkClassifier.AT_PUBLIC) { if (AENetworkClassifier.categoriseAddress(url.getHost()) != net) { return new InputSource(new ByteArrayInputStream( "<?xml version='1.0' encoding='UTF-8'?>".getBytes())); } } } String host = url.getHost(); InetAddress.getByName(host); // try connecting too as connection-refused will also bork XML parsing InputStream is = null; try { URLConnection con = url.openConnection(); con.setConnectTimeout(15 * 1000); con.setReadTimeout(15 * 1000); is = con.getInputStream(); byte[] buffer = new byte[32]; int pos = 0; while (pos < buffer.length) { int len = is.read(buffer, pos, buffer.length - pos); if (len <= 0) { break; } pos += len; } String str = new String(buffer, "UTF-8").trim().toLowerCase(Locale.US); if (!str.contains("<?xml")) { // not straightforward to check for naked DTDs, could be lots of <!-- commentry preamble which of course can occur // in HTML too buffer = new byte[32000]; pos = 0; while (pos < buffer.length) { int len = is.read(buffer, pos, buffer.length - pos); if (len <= 0) { break; } pos += len; } str += new String(buffer, "UTF-8").trim().toLowerCase(Locale.US); if (str.contains("<html") && str.contains("<head")) { throw (new Exception("Bad DTD")); } } } catch (Throwable e) { return new InputSource( new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes())); } finally { if (is != null) { try { is.close(); } catch (Throwable e) { } } } return (null); } catch (UnknownHostException e) { return new InputSource( new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes())); } catch (Throwable e) { return (null); } } }); // Step 3: parse the input file document = db.parse(input_stream); SimpleXMLParserDocumentNodeImpl[] root_nodes = parseNode(document, false); int root_node_count = 0; // remove any processing instructions such as <?xml-stylesheet for (int i = 0; i < root_nodes.length; i++) { SimpleXMLParserDocumentNodeImpl node = root_nodes[i]; if (node.getNode().getNodeType() != Node.PROCESSING_INSTRUCTION_NODE) { root_node = node; root_node_count++; } } if (root_node_count != 1) { throw (new SimpleXMLParserDocumentException( "invalid document - " + root_nodes.length + " root elements")); } } catch (Throwable e) { throw (new SimpleXMLParserDocumentException(e)); } }
From source file:org.ikasan.component.validator.xml.XMLValidator.java
/** * Implementation of the onEvent XMLValidation * * @param source - source to be validated * @throws TransformationException - Thrown if error parsing payload content *///from w w w . j av a2 s .c o m public Object convert(SOURCE source) throws EndpointException { ValidationResult<SOURCE, TARGET> validationResult = new ValidationResult<>(); validationResult.setSource(source); if (configuration.isSkipValidation()) { if (configuration.isReturnValidationResult()) { validationResult.setResult(ValidationResult.Result.VALID); return validationResult; } else { return source; } } try { DocumentBuilder builder = this.factory.newDocumentBuilder(); builder.setErrorHandler(this.errorHandler); InputStream sourceAsInputStream = this.createSourceAsBytes(source); builder.parse(sourceAsInputStream); if (!configuration.isReturnValidationResult()) { return source; } validationResult.setResult(ValidationResult.Result.VALID); } catch (SAXException e) { if (configuration.isThrowExceptionOnValidationFailure() || !configuration.isReturnValidationResult()) { throw new ValidationException(generateErrorMessage(e, source), e); } validationResult.setResult(ValidationResult.Result.INVALID); validationResult.setException(e); } catch (IOException e) { if (configuration.isThrowExceptionOnValidationFailure() || !configuration.isReturnValidationResult()) { throw new ValidationException(e); } validationResult.setResult(ValidationResult.Result.INVALID); validationResult.setException(e); } catch (ParserConfigurationException e) { if (configuration.isThrowExceptionOnValidationFailure() || !configuration.isReturnValidationResult()) { throw new ValidationException(e); } validationResult.setResult(ValidationResult.Result.INVALID); validationResult.setException(e); } return validationResult; }
From source file:org.jboss.ejb3.entity.PersistenceXmlLoader.java
private static Document loadURL(URL configURL, EntityResolver resolver) throws Exception { InputStream is = configURL != null ? configURL.openStream() : null; if (is == null) { throw new IOException("Failed to obtain InputStream from url: " + configURL); }/*from ww w. j a v a 2 s. com*/ List errors = new ArrayList(); DocumentBuilderFactory docBuilderFactory = null; docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setValidating(true); docBuilderFactory.setNamespaceAware(true); try { //otherwise Xerces fails in validation docBuilderFactory.setAttribute("http://apache.org/xml/features/validation/schema", true); } catch (IllegalArgumentException e) { docBuilderFactory.setValidating(false); docBuilderFactory.setNamespaceAware(false); } InputSource source = new InputSource(is); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); docBuilder.setEntityResolver(resolver); docBuilder.setErrorHandler(new PersistenceXmlLoader.ErrorLogger("XML InputStream", errors, resolver)); Document doc = docBuilder.parse(source); if (errors.size() != 0) { throw new PersistenceException("invalid persistence.xml", (Throwable) errors.get(0)); } return doc; }
From source file:org.jboss.elasticsearch.river.remote.sitemap.SiteMapParser.java
/** * Parse the given XML content.// ww w .ja v a2s . co m * * @param sitemapUrl * @param is * @throws UnknownFormatException */ private AbstractSiteMap processXml(URL sitemapUrl, InputSource is) throws UnknownFormatException { Document doc = null; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); db.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException exception) throws SAXException { logger.warn("Sitemap XML warning: " + exception.getMessage()); } @Override public void fatalError(SAXParseException exception) throws SAXException { logger.warn("Sitemap XML fatalError: " + exception.getMessage()); } @Override public void error(SAXParseException exception) throws SAXException { logger.warn("Sitemap XML error: " + exception.getMessage()); } }); doc = db.parse(is); } catch (Exception e) { throw new UnknownFormatException("Error parsing XML for " + sitemapUrl); } // See if this is a sitemap index NodeList nodeList = doc.getElementsByTagName("sitemapindex"); if (nodeList.getLength() > 0) { nodeList = doc.getElementsByTagName("sitemap"); return parseSitemapIndex(sitemapUrl, nodeList); } else if (doc.getElementsByTagName("urlset").getLength() > 0) { // This is a regular Sitemap return parseXmlSitemap(sitemapUrl, doc); } else if (doc.getElementsByTagName("link").getLength() > 0) { // Could be RSS or Atom return parseSyndicationFormat(sitemapUrl, doc); } throw new UnknownFormatException("Unknown XML format for " + sitemapUrl); }
From source file:org.jbpm.bpel.tools.WebAppDescriptorTool.java
private Document readWebServicesDescriptor() { DocumentBuilder documentBuilder = XmlUtil.getDocumentBuilder(); // capture parse errors in our problem handler documentBuilder.setErrorHandler(problemHandler.asSaxErrorHandler()); Document document = null;// w w w. j a v a 2 s.co m try { document = documentBuilder.parse(webServicesDescriptorFile); } catch (SAXException e) { problemHandler.add(new Problem(Problem.LEVEL_ERROR, "web services descriptor contains invalid xml: " + webServicesDescriptorFile, e)); } catch (IOException e) { problemHandler.add(new Problem(Problem.LEVEL_ERROR, "web services descriptor is not readable: " + webServicesDescriptorFile, e)); } finally { // reset error handling behavior documentBuilder.setErrorHandler(null); } return document; }
From source file:org.jbpm.bpel.tools.WebServicesDescriptorTool.java
private Document readJaxrpcMappingDocument() { DocumentBuilder documentBuilder = XmlUtil.getDocumentBuilder(); // capture parse errors in our problem handler documentBuilder.setErrorHandler(problemHandler.asSaxErrorHandler()); Document document = null;/*from w w w. j ava2 s. c om*/ try { document = documentBuilder.parse(jaxrpcMappingFile); } catch (SAXException e) { problemHandler.add(new Problem(Problem.LEVEL_ERROR, "jax-rpc mapping document contains invalid xml: " + jaxrpcMappingFile, e)); } catch (IOException e) { problemHandler.add(new Problem(Problem.LEVEL_ERROR, "jax-rpc mapping document is not readable: " + jaxrpcMappingFile, e)); } finally { // reset error handling behavior documentBuilder.setErrorHandler(null); } return document; }
From source file:org.jbpm.bpel.xml.BpelReader.java
/** * Reads a BPEL document into a process definition. * @param processDefinition the definition to read into * @param source an input source pointing to the BPEL document */// w w w . j av a 2 s .co m public void read(BpelProcessDefinition processDefinition, InputSource source) { DocumentBuilder documentBuilder = XmlUtil.getDocumentBuilder(); // capture parse errors in our problem handler documentBuilder.setErrorHandler(problemHandler.asSaxErrorHandler()); // save the document location String location = source.getSystemId(); processDefinition.setLocation(location); try { // parse content Document document = documentBuilder.parse(source); // halt on parse errors if (problemHandler.getProblemCount() > 0) return; // prepare a locator of imported documents, relative to the process document location ProcessWsdlLocator wsdlLocator = null; if (location != null) { try { wsdlLocator = new ProcessWsdlLocator(new URI(location)); wsdlLocator.setProblemHandler(problemHandler); } catch (URISyntaxException e) { problemHandler.add(new Problem(Problem.LEVEL_ERROR, "source system identifier is invalid: " + location, e)); } } // read process definition read(processDefinition, document.getDocumentElement(), wsdlLocator); } catch (SAXException e) { problemHandler .add(new Problem(Problem.LEVEL_ERROR, "bpel document contains invalid xml: " + location, e)); } catch (IOException e) { problemHandler.add(new Problem(Problem.LEVEL_ERROR, "bpel document is not readable: " + location, e)); } finally { // reset error handling behavior documentBuilder.setErrorHandler(null); } }
From source file:org.jbpm.bpel.xml.BpelReader.java
/** * Reads a process archive containing a BPEL document into a process definition. * @param processDefinition the definition to read into; its <code>location</code> property * specifies the process document entry within the archive * @param archive the archive to read//from ww w. ja va 2s .c om */ public void read(BpelProcessDefinition processDefinition, ProcessArchive archive) { String location = processDefinition.getLocation(); byte[] bpelEntry = archive.getEntry(location); // check whether the bpel document exists in the archive if (bpelEntry == null) { problemHandler.add(new Problem(Problem.LEVEL_ERROR, "process document entry not found")); return; } DocumentBuilder documentBuilder = XmlUtil.getDocumentBuilder(); // capture parse errors in our problem handler documentBuilder.setErrorHandler(problemHandler.asSaxErrorHandler()); try { // parse content Document document = documentBuilder.parse(new ByteArrayInputStream(bpelEntry)); // halt on parse errors if (problemHandler.getProblemCount() > 0) return; /* * prepare a locator of imported documents, relative to the process document location within * the archive */ ProcessWsdlLocator wsdlLocator = new ProcessArchiveWsdlLocator(new URI(location), archive); wsdlLocator.setProblemHandler(problemHandler); // read process definition read(processDefinition, document.getDocumentElement(), wsdlLocator); } catch (SAXException e) { problemHandler .add(new Problem(Problem.LEVEL_ERROR, "bpel document contains invalid xml: " + location, e)); } catch (IOException e) { problemHandler.add(new Problem(Problem.LEVEL_ERROR, "bpel document is not readable: " + location, e)); } catch (URISyntaxException e) { problemHandler.add( new Problem(Problem.LEVEL_ERROR, "process definition location is invalid: " + location, e)); } finally { // reset error handling behavior documentBuilder.setErrorHandler(null); } }
From source file:org.jbpm.bpel.xml.DeploymentDescriptorReader.java
public void read(DeploymentDescriptor deploymentDescriptor, InputSource source) { // get the thread-local parser DocumentBuilder builder = XmlUtil.getDocumentBuilder(); // install our problem handler as document parser's error handler builder.setErrorHandler(problemHandler.asSaxErrorHandler()); try {/*from w ww .jav a 2s .com*/ // parse content Document descriptorDoc = builder.parse(source); // halt on parse errors if (problemHandler.getProblemCount() > 0) return; Element descriptorElem = descriptorDoc.getDocumentElement(); // global scope readScope(descriptorElem, deploymentDescriptor); // target namespace String targetNamespace = XmlUtil.getAttribute(descriptorElem, BpelConstants.ATTR_TARGET_NAMESPACE); if (targetNamespace != null) deploymentDescriptor.setTargetNamespace(targetNamespace); // version String version = XmlUtil.getAttribute(descriptorElem, BpelConstants.ATTR_VERSION); if (version != null) deploymentDescriptor.setVersion(Integer.valueOf(version)); // service catalogs Element catalogsElem = XmlUtil.getElement(descriptorElem, BpelConstants.NS_DEPLOYMENT_DESCRIPTOR, BpelConstants.ELEM_SERVICE_CATALOGS); if (catalogsElem != null) { ServiceCatalog catalog = readServiceCatalogs(catalogsElem, source.getSystemId()); deploymentDescriptor.setServiceCatalog(catalog); } } catch (SAXException e) { problemHandler.add(new Problem(Problem.LEVEL_ERROR, "application descriptor contains invalid xml", e)); } catch (IOException e) { problemHandler.add(new Problem(Problem.LEVEL_ERROR, "application descriptor is not readable", e)); } finally { // reset error handling behavior builder.setErrorHandler(null); } }
From source file:org.jbpm.bpel.xml.ProcessWsdlLocator.java
private void upgradeWsdlDocumentIfNeeded(InputSource source) { // get the thread-local document parser DocumentBuilder documentParser = XmlUtil.getDocumentBuilder(); // install our problem handler as document parser's error handler documentParser.setErrorHandler(problemHandler.asSaxErrorHandler()); // parse content Document document;// w w w. j av a 2 s . c o m try { document = documentParser.parse(source); // halt on parse errors if (problemHandler.getProblemCount() > 0) return; } catch (IOException e) { Problem problem = new Problem(Problem.LEVEL_ERROR, "document is not readable", e); problem.setResource(latestImportURI); problemHandler.add(problem); return; } catch (SAXException e) { Problem problem = new Problem(Problem.LEVEL_ERROR, "document contains invalid xml", e); problem.setResource(latestImportURI); problemHandler.add(problem); return; } finally { // reset error handling behavior documentParser.setErrorHandler(null); } // check whether the wsdl document requires upgrading if (hasUpgradableElements(document)) { try { // create wsdl upgrader Transformer wsdlUpgrader = getWsdlUpgradeTemplates().newTransformer(); // install our problem handler as transformer's error listener wsdlUpgrader.setErrorListener(problemHandler.asTraxErrorListener()); // upgrade into memory stream ByteArrayOutputStream resultStream = new ByteArrayOutputStream(); wsdlUpgrader.transform(new DOMSource(document), new StreamResult(resultStream)); // replace existing source with upgraded document source.setByteStream(new ByteArrayInputStream(resultStream.toByteArray())); log.debug("upgraded wsdl document: " + latestImportURI); } catch (TransformerException e) { Problem problem = new Problem(Problem.LEVEL_ERROR, "wsdl upgrade failed", e); problem.setResource(latestImportURI); problemHandler.add(problem); } } else { // if the source is a stream, reset it InputStream sourceStream = source.getByteStream(); if (sourceStream != null) { try { sourceStream.reset(); } catch (IOException e) { log.error("could not reset source stream: " + latestImportURI, e); } } } }