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