XmlSchemaSet.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 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. 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. ClearGlobalComponents ();
  167. ArrayList al = new ArrayList ();
  168. al.AddRange (schemas);
  169. foreach (XmlSchema schema in al) {
  170. if (!schema.IsCompiled)
  171. schema.Compile (handler, this, xmlResolver);
  172. AddGlobalComponents (schema);
  173. }
  174. isCompiled = true;
  175. }
  176. private void ClearGlobalComponents ()
  177. {
  178. GlobalElements.Clear ();
  179. GlobalAttributes.Clear ();
  180. GlobalTypes.Clear ();
  181. // GlobalAttributeGroups.Clear ();
  182. // GlobalGroups.Clear ();
  183. }
  184. private void AddGlobalComponents (XmlSchema schema)
  185. {
  186. foreach (XmlSchemaElement el in schema.Elements.Values)
  187. GlobalElements.Add (el.QualifiedName, el);
  188. foreach (XmlSchemaAttribute a in schema.Attributes.Values)
  189. GlobalAttributes.Add (a.QualifiedName, a);
  190. foreach (XmlSchemaType t in schema.SchemaTypes.Values)
  191. GlobalTypes.Add (t.QualifiedName, t);
  192. }
  193. public bool Contains (string targetNamespace)
  194. {
  195. targetNamespace = GetSafeNs (targetNamespace);
  196. foreach (XmlSchema schema in schemas)
  197. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  198. return true;
  199. return false;
  200. }
  201. public bool Contains (XmlSchema targetNamespace)
  202. {
  203. foreach (XmlSchema schema in schemas)
  204. if (schema == targetNamespace)
  205. return true;
  206. return false;
  207. }
  208. public void CopyTo (XmlSchema [] array, int index)
  209. {
  210. schemas.CopyTo (array, index);
  211. }
  212. internal void CopyTo (Array array, int index)
  213. {
  214. schemas.CopyTo (array, index);
  215. }
  216. string GetSafeNs (string ns)
  217. {
  218. return ns == null ? "" : ns;
  219. }
  220. internal void OnValidationError (object o, ValidationEventArgs e)
  221. {
  222. if (col != null)
  223. col.OnValidationError (o, e);
  224. if (ValidationEventHandler != null)
  225. ValidationEventHandler (o, e);
  226. else if (e.Severity == XmlSeverityType.Error)
  227. throw e.Exception;
  228. }
  229. [MonoTODO ("Check exact behavior")]
  230. public XmlSchema Remove (XmlSchema schema)
  231. {
  232. if (schema == null)
  233. throw new ArgumentNullException ("schema");
  234. ArrayList al = new ArrayList ();
  235. al.AddRange (schemas);
  236. if (!al.Contains (schema))
  237. return null;
  238. // FIXME: I have no idea why Remove() might throw
  239. // XmlSchemaException, except for the case it compiles.
  240. if (!schema.IsCompiled)
  241. schema.Compile (handler, this, xmlResolver);
  242. schemas.Remove (schema);
  243. isCompiled = false;
  244. ClearGlobalComponents ();
  245. return schema;
  246. }
  247. [MonoTODO ("Check exact behavior")]
  248. public bool RemoveRecursive (XmlSchema schema)
  249. {
  250. if (schema == null)
  251. throw new ArgumentNullException ("schema");
  252. ArrayList al = new ArrayList ();
  253. al.AddRange (schemas);
  254. if (!al.Contains (schema))
  255. return false;
  256. al.Remove (schema);
  257. schemas.Remove (schema);
  258. ClearGlobalComponents ();
  259. foreach (XmlSchema s in al) {
  260. if (s.IsCompiled)
  261. AddGlobalComponents (schema);
  262. }
  263. return true;
  264. }
  265. public XmlSchema Reprocess (XmlSchema schema)
  266. {
  267. if (schema == null)
  268. throw new ArgumentNullException ("schema");
  269. ArrayList al = new ArrayList ();
  270. al.AddRange (schemas);
  271. if (!al.Contains (schema))
  272. throw new ArgumentException ("Target schema is not contained in the schema set.");
  273. ClearGlobalComponents ();
  274. foreach (XmlSchema s in al) {
  275. if (schema == s)
  276. schema.Compile (handler, this, xmlResolver);
  277. if (s.IsCompiled)
  278. AddGlobalComponents (schema);
  279. }
  280. return schema.IsCompiled ? schema : null;
  281. }
  282. public ICollection Schemas ()
  283. {
  284. return schemas;
  285. }
  286. [MonoTODO]
  287. public ICollection Schemas (string targetNamespace)
  288. {
  289. targetNamespace = GetSafeNs (targetNamespace);
  290. ArrayList al = new ArrayList ();
  291. foreach (XmlSchema schema in schemas)
  292. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  293. al.Add (schema);
  294. return al;
  295. }
  296. internal bool MissedSubComponents (string targetNamespace)
  297. {
  298. foreach (XmlSchema s in Schemas (targetNamespace))
  299. if (s.missedSubComponents)
  300. return true;
  301. return false;
  302. }
  303. }
  304. }