XmlSchemaSet.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. && targetNamespace.Length > 0)
  136. schema.TargetNamespace = targetNamespace;
  137. Add (schema);
  138. return schema;
  139. }
  140. [MonoTODO ("Check the exact behavior when namespaces are in conflict (but it would be preferable to wait for 2.0 RTM).")]
  141. public void Add (XmlSchemaSet schemaSet)
  142. {
  143. ArrayList al = new ArrayList ();
  144. foreach (XmlSchema schema in schemaSet.schemas) {
  145. if (!schemas.Contains (schema))
  146. al.Add (schema);
  147. else
  148. AddGlobalComponents (schema);
  149. }
  150. foreach (XmlSchema schema in al)
  151. Add (schema);
  152. }
  153. [MonoTODO ("We need to research more about the expected behavior")]
  154. public XmlSchema Add (XmlSchema schema)
  155. {
  156. schemas.Add (schema);
  157. AddGlobalComponents (schema);
  158. return schema;
  159. }
  160. [MonoTODO ("It should be the actual compilation engine.")]
  161. public void Compile ()
  162. {
  163. foreach (XmlSchema schema in schemas) {
  164. if (!schema.IsCompiled) {
  165. schema.Compile (handler, this, xmlResolver);
  166. AddGlobalComponents (schema);
  167. }
  168. }
  169. isCompiled = true;
  170. }
  171. private void AddGlobalComponents (XmlSchema schema)
  172. {
  173. foreach (XmlSchemaElement el in schema.Elements.Values)
  174. GlobalElements.Add (el.QualifiedName, el);
  175. foreach (XmlSchemaAttribute a in schema.Attributes.Values)
  176. GlobalAttributes.Add (a.QualifiedName, a);
  177. foreach (XmlSchemaType t in schema.SchemaTypes.Values)
  178. GlobalTypes.Add (t.QualifiedName, t);
  179. }
  180. public bool Contains (string targetNamespace)
  181. {
  182. targetNamespace = GetSafeNs (targetNamespace);
  183. foreach (XmlSchema schema in schemas)
  184. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  185. return true;
  186. return false;
  187. }
  188. public bool Contains (XmlSchema targetNamespace)
  189. {
  190. foreach (XmlSchema schema in schemas)
  191. if (schema == targetNamespace)
  192. return true;
  193. return false;
  194. }
  195. public void CopyTo (XmlSchema [] array, int index)
  196. {
  197. schemas.CopyTo (array, index);
  198. }
  199. internal void CopyTo (Array array, int index)
  200. {
  201. schemas.CopyTo (array, index);
  202. }
  203. string GetSafeNs (string ns)
  204. {
  205. return ns == null ? "" : ns;
  206. }
  207. internal void OnValidationError (object o, ValidationEventArgs e)
  208. {
  209. if (col != null)
  210. col.OnValidationError (o, e);
  211. if (ValidationEventHandler != null)
  212. ValidationEventHandler (o, e);
  213. else if (e.Severity == XmlSeverityType.Error)
  214. throw e.Exception;
  215. }
  216. [MonoTODO ("Check exact behavior")]
  217. public XmlSchema Remove (XmlSchema schema)
  218. {
  219. schemas.Remove (schema);
  220. return schema;
  221. }
  222. [MonoTODO]
  223. public bool RemoveRecursive (XmlSchema schema)
  224. {
  225. throw new NotImplementedException ();
  226. }
  227. [MonoTODO]
  228. public XmlSchema Reprocess (XmlSchema schema)
  229. {
  230. throw new NotImplementedException ();
  231. }
  232. public ICollection Schemas ()
  233. {
  234. return schemas;
  235. }
  236. [MonoTODO]
  237. public ICollection Schemas (string targetNamespace)
  238. {
  239. targetNamespace = GetSafeNs (targetNamespace);
  240. ArrayList al = new ArrayList ();
  241. foreach (XmlSchema schema in schemas)
  242. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  243. al.Add (schema);
  244. return al;
  245. }
  246. internal bool MissedSubComponents (string targetNamespace)
  247. {
  248. foreach (XmlSchema s in Schemas (targetNamespace))
  249. if (s.missedSubComponents)
  250. return true;
  251. return false;
  252. }
  253. }
  254. }