XmlSchemas.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. return null;
  59. if (!schema.IsCompiled) {
  60. schema.Compile (null);
  61. }
  62. XmlSchemaObjectTable tbl = null;
  63. if (type == typeof (XmlSchemaSimpleType) || type == typeof (XmlSchemaComplexType))
  64. tbl = schema.SchemaTypes;
  65. else if (type == typeof (XmlSchemaAttribute))
  66. tbl = schema.Attributes;
  67. else if (type == typeof (XmlSchemaAttributeGroup))
  68. tbl = schema.AttributeGroups;
  69. else if (type == typeof (XmlSchemaElement))
  70. tbl = schema.Elements;
  71. else if (type == typeof (XmlSchemaGroup))
  72. tbl = schema.Groups;
  73. else if (type == typeof (XmlSchemaNotation))
  74. tbl = schema.Notations;
  75. object res = (tbl != null) ? tbl [name] : null;
  76. if (res != null && res.GetType () != type) return null;
  77. else return res;
  78. }
  79. public int IndexOf (XmlSchema schema)
  80. {
  81. return List.IndexOf (schema);
  82. }
  83. public void Insert (int index, XmlSchema schema)
  84. {
  85. List.Insert (index, schema);
  86. }
  87. public static bool IsDataSet (XmlSchema schema)
  88. {
  89. XmlSchemaElement el = schema.Items.Count == 1 ?
  90. schema.Items [0] as XmlSchemaElement : null;
  91. if (el != null && el.UnhandledAttributes.Length > 0) {
  92. for (int i = 0; i < el.UnhandledAttributes.Length; i++) {
  93. XmlAttribute attr = el.UnhandledAttributes [i];
  94. if (attr.NamespaceURI == msdataNS && attr.LocalName == "IsDataSet")
  95. return (attr.Value.ToLower (System.Globalization.CultureInfo.InvariantCulture) == "true");
  96. }
  97. }
  98. return false;
  99. }
  100. protected override void OnClear ()
  101. {
  102. table.Clear ();
  103. }
  104. protected override void OnInsert (int index, object value)
  105. {
  106. string ns = ((XmlSchema) value).TargetNamespace;
  107. if (ns == null) ns = "";
  108. table [ns] = value;
  109. }
  110. protected override void OnRemove (int index, object value)
  111. {
  112. table.Remove (value);
  113. }
  114. protected override void OnSet (int index, object oldValue, object newValue)
  115. {
  116. string ns = ((XmlSchema) oldValue).TargetNamespace;
  117. if (ns == null) ns = "";
  118. table [ns] = newValue;
  119. }
  120. public void Remove (XmlSchema schema)
  121. {
  122. List.Remove (schema);
  123. }
  124. #endregion // Methods
  125. }
  126. }