XmlSchemaSet.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 = new XmlUrlResolver ();
  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. [CLSCompliant(false)]
  116. public XmlResolver XmlResolver {
  117. set { xmlResolver = value; }
  118. #if NET_2_0
  119. internal get { return xmlResolver; }
  120. #endif
  121. }
  122. public XmlSchema Add (string targetNamespace, string url)
  123. {
  124. targetNamespace = GetSafeNs (targetNamespace);
  125. XmlTextReader r = null;
  126. try {
  127. r = new XmlTextReader (url, nameTable);
  128. return Add (targetNamespace, r);
  129. } finally {
  130. if (r != null)
  131. r.Close ();
  132. }
  133. }
  134. [MonoTODO ("It has weird namespace duplication check that is different from Add(XmlSchema).")]
  135. public XmlSchema Add (string targetNamespace, XmlReader reader)
  136. {
  137. XmlSchema schema = XmlSchema.Read (reader, handler);
  138. if (targetNamespace != null
  139. && targetNamespace.Length > 0)
  140. schema.TargetNamespace = targetNamespace;
  141. Add (schema);
  142. return schema;
  143. }
  144. [MonoTODO ("Check the exact behavior when namespaces are in conflict (but it would be preferable to wait for 2.0 RTM).")]
  145. public void Add (XmlSchemaSet schemaSet)
  146. {
  147. ArrayList al = new ArrayList ();
  148. foreach (XmlSchema schema in schemaSet.schemas) {
  149. if (!schemas.Contains (schema))
  150. al.Add (schema);
  151. else
  152. AddGlobalComponents (schema);
  153. }
  154. foreach (XmlSchema schema in al)
  155. Add (schema);
  156. }
  157. [MonoTODO ("We need to research more about the expected behavior")]
  158. public XmlSchema Add (XmlSchema schema)
  159. {
  160. schemas.Add (schema);
  161. AddGlobalComponents (schema);
  162. return schema;
  163. }
  164. [MonoTODO ("It should be the actual compilation engine.")]
  165. public void Compile ()
  166. {
  167. ClearGlobalComponents ();
  168. ArrayList al = new ArrayList ();
  169. al.AddRange (schemas);
  170. foreach (XmlSchema schema in al) {
  171. if (!schema.IsCompiled)
  172. schema.Compile (handler, this, xmlResolver);
  173. AddGlobalComponents (schema);
  174. }
  175. isCompiled = true;
  176. }
  177. private void ClearGlobalComponents ()
  178. {
  179. GlobalElements.Clear ();
  180. GlobalAttributes.Clear ();
  181. GlobalTypes.Clear ();
  182. // GlobalAttributeGroups.Clear ();
  183. // GlobalGroups.Clear ();
  184. }
  185. private void AddGlobalComponents (XmlSchema schema)
  186. {
  187. foreach (XmlSchemaElement el in schema.Elements.Values)
  188. GlobalElements.Add (el.QualifiedName, el);
  189. foreach (XmlSchemaAttribute a in schema.Attributes.Values)
  190. GlobalAttributes.Add (a.QualifiedName, a);
  191. foreach (XmlSchemaType t in schema.SchemaTypes.Values)
  192. GlobalTypes.Add (t.QualifiedName, t);
  193. }
  194. public bool Contains (string targetNamespace)
  195. {
  196. targetNamespace = GetSafeNs (targetNamespace);
  197. foreach (XmlSchema schema in schemas)
  198. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  199. return true;
  200. return false;
  201. }
  202. public bool Contains (XmlSchema targetNamespace)
  203. {
  204. foreach (XmlSchema schema in schemas)
  205. if (schema == targetNamespace)
  206. return true;
  207. return false;
  208. }
  209. public void CopyTo (XmlSchema [] array, int index)
  210. {
  211. schemas.CopyTo (array, index);
  212. }
  213. internal void CopyTo (Array array, int index)
  214. {
  215. schemas.CopyTo (array, index);
  216. }
  217. string GetSafeNs (string ns)
  218. {
  219. return ns == null ? "" : ns;
  220. }
  221. internal void OnValidationError (object o, ValidationEventArgs e)
  222. {
  223. if (col != null)
  224. col.OnValidationError (o, e);
  225. if (ValidationEventHandler != null)
  226. ValidationEventHandler (o, e);
  227. else if (e.Severity == XmlSeverityType.Error)
  228. throw e.Exception;
  229. }
  230. [MonoTODO ("Check exact behavior")]
  231. public XmlSchema Remove (XmlSchema schema)
  232. {
  233. if (schema == null)
  234. throw new ArgumentNullException ("schema");
  235. ArrayList al = new ArrayList ();
  236. al.AddRange (schemas);
  237. if (!al.Contains (schema))
  238. return null;
  239. // FIXME: I have no idea why Remove() might throw
  240. // XmlSchemaException, except for the case it compiles.
  241. if (!schema.IsCompiled)
  242. schema.Compile (handler, this, xmlResolver);
  243. schemas.Remove (schema);
  244. isCompiled = false;
  245. ClearGlobalComponents ();
  246. return schema;
  247. }
  248. [MonoTODO ("Check exact behavior")]
  249. public bool RemoveRecursive (XmlSchema schema)
  250. {
  251. if (schema == null)
  252. throw new ArgumentNullException ("schema");
  253. ArrayList al = new ArrayList ();
  254. al.AddRange (schemas);
  255. if (!al.Contains (schema))
  256. return false;
  257. al.Remove (schema);
  258. schemas.Remove (schema);
  259. ClearGlobalComponents ();
  260. foreach (XmlSchema s in al) {
  261. if (s.IsCompiled)
  262. AddGlobalComponents (schema);
  263. }
  264. return true;
  265. }
  266. public XmlSchema Reprocess (XmlSchema schema)
  267. {
  268. if (schema == null)
  269. throw new ArgumentNullException ("schema");
  270. ArrayList al = new ArrayList ();
  271. al.AddRange (schemas);
  272. if (!al.Contains (schema))
  273. throw new ArgumentException ("Target schema is not contained in the schema set.");
  274. ClearGlobalComponents ();
  275. foreach (XmlSchema s in al) {
  276. if (schema == s)
  277. schema.Compile (handler, this, xmlResolver);
  278. if (s.IsCompiled)
  279. AddGlobalComponents (schema);
  280. }
  281. return schema.IsCompiled ? schema : null;
  282. }
  283. public ICollection Schemas ()
  284. {
  285. return schemas;
  286. }
  287. [MonoTODO]
  288. public ICollection Schemas (string targetNamespace)
  289. {
  290. targetNamespace = GetSafeNs (targetNamespace);
  291. ArrayList al = new ArrayList ();
  292. foreach (XmlSchema schema in schemas)
  293. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  294. al.Add (schema);
  295. return al;
  296. }
  297. internal bool MissedSubComponents (string targetNamespace)
  298. {
  299. foreach (XmlSchema s in Schemas (targetNamespace))
  300. if (s.missedSubComponents)
  301. return true;
  302. return false;
  303. }
  304. }
  305. }