From 181d10350c45a24acf0e79dabdaeb3acd03fc1aa Mon Sep 17 00:00:00 2001 From: Gert Driesen Date: Tue, 28 Feb 2006 19:18:36 +0000 Subject: [PATCH] * XmlSchema.cs: Do not define namespace for zero-length TargetNamespace and report XmlSchemaException when compiling XmlSchema with zero-length TargetNamespace. Fixes bug #77391. * XmlSchemaTests.cs: Added test for writing XmlSchema with zero-length TargetNamespace. Added test for compiling XmlSchema with zero-length TargetNamespace. Replaced Console.WriteLine with AssertEquals. svn path=/trunk/mcs/; revision=57415 --- .../System.XML/System.Xml.Schema/ChangeLog | 6 +++++ .../System.XML/System.Xml.Schema/XmlSchema.cs | 5 +++- .../Test/System.Xml.Schema/ChangeLog | 7 +++++ .../Test/System.Xml.Schema/XmlSchemaTests.cs | 27 +++++++++++++++++-- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/mcs/class/System.XML/System.Xml.Schema/ChangeLog b/mcs/class/System.XML/System.Xml.Schema/ChangeLog index f63091447d8..8b742ced204 100644 --- a/mcs/class/System.XML/System.Xml.Schema/ChangeLog +++ b/mcs/class/System.XML/System.Xml.Schema/ChangeLog @@ -1,3 +1,9 @@ +2006-02-28 Gert Driesen + + * XmlSchema.cs: Do not define namespace for zero-length TargetNamespace + and report XmlSchemaException when compiling XmlSchema with + zero-length TargetNamespace. + 2006-02-01 Atsushi Enomoto * BuiltInDatatype.cs : anyURI could be such relative path that diff --git a/mcs/class/System.XML/System.Xml.Schema/XmlSchema.cs b/mcs/class/System.XML/System.Xml.Schema/XmlSchema.cs index c415f13c6f8..10fdeedadc2 100644 --- a/mcs/class/System.XML/System.Xml.Schema/XmlSchema.cs +++ b/mcs/class/System.XML/System.Xml.Schema/XmlSchema.cs @@ -337,6 +337,9 @@ namespace System.Xml.Schema //4. targetNamespace should be of type anyURI or absent if (TargetNamespace != null) { + if (TargetNamespace.Length == 0) + error (handler, "The targetNamespace attribute cannot have have empty string as its value."); + if(!XmlSchemaUtil.CheckAnyUri (TargetNamespace)) error(handler, TargetNamespace+" is not a valid value for targetNamespace attribute of schema"); } @@ -905,7 +908,7 @@ namespace System.Xml.Schema // Add the xml schema namespace. (It is done // only when no entry exists in Namespaces). nss.Add ("xs", XmlSchema.Namespace); - if (TargetNamespace != null) + if (TargetNamespace != null && TargetNamespace.Length != 0) nss.Add ("tns", TargetNamespace); } diff --git a/mcs/class/System.XML/Test/System.Xml.Schema/ChangeLog b/mcs/class/System.XML/Test/System.Xml.Schema/ChangeLog index fd3032e8ad0..74cc73333c9 100644 --- a/mcs/class/System.XML/Test/System.Xml.Schema/ChangeLog +++ b/mcs/class/System.XML/Test/System.Xml.Schema/ChangeLog @@ -1,3 +1,10 @@ +2006-02-28 Gert Driesen + + * XmlSchemaTests.cs: Added test for writing XmlSchema with + zero-length TargetNamespace. Added test for compiling XmlSchema + with zero-length TargetNamespace. Replaced Console.WriteLine with + AssertEquals. + 2006-02-06 Atsushi Enomoto * XmlSchemaDatatypeTests.cs: (TestAnyType) NotDotNet -> Ignore. diff --git a/mcs/class/System.XML/Test/System.Xml.Schema/XmlSchemaTests.cs b/mcs/class/System.XML/Test/System.Xml.Schema/XmlSchemaTests.cs index cf2e11bf26b..ceb0c971933 100644 --- a/mcs/class/System.XML/Test/System.Xml.Schema/XmlSchemaTests.cs +++ b/mcs/class/System.XML/Test/System.Xml.Schema/XmlSchemaTests.cs @@ -130,13 +130,26 @@ namespace MonoTests.System.Xml AssertCompiledElement (element, qname, cType); } + [Test] + [ExpectedException (typeof (XmlSchemaException))] + public void TestCompile_ZeroLength_TargetNamespace () + { + XmlSchema schema = new XmlSchema (); + schema.TargetNamespace = string.Empty; + Assert (!schema.IsCompiled); + + // MS.NET 1.x: The Namespace '' is an invalid URI. + // MS.NET 2.0: The targetNamespace attribute cannot have empty string as its value. + schema.Compile (null); + } + [Test] [ExpectedException (typeof (XmlSchemaException))] public void TestCompileNonSchema () { XmlTextReader xtr = new XmlTextReader ("", XmlNodeType.Document, null); XmlSchema schema = XmlSchema.Read (xtr, null); - xtr.Close (); + xtr.Close (); } [Test] @@ -200,7 +213,17 @@ namespace MonoTests.System.Xml xw = new XmlTextWriter (sw); xs.TargetNamespace = "urn:foo"; xs.Write (xw); - Console.WriteLine ("#2", "", doc.DocumentElement.OuterXml); + doc.LoadXml (sw.ToString ()); + AssertEquals ("#2", "", doc.DocumentElement.OuterXml); + + // Zero-length TargetNamespace + xs = new XmlSchema (); + sw = new StringWriter (); + xw = new XmlTextWriter (sw); + xs.TargetNamespace = string.Empty; + xs.Write (xw); + doc.LoadXml (sw.ToString ()); + AssertEquals ("#2b", "", doc.DocumentElement.OuterXml); // XmlSerializerNamespaces xs = new XmlSchema (); -- 2.25.1