Author: bimargulies
Date: Sun Dec 14 14:27:37 2008
New Revision: 726564

URL: http://svn.apache.org/viewvc?rev=726564&view=rev
Log:
Merged revisions 726532 via svnmerge from 
https://svn.apache.org/repos/asf/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH

........
  r726532 | bimargulies | 2008-12-14 16:25:18 -0500 (Sun, 14 Dec 2008) | 5 lines
  
  WSCOMMONS-377.
  
  Add functionality so that the base URI of an XmlSchemaCollection is used to 
resolve imports inside of a 
  schema when that schemas's system ID is not usable as a base URI. I'm calling 
this a fix to 377.
........

Added:
    
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/resolver/CollectionURIResolver.java
      - copied unchanged from r726532, 
webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/resolver/CollectionURIResolver.java
    
webservices/commons/trunk/modules/XmlSchema/src/test/java/tests/WSCOMMONS377Test.java
      - copied unchanged from r726532, 
webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/test/java/tests/WSCOMMONS377Test.java
    
webservices/commons/trunk/modules/XmlSchema/src/test/test-resources/WSCOMMONS-377/
      - copied from r726532, 
webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/test/test-resources/WSCOMMONS-377/
    
webservices/commons/trunk/modules/XmlSchema/src/test/test-resources/WSCOMMONS-377/importedElements.xsd
      - copied unchanged from r726532, 
webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/test/test-resources/WSCOMMONS-377/importedElements.xsd
    
webservices/commons/trunk/modules/XmlSchema/src/test/test-resources/WSCOMMONS-377/importedTypes.xsd
      - copied unchanged from r726532, 
webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/test/test-resources/WSCOMMONS-377/importedTypes.xsd
    
webservices/commons/trunk/modules/XmlSchema/src/test/test-resources/WSCOMMONS-377/importing.wsdl
      - copied unchanged from r726532, 
webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/test/test-resources/WSCOMMONS-377/importing.wsdl
Modified:
    webservices/commons/trunk/modules/XmlSchema/   (props changed)
    
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java
    
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java
    
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/resolver/DefaultURIResolver.java

Propchange: webservices/commons/trunk/modules/XmlSchema/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Sun Dec 14 14:27:37 2008
@@ -1 +1 @@
-/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH:1-726531
+/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH:1-726532

Modified: 
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java?rev=726564&r1=726563&r2=726564&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java
 (original)
+++ 
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java
 Sun Dec 14 14:27:37 2008
@@ -126,15 +126,15 @@
        /**
         * handles the schema element
         * @param schemaEl
-        * @param uri
+        * @param systemId
         */
