[jboss-svn-commits] JBL Code SVN: r6503 - labs/jbossesb/trunk/product/etc/schemas/validation/src/org/jboss/soa/esb/services/xml

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sat Sep 30 19:14:13 EDT 2006


Author: mark.little at jboss.com
Date: 2006-09-30 19:14:11 -0400 (Sat, 30 Sep 2006)
New Revision: 6503

Added:
   labs/jbossesb/trunk/product/etc/schemas/validation/src/org/jboss/soa/esb/services/xml/XmlValidation.java
   labs/jbossesb/trunk/product/etc/schemas/validation/src/org/jboss/soa/esb/services/xml/XmlValidationTest.java
Log:


Added: labs/jbossesb/trunk/product/etc/schemas/validation/src/org/jboss/soa/esb/services/xml/XmlValidation.java
===================================================================
--- labs/jbossesb/trunk/product/etc/schemas/validation/src/org/jboss/soa/esb/services/xml/XmlValidation.java	2006-09-30 23:13:57 UTC (rev 6502)
+++ labs/jbossesb/trunk/product/etc/schemas/validation/src/org/jboss/soa/esb/services/xml/XmlValidation.java	2006-09-30 23:14:11 UTC (rev 6503)
@@ -0,0 +1,127 @@
+/*
+* 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.services.xml;
+
+import org.xml.sax.SAXException;
+import org.w3c.dom.Document;
+import org.jboss.logging.Logger;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.validation.Validator;
+import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.Schema;
+import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.XMLConstants;
+import java.io.File;
+
+/**
+ * A Helper classes used to validate xml files against supplied schemas.
+ *
+ * @author $Revision$
+ *         $Id$
+ */
+public class XmlValidation {
+
+    /**
+     * Our Logger
+     */
+    private Logger log = Logger.getLogger(XmlValidation.class);
+    /**
+     * The xml base directory.
+     */
+    private String xmlBaseDirectory = null;
+    /**
+     * The xml base directory key.
+     */
+    private String xmlBaseDirectoryKey = "xml.base.dir";
+    /**
+     * The xsd base directory.
+     */
+    private String xsdBaseDirectory = null;
+    /**
+     * The xsd base directory key.
+     */
+    private String xsdBaseDirectoryKey = "xsd.base.dir";
+    /**
+     * The path separator
+      */
+    private static String SEP = System.getProperty("file.separator");
+
+    /**
+     * Default class constructor
+     */
+    public XmlValidation() {
+        if(System.getProperty(xmlBaseDirectoryKey) != null) {
+            xmlBaseDirectory = System.getProperty(xmlBaseDirectoryKey);
+            log.info("Using xml base directory " + xmlBaseDirectory);
+        }
+        if(System.getProperty(xsdBaseDirectoryKey) != null) {
+            xsdBaseDirectory = System.getProperty(xsdBaseDirectoryKey);
+            log.info("Using xsd base directory " + xsdBaseDirectory);
+        }
+    }
+
+    /**
+     * Validation method used to validate an xml file against an xsd.
+     * @param xml The xml file to be validated.
+     * @param xsd The schema to validate against.
+     * @return  Boolean true/false indicating successful validation.
+     * @throws Exception Failure during validation.
+     */
+    public boolean validate(String xml, String xsd) throws Exception {
+        if(log.isInfoEnabled()) {
+            log.info("Validating " + xml + " Against " + xsd);
+        }
+        boolean isValid = false;
+        File xmlFile = new File(xmlBaseDirectory + SEP + xml);
+        File xsdFile = new File(xsdBaseDirectory + SEP + xsd);
+        log.info("Xml File=" + xmlFile.getAbsolutePath());
+        log.info("Xsd File=" + xsdFile.getAbsolutePath());
+        DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+        Document document = parser.parse(xmlFile);
+        //log.info("Xml=" + document.toString());
+        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+        Source schemaFile = new StreamSource(xsdFile);
+        //log.info("Xsd=" + schemaFile.toString());
+        Schema schema = factory.newSchema(schemaFile);
+        Validator validator = schema.newValidator();
+        try {
+            validator.validate(new DOMSource(document));
+            isValid = true;
+        } catch (SAXException e) {
+            log.error("Failed to validate xml",e);
+            throw e;
+        }
+        if(log.isInfoEnabled()) {
+            log.info("IsValid = " + isValid);
+        }
+        return isValid;
+    }
+
+
+
+
+
+}

Added: labs/jbossesb/trunk/product/etc/schemas/validation/src/org/jboss/soa/esb/services/xml/XmlValidationTest.java
===================================================================
--- labs/jbossesb/trunk/product/etc/schemas/validation/src/org/jboss/soa/esb/services/xml/XmlValidationTest.java	2006-09-30 23:13:57 UTC (rev 6502)
+++ labs/jbossesb/trunk/product/etc/schemas/validation/src/org/jboss/soa/esb/services/xml/XmlValidationTest.java	2006-09-30 23:14:11 UTC (rev 6503)
@@ -0,0 +1,59 @@
+package org.jboss.soa.esb.services.xml;
+
+import junit.framework.TestCase;
+import org.jboss.logging.Logger;
+
+
+public class XmlValidationTest extends TestCase  {
+
+    /**
+     * Our Logger
+     */
+    private Logger log = Logger.getLogger(XmlValidationTest.class);
+
+    /**
+     * Validate the Email EPR
+     * @throws Exception
+     */
+    public void testBasicEmailEPR() throws Exception {
+        log.info("Running Basic Email EPR Test");
+        XmlValidation xmlValidator = new XmlValidation();
+        boolean isValid = xmlValidator.validate("basic-email-epr-example.xml","email-epr.xsd");
+        assertTrue("Expecting basic-email-epr-example.xml to be valid.", isValid);
+    }
+
+    /**
+     * Validate the JMS EPR
+     * @throws Exception
+     */
+    public void testBasicJMSEPR() throws Exception {
+        log.info("Running Basic JMS EPR Test");
+        XmlValidation xmlValidator = new XmlValidation();
+        boolean isValid = xmlValidator.validate("basic-jms-epr-example.xml","jms-epr.xsd");
+        assertTrue("Expecting basic-jms-epr-example.xml to be valid.", isValid);
+    }
+
+    /**
+     * Validate the JDBC EPR
+     * @throws Exception
+     */
+    public void testBasicJDBCEPR() throws Exception {
+        log.info("Running Basic JDBC EPR Test");
+        XmlValidation xmlValidator = new XmlValidation();
+        boolean isValid = xmlValidator.validate("basic-jdbc-epr-example.xml","jdbc-epr.xsd");
+        assertTrue("Expecting basic-jdbc-epr-example.xml to be valid.", isValid);
+    }
+
+    /**
+     * Validate the FTP EPR
+     * @throws Exception
+     */
+    public void testBasicFTPEPR() throws Exception {
+        log.info("Running Basic FTP EPR Test");
+        XmlValidation xmlValidator = new XmlValidation();
+        boolean isValid = xmlValidator.validate("basic-ftp-epr-example.xml","ftp-epr.xsd");
+        assertTrue("Expecting basic-ftp-epr-example.xml to be valid.", isValid);
+    }
+
+
+}




More information about the jboss-svn-commits mailing list