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
42 bool readOnly = false;
44 internal XmlNamedNodeMap (XmlNode parent)
47 nodeList = new ArrayList ();
50 public virtual int Count {
51 get { return nodeList.Count; }
54 public virtual IEnumerator GetEnumerator ()
56 return nodeList.GetEnumerator ();
59 public virtual XmlNode GetNamedItem (string name)
61 for (int i = 0; i < nodeList.Count; i++) {
62 XmlNode node = (XmlNode) nodeList [i];
63 if (node.Name == name)
69 public virtual XmlNode GetNamedItem (string localName, string namespaceURI)
71 for (int i = 0; i < nodeList.Count; i++) {
72 XmlNode node = (XmlNode) nodeList [i];
73 if ((node.LocalName == localName)
74 && (node.NamespaceURI == namespaceURI))
81 public virtual XmlNode Item (int index)
83 if (index < 0 || index >= nodeList.Count)
86 return (XmlNode) nodeList [index];
89 public virtual XmlNode RemoveNamedItem (string name)
91 for (int i = 0; i < nodeList.Count; i++) {
92 XmlNode node = (XmlNode) nodeList [i];
93 if (node.Name == name) {
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;
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);
116 public virtual XmlNode RemoveNamedItem (string localName, string namespaceURI)
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);
129 public virtual XmlNode SetNamedItem (XmlNode node)
131 return SetNamedItem (node, -1, true);
134 internal XmlNode SetNamedItem (XmlNode node, bool raiseEvent)
136 return SetNamedItem (node, -1, raiseEvent);
139 internal XmlNode SetNamedItem (XmlNode node, int pos, bool raiseEvent)
141 if (readOnly || (node.OwnerDocument != parent.OwnerDocument))
142 throw new ArgumentException ("Cannot add to NodeMap.");
145 parent.OwnerDocument.onNodeInserting (node, parent);
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) {
155 nodeList.Insert (pos, node);
163 nodeList.Insert (pos, node);
168 parent.OwnerDocument.onNodeInserted (node, parent);
173 internal ArrayList Nodes { get { return nodeList; } }