[jboss-svn-commits] JBL Code SVN: r36109 - in labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta: src/org/jboss/soa/esb/actions/validation and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Nov 29 09:44:18 EST 2010


Author: kevin.conner at jboss.com
Date: 2010-11-29 09:44:14 -0500 (Mon, 29 Nov 2010)
New Revision: 36109

Added:
   labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/importtest.xml
   labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/importtest.xsd
   labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/shipto.xsd
Modified:
   labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/internal/soa/esb/util/XMLHelper.java
   labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/validation/SchemaValidationAction.java
   labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/SchemaValidationActionUnitTest.java
Log:
Add support for schema import/include in SchemaValidationAction: JBESB-3538

Modified: labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/internal/soa/esb/util/XMLHelper.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/internal/soa/esb/util/XMLHelper.java	2010-11-29 13:53:50 UTC (rev 36108)
+++ labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/internal/soa/esb/util/XMLHelper.java	2010-11-29 14:44:14 UTC (rev 36109)
@@ -33,6 +33,7 @@
 import java.net.URL;
 import java.util.concurrent.atomic.AtomicReference;
 
+import javax.xml.XMLConstants;
 import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -377,11 +378,27 @@
      */
     public static Schema getSchema(final String resource) throws SAXException
     {
+        return getSchema(resource, XMLConstants.W3C_XML_SCHEMA_NS_URI) ;
+    }
+    
+    /**
+     * Get the schema for the specified resource.
+     * @param resource The schema resource to parse.
+     * @param schemaLanguage The schema language.
+     * @return The resource schema for validation. 
+     * @throws SAXException For errors during parsing.
+     */
+    public static Schema getSchema(final String resource, final String schemaLanguage) throws SAXException
+    {
         SchemaResolver schemaResolver;
         try
         {
             URI schemaUri = getResourceUri(resource, XMLHelper.class) ;
-            log.debug("schemaUri : " + schemaUri);
+            final boolean debugEnabled = log.isDebugEnabled() ;
+            if (debugEnabled)
+            {
+                log.debug("schemaUri : " + schemaUri);
+            }
             schemaResolver = new SchemaResolver(schemaUri);
             
             URL schemaUrl;
@@ -393,9 +410,12 @@
             {
                 schemaUrl = schemaUri.toURL();
             }
-            log.debug("schemaUrl : " + schemaUrl);
+            if (debugEnabled)
+            {
+                log.debug("schemaUrl : " + schemaUrl);
+            }
             
-            return getSchema(schemaUrl, schemaResolver);
+            return getSchema(schemaUrl, schemaResolver, schemaLanguage);
         } 
         catch (final URISyntaxException e)
         {
@@ -423,7 +443,23 @@
      */
     public static Schema getSchema(final URL resource, final LSResourceResolver resolver) throws SAXException, IOException
     {
-        final SchemaFactory schemaFactory = newSchemaFactory();
+        return getSchema(resource, resolver, XMLConstants.W3C_XML_SCHEMA_NS_URI) ;
+    }
+    
+    /**
+     * Get the schema for the specified resource and use the specified resolver to locate 
+     * external resources, for example import schemas in the xsd.
+     * 
+     * @param resource  The schema resource to parse.
+     * @param resolver  The {@link LSResourceResolver} for locating external resources.
+     * @param schemaLanguage The schema language.
+     * @return Schema   The resource schema for validation. 
+     * @throws SAXException
+     * @throws IOException 
+     */
+    public static Schema getSchema(final URL resource, final LSResourceResolver resolver, final String schemaLanguage) throws SAXException, IOException
+    {
+        final SchemaFactory schemaFactory = newSchemaFactory(schemaLanguage);
         schemaFactory.setResourceResolver(resolver);
         
         final InputStream resourceIS = resource.openStream();
@@ -490,7 +526,7 @@
             final InputStream resourceIS = ClassUtil.getResourceAsStream(resources[count], XMLHelper.class) ;
             sources[count] = new StreamSource(resourceIS) ;
         }
-        return newSchemaFactory().newSchema(sources) ;
+        return newSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(sources) ;
     }
     
     /**
@@ -590,9 +626,9 @@
         return (handler1.getRootElement().equals(handler2.getRootElement())) ;
     }
 
-    private static SchemaFactory newSchemaFactory()
+    private static SchemaFactory newSchemaFactory(final String schemaLanguage)
     {
-        return SchemaFactory.newInstance( "http://www.w3.org/2001/XMLSchema" );
+        return SchemaFactory.newInstance(schemaLanguage);
     }
 
     /**

Modified: labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/validation/SchemaValidationAction.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/validation/SchemaValidationAction.java	2010-11-29 13:53:50 UTC (rev 36108)
+++ labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/validation/SchemaValidationAction.java	2010-11-29 14:44:14 UTC (rev 36109)
@@ -8,10 +8,10 @@
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamSource;
 import javax.xml.validation.Schema;
-import javax.xml.validation.SchemaFactory;
 import javax.xml.validation.Validator;
 
 import org.apache.log4j.Logger;
+import org.jboss.internal.soa.esb.util.XMLHelper;
 import org.jboss.soa.esb.ConfigurationException;
 import org.jboss.soa.esb.actions.AbstractActionPipelineProcessor;
 import org.jboss.soa.esb.actions.ActionProcessingException;
@@ -19,7 +19,6 @@
 import org.jboss.soa.esb.listeners.message.MessageDeliverException;
 import org.jboss.soa.esb.message.Message;
 import org.jboss.soa.esb.message.MessagePayloadProxy;
-import org.jboss.soa.esb.util.ClassUtil;
 import org.xml.sax.SAXException;
 
 /**
@@ -76,10 +75,26 @@
     public SchemaValidationAction(final ConfigTree config) throws ConfigurationException
     {
         xsd = config.getRequiredAttribute("schema");
+        final String schemaLocation ;
+        if (xsd.startsWith("/"))
+        {
+            schemaLocation = xsd ;
+        }
+        else
+        {
+            schemaLocation = '/' + getClass().getPackage().getName().replace('.', '/') + '/' + xsd ;
+        }
         schemaLanguage = config.getAttribute("schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI);
 
         payloadProxy = new MessagePayloadProxy(config);
-        schema = createSchema(xsd);
+        try
+        {
+            schema = XMLHelper.getSchema(schemaLocation, schemaLanguage);
+        }
+        catch (final SAXException e)
+        {
+            throw new ConfigurationException("Could not create a validator for schema '" + xsd + "'", e);
+        }
     }
 
     /**
@@ -155,25 +170,4 @@
     {
     	return new StreamSource(new StringReader(xml));
     }
-
-    /**
-     * Creates a Validator instance which will be used for schema validation.
-     *
-     * @param xsd           The schema to validate against.
-     * @return Validator    The Validator instance.
-     *
-     * @throws ConfigurationException Is an exception is thrown while trying to create the schema.
-     */
-    private Schema createSchema(final String xsd) throws ConfigurationException
-    {
-        final SchemaFactory schemafactory = SchemaFactory.newInstance(schemaLanguage);
-        try
-        {
-            return schemafactory.newSchema(new StreamSource(ClassUtil.getResourceAsStream(xsd, getClass())));
-        }
-        catch (final SAXException e)
-        {
-            throw new ConfigurationException("Could not create a validator for schema '" + xsd + "'", e);
-        }
-    }
 }

