XmlAttributeCollection.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // System.Xml.XmlAttributeCollection
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C) 2002 Jason Diamond http://injektilo.org/
  9. // (C) 2002 Atsushi Enomoto
  10. //
  11. using System;
  12. using System.Collections;
  13. namespace System.Xml
  14. {
  15. public class XmlAttributeCollection : XmlNamedNodeMap, ICollection
  16. {
  17. XmlElement ownerElement;
  18. internal XmlAttributeCollection (XmlNode parent) : base (parent)
  19. {
  20. ownerElement = parent as XmlElement;
  21. if(ownerElement == null)
  22. throw new XmlException ("invalid construction for XmlAttributeCollection.");
  23. }
  24. bool ICollection.IsSynchronized {
  25. get {
  26. throw new NotImplementedException ();
  27. }
  28. }
  29. bool IsReadOnly {
  30. get {
  31. return ownerElement.IsReadOnly;
  32. }
  33. }
  34. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  35. public virtual XmlAttribute this [string name] {
  36. get {
  37. return (XmlAttribute) GetNamedItem (name);
  38. }
  39. }
  40. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  41. public virtual XmlAttribute this [int i] {
  42. get {
  43. return (XmlAttribute) Nodes [i];
  44. }
  45. }
  46. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  47. public virtual XmlAttribute this [string localName, string namespaceURI] {
  48. get {
  49. return (XmlAttribute) GetNamedItem (localName, namespaceURI);
  50. }
  51. }
  52. object ICollection.SyncRoot {
  53. get {
  54. throw new NotImplementedException ();
  55. }
  56. }
  57. public virtual XmlAttribute Append (XmlAttribute node)
  58. {
  59. XmlNode xmlNode = this.SetNamedItem (node);
  60. return node;
  61. }
  62. public void CopyTo (XmlAttribute[] array, int index)
  63. {
  64. // assuming that Nodes is a correct collection.
  65. for(int i=0; i<Nodes.Count; i++)
  66. array [index + i] = Nodes [i] as XmlAttribute;
  67. }
  68. void ICollection.CopyTo (Array array, int index)
  69. {
  70. // assuming that Nodes is a correct collection.
  71. array.CopyTo (Nodes.ToArray (typeof(XmlAttribute)), index);
  72. }
  73. public virtual XmlAttribute InsertAfter (XmlAttribute newNode, XmlAttribute refNode)
  74. {
  75. if(newNode.OwnerDocument != this.ownerElement.OwnerDocument)
  76. throw new ArgumentException ("different document created this newNode.");
  77. ownerElement.OwnerDocument.onNodeInserting (newNode, null);
  78. int pos = Nodes.Count + 1;
  79. if(refNode != null)
  80. {
  81. for(int i=0; i<Nodes.Count; i++)
  82. {
  83. XmlNode n = Nodes [i] as XmlNode;
  84. if(n == refNode)
  85. {
  86. pos = i + 1;
  87. break;
  88. }
  89. }
  90. if(pos > Nodes.Count)
  91. throw new XmlException ("refNode not found in this collection.");
  92. }
  93. else
  94. pos = 0;
  95. SetNamedItem (newNode, pos);
  96. ownerElement.OwnerDocument.onNodeInserted (newNode, null);
  97. return newNode;
  98. }
  99. public virtual XmlAttribute InsertBefore (XmlAttribute newNode, XmlAttribute refNode)
  100. {
  101. if(newNode.OwnerDocument != this.ownerElement.OwnerDocument)
  102. throw new ArgumentException ("different document created this newNode.");
  103. ownerElement.OwnerDocument.onNodeInserting (newNode, null);
  104. int pos = Nodes.Count;
  105. if(refNode != null)
  106. {
  107. for(int i=0; i<Nodes.Count; i++)
  108. {
  109. XmlNode n = Nodes [i] as XmlNode;
  110. if(n == refNode)
  111. {
  112. pos = i;
  113. break;
  114. }
  115. }
  116. if(pos == Nodes.Count)
  117. throw new XmlException ("refNode not found in this collection.");
  118. }
  119. SetNamedItem (newNode, pos);
  120. ownerElement.OwnerDocument.onNodeInserted (newNode, null);
  121. return newNode;
  122. }
  123. public virtual XmlAttribute Prepend (XmlAttribute node)
  124. {
  125. return this.InsertAfter (node, null);
  126. }
  127. public virtual XmlAttribute Remove (XmlAttribute node)
  128. {
  129. if (node == null)
  130. throw new ArgumentException ("Specified node is null.");
  131. if (node.OwnerDocument != this.ownerElement.OwnerDocument)
  132. throw new ArgumentException ("Specified node is in a different document.");
  133. XmlAttribute retAttr = null;
  134. foreach (XmlAttribute attr in Nodes) {
  135. if (attr == node) {
  136. retAttr = attr;
  137. break;
  138. }
  139. }
  140. if(retAttr != null) {
  141. ownerElement.OwnerDocument.onNodeRemoving (node, null);
  142. base.RemoveNamedItem (retAttr.LocalName, retAttr.NamespaceURI);
  143. ownerElement.OwnerDocument.onNodeRemoved (node, null);
  144. }
  145. return retAttr;
  146. }
  147. public virtual void RemoveAll ()
  148. {
  149. while(Count > 0)
  150. Remove ((XmlAttribute)Nodes [0]);
  151. }
  152. public virtual XmlAttribute RemoveAt (int i)
  153. {
  154. if(Nodes.Count <= i)
  155. return null;
  156. return Remove ((XmlAttribute)Nodes [i]);
  157. }
  158. public override XmlNode SetNamedItem (XmlNode node)
  159. {
  160. return SetNamedItem(node, -1);
  161. }
  162. [MonoTODO("event handling")]
  163. internal new XmlNode SetNamedItem (XmlNode node, int pos)
  164. {
  165. if(IsReadOnly)
  166. throw new XmlException ("this AttributeCollection is read only.");
  167. return base.SetNamedItem (node, pos);
  168. }
  169. }
  170. }