XmlSchemaSet.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. ArrayList schemas;
  49. XmlSchemaObjectTable attributes;
  50. XmlSchemaObjectTable elements;
  51. XmlSchemaObjectTable types;
  52. XmlSchemaObjectTable attributeGroups;
  53. XmlSchemaObjectTable groups;
  54. XmlSchemaCollection col;
  55. ValidationEventHandler handler;
  56. bool isCompiled;
  57. internal Guid CompilationId;
  58. public XmlSchemaSet ()
  59. : this (new NameTable ())
  60. {
  61. handler = new ValidationEventHandler (this.OnValidationError);
  62. }
  63. public XmlSchemaSet (XmlNameTable nameTable)
  64. {
  65. if (nameTable == null)
  66. throw new ArgumentNullException ("nameTable");
  67. this.nameTable = nameTable;
  68. schemas = new ArrayList ();
  69. CompilationId = Guid.NewGuid ();
  70. elements = new XmlSchemaObjectTable ();
  71. attributes = new XmlSchemaObjectTable ();
  72. types = new XmlSchemaObjectTable ();
  73. }
  74. public event ValidationEventHandler ValidationEventHandler;
  75. public int Count {
  76. get { return schemas.Count; }
  77. }
  78. public XmlSchemaObjectTable GlobalAttributes {
  79. get { return attributes; }
  80. }
  81. public XmlSchemaObjectTable GlobalElements {
  82. get { return elements; }
  83. }
  84. public XmlSchemaObjectTable GlobalTypes {
  85. get { return types; }
  86. }
  87. public bool IsCompiled {
  88. get { return isCompiled; }
  89. }
  90. public XmlNameTable NameTable {
  91. get { return nameTable; }
  92. }
  93. // This is mainly used for event delegating
  94. internal XmlSchemaCollection SchemaCollection {
  95. get {
  96. if (col == null) {
  97. lock (this) {
  98. col = new XmlSchemaCollection ();
  99. foreach (XmlSchema s in schemas)
  100. col.Add (s);
  101. }
  102. }
  103. return col;
  104. }
  105. }
  106. public XmlResolver XmlResolver {
  107. set { xmlResolver = value; }
  108. }
  109. public XmlSchema Add (string targetNamespace, string url)
  110. {
  111. targetNamespace = GetSafeNs (targetNamespace);
  112. XmlTextReader r = null;
  113. try {
  114. r = new XmlTextReader (url);
  115. return Add (targetNamespace, r);
  116. } finally {
  117. if (r != null)
  118. r.Close ();
  119. }
  120. }
  121. [MonoTODO ("It has weird namespace duplication check that is different from Add(XmlSchema).")]
  122. public XmlSchema Add (string targetNamespace, XmlReader reader)
  123. {
  124. XmlSchema schema = XmlSchema.Read (reader, handler);
  125. if (targetNamespace != null)
  126. schema.TargetNamespace = targetNamespace;
  127. Add (schema);
  128. return schema;
  129. }
  130. [MonoTODO ("Check the exact behavior when namespaces are in conflict (but it would be preferable to wait for 2.0 RTM).")]
  131. public void Add (XmlSchemaSet schemaSet)
  132. {
  133. ArrayList al = new ArrayList ();
  134. foreach (XmlSchema schema in schemaSet.schemas) {
  135. if (!schemas.Contains (schema))
  136. al.Add (schema);
  137. else
  138. AddGlobalComponents (schema);
  139. }
  140. foreach (XmlSchema schema in al)
  141. Add (schema);
  142. }
  143. [MonoTODO ("We need to research more about the expected behavior")]
  144. public XmlSchema Add (XmlSchema schema)
  145. {
  146. schemas.Add (schema);
  147. AddGlobalComponents (schema);
  148. return schema;
  149. }
  150. [MonoTODO ("It should be the actual compilation engine.")]
  151. public void Compile ()
  152. {
  153. foreach (XmlSchema schema in schemas) {
  154. if (!schema.IsCompiled) {
  155. schema.Compile (handler, this, xmlResolver);
  156. AddGlobalComponents (schema);
  157. }
  158. }
  159. isCompiled = true;
  160. }
  161. private void AddGlobalComponents (XmlSchema schema)
  162. {
  163. foreach (XmlSchemaElement el in schema.Elements.Values)
  164. elements.Add (el.QualifiedName, el);
  165. foreach (XmlSchemaAttribute a in schema.Attributes.Values)
  166. attributes.Add (a.QualifiedName, a);
  167. foreach (XmlSchemaType t in schema.SchemaTypes.Values)
  168. types.Add (t.QualifiedName, t);
  169. }
  170. public bool Contains (string targetNamespace)
  171. {
  172. targetNamespace = GetSafeNs (targetNamespace);
  173. foreach (XmlSchema schema in schemas)
  174. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  175. return true;
  176. return false;
  177. }
  178. public bool Contains (XmlSchema targetNamespace)
  179. {
  180. foreach (XmlSchema schema in schemas)
  181. if (schema == targetNamespace)
  182. return true;
  183. return false;
  184. }
  185. public void CopyTo (XmlSchema [] array, int index)
  186. {
  187. schemas.CopyTo (array, index);
  188. }
  189. internal void CopyTo (Array array, int index)
  190. {
  191. schemas.CopyTo (array, index);
  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 schemas;
  225. }
  226. [MonoTODO]
  227. public ICollection Schemas (string targetNamespace)
  228. {
  229. targetNamespace = GetSafeNs (targetNamespace);
  230. ArrayList al = new ArrayList ();
  231. foreach (XmlSchema schema in schemas)
  232. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  233. al.Add (schema);
  234. return al;
  235. }
  236. internal bool MissedSubComponents (string targetNamespace)
  237. {
  238. foreach (XmlSchema s in Schemas (targetNamespace))
  239. if (s.missedSubComponents)
  240. return true;
  241. return false;
  242. }
  243. }
  244. }