Merge pull request #799 from kebby/master
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Schema / Extensions.cs
1 //
2 // Authors:
3 //   Atsushi Enomoto
4 //
5 // Copyright 2007 Novell (http://www.novell.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26
27 using System;
28 using System.Xml;
29 using System.Xml.Linq;
30
31 namespace System.Xml.Schema
32 {
33         public static class Extensions
34         {
35                 public static IXmlSchemaInfo GetSchemaInfo (this XAttribute source)
36                 {
37                         return source.Annotation<IXmlSchemaInfo> ();
38                 }
39
40                 public static IXmlSchemaInfo GetSchemaInfo (this XElement source)
41                 {
42                         return source.Annotation<IXmlSchemaInfo> ();
43                 }
44
45                 public static void Validate (this XAttribute source, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler)
46                 {
47                         Validate (source, partialValidationType, schemas, validationEventHandler, false);
48                 }
49
50                 public static void Validate (this XAttribute source, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler, bool addSchemaInfo)
51                 {
52                         if (source == null)
53                                 throw new ArgumentNullException ("source");
54                         if (schemas == null)
55                                 throw new ArgumentNullException ("schemas");
56                         var nsmgr = new XmlNamespaceManager (new NameTable ());
57                         var v = new XmlSchemaValidator (nsmgr.NameTable, schemas, nsmgr, XmlSchemaValidationFlags.None);
58                         if (validationEventHandler != null)
59                                 v.ValidationEventHandler += validationEventHandler;
60                         if (partialValidationType != null)
61                                 v.Initialize (partialValidationType);
62                         else
63                                 v.Initialize ();
64                         var xi = addSchemaInfo ? new XmlSchemaInfo () : null;
65                         v.ValidateAttribute (source.Name.LocalName, source.Name.NamespaceName, source.Value, xi);
66                 }
67
68                 public static void Validate (this XDocument source, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler)
69                 {
70                         Validate (source, schemas, validationEventHandler, false);
71                 }
72
73                 public static void Validate (this XDocument source, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler, bool addSchemaInfo)
74                 {
75                         if (source == null)
76                                 throw new ArgumentNullException ("source");
77                         if (schemas == null)
78                                 throw new ArgumentNullException ("schemas");
79                         var xrs = new XmlReaderSettings () { ValidationType = ValidationType.Schema };
80                         xrs.Schemas = schemas;
81                         xrs.ValidationEventHandler += validationEventHandler;
82                         var xsource = new XNodeReader (source);
83                         var xr = XmlReader.Create (xsource, xrs);
84                         while (xr.Read ()) {
85                                 if (addSchemaInfo) {
86                                         if (xr.NodeType == XmlNodeType.Element) {
87                                                 xsource.CurrentNode.AddAnnotation (xr.SchemaInfo);
88                                                 while (xr.MoveToNextAttribute ())
89                                                         if (xr.NamespaceURI != XUtil.XmlnsNamespace)
90                                                                 xsource.GetCurrentAttribute ().AddAnnotation (xr.SchemaInfo);
91                                                 xr.MoveToElement ();
92                                         }
93                                 }
94                         }
95                 }
96
97                 [MonoTODO]
98                 public static void Validate (this XElement source, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler)
99                 {
100                         throw new NotImplementedException ();
101                 }
102
103                 [MonoTODO]
104                 public static void Validate (this XElement source, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler, bool addSchemaInfo)
105                 {
106                         throw new NotImplementedException ();
107                 }
108         }
109 }