Modified: labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/SchemaValidationActionUnitTest.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/SchemaValidationActionUnitTest.java	2010-11-29 13:53:50 UTC (rev 36108)
+++ labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/SchemaValidationActionUnitTest.java	2010-11-29 14:44:14 UTC (rev 36109)
@@ -44,20 +44,24 @@
  */
 public class SchemaValidationActionUnitTest
 {
-    private String xsdFileName = "test.xsd";
-
     @Test
     public void process() throws ActionProcessingException, ConfigurationException, UnsupportedEncodingException
     {
-        process("test.xml");
+        process("test.xml", "test.xsd");
     }
 
     @Test
+    public void processImport() throws ActionProcessingException, ConfigurationException, UnsupportedEncodingException
+    {
+        process("importtest.xml", "importtest.xsd");
+    }
+
+    @Test
     public void processInvalidXml() throws ActionProcessingException, ConfigurationException, UnsupportedEncodingException
     {
         try
         {
-            process("test-invalid.xml");
+            process("test-invalid.xml", "test.xsd");
             fail("processing invalid xml should fail validation");
         }
         catch (final Exception e)
@@ -66,7 +70,7 @@
         }
     }
 
-    private void process(final String xmlFileName) throws ConfigurationException, ActionProcessingException
+    private void process(final String xmlFileName, final String xsdFileName) throws ConfigurationException, ActionProcessingException
     {
         final ConfigTree config = createConfiguration(xsdFileName);
         final SchemaValidationAction action = new SchemaValidationAction(config);

Added: labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/importtest.xml
===================================================================
--- labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/importtest.xml	                        (rev 0)
+++ labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/importtest.xml	2010-11-29 14:44:14 UTC (rev 36109)
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+
+<order:shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://www.example.com/order importtest.xsd"
+    xmlns:order="http://www.example.com/order"
+    xmlns:shipto="http://www.example.com/shipto">
+
+    <order:orderperson>John Smith</order:orderperson>
+    <order:shipto>
+        <shipto:name>Ola Nordmann</shipto:name>
+        <shipto:address>Langgt 23</shipto:address>
+        <shipto:city>4000 Stavanger</shipto:city>
+        <shipto:country>Norway</shipto:country>
+    </order:shipto>
+    <order:item>
+        <order:title>Empire Burlesque</order:title>
+        <order:note>Special Edition</order:note>
+        <order:quantity>1</order:quantity>
+        <order:price>10.90</order:price>
+    </order:item>
+    <order:item>
+        <order:title>Hide your heart</order:title>
+        <order:quantity>1</order:quantity>
+        <order:price>9.90</order:price>
+    </order:item>
+</order:shiporder>
\ No newline at end of file


Property changes on: labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/importtest.xml
___________________________________________________________________
Name: svn:mime-type
   + text/xml
Name: svn:keywords
   + Rev Date
Name: svn:eol-style
   + native

Added: labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/importtest.xsd
===================================================================
--- labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/importtest.xsd	                        (rev 0)
+++ labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/importtest.xsd	2010-11-29 14:44:14 UTC (rev 36109)
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<xs:schema
+    targetNamespace="http://www.example.com/order"
+    xmlns:order="http://www.example.com/order"
+    xmlns:shipto="http://www.example.com/shipto"
+    xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
+
+    <xs:import namespace="http://www.example.com/shipto" schemaLocation="shipto.xsd"/>
+
+    <xs:element name="shiporder">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element name="orderperson" type="xs:string" />
+                <xs:element name="shipto" type="shipto:shipto"/>
+                <xs:element name="item" maxOccurs="unbounded">
+                    <xs:complexType>
+                        <xs:sequence>
+                            <xs:element name="title" type="xs:string" />
+                            <xs:element name="note" type="xs:string" minOccurs="0" />
+                            <xs:element name="quantity" type="xs:positiveInteger" />
+                            <xs:element name="price" type="xs:decimal" />
+                        </xs:sequence>
+                    </xs:complexType>
+                </xs:element>
+            </xs:sequence>
+            <xs:attribute name="orderid" type="xs:string" use="required" />
+        </xs:complexType>
+    </xs:element>
+</xs:schema>
\ No newline at end of file


Property changes on: labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/importtest.xsd
___________________________________________________________________
Name: svn:mime-type
   + text/xml
Name: svn:keywords
   + Rev Date
Name: svn:eol-style
   + native

Added: labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/shipto.xsd
===================================================================
--- labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/shipto.xsd	                        (rev 0)
+++ labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/shipto.xsd	2010-11-29 14:44:14 UTC (rev 36109)
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<xs:schema
+    targetNamespace="http://www.example.com/shipto"
+    xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
+    
+    <xs:complexType name="shipto">
+        <xs:sequence>
+            <xs:element name="name" type="xs:string" />
+            <xs:element name="address" type="xs:string" />
+            <xs:element name="city" type="xs:string" />
+            <xs:element name="country" type="xs:string" />
+        </xs:sequence>
+    </xs:complexType>
+</xs:schema>
\ No newline at end of file


Property changes on: labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/validation/shipto.xsd
___________________________________________________________________
Name: svn:mime-type
   + text/xml
Name: svn:keywords
   + Rev Date
Name: svn:eol-style
   + native



More information about the jboss-svn-commits mailing list