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