XmlSchemaSet.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. }
  71. public event ValidationEventHandler ValidationEventHandler;
  72. public int Count {
  73. get { return schemas.Count; }
  74. }
  75. public XmlSchemaObjectTable GlobalAttributes {
  76. get {
  77. if (attributes == null)
  78. attributes = new XmlSchemaObjectTable ();
  79. return attributes;
  80. }
  81. }
  82. public XmlSchemaObjectTable GlobalElements {
  83. get {
  84. if (elements == null)
  85. elements = new XmlSchemaObjectTable ();
  86. return elements;
  87. }
  88. }
  89. public XmlSchemaObjectTable GlobalTypes {
  90. get {
  91. if (types == null)
  92. types = new XmlSchemaObjectTable ();
  93. return types;
  94. }
  95. }
  96. public bool IsCompiled {
  97. get { return isCompiled; }
  98. }
  99. public XmlNameTable NameTable {
  100. get { return nameTable; }
  101. }
  102. // This is mainly used for event delegating
  103. internal XmlSchemaCollection SchemaCollection {
  104. get {
  105. if (col == null) {
  106. lock (this) {
  107. col = new XmlSchemaCollection ();
  108. foreach (XmlSchema s in schemas)
  109. col.Add (s);
  110. }
  111. }
  112. return col;
  113. }
  114. }
  115. public XmlResolver XmlResolver {
  116. set { xmlResolver = value; }
  117. }
  118. public XmlSchema Add (string targetNamespace, string url)
  119. {
  120. targetNamespace = GetSafeNs (targetNamespace);
  121. XmlTextReader r = null;
  122. try {
  123. r = new XmlTextReader (url, nameTable);
  124. return Add (targetNamespace, r);
  125. } finally {
  126. if (r != null)
  127. r.Close ();
  128. }
  129. }
  130. [MonoTODO ("It has weird namespace duplication check that is different from Add(XmlSchema).")]
  131. public XmlSchema Add (string targetNamespace, XmlReader reader)
  132. {
  133. XmlSchema schema = XmlSchema.Read (reader, handler);
  134. if (targetNamespace != null)
  135. schema.TargetNamespace = targetNamespace;
  136. Add (schema);
  137. return schema;
  138. }
  139. [MonoTODO ("Check the exact behavior when namespaces are in conflict (but it would be preferable to wait for 2.0 RTM).")]
  140. public void Add (XmlSchemaSet schemaSet)
  141. {
  142. ArrayList al = new ArrayList ();
  143. foreach (XmlSchema schema in schemaSet.schemas) {
  144. if (!schemas.Contains (schema))
  145. al.Add (schema);
  146. else
  147. AddGlobalComponents (schema);
  148. }
  149. foreach (XmlSchema schema in al)
  150. Add (schema);
  151. }
  152. [MonoTODO ("We need to research more about the expected behavior")]
  153. public XmlSchema Add (XmlSchema schema)
  154. {
  155. schemas.Add (schema);
  156. AddGlobalComponents (schema);
  157. return schema;
  158. }
  159. [MonoTODO ("It should be the actual compilation engine.")]
  160. public void Compile ()
  161. {
  162. foreach (XmlSchema schema in schemas) {
  163. if (!schema.IsCompiled) {
  164. schema.Compile (handler, this, xmlResolver);
  165. AddGlobalComponents (schema);
  166. }
  167. }
  168. isCompiled = true;
  169. }
  170. private void AddGlobalComponents (XmlSchema schema)
  171. {
  172. foreach (XmlSchemaElement el in schema.Elements.Values)
  173. GlobalElements.Add (el.QualifiedName, el);
  174. foreach (XmlSchemaAttribute a in schema.Attributes.Values)
  175. GlobalAttributes.Add (a.QualifiedName, a);
  176. foreach (XmlSchemaType t in schema.SchemaTypes.Values)
  177. GlobalTypes.Add (t.QualifiedName, t);
  178. }
  179. public bool Contains (string targetNamespace)
  180. {
  181. targetNamespace = GetSafeNs (targetNamespace);
  182. foreach (XmlSchema schema in schemas)
  183. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  184. return true;
  185. return false;
  186. }
  187. public bool Contains (XmlSchema targetNamespace)
  188. {
  189. foreach (XmlSchema schema in schemas)
  190. if (schema == targetNamespace)
  191. return true;
  192. return false;
  193. }
  194. public void CopyTo (XmlSchema [] array, int index)
  195. {
  196. schemas.CopyTo (array, index);
  197. }
  198. internal void CopyTo (Array array, int index)
  199. {
  200. schemas.CopyTo (array, index);
  201. }
  202. string GetSafeNs (string ns)
  203. {
  204. return ns == null ? "" : ns;
  205. }
  206. internal void OnValidationError (object o, ValidationEventArgs e)
  207. {
  208. if (col != null)
  209. col.OnValidationError (o, e);
  210. if (ValidationEventHandler != null)
  211. ValidationEventHandler (o, e);
  212. else if (e.Severity == XmlSeverityType.Error)
  213. throw e.Exception;
  214. }
  215. [MonoTODO ("Check exact behavior")]
  216. public XmlSchema Remove (XmlSchema schema)
  217. {
  218. schemas.Remove (schema);
  219. return schema;
  220. }
  221. [MonoTODO]
  222. public bool RemoveRecursive (XmlSchema schema)
  223. {
  224. throw new NotImplementedException ();
  225. }
  226. [MonoTODO]
  227. public XmlSchema Reprocess (XmlSchema schema)
  228. {
  229. throw new NotImplementedException ();
  230. }
  231. public ICollection Schemas ()
  232. {
  233. return schemas;
  234. }
  235. [MonoTODO]
  236. public ICollection Schemas (string targetNamespace)
  237. {
  238. targetNamespace = GetSafeNs (targetNamespace);
  239. ArrayList al = new ArrayList ();
  240. foreach (XmlSchema schema in schemas)
  241. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  242. al.Add (schema);
  243. return al;
  244. }
  245. internal bool MissedSubComponents (string targetNamespace)
  246. {
  247. foreach (XmlSchema s in Schemas (targetNamespace))
  248. if (s.missedSubComponents)
  249. return true;
  250. return false;
  251. }
  252. }
  253. }