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