XmlSchemaSet.cs 8.9 KB

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