XmlSchemaSet.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. Hashtable handledUris = new Hashtable ();
  177. foreach (XmlSchema schema in al)
  178. if (!schema.IsCompiled)
  179. schema.CompileSubset (ValidationEventHandler, this, xmlResolver, handledUris);
  180. // Process substitutionGroup first, as this process
  181. // involves both substituted and substituting elements
  182. // and hence it needs to be done before actual
  183. // validation (by current design of conformance checker).
  184. foreach (XmlSchema schema in al)
  185. foreach (XmlSchemaElement elem in schema.Elements.Values)
  186. elem.FillSubstitutionElementInfo ();
  187. foreach (XmlSchema schema in al)
  188. schema.Validate (ValidationEventHandler);
  189. foreach (XmlSchema schema in al)
  190. AddGlobalComponents (schema);
  191. isCompiled = true;
  192. }
  193. private void ClearGlobalComponents ()
  194. {
  195. GlobalElements.Clear ();
  196. GlobalAttributes.Clear ();
  197. GlobalTypes.Clear ();
  198. // GlobalAttributeGroups.Clear ();
  199. // GlobalGroups.Clear ();
  200. }
  201. private void AddGlobalComponents (XmlSchema schema)
  202. {
  203. foreach (XmlSchemaElement el in schema.Elements.Values)
  204. GlobalElements.Add (el.QualifiedName, el);
  205. foreach (XmlSchemaAttribute a in schema.Attributes.Values)
  206. GlobalAttributes.Add (a.QualifiedName, a);
  207. foreach (XmlSchemaType t in schema.SchemaTypes.Values)
  208. GlobalTypes.Add (t.QualifiedName, t);
  209. }
  210. public bool Contains (string targetNamespace)
  211. {
  212. targetNamespace = GetSafeNs (targetNamespace);
  213. foreach (XmlSchema schema in schemas)
  214. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  215. return true;
  216. return false;
  217. }
  218. public bool Contains (XmlSchema targetNamespace)
  219. {
  220. foreach (XmlSchema schema in schemas)
  221. if (schema == targetNamespace)
  222. return true;
  223. return false;
  224. }
  225. public void CopyTo (XmlSchema [] array, int index)
  226. {
  227. schemas.CopyTo (array, index);
  228. }
  229. internal void CopyTo (Array array, int index)
  230. {
  231. schemas.CopyTo (array, index);
  232. }
  233. string GetSafeNs (string ns)
  234. {
  235. return ns == null ? "" : ns;
  236. }
  237. [MonoTODO]
  238. // FIXME: Check exact behavior
  239. public XmlSchema Remove (XmlSchema schema)
  240. {
  241. if (schema == null)
  242. throw new ArgumentNullException ("schema");
  243. ArrayList al = new ArrayList ();
  244. al.AddRange (schemas);
  245. if (!al.Contains (schema))
  246. return null;
  247. // FIXME: I have no idea why Remove() might throw
  248. // XmlSchemaException, except for the case it compiles.
  249. if (!schema.IsCompiled)
  250. schema.CompileSubset (ValidationEventHandler, this, xmlResolver);
  251. schemas.Remove (schema);
  252. ResetCompile ();
  253. return schema;
  254. }
  255. void ResetCompile ()
  256. {
  257. isCompiled = false;
  258. ClearGlobalComponents ();
  259. }
  260. public bool RemoveRecursive (XmlSchema schema)
  261. {
  262. if (schema == null)
  263. throw new ArgumentNullException ("schema");
  264. ArrayList al = new ArrayList ();
  265. al.AddRange (schemas);
  266. if (!al.Contains (schema))
  267. return false;
  268. al.Remove (schema);
  269. schemas.Remove (schema);
  270. if (!IsCompiled)
  271. return true;
  272. ClearGlobalComponents ();
  273. foreach (XmlSchema s in al) {
  274. if (s.IsCompiled)
  275. AddGlobalComponents (schema);
  276. }
  277. return true;
  278. }
  279. public XmlSchema Reprocess (XmlSchema schema)
  280. {
  281. if (schema == null)
  282. throw new ArgumentNullException ("schema");
  283. ArrayList al = new ArrayList ();
  284. al.AddRange (schemas);
  285. if (!al.Contains (schema))
  286. throw new ArgumentException ("Target schema is not contained in the schema set.");
  287. ClearGlobalComponents ();
  288. foreach (XmlSchema s in al) {
  289. if (schema == s)
  290. schema.CompileSubset (ValidationEventHandler, this, xmlResolver);
  291. if (s.IsCompiled)
  292. AddGlobalComponents (schema);
  293. }
  294. return schema.IsCompiled ? schema : null;
  295. }
  296. public ICollection Schemas ()
  297. {
  298. return schemas;
  299. }
  300. public ICollection Schemas (string targetNamespace)
  301. {
  302. targetNamespace = GetSafeNs (targetNamespace);
  303. ArrayList al = new ArrayList ();
  304. foreach (XmlSchema schema in schemas)
  305. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  306. al.Add (schema);
  307. return al;
  308. }
  309. internal bool MissedSubComponents (string targetNamespace)
  310. {
  311. foreach (XmlSchema s in Schemas (targetNamespace))
  312. if (s.missedSubComponents)
  313. return true;
  314. return false;
  315. }
  316. }
  317. }