Added implementation of namepsace qualified GetElementsByTagName courtesy of
[mono.git] / mcs / class / System.XML / System.Xml / XmlElement.cs
1 //
2 // System.Xml.XmlAttribute
3 //
4 // Author:
5 //   Jason Diamond (jason@injektilo.org)
6 //
7 // (C) 2002 Jason Diamond  http://injektilo.org/
8 //
9
10 using System;
11 using System.Collections;
12
13 namespace System.Xml
14 {
15         public class XmlElement : XmlLinkedNode
16         {
17                 #region Fields
18
19                 private XmlAttributeCollection attributes;
20                 private XmlLinkedNode lastLinkedChild;
21                 private string localName;
22                 private string namespaceURI;
23                 private string prefix;
24
25                 #endregion
26
27                 #region Constructor
28
29                 protected internal XmlElement (
30                         string prefix, 
31                         string localName, 
32                         string namespaceURI, 
33                         XmlDocument doc) : base (doc)
34                 {
35                         this.prefix = prefix;
36                         this.localName = localName;
37                         this.namespaceURI = namespaceURI;
38
39                         attributes = new XmlAttributeCollection (this);
40                 }
41
42                 #endregion
43
44                 #region Properties
45
46                 public override XmlAttributeCollection Attributes {
47                         get { return attributes; }
48                 }
49
50                 public virtual bool HasAttributes {
51                         get { return attributes.Count > 0; }
52                 }
53
54                 [MonoTODO ("Setter.")]
55                 public override string InnerXml {
56                         get {
57                                 // Not sure why this is an override.  Passing through for now.
58                                 return base.InnerXml;
59                         }
60                         set { throw new NotImplementedException (); }
61                 }
62
63                 [MonoTODO]
64                 public bool IsEmpty {
65                         get { throw new NotImplementedException (); }
66
67                         set { throw new NotImplementedException (); }
68                 }
69
70                 internal override XmlLinkedNode LastLinkedChild {
71                         get { return lastLinkedChild; }
72
73                         set { lastLinkedChild = value; }
74                 }
75                 
76                 public override string LocalName {
77                         get { return localName; }
78                 }
79
80                 public override string Name {
81                         get { 
82                                 return prefix != String.Empty ? prefix + ":" + localName : localName; 
83                         }
84                 }
85
86                 public override string NamespaceURI {
87                         get { return namespaceURI; }
88                 }
89
90                 [MonoTODO]
91                 public override XmlNode NextSibling {
92                         get { 
93                                 return base.NextSibling; 
94                         }
95                 }
96
97                 public override XmlNodeType NodeType {
98                         get { 
99                                 return XmlNodeType.Element; 
100                         }
101                 }
102
103                 [MonoTODO]
104                 public override XmlDocument OwnerDocument {
105                         get { 
106                                 return base.OwnerDocument; 
107                         }
108                 }
109
110                 public override string Prefix {
111                         get { 
112                                 return prefix; 
113                         }
114                 }
115
116                 #endregion
117
118                 #region Methods
119                 
120                 [MonoTODO]
121                 public override XmlNode CloneNode (bool deep)
122                 {
123                         XmlNode node =  new XmlElement (prefix, localName, namespaceURI,
124                                                         OwnerDocument);
125
126                         for (int i = 0; i < node.Attributes.Count; i++)
127                                 node.AppendChild (node.Attributes [i].CloneNode (false));
128                         
129                         if (deep) {
130                                 while ((node != null) && (node.HasChildNodes)) {                                        
131                                         AppendChild (node.NextSibling.CloneNode (true));
132                                         node = node.NextSibling;
133                                 }
134                         } // shallow cloning
135                                 
136                         //
137                         // Reminder: Also look into Default attributes.
138                         //
139                         return node;
140                 }
141
142                 [MonoTODO]
143                 public virtual string GetAttribute (string name)
144                 {
145                         XmlNode attributeNode = Attributes.GetNamedItem (name);
146                         return attributeNode != null ? attributeNode.Value : String.Empty;
147                 }
148
149                 [MonoTODO]
150                 public virtual string GetAttribute (string localName, string namespaceURI)
151                 {
152                         XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
153                         return attributeNode != null ? attributeNode.Value : String.Empty;
154                 }
155
156                 [MonoTODO]
157                 public virtual XmlAttribute GetAttributeNode (string name)
158                 {
159                         XmlNode attributeNode = Attributes.GetNamedItem (name);
160                         return attributeNode != null ? attributeNode as XmlAttribute : null;
161                 }
162
163                 [MonoTODO]
164                 public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
165                 {
166                         XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
167                         return attributeNode != null ? attributeNode as XmlAttribute : null;
168                 }
169
170                 public virtual XmlNodeList GetElementsByTagName (string name)
171                 {
172                         ArrayList nodeArrayList = new ArrayList ();
173                         this.searchNodesRecursively (this, name, String.Empty, nodeArrayList);
174                         return new XmlNodeArrayList (nodeArrayList);
175                 }
176
177                 private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI, 
178                         ArrayList argArrayList)
179                 {
180                         XmlNodeList xmlNodeList = argNode.ChildNodes;
181                         foreach (XmlNode node in xmlNodeList)
182                         {
183                                 if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
184                                         argArrayList.Add (node);
185                                 else    
186                                         this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
187                         }
188                 }
189
190                 [MonoTODO]
191                 public virtual bool HasAttribute (string name)
192                 {
193                         XmlNode attributeNode = Attributes.GetNamedItem (name);
194                         return attributeNode != null;
195                 }
196
197                 [MonoTODO]
198                 public virtual bool HasAttribute (string localName, string namespaceURI)
199                 {
200                         throw new NotImplementedException ();
201                 }
202
203                 [MonoTODO ("Don't remove default attributes.")]
204                 public override void RemoveAll ()
205                 {
206                         // Remove the child nodes.
207                         base.RemoveAll ();
208
209                         // Remove all attributes.
210                         attributes.RemoveAll ();
211                 }
212
213                 [MonoTODO]
214                 public virtual void RemoveAllAttributes ()
215                 {
216                         throw new NotImplementedException ();
217                 }
218
219                 [MonoTODO]
220                 public virtual void RemoveAttribute (string name)
221                 {
222                         throw new NotImplementedException ();
223                 }
224
225                 [MonoTODO]
226                 public virtual void RemoveAttribute (string localName, string namespaceURI)
227                 {
228                         throw new NotImplementedException ();
229                 }
230
231                 [MonoTODO]
232                 public virtual XmlNode RemoveAttributeAt (int i)
233                 {
234                         throw new NotImplementedException ();
235                 }
236
237                 [MonoTODO]
238                 public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
239                 {
240                         throw new NotImplementedException ();
241                 }
242
243                 [MonoTODO]
244                 public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
245                 {
246                         throw new NotImplementedException ();
247                 }
248
249                 [MonoTODO]
250                 public virtual void SetAttribute (string name, string value)
251                 {
252                         XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
253                         attribute.SetParentNode (this);
254                         attribute.Value = value;
255                         Attributes.SetNamedItem (attribute);
256                 }
257
258                 [MonoTODO]
259                 public virtual string SetAttribute (string localName, string namespaceURI, string value)
260                 {
261                         throw new NotImplementedException ();
262                 }
263
264                 [MonoTODO]
265                 public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
266                 {
267                         newAttr.SetParentNode(this);
268                         XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
269                         return oldAttr != null ? oldAttr as XmlAttribute : null;
270                 }
271
272                 [MonoTODO]
273                 public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
274                 {
275                         throw new NotImplementedException ();
276                 }
277
278                 public override void WriteContentTo (XmlWriter w)
279                 {
280                         foreach(XmlNode childNode in ChildNodes)
281                                 childNode.WriteTo(w);
282                 }
283
284                 public override void WriteTo (XmlWriter w)
285                 {
286                         w.WriteStartElement(Prefix, LocalName, NamespaceURI);
287
288                         foreach(XmlNode attributeNode in Attributes)
289                                 attributeNode.WriteTo(w);
290
291                         WriteContentTo(w);
292
293                         w.WriteEndElement();
294                 }
295
296                 #endregion
297         }
298 }