XmlSchemaSet.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // XmlSchemaSet.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C)2003 Atsushi Enomoto
  8. // (C)2004 Novell Inc.
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. using System.ComponentModel;
  34. using System.IO;
  35. using System.Security.Policy;
  36. using System.Xml.Schema;
  37. using System.Xml.XPath;
  38. namespace System.Xml.Schema
  39. {
  40. #if NET_2_0
  41. public sealed class XmlSchemaSet
  42. #else
  43. internal sealed class XmlSchemaSet
  44. #endif
  45. {
  46. XmlNameTable nameTable;
  47. XmlResolver xmlResolver;
  48. ListDictionary schemas;
  49. XmlSchemaObjectTable attributes;
  50. XmlSchemaObjectTable elements;
  51. XmlSchemaObjectTable types;
  52. XmlSchemaCollection col;
  53. ValidationEventHandler handler;
  54. bool isCompiled;
  55. internal Guid CompilationId;
  56. public XmlSchemaSet ()
  57. : this (new NameTable ())
  58. {
  59. handler = new ValidationEventHandler (this.OnValidationError);
  60. }
  61. public XmlSchemaSet (XmlNameTable nameTable)
  62. {
  63. if (nameTable == null)
  64. throw new ArgumentNullException ("nameTable");
  65. this.nameTable = nameTable;
  66. schemas = new ListDictionary ();
  67. CompilationId = Guid.NewGuid ();
  68. }
  69. public event ValidationEventHandler ValidationEventHandler;
  70. public int Count {
  71. get { return schemas.Count; }
  72. }
  73. public XmlSchemaObjectTable GlobalAttributes {
  74. get {
  75. if (attributes == null) {
  76. lock (this) {
  77. attributes = new XmlSchemaObjectTable ();
  78. foreach (XmlSchema s in schemas.Values)
  79. foreach (XmlSchemaAttribute a in s.Attributes.Values)
  80. attributes.Add (a.QualifiedName, a);
  81. }
  82. }
  83. return attributes;
  84. }
  85. }
  86. public XmlSchemaObjectTable GlobalElements {
  87. get {
  88. if (elements == null) {
  89. lock (this) {
  90. elements = new XmlSchemaObjectTable ();
  91. foreach (XmlSchema s in schemas.Values)
  92. foreach (XmlSchemaElement e in s.Elements.Values)
  93. elements.Add (e.QualifiedName, e);
  94. }
  95. }
  96. return elements;
  97. }
  98. }
  99. public XmlSchemaObjectTable GlobalTypes {
  100. get {
  101. if (types == null) {
  102. lock (this) {
  103. types = new XmlSchemaObjectTable ();
  104. foreach (XmlSchema s in schemas.Values)
  105. foreach (XmlSchemaType t in s.SchemaTypes.Values)
  106. types.Add (t.QualifiedName, t);
  107. }
  108. }
  109. return types;
  110. }
  111. }
  112. public bool IsCompiled {
  113. get { return isCompiled; }
  114. }
  115. public XmlNameTable NameTable {
  116. get { return nameTable; }
  117. }
  118. // This is mainly used for event delegating
  119. internal XmlSchemaCollection SchemaCollection {
  120. get { return col; }
  121. set { col = value; }
  122. }
  123. public XmlResolver XmlResolver {
  124. set { xmlResolver = value; }
  125. }
  126. public XmlSchema Add (string targetNamespace, string url)
  127. {
  128. XmlTextReader r = null;
  129. try {
  130. r = new XmlTextReader (url);
  131. return Add (targetNamespace, r);
  132. } finally {
  133. if (r != null)
  134. r.Close ();
  135. }
  136. }
  137. // LAMESPEC? MS.NET allows multiple chameleon schema addition,
  138. // while rejects namespace-targeted schema.
  139. public XmlSchema Add (string targetNamespace, XmlReader reader)
  140. {
  141. XmlSchema schema = XmlSchema.Read (reader, handler);
  142. if (targetNamespace != null)
  143. schema.TargetNamespace = targetNamespace;
  144. XmlSchema existing = schemas [GetSafeNs (schema.TargetNamespace)] as XmlSchema;
  145. if (existing != null)
  146. throw new ArgumentException (String.Format ("Item has been already added. Target namespace is \"{0}\"", schema.TargetNamespace));
  147. return Add (schema);
  148. }
  149. [MonoTODO ("Check the exact behavior when namespaces are in conflict")]
  150. public void Add (XmlSchemaSet schemaSet)
  151. {
  152. foreach (XmlSchema schema in schemaSet.schemas)
  153. Add (schema);
  154. }
  155. [MonoTODO ("Currently no way to identify if the argument schema is error-prone or not.")]
  156. public XmlSchema Add (XmlSchema schema)
  157. {
  158. schemas [GetSafeNs (schema.TargetNamespace)] = schema;
  159. return schema;
  160. }
  161. [MonoTODO]
  162. public void Compile ()
  163. {
  164. foreach (XmlSchema schema in schemas.Values)
  165. schema.Compile (handler, col, xmlResolver);
  166. isCompiled = true;
  167. attributes = elements = types = null;
  168. }
  169. public bool Contains (string targetNamespace)
  170. {
  171. return schemas.Contains (targetNamespace);
  172. }
  173. public bool Contains (XmlSchema targetNamespace)
  174. {
  175. return schemas.Contains (targetNamespace);
  176. }
  177. public void CopyTo (XmlSchema[] array, int index)
  178. {
  179. schemas.CopyTo (array, index);
  180. }
  181. internal void CopyTo (Array array, int index)
  182. {
  183. schemas.CopyTo (array, index);
  184. }
  185. internal XmlSchema Get (string ns)
  186. {
  187. return (XmlSchema) schemas [GetSafeNs (ns)];
  188. }
  189. internal IEnumerator GetEnumerator ()
  190. {
  191. return schemas.GetEnumerator ();
  192. }
  193. string GetSafeNs (string ns)
  194. {
  195. return ns == null ? "" : ns;
  196. }
  197. internal void OnValidationError (object o, ValidationEventArgs e)
  198. {
  199. if (col != null)
  200. col.OnValidationError (o, e);
  201. if (ValidationEventHandler != null)
  202. ValidationEventHandler (o, e);
  203. else if (e.Severity == XmlSeverityType.Error)
  204. throw e.Exception;
  205. }
  206. [MonoTODO ("Check exact behavior")]
  207. public XmlSchema Remove (XmlSchema schema)
  208. {
  209. schemas.Remove (schema);
  210. return schema;
  211. }
  212. [MonoTODO]
  213. public bool RemoveRecursive (XmlSchema schema)
  214. {
  215. throw new NotImplementedException ();
  216. }
  217. [MonoTODO]
  218. public XmlSchema Reprocess (XmlSchema schema)
  219. {
  220. throw new NotImplementedException ();
  221. }
  222. public ICollection Schemas ()
  223. {
  224. return new ArrayList (schemas.Values);
  225. }
  226. [MonoTODO]
  227. public ICollection Schemas (string targetNamespace)
  228. {
  229. throw new NotImplementedException ();
  230. }
  231. }
  232. }