9a11634a67214b9fd6086e7eb04ac1e896289af8
[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, nodeArrayList);
174                         return new XmlNodeArrayList (nodeArrayList);
175                 }
176
177                 private void searchNodesRecursively (XmlNode argNode, string argName, ArrayList argArrayList)
178                 {
179                         XmlNodeList xmlNodeList = argNode.ChildNodes;
180                         foreach (XmlNode node in xmlNodeList){
181                                 if (node.Name.Equals (argName))
182                                         argArrayList.Add (node);
183                                 else    
184                                         this.searchNodesRecursively (node, argName, argArrayList);
185                         }
186                 }
187
188                 [MonoTODO]
189                 public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
190                 {
191                         throw new NotImplementedException ();
192                 }
193
194                 [MonoTODO]
195                 public virtual bool HasAttribute (string name)
196                 {
197                         XmlNode attributeNode = Attributes.GetNamedItem (name);
198                         return attributeNode != null;
199                 }
200
201                 [MonoTODO]
202                 public virtual bool HasAttribute (string localName, string namespaceURI)
203                 {
204                         throw new NotImplementedException ();
205                 }
206
207                 [MonoTODO ("Don't remove default attributes.")]
208                 public override void RemoveAll ()
209                 {
210                         // Remove the child nodes.
211                         base.RemoveAll ();
212
213                         // Remove all attributes.
214                         attributes.RemoveAll ();
215                 }
216
217                 [MonoTODO]
218                 public virtual void RemoveAllAttributes ()
219                 {
220                         throw new NotImplementedException ();
221                 }
222
223                 [MonoTODO]
224                 public virtual void RemoveAttribute (string name)
225                 {
226                         throw new NotImplementedException ();
227                 }
228
229                 [MonoTODO]
230                 public virtual void RemoveAttribute (string localName, string namespaceURI)
231                 {
232                         throw new NotImplementedException ();
233                 }
234
235                 [MonoTODO]
236                 public virtual XmlNode RemoveAttributeAt (int i)
237                 {
238                         throw new NotImplementedException ();
239                 }
240
241                 [MonoTODO]
242                 public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
243                 {
244                         throw new NotImplementedException ();
245                 }
246
247                 [MonoTODO]
248                 public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
249                 {
250                         throw new NotImplementedException ();
251                 }
252
253                 [MonoTODO]
254                 public virtual void SetAttribute (string name, string value)
255                 {
256                         XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
257                         attribute.SetParentNode (this);
258                         attribute.Value = value;
259                         Attributes.SetNamedItem (attribute);
260                 }
261
262                 [MonoTODO]
263                 public virtual string SetAttribute (string localName, string namespaceURI, string value)
264                 {
265                         throw new NotImplementedException ();
266                 }
267
268                 [MonoTODO]
269                 public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
270                 {
271                         newAttr.SetParentNode(this);
272                         XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
273                         return oldAttr != null ? oldAttr as XmlAttribute : null;
274                 }
275
276                 [MonoTODO]
277                 public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
278                 {
279                         throw new NotImplementedException ();
280                 }
281
282                 public override void WriteContentTo (XmlWriter w)
283                 {
284                         foreach(XmlNode childNode in ChildNodes)
285                                 childNode.WriteTo(w);
286                 }
287
288                 public override void WriteTo (XmlWriter w)
289                 {
290                         w.WriteStartElement(Prefix, LocalName, NamespaceURI);
291
292                         foreach(XmlNode attributeNode in Attributes)
293                                 attributeNode.WriteTo(w);
294
295                         WriteContentTo(w);
296
297                         w.WriteEndElement();
298                 }
299
300                 #endregion
301         }
302 }