XmlSchemas.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // System.Xml.Serialization.XmlSchemas
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. using System.Collections;
  10. using System.Xml.Schema;
  11. namespace System.Xml.Serialization {
  12. public class XmlSchemas : CollectionBase {
  13. #region Fields
  14. private static string msdataNS = "urn:schemas-microsoft-com:xml-msdata";
  15. Hashtable table = new Hashtable ();
  16. #endregion
  17. #region Constructors
  18. public XmlSchemas ()
  19. {
  20. }
  21. #endregion // Constructors
  22. #region Properties
  23. public XmlSchema this [int index] {
  24. get {
  25. if (index < 0 || index > Count)
  26. throw new ArgumentOutOfRangeException ();
  27. return (XmlSchema) List [index];
  28. }
  29. set { List [index] = value; }
  30. }
  31. public XmlSchema this [string ns] {
  32. get { return (XmlSchema) table[ns!=null?ns:""]; }
  33. }
  34. #endregion // Properties
  35. #region Methods
  36. public int Add (XmlSchema schema)
  37. {
  38. Insert (Count, schema);
  39. return (Count - 1);
  40. }
  41. public void Add (XmlSchemas schemas)
  42. {
  43. foreach (XmlSchema schema in schemas)
  44. Add (schema);
  45. }
  46. public bool Contains (XmlSchema schema)
  47. {
  48. return List.Contains (schema);
  49. }
  50. public void CopyTo (XmlSchema[] array, int index)
  51. {
  52. List.CopyTo (array, index);
  53. }
  54. public object Find (XmlQualifiedName name, Type type)
  55. {
  56. XmlSchema schema = table [name.Namespace] as XmlSchema;
  57. if (schema == null)
  58. {
  59. // An schema may import other schemas. An imported schema would
  60. // not be in the table, but its elements (although from another
  61. // namespace) would be in the schema that imported it. So, we
  62. // need know to check for every schema in the table.
  63. foreach (XmlSchema s in this)
  64. {
  65. object ob = Find (s, name, type);
  66. if (ob != null) return ob;
  67. }
  68. return null;
  69. }
  70. else
  71. return Find (schema, name, type);
  72. }
  73. object Find (XmlSchema schema, XmlQualifiedName name, Type type)
  74. {
  75. if (!schema.IsCompiled) {
  76. schema.Compile (null);
  77. }
  78. XmlSchemaObjectTable tbl = null;
  79. if (type == typeof (XmlSchemaSimpleType) || type == typeof (XmlSchemaComplexType))
  80. tbl = schema.SchemaTypes;
  81. else if (type == typeof (XmlSchemaAttribute))
  82. tbl = schema.Attributes;
  83. else if (type == typeof (XmlSchemaAttributeGroup))
  84. tbl = schema.AttributeGroups;
  85. else if (type == typeof (XmlSchemaElement))
  86. tbl = schema.Elements;
  87. else if (type == typeof (XmlSchemaGroup))
  88. tbl = schema.Groups;
  89. else if (type == typeof (XmlSchemaNotation))
  90. tbl = schema.Notations;
  91. object res = (tbl != null) ? tbl [name] : null;
  92. if (res != null && res.GetType () != type) return null;
  93. else return res;
  94. }
  95. public int IndexOf (XmlSchema schema)
  96. {
  97. return List.IndexOf (schema);
  98. }
  99. public void Insert (int index, XmlSchema schema)
  100. {
  101. List.Insert (index, schema);
  102. }
  103. public static bool IsDataSet (XmlSchema schema)
  104. {
  105. XmlSchemaElement el = schema.Items.Count == 1 ?
  106. schema.Items [0] as XmlSchemaElement : null;
  107. if (el != null && el.UnhandledAttributes.Length > 0) {
  108. for (int i = 0; i < el.UnhandledAttributes.Length; i++) {
  109. XmlAttribute attr = el.UnhandledAttributes [i];
  110. if (attr.NamespaceURI == msdataNS && attr.LocalName == "IsDataSet")
  111. return (attr.Value.ToLower (System.Globalization.CultureInfo.InvariantCulture) == "true");
  112. }
  113. }
  114. return false;
  115. }
  116. protected override void OnClear ()
  117. {
  118. table.Clear ();
  119. }
  120. protected override void OnInsert (int index, object value)
  121. {
  122. string ns = ((XmlSchema) value).TargetNamespace;
  123. if (ns == null) ns = "";
  124. table [ns] = value;
  125. }
  126. protected override void OnRemove (int index, object value)
  127. {
  128. table.Remove (value);
  129. }
  130. protected override void OnSet (int index, object oldValue, object newValue)
  131. {
  132. string ns = ((XmlSchema) oldValue).TargetNamespace;
  133. if (ns == null) ns = "";
  134. table [ns] = newValue;
  135. }
  136. public void Remove (XmlSchema schema)
  137. {
  138. List.Remove (schema);
  139. }
  140. #endregion // Methods
  141. }
  142. }