2 // System.Xml.XmlNamedNodeMap
5 // Jason Diamond (jason@injektilo.org)
6 // Duncan Mak (duncan@ximian.com)
8 // (C) 2002 Jason Diamond http://injektilo.org/
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
38 public class XmlNamedNodeMap : IEnumerable
40 static readonly IEnumerator emptyEnumerator = new XmlNode [0].GetEnumerator ();
44 bool readOnly = false;
46 internal XmlNamedNodeMap (XmlNode parent)
51 private ArrayList NodeList {
54 nodeList = new ArrayList ();
59 public virtual int Count {
60 get { return nodeList == null ? 0 : nodeList.Count; }
63 public virtual IEnumerator GetEnumerator ()
66 return emptyEnumerator;
67 return nodeList.GetEnumerator ();
70 public virtual XmlNode GetNamedItem (string name)
75 for (int i = 0; i < nodeList.Count; i++) {
76 XmlNode node = (XmlNode) nodeList [i];
77 if (node.Name == name)
83 public virtual XmlNode GetNamedItem (string localName, string namespaceURI)
88 for (int i = 0; i < nodeList.Count; i++) {
89 XmlNode node = (XmlNode) nodeList [i];
90 if ((node.LocalName == localName)
91 && (node.NamespaceURI == namespaceURI))
98 public virtual XmlNode Item (int index)
100 if (nodeList == null || index < 0 || index >= nodeList.Count)
103 return (XmlNode) nodeList [index];
106 public virtual XmlNode RemoveNamedItem (string name)
108 if (nodeList == null)
111 for (int i = 0; i < nodeList.Count; i++) {
112 XmlNode node = (XmlNode) nodeList [i];
113 if (node.Name == name) {
115 throw new InvalidOperationException ("Cannot remove. This node is read only: " + name);
116 nodeList.Remove (node);
117 // Since XmlAttributeCollection does not override
118 // it while attribute have to keep it in the
119 // collection, it adds to the collection immediately.
120 XmlAttribute attr = node as XmlAttribute;
122 DTDAttributeDefinition def = attr.GetAttributeDefinition ();
123 if (def != null && def.DefaultValue != null) {
124 XmlAttribute newAttr = attr.OwnerDocument.CreateAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, true, false);
125 newAttr.Value = def.DefaultValue;
126 newAttr.SetDefault ();
127 attr.OwnerElement.SetAttributeNode (newAttr);
136 public virtual XmlNode RemoveNamedItem (string localName, string namespaceURI)
138 if (nodeList == null)
141 for (int i = 0; i < nodeList.Count; i++) {
142 XmlNode node = (XmlNode) nodeList [i];
143 if ((node.LocalName == localName)
144 && (node.NamespaceURI == namespaceURI)) {
145 nodeList.Remove (node);
152 public virtual XmlNode SetNamedItem (XmlNode node)
154 return SetNamedItem (node, -1, true);
157 internal XmlNode SetNamedItem (XmlNode node, bool raiseEvent)
159 return SetNamedItem (node, -1, raiseEvent);
162 internal XmlNode SetNamedItem (XmlNode node, int pos, bool raiseEvent)
164 if (readOnly || (node.OwnerDocument != parent.OwnerDocument))
165 throw new ArgumentException ("Cannot add to NodeMap.");
168 parent.OwnerDocument.onNodeInserting (node, parent);
171 for (int i = 0; i < NodeList.Count; i++) {
172 XmlNode x = (XmlNode) nodeList [i];
173 if(x.LocalName == node.LocalName && x.NamespaceURI == node.NamespaceURI) {
178 nodeList.Insert (pos, node);
186 nodeList.Insert (pos, node);
188 // LAMESPEC: It should return null here, but
189 // it just returns the input node.
193 parent.OwnerDocument.onNodeInserted (node, parent);
198 internal ArrayList Nodes { get { return NodeList; } }