Author: dandiep
Date: Thu May  4 13:12:30 2006
New Revision: 399846

URL: http://svn.apache.org/viewcvs?rev=399846&view=rev
Log:
o Support schemas which circularly reference eachother
o Add convience methods in XmlSchemaCollection: getXmlSchema(string systemId) 
and getXmlSchemas()

Added:
    webservices/commons/trunk/modules/XmlSchema/test-resources/circular/
    webservices/commons/trunk/modules/XmlSchema/test-resources/circular/a.xsd
    webservices/commons/trunk/modules/XmlSchema/test-resources/circular/b.xsd
    
webservices/commons/trunk/modules/XmlSchema/test/tests/CircularSchemaTest.java
Modified:
    
webservices/commons/trunk/modules/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java
    
webservices/commons/trunk/modules/XmlSchema/src/org/apache/ws/commons/schema/XmlSchemaCollection.java

Modified: 
webservices/commons/trunk/modules/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java?rev=399846&r1=399845&r2=399846&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java
 (original)
+++ 
webservices/commons/trunk/modules/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java
 Thu May  4 13:12:30 2006
@@ -22,6 +22,7 @@
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.Attr;
+import org.xml.sax.InputSource;
 import org.apache.ws.commons.schema.utils.XDOMUtil;
 import org.apache.ws.commons.schema.utils.Tokenizer;
 import org.apache.ws.commons.schema.constants.Constants;
@@ -46,8 +47,6 @@
         schema = new XmlSchema(collection);
     }
 
