2008-06-06 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Fri, 6 Jun 2008 10:07:22 +0000 (10:07 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Fri, 6 Jun 2008 10:07:22 +0000 (10:07 -0000)
* XmlSchema.cs : fixed bug #397612, patch by James Fitzsimons.
  in ProcessExternal(), Make xs:import without schemaLocation work
  fine as if it were with schemaLocation.

2008-06-06  James Fitzsimons <james.fitzsimons@gmail.com>

* XmlSchemaTests.cs: added test for bug #397612.
* XmlSchemaAssertion.cs : make sure to close schema XmlReader.

2008-06-06  Atsushi Enomoto  <atsushi@ximian.com>

* importNamespaceTest.xsd, xsdimporttest.xml, importedNamespace.xsd:
  new test files.

svn path=/trunk/mcs/; revision=105144

mcs/class/System.XML/System.Xml.Schema/ChangeLog
mcs/class/System.XML/System.Xml.Schema/XmlSchema.cs
mcs/class/System.XML/Test/System.Xml.Schema/ChangeLog
mcs/class/System.XML/Test/System.Xml.Schema/XmlSchemaAssertion.cs
mcs/class/System.XML/Test/System.Xml.Schema/XmlSchemaTests.cs
mcs/class/System.XML/Test/XmlFiles/xsd/ChangeLog
mcs/class/System.XML/Test/XmlFiles/xsd/importNamespaceTest.xsd [new file with mode: 0644]
mcs/class/System.XML/Test/XmlFiles/xsd/importedNamespace.xsd [new file with mode: 0644]
mcs/class/System.XML/Test/XmlFiles/xsd/xsdimporttest.xml [new file with mode: 0644]

index e9123debcc758b7b3fcec6bd6ecca46379b1434a..cabc0d81685c2edcd294331b81e772f30b59de3a 100644 (file)
@@ -1,3 +1,9 @@
+2008-06-06  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * XmlSchema.cs : fixed bug #397612, patch by James Fitzsimons.
+         in ProcessExternal(), Make xs:import without schemaLocation work
+         fine as if it were with schemaLocation.
+
 2008-06-06  James Fitzsimons <james.fitzsimons@gmail.com>
 
        * XmlSchema.cs : Changed GetResolvedUri to return Uri in correct case
index b4dbed9c33ccf57e5b056fe64f1d773484d6632a..9851f10c024b876686d8a45c93f20205d901af8c 100644 (file)
@@ -368,7 +368,7 @@ namespace System.Xml.Schema
                        // First, we run into inclusion schemas to collect 
                        // compilation target items into compiledItems.
                        for (int i = 0; i < Includes.Count; i++)
-                               ProcessExternal (handler, handledUris, resolver, Includes [i] as XmlSchemaExternal);
+                               ProcessExternal (handler, handledUris, resolver, Includes [i] as XmlSchemaExternal, col);
 
                        // Compilation phase.
                        // At least each Compile() must give unique (qualified) name for each component.
@@ -476,18 +476,21 @@ namespace System.Xml.Schema
 #endif
                }
 
-               void ProcessExternal (ValidationEventHandler handler, Hashtable handledUris, XmlResolver resolver, XmlSchemaExternal ext)
+               void ProcessExternal (ValidationEventHandler handler, Hashtable handledUris, XmlResolver resolver, XmlSchemaExternal ext, XmlSchemaSet col)
                {
                        if (ext == null) {
                                error (handler, String.Format ("Object of Type {0} is not valid in Includes Property of XmlSchema", ext.GetType().Name));
                                return;
                        }
 
-                       if (ext.SchemaLocation == null) 
+                       // The only case we want to handle where the SchemaLocation is null is if the external is an import.
+                       XmlSchemaImport import = ext as XmlSchemaImport;
+                       if (ext.SchemaLocation == null && import == null)
                                return;
-//                     if (ext.Schema != null) // already read
-//                             return;
-
+                       
+                       XmlSchema includedSchema = null;
+               if (ext.SchemaLocation != null)
+               {
                        Stream stream = null;
                        string url = null;
                        if (resolver != null) {
@@ -504,7 +507,7 @@ namespace System.Xml.Schema
                                        stream = null;
                                }
                        }
-
+               
                        // Process redefinition children in advance.
                        XmlSchemaRedefine redefine = ext as XmlSchemaRedefine;
                        if (redefine != null) {
@@ -521,7 +524,6 @@ namespace System.Xml.Schema
                                }
                        }
 
-                       XmlSchema includedSchema = null;
                        if (stream == null) {
                                // It is missing schema components.
                                missedSubComponents = true;
@@ -539,16 +541,35 @@ namespace System.Xml.Schema
                        }
                        includedSchema.SetParent ();
                        ext.Schema = includedSchema;
