2005-12-05 Lluis Sanchez Gual <lluis@novell.com>
[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 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Collections;
34 using Mono.Xml;
35
36 namespace System.Xml
37 {
38         public class XmlNamedNodeMap : IEnumerable
39         {
40                 XmlNode parent;
41                 ArrayList nodeList;
42                 bool readOnly = false;
43
44                 internal XmlNamedNodeMap (XmlNode parent)
45                 {
46                         this.parent = parent;
47                         nodeList = new ArrayList ();
48                 }
49
50                 public virtual int Count {
51                         get { return nodeList.Count; }
52                 }
53
54                 public virtual IEnumerator GetEnumerator () 
55                 {
56                         return nodeList.GetEnumerator ();
57                 }
58
59                 public virtual XmlNode GetNamedItem (string name)
60                 {
61                         for (int i = 0; i < nodeList.Count; i++) {
62                                 XmlNode node = (XmlNode) nodeList [i];
63                                 if (node.Name == name)
64                                         return node;
65                         }
66                         return null;
67                 }
68
69                 public virtual XmlNode GetNamedItem (string localName, string namespaceURI)
70                 {
71                         for (int i = 0; i < nodeList.Count; i++) {
72                                 XmlNode node = (XmlNode) nodeList [i];
73                                 if ((node.LocalName == localName)
74                                     && (node.NamespaceURI == namespaceURI))
75                                         return node;
76                         }
77
78                         return null;
79                 }
80                 
81                 public virtual XmlNode Item (int index)
82                 {
83                         if (index < 0 || index >= nodeList.Count)
84                                 return null;
85                         else
86                                 return (XmlNode) nodeList [index];
87                 }
88
89                 public virtual XmlNode RemoveNamedItem (string name)
90                 {
91                         for (int i = 0; i < nodeList.Count; i++) {
92                                 XmlNode node = (XmlNode) nodeList [i];
93                                 if (node.Name == name) {
94                                         if (node.IsReadOnly)
95                                                 throw new InvalidOperationException ("Cannot remove. This node is read only: " + name);
96                                         nodeList.Remove (node);
97                                         // Since XmlAttributeCollection does not override
98                                         // it while attribute have to keep it in the
99                                         // collection, it adds to the collection immediately.
100                                         XmlAttribute attr = node as XmlAttribute;
101                                         if (attr != null) {
102                                                 DTDAttributeDefinition def = attr.GetAttributeDefinition ();
103                                                 if (def != null && def.DefaultValue != null) {
104                                                         XmlAttribute newAttr = attr.OwnerDocument.CreateAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, true, false);
105                                                         newAttr.Value = def.DefaultValue;
106                                                         newAttr.SetDefault ();
107                                                         attr.OwnerElement.SetAttributeNode (newAttr);
108                                                 }
109                                         }
110                                         return node;
111                                 }
112                         }
113                         return null;
114                 }
115
116                 public virtual XmlNode RemoveNamedItem (string localName, string namespaceURI)
117                 {
118                         for (int i = 0; i < nodeList.Count; i++) {
119                                 XmlNode node = (XmlNode) nodeList [i];
120                                 if ((node.LocalName == localName)
121                                     && (node.NamespaceURI == namespaceURI)) {
122                                         nodeList.Remove (node);
123                                         return node;
124                                 }
125                         }
126                         return null;
127                 }
128
129                 public virtual XmlNode SetNamedItem (XmlNode node)
130                 {
131                         return SetNamedItem (node, -1, true);
132                 }
133
134                 internal XmlNode SetNamedItem (XmlNode node, bool raiseEvent)
135                 {
136                         return SetNamedItem (node, -1, raiseEvent);
137                 }
138
139                 internal XmlNode SetNamedItem (XmlNode node, int pos, bool raiseEvent)
140                 {
141                         if (readOnly || (node.OwnerDocument != parent.OwnerDocument))
142                                 throw new ArgumentException ("Cannot add to NodeMap.");
143
144                         if (raiseEvent)
145                                 parent.OwnerDocument.onNodeInserting (node, parent);
146
147                         try {
148                                 for (int i = 0; i < nodeList.Count; i++) {
149                                         XmlNode x = (XmlNode) nodeList [i];
150                                         if(x.LocalName == node.LocalName && x.NamespaceURI == node.NamespaceURI) {
151                                                 nodeList.Remove (x);
152                                                 if (pos < 0)
153                                                         nodeList.Add (node);
154                                                 else
155                                                         nodeList.Insert (pos, node);
156                                                 return x;
157                                         }
158                                 }
159                         
160                                 if(pos < 0)
161                                         nodeList.Add (node);
162                                 else
163                                         nodeList.Insert (pos, node);
164
165                                 return node;
166                         } finally {
167                                 if (raiseEvent)
168                                         parent.OwnerDocument.onNodeInserted (node, parent);
169                         }
170
171                 }
172
173                 internal ArrayList Nodes { get { return nodeList; } }
174         }
175 }