-
-
     XmlSchema build(Document doc, String uri, ValidationEventHandler veh) {
         Element schemaEl = doc.getDocumentElement();
         return handleXmlSchemaElement(schemaEl, uri);
@@ -58,6 +57,11 @@
 
         setNamespaceAttributes(schema, schemaEl);
 
+        if (uri != null)
+            collection.systemId2Schemas.put(uri, schema);
+        
+        collection.schemas.add(schema);
+        
         // only populate it if it isn't already in there
         if(!collection.namespaces.containsKey(schema.targetNamespace)){
             collection.namespaces.put(schema.targetNamespace, schema);
@@ -1907,15 +1911,23 @@
                                String schemaLocation,
                                String baseUri) {
         //use the entity resolver provided
-        try {
-            return collection.read(
-                    collection.schemaResolver.
-                            
resolveEntity(targetNamespace,schemaLocation,baseUri)
-                    , null);
-        } catch (Exception e) {
-            throw new RuntimeException(e);
+        XmlSchema schema = null;
+        InputSource source = collection.schemaResolver.
+            resolveEntity(targetNamespace,schemaLocation,baseUri);
+        
+        if (source.getSystemId() != null) {
+            schema = collection.getXmlSchema(source.getSystemId());
+        }
+        
+        if (schema == null) {
+            try {
+                return collection.read(source, null);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
         }
-
+        
+        return schema;
     }
 
     /**

Modified: 
webservices/commons/trunk/modules/XmlSchema/src/org/apache/ws/commons/schema/XmlSchemaCollection.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/XmlSchema/src/org/apache/ws/commons/schema/XmlSchemaCollection.java?rev=399846&r1=399845&r2=399846&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/XmlSchema/src/org/apache/ws/commons/schema/XmlSchemaCollection.java
 (original)
+++ 
webservices/commons/trunk/modules/XmlSchema/src/org/apache/ws/commons/schema/XmlSchemaCollection.java
 Thu May  4 13:12:30 2006
@@ -21,9 +21,12 @@
 import java.io.IOException;
 import java.io.Reader;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 
 import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilder;
@@ -63,6 +66,11 @@
     Map inScopeNamespaces = new HashMap();
 
     /**
+     * Schemas in this colelction sorted by system id.
+     */
+    Map systemId2Schemas = new HashMap();
+    
+    /**
      * An org.xml.sax.EntityResolver that is used to
      * resolve the imports/includes
      */
@@ -70,6 +78,11 @@
 
     XmlSchema xsd = new XmlSchema(XmlSchema.SCHEMA_NS, this);
 
+    /** 
+     * A Set of all the scehmas in this collection.
+     */
+    Set schemas = new HashSet();
+    
     /**
      * Set the base URI. This is used when schemas need to be
      * loaded from relative locations
@@ -267,6 +280,23 @@
         init();
     }
 
+    /**
+     * Retreive an XmlSchema from the collection by its system ID.
+     * @param systemId
+     * @return
+     */
+    public XmlSchema getXmlSchema(String systemId) {
+        return (XmlSchema) systemId2Schemas.get(systemId);
+    }
+    
+    /**
+     * Return a Set of all the XmlSchemas in this collection.
+     * @return
+     */
+    public Set getXmlSchemas() {
+        return Collections.unmodifiableSet(schemas);
+    }
+    
     public XmlSchemaElement getElementByQName(QName qname) {
         XmlSchema schema = (XmlSchema)namespaces.get(qname.getNamespaceURI());
         if (schema == null) {

Added: webservices/commons/trunk/modules/XmlSchema/test-resources/circular/a.xsd
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/XmlSchema/test-resources/circular/a.xsd?rev=399846&view=auto
==============================================================================
--- webservices/commons/trunk/modules/XmlSchema/test-resources/circular/a.xsd 
(added)
+++ webservices/commons/trunk/modules/XmlSchema/test-resources/circular/a.xsd 
Thu May  4 13:12:30 2006
@@ -0,0 +1,8 @@
+<schema targetNamespace="urn:xmlschema:recursive:a"
+        xmlns="http://www.w3.org/2001/XMLSchema";
+        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/";
+        elementFormDefault="qualified" >
+        
+    <import namespace="urn:xmlschema:recursive:b" 
schemaLocation="b.xsd"></import>
+    
+</schema>
\ No newline at end of file

Added: webservices/commons/trunk/modules/XmlSchema/test-resources/circular/b.xsd
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/XmlSchema/test-resources/circular/b.xsd?rev=399846&view=auto
==============================================================================
--- webservices/commons/trunk/modules/XmlSchema/test-resources/circular/b.xsd 
(added)
+++ webservices/commons/trunk/modules/XmlSchema/test-resources/circular/b.xsd 
Thu May  4 13:12:30 2006
@@ -0,0 +1,8 @@
+<schema targetNamespace="urn:xmlschema:recursive:b"
+        xmlns="http://www.w3.org/2001/XMLSchema";
+        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/";
+        elementFormDefault="qualified" >
+        
+    <import namespace="urn:xmlschema:recursive:a" 
schemaLocation="a.xsd"></import>
+    
+</schema>
\ No newline at end of file

Added: 
webservices/commons/trunk/modules/XmlSchema/test/tests/CircularSchemaTest.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/XmlSchema/test/tests/CircularSchemaTest.java?rev=399846&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/XmlSchema/test/tests/CircularSchemaTest.java 
(added)
+++ 
webservices/commons/trunk/modules/XmlSchema/test/tests/CircularSchemaTest.java 
Thu May  4 13:12:30 2006
@@ -0,0 +1,27 @@
+package tests;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import org.apache.ws.commons.schema.XmlSchema;
+import org.apache.ws.commons.schema.XmlSchemaCollection;
+import org.xml.sax.InputSource;
+
+public class CircularSchemaTest extends TestCase
+{
+    public void testCircular() throws Exception {
+        XmlSchemaCollection schemas = new XmlSchemaCollection();
+        File file = new File("test-resources/circular/a.xsd");
+        InputSource source = new InputSource(new FileInputStream(file));
+        source.setSystemId(file.toURL().toString());
+        
+        XmlSchema schema = schemas.read(source, null);
+        
+        Set xmlSchemas = schemas.getXmlSchemas();
+        assertNotNull(xmlSchemas);
+        assertEquals(2, xmlSchemas.size());
+    }
+}
\ No newline at end of file


Reply via email to