[jboss-svn-commits] JBL Code SVN: r8283 - labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/config
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Dec 13 06:56:01 EST 2006
Author: ddegroff
Date: 2006-12-13 06:56:00 -0500 (Wed, 13 Dec 2006)
New Revision: 8283
Added:
labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/config/XmlValidation.java
Log:
Rough version of XmlValidation.
Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/config/XmlValidation.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/config/XmlValidation.java 2006-12-13 11:42:01 UTC (rev 8282)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/config/XmlValidation.java 2006-12-13 11:56:00 UTC (rev 8283)
@@ -0,0 +1,217 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.soa.esb.listeners.config;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashSet;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.Validator;
+import javax.xml.XMLConstants;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
+/**
+ * A Helper classes used to validate xml files against supplied schemas.
+ *
+ * @author $Revision$
+ * $Id$
+ */
+public class XmlValidation implements XmlValidator {
+
+ /**
+ * Our Logger
+ */
+ private Logger log = Logger.getLogger(this.getClass());
+
+ private InputSource xmlSource, validationSource;
+
+ /**
+ * The path separator
+ */
+ private static String SEP = System.getProperty("file.separator");
+
+ private boolean isValid = true;
+
+ private Collection<String> validationResults = new HashSet<String>();
+
+ private Document xmlDocument;
+
+ public XmlValidation() {
+ }
+
+ /**
+ * Validation method used to validate an xml file against a default xsd (jbossesb.xsd).
+ * @param xmlSource The xml input source to be validated.
+ * @return Boolean true/false indicating successful validation.
+ * @throws XmlValidatorException Failure during validation.
+ */
+ public boolean validate(InputSource xmlSource) throws XmlValidatorException {
+ boolean isValid = false;
+
+ setXMLDocument(xmlSource);
+
+ try {
+ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+ Source schemaFile = new StreamSource("/jboss-esb/product/jbossesb.xsd");
+ Schema schema = factory.newSchema(schemaFile);
+ // Request validation
+ Validator validator = schema.newValidator();
+ // Register the error handler
+ validator.setErrorHandler(new XmlErrorHandler());
+
+ try {
+ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder parser = builderFactory.newDocumentBuilder();
+
+ validator.validate(new DOMSource(parser.parse(xmlSource)));
+ isValid = true;
+ } catch (SAXException e) {
+ log.error("Failed to validate xml",e);
+ throw e;
+ } catch (ParserConfigurationException e) {
+ log.error("ParserConfigurationException occurred: " + e.getMessage());
+ } catch (IOException e) {
+ log.error("IOException occurred: " + e.getMessage());
+ }
+ } catch (SAXException e) {
+ log.error("SAXException occurred: " + e.getMessage());
+ }
+
+ return isValid;
+ }
+
+ /**
+ * Validation method used to validate an xml input source against an xsd input source.
+ * @param xmlSource The xml input source to be validated.
+ * @param validationSource The schema input source to validate against.
+ * @return Boolean true/false indicating successful validation.
+ * @throws Exception Failure during validation.
+ */
+ public boolean validate(InputSource xmlSource, InputSource validationSource) throws XmlValidatorException {
+ boolean isValid = false;
+
+ setXMLDocument(xmlSource);
+
+ try {
+ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+ Schema schema = factory.newSchema((Source)validationSource);
+ // Request validation
+ Validator validator = schema.newValidator();
+ // Register the error handler
+ validator.setErrorHandler(new XmlErrorHandler());
+
+ try {
+ validator.validate(new DOMSource(xmlDocument));
+ isValid = true;
+ } catch (SAXException e) {
+ log.error("Failed to validate xml",e);
+ throw e;
+ } catch (IOException e) {
+ log.error("IOException occurred: " + e.getMessage());
+ }
+ } catch (SAXException e) {
+ log.error("SAXException occurred: " + e.getMessage());
+ }
+
+ return isValid;
+ }
+
+ class XmlErrorHandler implements ErrorHandler {
+ public void warning(SAXParseException exception) throws SAXException {
+ // Bring things to a crashing halt
+ validationResults.add("**Parsing Warning**" +
+ " Line: " +
+ exception.getLineNumber() +
+ " URI: " +
+ exception.getSystemId() +
+ " Message: " +
+ exception.getMessage());
+ throw new SAXException("Warning encountered");
+ }
+
+ public void error(SAXParseException exception) throws SAXException {
+ // Bring things to a crashing halt
+ validationResults.add("**Parsing Error**" +
+ " Line: " +
+ exception.getLineNumber() +
+ " URI: " +
+ exception.getSystemId() +
+ " Message: " +
+ exception.getMessage());
+ throw new SAXException("Error encountered");
+ }
+
+ public void fatalError(SAXParseException exception) throws SAXException {
+ // Bring things to a crashing halt
+ validationResults.add("**Parsing Fatal Error**" +
+ " Line: " +
+ exception.getLineNumber() +
+ " URI: " +
+ exception.getSystemId() +
+ " Message: " +
+ exception.getMessage());
+ throw new SAXException("Fatal Error encountered");
+ }
+ }
+
+ public Collection<String> getValidationResults() {
+ return validationResults;
+ }
+
+ public void setXMLDocument(InputSource xmlSource) {
+ try {
+ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder parser = builderFactory.newDocumentBuilder();
+
+ xmlDocument = parser.parse(xmlSource);
+ log.info(xmlDocument.toString());
+ } catch (SAXException e) {
+ System.err.println(e);
+ } catch (ParserConfigurationException e) {
+ System.err.println(e);
+ } catch (IOException e) {
+ System.err.println(e);
+ }
+ }
+
+ public Document getXMLDocument() {
+ return xmlDocument;
+ }
+
+}
More information about the jboss-svn-commits
mailing list