2
0

XmlSchemaSet.cs 7.1 KB

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