Extensions.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #if !NET_2_1
  27. using System;
  28. using System.Xml;
  29. using System.Xml.Linq;
  30. namespace System.Xml.Schema
  31. {
  32. public static class Extensions
  33. {
  34. public static IXmlSchemaInfo GetSchemaInfo (this XAttribute attribute)
  35. {
  36. return attribute.Annotation<IXmlSchemaInfo> ();
  37. }
  38. public static IXmlSchemaInfo GetSchemaInfo (this XElement element)
  39. {
  40. return element.Annotation<IXmlSchemaInfo> ();
  41. }
  42. public static void Validate (this XAttribute attribute, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler handler)
  43. {
  44. Validate (attribute, partialValidationType, schemas, handler, false);
  45. }
  46. public static void Validate (this XAttribute attribute, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler handler, bool addSchemaInfo)
  47. {
  48. if (attribute == null)
  49. throw new ArgumentNullException ("attribute");
  50. if (schemas == null)
  51. throw new ArgumentNullException ("schemas");
  52. var nsmgr = new XmlNamespaceManager (new NameTable ());
  53. var v = new XmlSchemaValidator (nsmgr.NameTable, schemas, nsmgr, XmlSchemaValidationFlags.None);
  54. if (handler != null)
  55. v.ValidationEventHandler += handler;
  56. if (partialValidationType != null)
  57. v.Initialize (partialValidationType);
  58. else
  59. v.Initialize ();
  60. var xi = addSchemaInfo ? new XmlSchemaInfo () : null;
  61. v.ValidateAttribute (attribute.Name.LocalName, attribute.Name.NamespaceName, attribute.Value, xi);
  62. }
  63. public static void Validate (this XDocument document, XmlSchemaSet schemas, ValidationEventHandler handler)
  64. {
  65. Validate (document, schemas, handler, false);
  66. }
  67. public static void Validate (this XDocument document, XmlSchemaSet schemas, ValidationEventHandler handler, bool addSchemaInfo)
  68. {
  69. if (document == null)
  70. throw new ArgumentNullException ("document");
  71. if (schemas == null)
  72. throw new ArgumentNullException ("schemas");
  73. var xrs = new XmlReaderSettings () { ValidationType = ValidationType.Schema };
  74. xrs.Schemas = schemas;
  75. xrs.ValidationEventHandler += handler;
  76. var source = new XNodeReader (document);
  77. var xr = XmlReader.Create (source, xrs);
  78. while (xr.Read ()) {
  79. if (addSchemaInfo) {
  80. if (xr.NodeType == XmlNodeType.Element) {
  81. source.CurrentNode.AddAnnotation (xr.SchemaInfo);
  82. while (xr.MoveToNextAttribute ())
  83. if (xr.NamespaceURI != XUtil.XmlnsNamespace)
  84. source.GetCurrentAttribute ().AddAnnotation (xr.SchemaInfo);
  85. xr.MoveToElement ();
  86. }
  87. }
  88. }
  89. }
  90. [MonoTODO]
  91. public static void Validate (this XElement element, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler handler)
  92. {
  93. throw new NotImplementedException ();
  94. }
  95. [MonoTODO]
  96. public static void Validate (this XElement element, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler handler, bool addSchemaInfo)
  97. {
  98. throw new NotImplementedException ();
  99. }
  100. }
  101. }
  102. #endif