XmlSchemas.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. return Find (schema, name, type);
  108. }
  109. object Find (XmlSchema schema, XmlQualifiedName name, Type type)
  110. {
  111. if (!schema.IsCompiled) {
  112. schema.Compile (null);
  113. }
  114. XmlSchemaObjectTable tbl = null;
  115. if (type == typeof (XmlSchemaSimpleType) || type == typeof (XmlSchemaComplexType))
  116. tbl = schema.SchemaTypes;
  117. else if (type == typeof (XmlSchemaAttribute))
  118. tbl = schema.Attributes;
  119. else if (type == typeof (XmlSchemaAttributeGroup))
  120. tbl = schema.AttributeGroups;
  121. else if (type == typeof (XmlSchemaElement))
  122. tbl = schema.Elements;
  123. else if (type == typeof (XmlSchemaGroup))
  124. tbl = schema.Groups;
  125. else if (type == typeof (XmlSchemaNotation))
  126. tbl = schema.Notations;
  127. object res = (tbl != null) ? tbl [name] : null;
  128. if (res != null && res.GetType () != type) return null;
  129. else return res;
  130. }
  131. public int IndexOf (XmlSchema schema)
  132. {
  133. return List.IndexOf (schema);
  134. }
  135. public void Insert (int index, XmlSchema schema)
  136. {
  137. List.Insert (index, schema);
  138. }
  139. public static bool IsDataSet (XmlSchema schema)
  140. {
  141. XmlSchemaElement el = schema.Items.Count == 1 ?
  142. schema.Items [0] as XmlSchemaElement : null;
  143. if (el != null && el.UnhandledAttributes.Length > 0) {
  144. for (int i = 0; i < el.UnhandledAttributes.Length; i++) {
  145. XmlAttribute attr = el.UnhandledAttributes [i];
  146. if (attr.NamespaceURI == msdataNS && attr.LocalName == "IsDataSet")
  147. return (attr.Value.ToLower (System.Globalization.CultureInfo.InvariantCulture) == "true");
  148. }
  149. }
  150. return false;
  151. }
  152. protected override void OnClear ()
  153. {
  154. table.Clear ();
  155. }
  156. protected override void OnInsert (int index, object value)
  157. {
  158. string ns = ((XmlSchema) value).TargetNamespace;
  159. if (ns == null) ns = "";
  160. table [ns] = value;
  161. }
  162. protected override void OnRemove (int index, object value)
  163. {
  164. table.Remove (value);
  165. }
  166. protected override void OnSet (int index, object oldValue, object newValue)
  167. {
  168. string ns = ((XmlSchema) oldValue).TargetNamespace;
  169. if (ns == null) ns = "";
  170. table [ns] = newValue;
  171. }
  172. public void Remove (XmlSchema schema)
  173. {
  174. List.Remove (schema);
  175. }
  176. #endregion // Methods
  177. }
  178. }