XmlAttributeCollection.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. [MonoTODO]
  35. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  36. public virtual XmlAttribute this [string name] {
  37. get {
  38. return (XmlAttribute) GetNamedItem (name);
  39. }
  40. }
  41. [MonoTODO]
  42. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  43. public virtual XmlAttribute this [int i] {
  44. get {
  45. return (XmlAttribute) Nodes [i];
  46. }
  47. }
  48. [MonoTODO]
  49. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  50. public virtual XmlAttribute this [string localName, string namespaceURI] {
  51. get {
  52. return (XmlAttribute) GetNamedItem (localName, namespaceURI);
  53. }
  54. }
  55. object ICollection.SyncRoot {
  56. get {
  57. throw new NotImplementedException ();
  58. }
  59. }
  60. public virtual XmlAttribute Append (XmlAttribute node)
  61. {
  62. XmlNode xmlNode = this.SetNamedItem (node);
  63. return node;
  64. }
  65. public void CopyTo (XmlAttribute[] array, int index)
  66. {
  67. // assuming that Nodes is a correct collection.
  68. for(int i=0; i<Nodes.Count; i++)
  69. array [index + i] = Nodes [i] as XmlAttribute;
  70. }
  71. [MonoTODO] // I don't know why this method is required...
  72. void ICollection.CopyTo (Array array, int index)
  73. {
  74. // assuming that Nodes is a correct collection.
  75. array.CopyTo (Nodes.ToArray (typeof(XmlAttribute)), index);
  76. }
  77. public virtual XmlAttribute InsertAfter (XmlAttribute newNode, XmlAttribute refNode)
  78. {
  79. if(newNode.OwnerDocument != this.ownerElement.OwnerDocument)
  80. throw new ArgumentException ("different document created this newNode.");
  81. ownerElement.OwnerDocument.onNodeInserting (newNode, null);
  82. int pos = Nodes.Count + 1;
  83. if(refNode != null)
  84. {
  85. for(int i=0; i<Nodes.Count; i++)
  86. {
  87. XmlNode n = Nodes [i] as XmlNode;
  88. if(n == refNode)
  89. {
  90. pos = i + 1;
  91. break;
  92. }
  93. }
  94. if(pos > Nodes.Count)
  95. throw new XmlException ("refNode not found in this collection.");
  96. }
  97. else
  98. pos = 0;
  99. SetNamedItem (newNode, pos);
  100. ownerElement.OwnerDocument.onNodeInserted (newNode, null);
  101. return newNode;
  102. }
  103. public virtual XmlAttribute InsertBefore (XmlAttribute newNode, XmlAttribute refNode)
  104. {
  105. if(newNode.OwnerDocument != this.ownerElement.OwnerDocument)
  106. throw new ArgumentException ("different document created this newNode.");
  107. ownerElement.OwnerDocument.onNodeInserting (newNode, null);
  108. int pos = Nodes.Count;
  109. if(refNode != null)
  110. {
  111. for(int i=0; i<Nodes.Count; i++)
  112. {
  113. XmlNode n = Nodes [i] as XmlNode;
  114. if(n == refNode)
  115. {
  116. pos = i;
  117. break;
  118. }
  119. }
  120. if(pos == Nodes.Count)
  121. throw new XmlException ("refNode not found in this collection.");
  122. }
  123. SetNamedItem (newNode, pos);
  124. ownerElement.OwnerDocument.onNodeInserted (newNode, null);
  125. return newNode;
  126. }
  127. public virtual XmlAttribute Prepend (XmlAttribute node)
  128. {
  129. return this.InsertAfter (node, null);
  130. }
  131. public virtual XmlAttribute Remove (XmlAttribute node)
  132. {
  133. if(node == null || node.OwnerDocument != this.ownerElement.OwnerDocument)
  134. throw new ArgumentException ("node is null or different document created this node.");
  135. XmlAttribute retAttr = null;
  136. foreach(XmlAttribute attr in Nodes)
  137. {
  138. if(attr == node)
  139. {
  140. retAttr = attr;
  141. break;
  142. }
  143. }
  144. if(retAttr != null)
  145. {
  146. ownerElement.OwnerDocument.onNodeRemoving (node, null);
  147. base.RemoveNamedItem (retAttr.LocalName, retAttr.NamespaceURI);
  148. ownerElement.OwnerDocument.onNodeRemoved (node, null);
  149. }
  150. return retAttr;
  151. }
  152. public virtual void RemoveAll ()
  153. {
  154. while(Count > 0)
  155. Remove ((XmlAttribute)Nodes [0]);
  156. }
  157. public virtual XmlAttribute RemoveAt (int i)
  158. {
  159. if(Nodes.Count <= i)
  160. return null;
  161. return Remove ((XmlAttribute)Nodes [i]);
  162. }
  163. public override XmlNode SetNamedItem (XmlNode node)
  164. {
  165. return SetNamedItem(node, -1);
  166. }
  167. [MonoTODO("event handling")]
  168. internal new XmlNode SetNamedItem (XmlNode node, int pos)
  169. {
  170. if(IsReadOnly)
  171. throw new XmlException ("this AttributeCollection is read only.");
  172. return base.SetNamedItem (node, pos);
  173. }
  174. }
  175. }