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