+               }
 
                        // Set - actual - target namespace for the included schema * before compilation*.
-                       XmlSchemaImport import = ext as XmlSchemaImport;
                        if (import != null) {
-                               if (TargetNamespace == includedSchema.TargetNamespace) {
-                                       error (handler, "Target namespace must be different from that of included schema.");
-                                       return;
-                               } else if (includedSchema.TargetNamespace != import.Namespace) {
-                                       error (handler, "Attribute namespace and its importing schema's target namespace must be the same.");
-                                       return;
+                               if (ext.Schema == null && ext.SchemaLocation == null) {
+                                       // if a schema location wasn't specified, check the other schemas we have to see if one of those
+                                       // is a match.
+                                       foreach(XmlSchema schema in col.Schemas())
+                                       {
+                                               if (schema.TargetNamespace == import.Namespace)
+                                               {
+                                                       includedSchema = schema;
+                                                       includedSchema.schemas = schemas;
+                                                       includedSchema.SetParent ();
+                                                       ext.Schema = includedSchema;
+                                                       break;
+                                               }
+                                       }
+                                       // handle case where target namespace doesn't exist in schema collection - i.e can't find it at all
+                                       if (includedSchema == null)
+                                               return;
+                               } else {
+                                       if (TargetNamespace == includedSchema.TargetNamespace) {
+                                               error (handler, "Target namespace must be different from that of included schema.");
+                                               return;
+                                       } else if (includedSchema.TargetNamespace != import.Namespace) {
+                                               error (handler, "Attribute namespace and its importing schema's target namespace must be the same.");
+                                               return;
+                                       }
                                }
                        } else {
                                if (TargetNamespace == null && 
@@ -563,13 +584,14 @@ namespace System.Xml.Schema
 
                        // Do not compile included schema here.
 
-                       AddExternalComponentsTo (includedSchema, compilationItems, handler, handledUris, resolver);
+                       AddExternalComponentsTo (includedSchema, compilationItems, handler, handledUris, resolver, col);
                }
 
-               void AddExternalComponentsTo (XmlSchema s, XmlSchemaObjectCollection items, ValidationEventHandler handler, Hashtable handledUris, XmlResolver resolver)
+
+               void AddExternalComponentsTo (XmlSchema s, XmlSchemaObjectCollection items, ValidationEventHandler handler, Hashtable handledUris, XmlResolver resolver, XmlSchemaSet col)
                {
                        foreach (XmlSchemaExternal ext in s.Includes)
-                               ProcessExternal (handler, handledUris, resolver, ext);
+                               ProcessExternal (handler, handledUris, resolver, ext, col);
 //                             if (ext.Schema != null)
 //                                     AddExternalComponentsTo (ext.Schema, items);
                        foreach (XmlSchemaObject obj in s.Items)
index c3bcc5c324278d87d2baa8aee94edbfffddad973..50c29662ebdd424b0b28944db1ba6e4106162cdb 100644 (file)
@@ -1,3 +1,8 @@
+2008-06-06  James Fitzsimons <james.fitzsimons@gmail.com>
+
+       * XmlSchemaTests.cs: added test for bug #397612.
+       * XmlSchemaAssertion.cs : make sure to close schema XmlReader.
+
 2008-06-06  James Fitzsimons <james.fitzsimons@gmail.com>
 
        * XmlSchemaTests.cs: added test to support change to 
index 2fe553dd744a0e5470d7af1e3bf35bd92cef40da..7e55ed0ed7747eb2bc92a2bd69a531d23aa0eb03 100644 (file)
@@ -20,7 +20,10 @@ namespace MonoTests.System.Xml
        {
                protected XmlSchema GetSchema (string path)
                {
-                       return XmlSchema.Read (new XmlTextReader (path), null);
+                       XmlTextReader reader = new XmlTextReader (path);
+                       XmlSchema schema = XmlSchema.Read (reader, null);
+                       reader.Close ();
+                       return schema;
                }
 
                protected XmlQualifiedName QName (string name, string ns)
index c58efe130db5dd0d8a44eff91506000ac12a2d49..a4f36651731df8a001ff0c56f630559b16baf45e 100644 (file)
@@ -477,6 +477,23 @@ namespace MonoTests.System.Xml
                        
                        AssertEquals ("assembly://MyAssembly.Name/MyProjectNameSpace/objects.xsd", resolver.ReceivedUri.OriginalString);
                }
+
+               [Test]
+               public void TestImportNoSchemaLocation()
+               {
+                       XmlSchemaSet schemaSet = new XmlSchemaSet ();
+                       schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importNamespaceTest.xsd"));
+                       schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importedNamespace.xsd"));
+                       
+                       XmlReaderSettings settings = new XmlReaderSettings ();
+                       settings.Schemas.Add (schemaSet);
+                       settings.ValidationType = ValidationType.Schema;
+                       
+                       XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsdimporttest.xml", settings);
+                       
+                       // Parse the file. 
+                       while (reader.Read()) {}
+               }
 #endif
        }
 }
index 107eee798757402571d65e3c10fff9953f97d684..5ef02f4df1ce89130d50c6fc5f6d81769e26efc1 100644 (file)
@@ -1,3 +1,8 @@
+2008-06-06  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * importNamespaceTest.xsd, xsdimporttest.xml, importedNamespace.xsd:
+         new test files.
+
 2008-06-06  Atsushi Enomoto  <atsushi@ximian.com>
 
        * resolveUriSchema.xsd : (actually I've incorrectly added it before
diff --git a/mcs/class/System.XML/Test/XmlFiles/xsd/importNamespaceTest.xsd b/mcs/class/System.XML/Test/XmlFiles/xsd/importNamespaceTest.xsd
new file mode 100644 (file)
index 0000000..ab5c1e2
--- /dev/null
@@ -0,0 +1,105 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+                  xmlns="http://tempuri.org/importNamespaceTest.xsd"
+                  targetNamespace="http://tempuri.org/importNamespaceTest.xsd"
+                  xmlns:objects="http://tempuri.org/importedNamespace.xsd"
+                  elementFormDefault="qualified">
+
+       <xs:import namespace="http://tempuri.org/importedNamespace.xsd" />
+
+       <xs:element name="root">
+               <xs:complexType>
+                       <xs:sequence>
+                               <xs:element name="feisty">
+                                       <xs:complexType>
+                                               <xs:group ref="objects:objectList" minOccurs="1" maxOccurs="unbounded"/>
+                                       </xs:complexType>
+                               </xs:element>
+                       </xs:sequence>
+               </xs:complexType>
+       </xs:element>
+</xs:schema>
+<?xml version="1.0" encoding="UTF-8" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+                  xmlns="http://tempuri.org/importNamespaceTest.xsd"
+                  targetNamespace="http://tempuri.org/importNamespaceTest.xsd"
+                  xmlns:objects="http://tempuri.org/importedNamespace.xsd"
+                  elementFormDefault="qualified">
+
+       <xs:import namespace="http://tempuri.org/importedNamespace.xsd" />
+
+       <xs:element name="root">
+               <xs:complexType>
+                       <xs:sequence>
+                               <xs:element name="feisty">
+                                       <xs:complexType>
+                                               <xs:group ref="objects:objectList" minOccurs="1" maxOccurs="unbounded"/>
+                                       </xs:complexType>
+                               </xs:element>
+                       </xs:sequence>
+               </xs:complexType>
+       </xs:element>
+</xs:schema>
+<?xml version="1.0" encoding="UTF-8" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+                  xmlns="http://tempuri.org/importNamespaceTest.xsd"
+                  targetNamespace="http://tempuri.org/importNamespaceTest.xsd"
+                  xmlns:objects="http://tempuri.org/importedNamespace.xsd"
+                  elementFormDefault="qualified">
+
+       <xs:import namespace="http://tempuri.org/importedNamespace.xsd" />
+
+       <xs:element name="root">
+               <xs:complexType>
+                       <xs:sequence>
+                               <xs:element name="feisty">
+                                       <xs:complexType>
+                                               <xs:group ref="objects:objectList" minOccurs="1" maxOccurs="unbounded"/>
+                                       </xs:complexType>
+                               </xs:element>
+                       </xs:sequence>
+               </xs:complexType>
+       </xs:element>
+</xs:schema>
+<?xml version="1.0" encoding="UTF-8" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+                  xmlns="http://tempuri.org/importNamespaceTest.xsd"
+                  targetNamespace="http://tempuri.org/importNamespaceTest.xsd"
+                  xmlns:objects="http://tempuri.org/importedNamespace.xsd"
+                  elementFormDefault="qualified">
+
+       <xs:import namespace="http://tempuri.org/importedNamespace.xsd" />
+
+       <xs:element name="root">
+               <xs:complexType>
+                       <xs:sequence>
+                               <xs:element name="feisty">
+                                       <xs:complexType>
+                                               <xs:group ref="objects:objectList" minOccurs="1" maxOccurs="unbounded"/>
+                                       </xs:complexType>
+                               </xs:element>
+                       </xs:sequence>
+               </xs:complexType>
+       </xs:element>
+</xs:schema>
+<?xml version="1.0" encoding="UTF-8" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+                  xmlns="http://tempuri.org/importNamespaceTest.xsd"
+                  targetNamespace="http://tempuri.org/importNamespaceTest.xsd"
+                  xmlns:objects="http://tempuri.org/importedNamespace.xsd"
+                  elementFormDefault="qualified">
+
+       <xs:import namespace="http://tempuri.org/importedNamespace.xsd" />
+
+       <xs:element name="root">
+               <xs:complexType>
+                       <xs:sequence>
+                               <xs:element name="feisty">
+                                       <xs:complexType>
+                                               <xs:group ref="objects:objectList" minOccurs="1" maxOccurs="unbounded"/>
+                                       </xs:complexType>
+                               </xs:element>
+                       </xs:sequence>
+               </xs:complexType>
+       </xs:element>
+</xs:schema>
diff --git a/mcs/class/System.XML/Test/XmlFiles/xsd/importedNamespace.xsd b/mcs/class/System.XML/Test/XmlFiles/xsd/importedNamespace.xsd
new file mode 100644 (file)
index 0000000..5c6f0aa
--- /dev/null
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+                  xmlns="http://tempuri.org/importedNamespace.xsd"
+                  targetNamespace="http://tempuri.org/importedNamespace.xsd"
+                  elementFormDefault="qualified">
+        <xs:group name="objectList">
+        <xs:sequence>
+            <xs:element name="description" type="xs:string" minOccurs="0"/>
+        </xs:sequence>   
+        </xs:group>
+</xs:schema>
+<?xml version="1.0" encoding="UTF-8" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+                  xmlns="http://tempuri.org/importedNamespace.xsd"
+                  targetNamespace="http://tempuri.org/importedNamespace.xsd"
+                  elementFormDefault="qualified">
+        <xs:group name="objectList">
+        <xs:sequence>
+            <xs:element name="description" type="xs:string" minOccurs="0"/>
+        </xs:sequence>   
+        </xs:group>
+</xs:schema>
+<?xml version="1.0" encoding="UTF-8" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+                  xmlns="http://tempuri.org/importedNamespace.xsd"
+                  targetNamespace="http://tempuri.org/importedNamespace.xsd"
+                  elementFormDefault="qualified">
+        <xs:group name="objectList">
+        <xs:sequence>
+            <xs:element name="description" type="xs:string" minOccurs="0"/>
+        </xs:sequence>   
+        </xs:group>
+</xs:schema>
+<?xml version="1.0" encoding="UTF-8" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+                  xmlns="http://tempuri.org/importedNamespace.xsd"
+                  targetNamespace="http://tempuri.org/importedNamespace.xsd"
+                  elementFormDefault="qualified">
+        <xs:group name="objectList">
+        <xs:sequence>
+            <xs:element name="description" type="xs:string" minOccurs="0"/>
+        </xs:sequence>   
+        </xs:group>
+</xs:schema>
+<?xml version="1.0" encoding="UTF-8" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+                  xmlns="http://tempuri.org/importedNamespace.xsd"
+                  targetNamespace="http://tempuri.org/importedNamespace.xsd"
+                  elementFormDefault="qualified">
+        <xs:group name="objectList">
+        <xs:sequence>
+            <xs:element name="description" type="xs:string" minOccurs="0"/>
+        </xs:sequence>   
+        </xs:group>
+</xs:schema>
diff --git a/mcs/class/System.XML/Test/XmlFiles/xsd/xsdimporttest.xml b/mcs/class/System.XML/Test/XmlFiles/xsd/xsdimporttest.xml
new file mode 100644 (file)
index 0000000..7015479
--- /dev/null
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<root xmlns="http://tempuri.org/importNamespaceTest.xsd"
+               xmlns:obj="http://tempuri.org/importedNamespace.xsd">
+       <feisty/>
+</root>
+
+<?xml version="1.0" encoding="utf-8" ?>
+<root xmlns="http://tempuri.org/importNamespaceTest.xsd"
+               xmlns:obj="http://tempuri.org/importedNamespace.xsd">
+       <feisty/>
+</root>
+
+<?xml version="1.0" encoding="utf-8" ?>
+<root xmlns="http://tempuri.org/importNamespaceTest.xsd"
+               xmlns:obj="http://tempuri.org/importedNamespace.xsd">
+       <feisty/>
+</root>
+
+<?xml version="1.0" encoding="utf-8" ?>
+<root xmlns="http://tempuri.org/importNamespaceTest.xsd"
+               xmlns:obj="http://tempuri.org/importedNamespace.xsd">
+       <feisty/>
+</root>
+
+<?xml version="1.0" encoding="utf-8" ?>
+<root xmlns="http://tempuri.org/importNamespaceTest.xsd"
+               xmlns:obj="http://tempuri.org/importedNamespace.xsd">
+       <feisty/>
+</root>
+