XmlSchemaSet.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. Hashtable idCollection;
  55. XmlSchemaObjectTable namedIdentities;
  56. XmlSchemaCompilationSettings settings =
  57. new XmlSchemaCompilationSettings ();
  58. bool isCompiled;
  59. internal Guid CompilationId;
  60. public XmlSchemaSet ()
  61. : this (new NameTable ())
  62. {
  63. }
  64. public XmlSchemaSet (XmlNameTable nameTable)
  65. {
  66. if (nameTable == null)
  67. throw new ArgumentNullException ("nameTable");
  68. this.nameTable = nameTable;
  69. schemas = new ArrayList ();
  70. CompilationId = Guid.NewGuid ();
  71. }
  72. public event ValidationEventHandler ValidationEventHandler;
  73. public int Count {
  74. get { return schemas.Count; }
  75. }
  76. public XmlSchemaObjectTable GlobalAttributes {
  77. get {
  78. if (attributes == null)
  79. attributes = new XmlSchemaObjectTable ();
  80. return attributes;
  81. }
  82. }
  83. public XmlSchemaObjectTable GlobalElements {
  84. get {
  85. if (elements == null)
  86. elements = new XmlSchemaObjectTable ();
  87. return elements;
  88. }
  89. }
  90. public XmlSchemaObjectTable GlobalTypes {
  91. get {
  92. if (types == null)
  93. types = new XmlSchemaObjectTable ();
  94. return types;
  95. }
  96. }
  97. public bool IsCompiled {
  98. get { return isCompiled; }
  99. }
  100. public XmlNameTable NameTable {
  101. get { return nameTable; }
  102. }
  103. public XmlSchemaCompilationSettings CompilationSettings {
  104. get { return settings; }
  105. set { settings = value; }
  106. }
  107. public XmlResolver XmlResolver {
  108. set { xmlResolver = value; }
  109. #if NET_2_0
  110. internal get { return xmlResolver; }
  111. #else
  112. get { return xmlResolver; }
  113. #endif
  114. }
  115. internal Hashtable IDCollection {
  116. get {
  117. if (idCollection == null)
  118. idCollection = new Hashtable ();
  119. return idCollection;
  120. }
  121. }
  122. internal XmlSchemaObjectTable NamedIdentities {
  123. get {
  124. if (namedIdentities == null)
  125. namedIdentities = new XmlSchemaObjectTable();
  126. return namedIdentities;
  127. }
  128. }
  129. public XmlSchema Add (string targetNamespace, string url)
  130. {
  131. XmlTextReader r = null;
  132. try {
  133. r = new XmlTextReader (url, nameTable);
  134. return Add (targetNamespace, r);
  135. } finally {
  136. if (r != null)
  137. r.Close ();
  138. }
  139. }
  140. public XmlSchema Add (string targetNamespace, XmlReader reader)
  141. {
  142. XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
  143. if (schema.TargetNamespace == null)
  144. schema.TargetNamespace = targetNamespace;
  145. else if (targetNamespace != null && schema.TargetNamespace != targetNamespace)
  146. throw new XmlSchemaException ("The actual targetNamespace in the schema does not match the parameter.");
  147. Add (schema);
  148. return schema;
  149. }
  150. [MonoTODO]
  151. // FIXME: Check the exact behavior when namespaces are in conflict (but it would be preferable to wait for 2.0 RTM)
  152. public void Add (XmlSchemaSet schemaSet)
  153. {
  154. ArrayList al = new ArrayList ();
  155. foreach (XmlSchema schema in schemaSet.schemas) {
  156. if (!schemas.Contains (schema))
  157. al.Add (schema);
  158. }
  159. foreach (XmlSchema schema in al)
  160. Add (schema);
  161. }
  162. public XmlSchema Add (XmlSchema schema)
  163. {
  164. schemas.Add (schema);
  165. ResetCompile ();
  166. return schema;
  167. }
  168. // FIXME: It should be the actual compilation engine.
  169. public void Compile ()
  170. {
  171. ClearGlobalComponents ();
  172. ArrayList al = new ArrayList ();
  173. al.AddRange (schemas);
  174. IDCollection.Clear ();
  175. NamedIdentities.Clear ();
  176. foreach (XmlSchema schema in al)
  177. if (!schema.IsCompiled)
  178. schema.CompileSubset (ValidationEventHandler, this, xmlResolver);
  179. foreach (XmlSchema schema in al)
  180. schema.Validate (ValidationEventHandler);
  181. foreach (XmlSchema schema in al)
  182. AddGlobalComponents (schema);
  183. isCompiled = true;
  184. }
  185. private void ClearGlobalComponents ()
  186. {
  187. GlobalElements.Clear ();
  188. GlobalAttributes.Clear ();
  189. GlobalTypes.Clear ();
  190. // GlobalAttributeGroups.Clear ();
  191. // GlobalGroups.Clear ();
  192. }
  193. private void AddGlobalComponents (XmlSchema schema)
  194. {
  195. foreach (XmlSchemaElement el in schema.Elements.Values)
  196. GlobalElements.Add (el.QualifiedName, el);
  197. foreach (XmlSchemaAttribute a in schema.Attributes.Values)
  198. GlobalAttributes.Add (a.QualifiedName, a);
  199. foreach (XmlSchemaType t in schema.SchemaTypes.Values)
  200. GlobalTypes.Add (t.QualifiedName, t);
  201. }
  202. public bool Contains (string targetNamespace)
  203. {
  204. targetNamespace = GetSafeNs (targetNamespace);
  205. foreach (XmlSchema schema in schemas)
  206. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  207. return true;
  208. return false;
  209. }
  210. public bool Contains (XmlSchema targetNamespace)
  211. {
  212. foreach (XmlSchema schema in schemas)
  213. if (schema == targetNamespace)
  214. return true;
  215. return false;
  216. }
  217. public void CopyTo (XmlSchema [] array, int index)
  218. {
  219. schemas.CopyTo (array, index);
  220. }
  221. internal void CopyTo (Array array, int index)
  222. {
  223. schemas.CopyTo (array, index);
  224. }
  225. string GetSafeNs (string ns)
  226. {
  227. return ns == null ? "" : ns;
  228. }
  229. [MonoTODO]
  230. // FIXME: 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.CompileSubset (ValidationEventHandler, this, xmlResolver);
  243. schemas.Remove (schema);
  244. ResetCompile ();
  245. return schema;
  246. }
  247. void ResetCompile ()
  248. {
  249. isCompiled = false;
  250. ClearGlobalComponents ();
  251. }
  252. public bool RemoveRecursive (XmlSchema schema)
  253. {
  254. if (schema == null)
  255. throw new ArgumentNullException ("schema");
  256. ArrayList al = new ArrayList ();
  257. al.AddRange (schemas);
  258. if (!al.Contains (schema))
  259. return false;
  260. al.Remove (schema);
  261. schemas.Remove (schema);
  262. if (!IsCompiled)
  263. return true;
  264. ClearGlobalComponents ();
  265. foreach (XmlSchema s in al) {
  266. if (s.IsCompiled)
  267. AddGlobalComponents (schema);
  268. }
  269. return true;
  270. }
  271. public XmlSchema Reprocess (XmlSchema schema)
  272. {
  273. if (schema == null)
  274. throw new ArgumentNullException ("schema");
  275. ArrayList al = new ArrayList ();
  276. al.AddRange (schemas);
  277. if (!al.Contains (schema))
  278. throw new ArgumentException ("Target schema is not contained in the schema set.");
  279. ClearGlobalComponents ();
  280. foreach (XmlSchema s in al) {
  281. if (schema == s)
  282. schema.CompileSubset (ValidationEventHandler, this, xmlResolver);
  283. if (s.IsCompiled)
  284. AddGlobalComponents (schema);
  285. }
  286. return schema.IsCompiled ? schema : null;
  287. }
  288. public ICollection Schemas ()
  289. {
  290. return schemas;
  291. }
  292. public ICollection Schemas (string targetNamespace)
  293. {
  294. targetNamespace = GetSafeNs (targetNamespace);
  295. ArrayList al = new ArrayList ();
  296. foreach (XmlSchema schema in schemas)
  297. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  298. al.Add (schema);
  299. return al;
  300. }
  301. internal bool MissedSubComponents (string targetNamespace)
  302. {
  303. foreach (XmlSchema s in Schemas (targetNamespace))
  304. if (s.missedSubComponents)
  305. return true;
  306. return false;
  307. }
  308. }
  309. }