XmlSchemaSet.cs 8.7 KB

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