-       XmlSchema handleXmlSchemaElement(Element schemaEl, String uri) {
+       XmlSchema handleXmlSchemaElement(Element schemaEl, String systemId) {
                // get all the attributes along with the namespace declns
                
schema.setNamespaceContext(NodeNamespaceContext.getNamespaceContext(schemaEl));
                setNamespaceAttributes(schema, schemaEl);
 
                XmlSchemaCollection.SchemaKey schemaKey = new 
XmlSchemaCollection.SchemaKey(
-                               schema.logicalTargetNamespace, uri);
+                               schema.logicalTargetNamespace, systemId);
                if (!collection.containsSchema(schemaKey)) {
                        collection.addSchema(schemaKey, schema);
                        schema.parent = collection; // establish parentage now.
@@ -153,7 +153,7 @@
                        schema.id = schemaEl.getAttribute("id");
                }
 
-               schema.setSourceURI(uri);
+               schema.setSourceURI(systemId);
 
                /***********
                 * for ( each childElement)

Modified: 
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java?rev=726564&r1=726563&r2=726564&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java
 (original)
+++ 
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java
 Sun Dec 14 14:27:37 2008
@@ -42,6 +42,7 @@
 
 import org.apache.ws.commons.schema.constants.Constants;
 import org.apache.ws.commons.schema.extensions.ExtensionRegistry;
+import org.apache.ws.commons.schema.resolver.CollectionURIResolver;
 import org.apache.ws.commons.schema.resolver.DefaultURIResolver;
 import org.apache.ws.commons.schema.resolver.URIResolver;
 import org.apache.ws.commons.schema.utils.DOMUtil;
@@ -94,20 +95,27 @@
        }
        
        
-       
-    static class SchemaKey {
+       /**
+        * Key that identifies a schema in a collection, composed of a 
targetNamespace
+        * and a system ID. 
+        */
+    public static class SchemaKey {
         private final String namespace;
         private final String systemId;
         SchemaKey(String pNamespace, String pSystemId) {
             namespace = pNamespace == null ? Constants.NULL_NS_URI : 
pNamespace;
             systemId = pSystemId == null ? "" : pSystemId;
         }
+        
         String getNamespace() { return namespace; }
+        
         String getSystemId() { return systemId; }
+        
         public int hashCode() {
             final int PRIME = 31;
             return (PRIME + namespace.hashCode()) * PRIME + 
systemId.hashCode();
         }
+        
         public boolean equals(Object obj) {
             if (this == obj)
                 return true;
@@ -118,6 +126,7 @@
             final SchemaKey other = (SchemaKey) obj;
             return namespace.equals(other.namespace)  &&  
systemId.equals(other.systemId);
         }
+        
         public String toString() {
             return Constants.NULL_NS_URI.equals(namespace) ?
                     systemId : ("{" + namespace + "}" + systemId);
@@ -156,10 +165,15 @@
     /**
      * Set the base URI. This is used when schemas need to be
      * loaded from relative locations
-     * @param baseUri  baseUri for this
+     * @param baseUri  baseUri for this collection.
      */
-    public void setBaseUri(String baseUri){
+    public void setBaseUri(String baseUri) {
         this.baseUri = baseUri;
+        if(schemaResolver instanceof CollectionURIResolver) {
+               CollectionURIResolver resolverWithBase = 
+                       (CollectionURIResolver) schemaResolver;
+               resolverWithBase.setCollectionBaseURI(baseUri);
+        }
     }
 
     /**
@@ -396,10 +410,26 @@
         return doc;
     }
 
+    /**
+     * Read an XML schema into the collection from a SAX InputSource.
+     * Schemas in a collection must be unique in the concatenation of system 
ID and
+     * targetNamespace. In this API, the systemID is taken from the source.
+     * @param inputSource the XSD document.
+     * @param veh handler that is called back for validation.
+     * @return the XML schema object.
+     */
     public XmlSchema read(InputSource inputSource, ValidationEventHandler veh) 
{
         return read(inputSource, veh, null);
     }
-
+    
+    /**
+     * Read an XML schema into the collection from a TRaX source. 
+     * Schemas in a collection must be unique in the concatenation of system 
ID and
+     * targetNamespace. In this API, the systemID is taken from the Source.
+     * @param source the XSD document.
+     * @param veh handler that is called back for validation.
+     * @return the XML schema object.
+     */
     public XmlSchema read(Source source, ValidationEventHandler veh) {
         if (source instanceof SAXSource) {
             return read(((SAXSource) source).getInputSource(), veh);
@@ -422,12 +452,27 @@
         }
     }
 
+    /**
+     * Read an XML schema into the collection from a DOM document. 
+     * Schemas in a collection must be unique in the concatenation of system 
ID and
+     * targetNamespace. In this API, the systemID is taken from the document.
+     * @param doc the XSD document.
+     * @param veh handler that is called back for validation.
+     * @return the XML schema object.
+     */
     public XmlSchema read(Document doc, ValidationEventHandler veh) {
         SchemaBuilder builder = new SchemaBuilder(this, null);
         return builder.build(doc, null, veh);
     }
 
    
+    /**
+     * Read an XML Schema into the collection from a DOM element. Schemas in a 
collection
+     * must be unique in the concatentation of System ID and targetNamespace. 
The system ID will 
+     * be empty for this API.
+     * @param elem the DOM element for the schema.
+     * @return the XmlSchema
+     */
     public XmlSchema read(Element elem) {
         SchemaBuilder builder = new SchemaBuilder(this, null);
         XmlSchema xmlSchema = builder.handleXmlSchemaElement(elem, null);
@@ -435,25 +480,51 @@
         return xmlSchema;
     }
 
-    public XmlSchema read(Document doc, String uri, ValidationEventHandler 
veh) {
-        return read(doc, uri, veh, null);
+    /**
+     * Read an XML Schema from a complete XSD XML DOM Document into this 
collection.
+     * Schemas in a collection must be unique in
+     * the concatenation of SystemId and targetNamespace.
+     * @param doc The schema document.
+     * @param systemId System ID for this schema.
+     * @param veh handler to be called to check validity of the schema.
+     * @return the schema object.
+     */
+    public XmlSchema read(Document doc, String systemId, 
ValidationEventHandler veh) {
+        return read(doc, systemId, veh, null);
     }
 
-    public XmlSchema read(Document doc, String uri, ValidationEventHandler veh,
+    /**
+     * Read an XML Schema from a complete XSD XML DOM Document into this 
collection.
+     *  Schemas in a collection must be unique in
+     * the concatenation of SystemId and targetNamespace.
+     * @param doc Source document.
+     * @param systemId System id.
+     * @param veh Stub for future capability to handle validation errors.
+     * @param validator object that is called back to check validity of the 
target namespace.
+     * @return the schema object.
+     */
+    public XmlSchema read(Document doc, String systemId, 
ValidationEventHandler veh,
             TargetNamespaceValidator validator) {
         SchemaBuilder builder = new SchemaBuilder(this, validator);
-        XmlSchema schema = builder.build(doc, uri, veh);
+        XmlSchema schema = builder.build(doc, systemId, veh);
         schema.setInputEncoding(DOMUtil.getInputEncoding(doc));
                return schema;
     }
 
-    public XmlSchema read(Element elem, String uri) {
+    /**
+     * Read a schema from a DOM tree into the collection. The schemas in a 
collection must be unique
+     * in the concatenation of the target namespace and the system ID.  
+     * @param elem xs:schema DOM element.
+     * @param systemId System id.
+     * @return the schema object.
+     */
+    public XmlSchema read(Element elem, String systemId) {
         SchemaBuilder builder = new SchemaBuilder(this, null);
-        XmlSchema xmlSchema = builder.handleXmlSchemaElement(elem, uri);
+        XmlSchema xmlSchema = builder.handleXmlSchemaElement(elem, systemId);
         
xmlSchema.setInputEncoding(DOMUtil.getInputEncoding(elem.getOwnerDocument()));
         return xmlSchema;
     }
-
+    
     /**
      * Creates new XmlSchemaCollection
      */
@@ -462,7 +533,7 @@
     }
 
     /**
-     * Retrieve a set of XmlSchema instances with the given its system ID.
+     * Retrieve a set containing the XmlSchema instances with the given system 
ID.
      * In general, this will return a single instance, or none. However,
      * if the schema has no targetNamespace attribute and was included
      * from schemata with different target namespaces, then it may
@@ -494,33 +565,46 @@
         return (XmlSchema[]) c.toArray(new XmlSchema[c.size()]);
     }
 
+    
+    /**
+     * Retrieve a global element from the schema collection. 
+     * @param qname the element QName.
+     * @return the element object, or null.
+     */
     public XmlSchemaElement getElementByQName(QName qname) {
-        String uri = qname.getNamespaceURI();
-        for (Iterator iter = schemas.entrySet().iterator();  iter.hasNext();  
) {
-            Map.Entry entry = (Map.Entry) iter.next();
-            if (((SchemaKey) entry.getKey()).getNamespace().equals(uri)) {
-                XmlSchemaElement element = ((XmlSchema) 
entry.getValue()).getElementByName(qname);
-                if (element != null) {
-                    return element;
-                }
-        }
-        }
-        return null;
-    }
+               String uri = qname.getNamespaceURI();
+               for (Iterator iter = schemas.entrySet().iterator(); 
iter.hasNext();) {
+                       Map.Entry entry = (Map.Entry) iter.next();
+                       if (((SchemaKey) 
entry.getKey()).getNamespace().equals(uri)) {
+                               XmlSchemaElement element = ((XmlSchema) 
entry.getValue())
+                                               .getElementByName(qname);
+                               if (element != null) {
+                                       return element;
+                               }
+                       }
+               }
+               return null;
+       }
 
+    /**
+     * Retrieve a global type from the schema collection.
+     * @param schemaTypeName the QName of the type.
+     * @return the type object, or null.
+     */
     public XmlSchemaType getTypeByQName(QName schemaTypeName) {
-        String uri = schemaTypeName.getNamespaceURI();
-        for (Iterator iter = schemas.entrySet().iterator();  iter.hasNext();  
) {
-            Map.Entry entry = (Map.Entry) iter.next();
-            if (((SchemaKey) entry.getKey()).getNamespace().equals(uri)) {
-                XmlSchemaType type = ((XmlSchema) 
entry.getValue()).getTypeByName(schemaTypeName);
-                if (type != null) {
-                    return type;
-                }
-        }
-        }
-        return null;
-    }
+               String uri = schemaTypeName.getNamespaceURI();
+               for (Iterator iter = schemas.entrySet().iterator(); 
iter.hasNext();) {
+                       Map.Entry entry = (Map.Entry) iter.next();
+                       if (((SchemaKey) 
entry.getKey()).getNamespace().equals(uri)) {
+                               XmlSchemaType type = ((XmlSchema) 
entry.getValue())
+                                               .getTypeByName(schemaTypeName);
+                               if (type != null) {
+                                       return type;
+                               }
+                       }
+               }
+               return null;
+       }
     
     /**
      * Find a global attribute by QName in this collection of schemas.
@@ -578,23 +662,49 @@
         unresolvedTypes.remove(typeName);
     }
 
+    /**
+     * Retrieve the namespace context.
+     * @return the namespace context.
+     */
     public NamespacePrefixList getNamespaceContext() {
         return namespaceContext;
     }
 
+    /**
+     * Set the namespace context for this collection, which controls the 
assignment of
+     * namespace prefixes to namespaces.
+     * @param namespaceContext the context.
+     */
     public void setNamespaceContext(NamespacePrefixList namespaceContext) {
         this.namespaceContext = namespaceContext;
     }
 
-    public void push(SchemaKey pKey){
+    /**
+     * Push a schema onto the stack of schemas.
+     *  This function, while public, is probably not useful outside of 
+     * the implementation.
+     * @param pKey the schema key.
+     */
+    public void push(SchemaKey pKey) {
         stack.push(pKey);
     }
 
-    public void pop(){
+    /**
+     * Pop the stack of schemas. This function, while public, is probably not 
useful outside of 
+     * the implementation.
+     */
+    public void pop() {
         stack.pop();
     }
 
-    public boolean check(SchemaKey pKey){
+    /**
+     * Return an indication of whether a particular schema is in the working 
stack of 
+     * schemas. This function, while public, is probably not useful outside of 
+     * the implementation.
+     * @param pKey schema key
+     * @return true if the schema is in the stack.
+     */
+    public boolean check(SchemaKey pKey) {
         return (stack.indexOf(pKey)==-1);
     }
 

Modified: 
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/resolver/DefaultURIResolver.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/resolver/DefaultURIResolver.java?rev=726564&r1=726563&r2=726564&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/resolver/DefaultURIResolver.java
 (original)
+++ 
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/resolver/DefaultURIResolver.java
 Sun Dec 14 14:27:37 2008
@@ -31,28 +31,36 @@
 /**
  * This resolver provides the means of resolving the imports and includes of a
  * given schema document. The system will call this default resolver if there
- * is no other resolver present in the system
+ * is no other resolver present in the system. 
  */
-public class DefaultURIResolver implements URIResolver {
+public class DefaultURIResolver implements CollectionURIResolver {
+       
+       private String collectionBaseURI;
 
 
     /**
-     * As for the resolver the publid ID is the target namespace of the
-     * schema and the schemaLocation is the value of the schema location
-     * @param namespace
-     * @param schemaLocation
-     * @param baseUri
+     * Try to resolve a schema location to some data.
+     * @param namespace targt namespace.
+     * @param schemaLocation system ID.
+     * @param baseUri base URI for the schema.
      */
     public InputSource resolveEntity(String namespace,
                                      String schemaLocation,
-                                     String baseUri){
+                                     String baseUri) {
 
         if (baseUri!=null) 
         {
             try
             {
                 File baseFile = new File(baseUri);
-                if (baseFile.exists()) baseUri = baseFile.toURI().toString();
+                if (baseFile.exists()) {
+                       baseUri = baseFile.toURI().toString();
+                } else if(collectionBaseURI != null) {
+                       baseFile = new File(collectionBaseURI);
+                    if (baseFile.exists()) {
+                       baseUri = baseFile.toURI().toString();
+                    }
+                }
                 
                 String ref = new URI(baseUri).resolve(new 
URI(schemaLocation)).toString();
 
@@ -161,4 +169,20 @@
 
         return new URL("file", "", path);
     }    // getFileURL
+
+    /**
+     * Get the base URI derived from a schema collection. It serves as a 
fallback from the specified base.
+     * @return URI
+     */
+       public String getCollectionBaseURI() {
+               return collectionBaseURI;
+       }
+
+       /**
+        * set the collection base URI, which serves as a fallback from the 
base of the immediate schema.
+        * @param collectionBaseURI the URI.
+        */
+       public void setCollectionBaseURI(String collectionBaseURI) {
+               this.collectionBaseURI = collectionBaseURI;
+       }
 }


Reply via email to