(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.XML / System.Xml / XmlNamedNodeMap.cs
blob37b147954f826801b8f43e7252f122346d77372f
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 //
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.
32 using System;
33 using System.Collections;
34 using Mono.Xml;
36 namespace System.Xml
38 public class XmlNamedNodeMap : IEnumerable
40 XmlNode parent;
41 ArrayList nodeList;
42 bool readOnly = false;
44 internal XmlNamedNodeMap (XmlNode parent)
46 this.parent = 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)
64 return node;
66 return null;
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))
75 return node;
78 return null;
81 public virtual XmlNode Item (int index)
83 if (index < 0 || index >= nodeList.Count)
84 return null;
85 else
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) {
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);
110 return node;
113 return null;
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);
123 return node;
126 return null;
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.");
144 if (raiseEvent)
145 parent.OwnerDocument.onNodeInserting (node, parent);
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;
160 if(pos < 0)
161 nodeList.Add (node);
162 else
163 nodeList.Insert (pos, node);
165 return node;
166 } finally {
167 if (raiseEvent)
168 parent.OwnerDocument.onNodeInserted (node, parent);
173 internal ArrayList Nodes { get { return nodeList; } }