XmlSchemas.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // System.Xml.Serialization.XmlSchemas
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections;
  30. using System.Xml.Schema;
  31. namespace System.Xml.Serialization {
  32. public class XmlSchemas : CollectionBase {
  33. #region Fields
  34. private static string msdataNS = "urn:schemas-microsoft-com:xml-msdata";
  35. Hashtable table = new Hashtable ();
  36. #endregion
  37. #region Constructors
  38. public XmlSchemas ()
  39. {
  40. }
  41. #endregion // Constructors
  42. #region Properties
  43. public XmlSchema this [int index] {
  44. get {
  45. if (index < 0 || index > Count)
  46. throw new ArgumentOutOfRangeException ();
  47. return (XmlSchema) List [index];
  48. }
  49. set { List [index] = value; }
  50. }
  51. public XmlSchema this [string ns] {
  52. get { return (XmlSchema) table[ns!=null?ns:""]; }
  53. }
  54. #if NET_2_0
  55. [MonoTODO]
  56. public bool IsCompiled
  57. {
  58. get { throw new NotImplementedException (); }
  59. }
  60. [MonoTODO]
  61. public ICollection Schemas
  62. {
  63. get { throw new NotImplementedException (); }
  64. }
  65. [MonoTODO]
  66. public void Compile (ValidationEventHandler handler, bool fullCompile)
  67. {
  68. }
  69. #endif
  70. #endregion // Properties
  71. #region Methods
  72. public int Add (XmlSchema schema)
  73. {
  74. Insert (Count, schema);
  75. return (Count - 1);
  76. }
  77. public void Add (XmlSchemas schemas)
  78. {
  79. foreach (XmlSchema schema in schemas)
  80. Add (schema);
  81. }
  82. public bool Contains (XmlSchema schema)
  83. {
  84. return List.Contains (schema);
  85. }
  86. public void CopyTo (XmlSchema[] array, int index)
  87. {
  88. List.CopyTo (array, index);
  89. }
  90. public object Find (XmlQualifiedName name, Type type)
  91. {
  92. XmlSchema schema = table [name.Namespace] as XmlSchema;
  93. if (schema == null)
  94. {
  95. // An schema may import other schemas. An imported schema would
  96. // not be in the table, but its elements (although from another
  97. // namespace) would be in the schema that imported it. So, we
  98. // need know to check for every schema in the table.
  99. foreach (XmlSchema s in this)
  100. {
  101. object ob = Find (s, name, type);
  102. if (ob != null) return ob;
  103. }
  104. return null;
  105. }
  106. else {
  107. object fschema = Find (schema, name, type);
  108. #if NET_2_0
  109. if (fschema == null) {
  110. // still didn't find it
  111. // (possibly table[name.Namespace] was overwritten in table due to duplicate "" keys),
  112. // so look in all schemas (for consistiency with MS behaviour)
  113. foreach (XmlSchema s in this) {
  114. object ob = Find (s, name, type);
  115. if (ob != null) return ob;
  116. }
  117. }
  118. #endif
  119. return fschema;
  120. }
  121. }
  122. object Find (XmlSchema schema, XmlQualifiedName name, Type type)
  123. {
  124. if (!schema.IsCompiled) {
  125. schema.Compile (null);
  126. }
  127. XmlSchemaObjectTable tbl = null;
  128. if (type == typeof (XmlSchemaSimpleType) || type == typeof (XmlSchemaComplexType))
  129. tbl = schema.SchemaTypes;
  130. else if (type == typeof (XmlSchemaAttribute))
  131. tbl = schema.Attributes;
  132. else if (type == typeof (XmlSchemaAttributeGroup))
  133. tbl = schema.AttributeGroups;
  134. else if (type == typeof (XmlSchemaElement))
  135. tbl = schema.Elements;
  136. else if (type == typeof (XmlSchemaGroup))
  137. tbl = schema.Groups;
  138. else if (type == typeof (XmlSchemaNotation))
  139. tbl = schema.Notations;
  140. object res = (tbl != null) ? tbl [name] : null;
  141. if (res != null && res.GetType () != type) return null;
  142. else return res;
  143. }
  144. public int IndexOf (XmlSchema schema)
  145. {
  146. return List.IndexOf (schema);
  147. }
  148. public void Insert (int index, XmlSchema schema)
  149. {
  150. List.Insert (index, schema);
  151. }
  152. public static bool IsDataSet (XmlSchema schema)
  153. {
  154. XmlSchemaElement el = schema.Items.Count == 1 ?
  155. schema.Items [0] as XmlSchemaElement : null;
  156. if (el != null && el.UnhandledAttributes != null && el.UnhandledAttributes.Length > 0) {
  157. for (int i = 0; i < el.UnhandledAttributes.Length; i++) {
  158. XmlAttribute attr = el.UnhandledAttributes [i];
  159. if (attr.NamespaceURI == msdataNS && attr.LocalName == "IsDataSet")
  160. return (attr.Value.ToLower (System.Globalization.CultureInfo.InvariantCulture) == "true");
  161. }
  162. }
  163. return false;
  164. }
  165. protected override void OnClear ()
  166. {
  167. table.Clear ();
  168. }
  169. protected override void OnInsert (int index, object value)
  170. {
  171. string ns = ((XmlSchema) value).TargetNamespace;
  172. if (ns == null) ns = "";
  173. #if ONLY_1_1
  174. if (table.Contains (ns))
  175. throw new InvalidOperationException ("A schema with the namespace '" + ns + "' has already been added.");
  176. #endif
  177. table [ns] = value;
  178. }
  179. protected override void OnRemove (int index, object value)
  180. {
  181. table.Remove (value);
  182. }
  183. protected override void OnSet (int index, object oldValue, object newValue)
  184. {
  185. string ns = ((XmlSchema) oldValue).TargetNamespace;
  186. if (ns == null) ns = "";
  187. table [ns] = newValue;
  188. }
  189. public void Remove (XmlSchema schema)
  190. {
  191. List.Remove (schema);
  192. }
  193. #endregion // Methods
  194. }
  195. }