Implemented XDocument.Validate().
authorAtsushi Eno <atsushi@ximian.com>
Mon, 29 Nov 2010 12:39:16 +0000 (21:39 +0900)
committerAtsushi Eno <atsushi@ximian.com>
Mon, 29 Nov 2010 12:39:16 +0000 (21:39 +0900)
mcs/class/System.Xml.Linq/System.Xml.Linq/XNodeReader.cs
mcs/class/System.Xml.Linq/System.Xml.Linq/XObject.cs
mcs/class/System.Xml.Linq/System.Xml.Schema/Extensions.cs
mcs/class/System.Xml.Linq/Test/System.Xml.Schema/ExtensionsTest.cs

index 50833464023ae7acfe43c315861407a87c816ea2..942f1776966b2434f8aa71c364d1e676515c6885 100644 (file)
@@ -130,7 +130,7 @@ namespace System.Xml.Linq
                        get { return !EOF && attr < 0 && node is XElement ? ((XElement) node).IsEmpty : false; }
                }
 
-               XAttribute GetCurrentAttribute ()
+               internal XAttribute GetCurrentAttribute ()
                {
                        return GetXAttribute (attr);
                }
@@ -515,5 +515,10 @@ namespace System.Xml.Linq
                {
                        throw new NotSupportedException ();
                }
+               
+               // Note that this does not return attribute node.
+               internal XNode CurrentNode {
+                       get { return node; }
+               }
        }
 }
index f73b7ada89a82c26596e300251c64fcd4871c441..1f802203db320a427c6b57cdb516ccc74fde8133 100644 (file)
@@ -26,6 +26,7 @@
 
 using System;
 using System.Collections.Generic;
+using System.Linq;
 using System.Xml;
 
 namespace System.Xml.Linq
@@ -92,11 +93,7 @@ namespace System.Xml.Linq
 
                public object Annotation (Type type)
                {
-                       if (annotations != null)
-                               foreach (object o in annotations)
-                                       if (o.GetType () == type)
-                                               return o;
-                       return null;
+                       return Annotations (type).FirstOrDefault ();
                }
 
                public IEnumerable<T> Annotations<T> () where T : class
@@ -107,10 +104,12 @@ namespace System.Xml.Linq
 
                public IEnumerable<object> Annotations (Type type)
                {
+                       if (type == null)
+                               throw new ArgumentNullException ("type");
                        if (annotations == null)
                                yield break;
                        foreach (object o in annotations)
-                               if (o.GetType () == type)
+                               if (type.IsAssignableFrom (o.GetType ()))
                                        yield return o;
                }
 
@@ -124,7 +123,7 @@ namespace System.Xml.Linq
                        if (annotations == null)
                                return;
                        for (int i = 0; i < annotations.Count; i++)
-                               if (annotations [i].GetType () == type)
+                               if (type.IsAssignableFrom (annotations [i].GetType ()))
                                        annotations.RemoveAt (i);
                }
 
index fe8a41c09153379e6e349a25886eb57c32c853e8..18aeb4d2ef94edc71dd39318f0f9b5390e4fde85 100644 (file)
@@ -32,19 +32,16 @@ using System.Xml.Linq;
 
 namespace System.Xml.Schema
 {
-       [MonoTODO]
        public static class Extensions
        {
-               [MonoTODO]
                public static IXmlSchemaInfo GetSchemaInfo (this XAttribute attribute)
                {
-                       throw new NotImplementedException ();
+                       return attribute.Annotation<IXmlSchemaInfo> ();
                }
 
-               [MonoTODO]
                public static IXmlSchemaInfo GetSchemaInfo (this XElement element)
                {
-                       throw new NotImplementedException ();
+                       return element.Annotation<IXmlSchemaInfo> ();
                }
 
                [MonoTODO]
@@ -59,16 +56,33 @@ namespace System.Xml.Schema
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
                public static void Validate (this XDocument document, XmlSchemaSet schemas, ValidationEventHandler handler)
                {
-                       throw new NotImplementedException ();
+                       Validate (document, schemas, handler, false);
                }
 
-               [MonoTODO]
                public static void Validate (this XDocument document, XmlSchemaSet schemas, ValidationEventHandler handler, bool addSchemaInfo)
                {
-                       throw new NotImplementedException ();
+                       if (document == null)
+                               throw new ArgumentNullException ("document");
+                       if (schemas == null)
+                               throw new ArgumentNullException ("schemas");
+                       var xrs = new XmlReaderSettings () { ValidationType = ValidationType.Schema };
+                       xrs.Schemas = schemas;
+                       xrs.ValidationEventHandler += handler;
+                       var source = new XNodeReader (document);
+                       var xr = XmlReader.Create (source, xrs);
+                       while (xr.Read ()) {
+                               if (addSchemaInfo) {
+                                       if (xr.NodeType == XmlNodeType.Element) {
+                                               source.CurrentNode.AddAnnotation (xr.SchemaInfo);
+                                               while (xr.MoveToNextAttribute ())
+                                                       if (xr.NamespaceURI != XUtil.XmlnsNamespace)
+                                                               source.GetCurrentAttribute ().AddAnnotation (xr.SchemaInfo);
+                                               xr.MoveToElement ();
+                                       }
+                               }
+                       }
                }
 
                [MonoTODO]
index 691b8abd877683675e8fd4b60af2e83cb3ca1b1f..9dee2bda837c45c7776620f9d25f2540974792de 100644 (file)
@@ -128,7 +128,6 @@ namespace MonoTests.System.Xml.Schema
 
                // test succesfull validation
                [Test]
-               [Category ("NotWorking")]
                public void XDocumentSuccessValidate () 
                {       
                        validationSucceded = true;
@@ -139,7 +138,6 @@ namespace MonoTests.System.Xml.Schema
 
                // test failed validation
                [Test]
-               [Category ("NotWorking")]
                public void XDocumentFailValidate ()
                {       
                        String elementName = "AlteringElementName";
@@ -161,7 +159,6 @@ namespace MonoTests.System.Xml.Schema
                 */
                [Test]
                [ExpectedException (typeof (XmlSchemaValidationException))]
-               [Category ("NotWorking")]
                public void XDocumentThrowExceptionValidate ()
                {
                        String elementName = "AlteringElementName";
@@ -179,7 +176,6 @@ namespace MonoTests.System.Xml.Schema
                 * the post-schema-validation infoset (PSVI)
                 */
                [Test]
-               [Category ("NotWorking")]
                public void XDocumentAddSchemaInfoValidate ()
                {      
                        // no. of elements before validation
@@ -231,7 +227,6 @@ namespace MonoTests.System.Xml.Schema
                 * the post-schema-validation infoset (PSVI).
                 */
                [Test]
-               [Category ("NotWorking")]
                public void XDocumentNoSchemaInfoValidate ()
                {
                        // no. of elements before validation