2003-04-25 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / System.Xml / XmlNamedNodeMap.cs
1 //
2 // System.Xml.XmlNamedNodeMap
3 //
4 // Author:
5 //   Jason Diamond (jason@injektilo.org)
6 //   Duncan Mak  (duncan@ximian.com)
7 //
8 // (C) 2002 Jason Diamond  http://injektilo.org/
9 //
10
11 using System;
12 using System.Collections;
13
14 namespace System.Xml
15 {
16         public class XmlNamedNodeMap : IEnumerable
17         {
18                 XmlNode parent;
19                 ArrayList nodeList;
20                 bool readOnly = false;
21
22                 internal XmlNamedNodeMap (XmlNode parent)
23                 {
24                         this.parent = parent;
25                         nodeList = new ArrayList ();
26                 }
27
28                 public virtual int Count {
29                         get { return nodeList.Count; }
30                 }
31
32                 public virtual IEnumerator GetEnumerator () 
33                 {
34                         return nodeList.GetEnumerator ();
35                 }
36
37                 public virtual XmlNode GetNamedItem (string name)
38                 {
39                         foreach (XmlNode node in nodeList) {
40                                 if (node.Name == name)
41                                         return node;
42                         }
43                         return null;
44                 }
45
46                 public virtual XmlNode GetNamedItem (string localName, string namespaceURI)
47                 {
48                         foreach (XmlNode node in nodeList) {
49                                 if ((node.LocalName == localName)
50                                     && (node.NamespaceURI == namespaceURI))
51                                         return node;
52                         }
53
54                         return null;
55                 }
56                 
57                 public virtual XmlNode Item (int index)
58                 {
59                         if (index < 0 || index > nodeList.Count)
60                                 return null;
61                         else
62                                 return (XmlNode) nodeList [index];
63                 }
64
65                 public virtual XmlNode RemoveNamedItem (string name)
66                 {                       
67                         foreach (XmlNode node in nodeList)
68                                 if (node.Name == name) {
69                                         if (node.IsReadOnly)
70                                                 throw new InvalidOperationException ("Cannot remove. This node is read only: " + name);
71                                         nodeList.Remove (node);
72                                         return node;
73                                 }
74                         return null;
75                 }
76
77                 public virtual XmlNode RemoveNamedItem (string localName, string namespaceURI)
78                 {
79                         foreach (XmlNode node in nodeList)
80                                 if ((node.LocalName == localName)
81                                     && (node.NamespaceURI == namespaceURI)) {
82                                         nodeList.Remove (node);
83                                         return node;
84                                 }
85
86                         return null;
87                 }
88
89                 public virtual XmlNode SetNamedItem (XmlNode node)
90                 {
91                         return SetNamedItem(node, -1);
92                 }
93
94                 internal XmlNode SetNamedItem (XmlNode node, int pos)
95                 {
96                         if (readOnly || (node.OwnerDocument != parent.OwnerDocument))
97                                 throw new ArgumentException ("Cannot add to NodeMap.");
98
99                         foreach (XmlNode x in nodeList)
100                                 if(x.LocalName == node.LocalName && x.NamespaceURI == node.NamespaceURI) {
101                                         nodeList.Remove (x);
102                                         nodeList.Add (node);
103                                         return x;
104                                 }
105                         
106                         if(pos < 0)
107                                 nodeList.Add (node);
108                         else
109                                 nodeList.Insert(pos, node);
110                         return null;
111                 }
112
113                 internal ArrayList Nodes { get { return nodeList; } }
114         }
115 }