XmlSchemaSet.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. XmlSchemaCompilationSettings settings =
  55. new XmlSchemaCompilationSettings ();
  56. XmlSchemaCollection col;
  57. ValidationEventHandler handler;
  58. bool isCompiled;
  59. internal Guid CompilationId;
  60. public XmlSchemaSet ()
  61. : this (new NameTable ())
  62. {
  63. handler = new ValidationEventHandler (this.OnValidationError);
  64. }
  65. public XmlSchemaSet (XmlNameTable nameTable)
  66. {
  67. if (nameTable == null)
  68. throw new ArgumentNullException ("nameTable");
  69. this.nameTable = nameTable;
  70. schemas = new ArrayList ();
  71. CompilationId = Guid.NewGuid ();
  72. }
  73. public event ValidationEventHandler ValidationEventHandler;
  74. public int Count {
  75. get { return schemas.Count; }
  76. }
  77. public XmlSchemaObjectTable GlobalAttributes {
  78. get {
  79. if (attributes == null)
  80. attributes = new XmlSchemaObjectTable ();
  81. return attributes;
  82. }
  83. }
  84. public XmlSchemaObjectTable GlobalElements {
  85. get {
  86. if (elements == null)
  87. elements = new XmlSchemaObjectTable ();
  88. return elements;
  89. }
  90. }
  91. public XmlSchemaObjectTable GlobalTypes {
  92. get {
  93. if (types == null)
  94. types = new XmlSchemaObjectTable ();
  95. return types;
  96. }
  97. }
  98. public bool IsCompiled {
  99. get { return isCompiled; }
  100. }
  101. public XmlNameTable NameTable {
  102. get { return nameTable; }
  103. }
  104. public XmlSchemaCompilationSettings CompilationSettings {
  105. get { return settings; }
  106. set { settings = value; }
  107. }
  108. // This is mainly used for event delegating
  109. internal XmlSchemaCollection SchemaCollection {
  110. get {
  111. if (col == null) {
  112. lock (this) {
  113. col = new XmlSchemaCollection ();
  114. foreach (XmlSchema s in schemas)
  115. col.Add (s);
  116. }
  117. }
  118. return col;
  119. }
  120. }
  121. public XmlResolver XmlResolver {
  122. set { xmlResolver = value; }
  123. #if NET_2_0
  124. internal get { return xmlResolver; }
  125. #endif
  126. }
  127. public XmlSchema Add (string targetNamespace, string url)
  128. {
  129. targetNamespace = GetSafeNs (targetNamespace);
  130. XmlTextReader r = null;
  131. try {
  132. r = new XmlTextReader (url, nameTable);
  133. return Add (targetNamespace, r);
  134. } finally {
  135. if (r != null)
  136. r.Close ();
  137. }
  138. }
  139. [MonoTODO ("It has weird namespace duplication check that is different from Add(XmlSchema).")]
  140. public XmlSchema Add (string targetNamespace, XmlReader reader)
  141. {
  142. XmlSchema schema = XmlSchema.Read (reader, handler);
  143. if (targetNamespace != null
  144. && targetNamespace.Length > 0)
  145. schema.TargetNamespace = targetNamespace;
  146. Add (schema);
  147. return schema;
  148. }
  149. [MonoTODO ("Check the exact behavior when namespaces are in conflict (but it would be preferable to wait for 2.0 RTM).")]
  150. public void Add (XmlSchemaSet schemaSet)
  151. {
  152. ArrayList al = new ArrayList ();
  153. foreach (XmlSchema schema in schemaSet.schemas) {
  154. if (!schemas.Contains (schema))
  155. al.Add (schema);
  156. else
  157. AddGlobalComponents (schema);
  158. }
  159. foreach (XmlSchema schema in al)
  160. Add (schema);
  161. }
  162. [MonoTODO ("We need to research more about the expected behavior")]
  163. public XmlSchema Add (XmlSchema schema)
  164. {
  165. schemas.Add (schema);
  166. AddGlobalComponents (schema);
  167. return schema;
  168. }
  169. [MonoTODO ("It should be the actual compilation engine.")]
  170. public void Compile ()
  171. {
  172. ClearGlobalComponents ();
  173. ArrayList al = new ArrayList ();
  174. al.AddRange (schemas);
  175. foreach (XmlSchema schema in al) {
  176. if (!schema.IsCompiled)
  177. schema.Compile (handler, this, xmlResolver);
  178. AddGlobalComponents (schema);
  179. }
  180. isCompiled = true;
  181. }
  182. private void ClearGlobalComponents ()
  183. {
  184. GlobalElements.Clear ();
  185. GlobalAttributes.Clear ();
  186. GlobalTypes.Clear ();
  187. // GlobalAttributeGroups.Clear ();
  188. // GlobalGroups.Clear ();
  189. }
  190. private void AddGlobalComponents (XmlSchema schema)
  191. {
  192. foreach (XmlSchemaElement el in schema.Elements.Values)
  193. GlobalElements.Add (el.QualifiedName, el);
  194. foreach (XmlSchemaAttribute a in schema.Attributes.Values)
  195. GlobalAttributes.Add (a.QualifiedName, a);
  196. foreach (XmlSchemaType t in schema.SchemaTypes.Values)
  197. GlobalTypes.Add (t.QualifiedName, t);
  198. }
  199. public bool Contains (string targetNamespace)
  200. {
  201. targetNamespace = GetSafeNs (targetNamespace);
  202. foreach (XmlSchema schema in schemas)
  203. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  204. return true;
  205. return false;
  206. }
  207. public bool Contains (XmlSchema targetNamespace)
  208. {
  209. foreach (XmlSchema schema in schemas)
  210. if (schema == targetNamespace)
  211. return true;
  212. return false;
  213. }
  214. public void CopyTo (XmlSchema [] array, int index)
  215. {
  216. schemas.CopyTo (array, index);
  217. }
  218. internal void CopyTo (Array array, int index)
  219. {
  220. schemas.CopyTo (array, index);
  221. }
  222. string GetSafeNs (string ns)
  223. {
  224. return ns == null ? "" : ns;
  225. }
  226. internal void OnValidationError (object o, ValidationEventArgs e)
  227. {
  228. if (col != null)
  229. col.OnValidationError (o, e);
  230. if (ValidationEventHandler != null)
  231. ValidationEventHandler (o, e);
  232. else if (e.Severity == XmlSeverityType.Error)
  233. throw e.Exception;
  234. }
  235. [MonoTODO ("Check exact behavior")]
  236. public XmlSchema Remove (XmlSchema schema)
  237. {
  238. if (schema == null)
  239. throw new ArgumentNullException ("schema");
  240. ArrayList al = new ArrayList ();
  241. al.AddRange (schemas);
  242. if (!al.Contains (schema))
  243. return null;
  244. // FIXME: I have no idea why Remove() might throw
  245. // XmlSchemaException, except for the case it compiles.
  246. if (!schema.IsCompiled)
  247. schema.Compile (handler, this, xmlResolver);
  248. schemas.Remove (schema);
  249. isCompiled = false;
  250. ClearGlobalComponents ();
  251. return schema;
  252. }
  253. [MonoTODO ("Check exact behavior")]
  254. public bool RemoveRecursive (XmlSchema schema)
  255. {
  256. if (schema == null)
  257. throw new ArgumentNullException ("schema");
  258. ArrayList al = new ArrayList ();
  259. al.AddRange (schemas);
  260. if (!al.Contains (schema))
  261. return false;
  262. al.Remove (schema);
  263. schemas.Remove (schema);
  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.Compile (handler, 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. [MonoTODO]
  293. public ICollection Schemas (string targetNamespace)
  294. {
  295. targetNamespace = GetSafeNs (targetNamespace);
  296. ArrayList al = new ArrayList ();
  297. foreach (XmlSchema schema in schemas)
  298. if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
  299. al.Add (schema);
  300. return al;
  301. }
  302. internal bool MissedSubComponents (string targetNamespace)
  303. {
  304. foreach (XmlSchema s in Schemas (targetNamespace))
  305. if (s.missedSubComponents)
  306. return true;
  307. return false;
  308. }
  309. }
  310. }