XmlSchemaSet.cs 8.7 KB

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