minor CLS conformance tweaks (visibility, virtual, abstract, sealed, etc...)
[mono.git] / mcs / class / System.XML / System.Xml / XmlNode.cs
1 //
2 // System.Xml.XmlNode
3 //
4 // Author:
5 //   Kral Ferch <kral_ferch@hotmail.com>
6 //
7 // (C) 2002 Kral Ferch
8 //
9
10 using System;
11 using System.Collections;
12 using System.IO;
13 using System.Xml.XPath;
14
15 namespace System.Xml
16 {
17         public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
18         {
19                 #region Fields
20
21                 XmlDocument ownerDocument;
22                 XmlNode parentNode;
23
24                 #endregion
25
26                 #region Constructors
27
28                 internal XmlNode (XmlDocument ownerDocument)
29                 {
30                         this.ownerDocument = ownerDocument;
31                 }
32
33                 #endregion
34
35                 #region Properties
36
37                 public virtual XmlAttributeCollection Attributes
38                 {
39                         get { return null; }
40                 }
41
42                 [MonoTODO]
43                 public virtual string BaseURI
44                 {
45                         get { throw new NotImplementedException (); }
46                 }
47
48                 public virtual XmlNodeList ChildNodes {
49                         get {
50                                 return new XmlNodeListChildren(this);
51                         }
52                 }
53
54                 public virtual XmlNode FirstChild {
55                         get {
56                                 if (LastChild != null) {
57                                         return LastLinkedChild.NextLinkedSibling;
58                                 }
59                                 else {
60                                         return null;
61                                 }
62                         }
63                 }
64
65                 public virtual bool HasChildNodes {
66                         get { return LastChild != null; }
67                 }
68
69                 [MonoTODO]
70                 public virtual string InnerText {
71                         get { throw new NotImplementedException (); }
72                         set { throw new NotImplementedException (); }
73                 }
74
75                 [MonoTODO("Setter.")]
76                 public virtual string InnerXml {
77                         get {
78                                 StringWriter sw = new StringWriter ();
79                                 XmlTextWriter xtw = new XmlTextWriter (sw);
80
81                                 WriteContentTo(xtw);
82
83                                 return sw.GetStringBuilder().ToString();
84                         }
85
86                         set { throw new NotImplementedException (); }
87                 }
88
89                 public virtual bool IsReadOnly {
90                         get { return false; }
91                 }
92
93                 [System.Runtime.CompilerServices.IndexerName("Item")]
94                 public virtual XmlElement this [string name] {
95                         get { 
96                                 foreach (XmlNode node in ChildNodes) {
97                                         if ((node.NodeType == XmlNodeType.Element) &&
98                                             (node.Name == name)) {
99                                                 return (XmlElement) node;
100                                         }
101                                 }
102
103                                 return null;
104                         }
105                 }
106
107                 [System.Runtime.CompilerServices.IndexerName("Item")]
108                 public virtual XmlElement this [string localname, string ns] {
109                         get { 
110                                 foreach (XmlNode node in ChildNodes) {
111                                         if ((node.NodeType == XmlNodeType.Element) &&
112                                             (node.LocalName == localname) && 
113                                             (node.NamespaceURI == ns)) {
114                                                 return (XmlElement) node;
115                                         }
116                                 }
117
118                                 return null;
119                         }
120                 }
121
122                 public virtual XmlNode LastChild {
123                         get { return LastLinkedChild; }
124                 }
125
126                 internal virtual XmlLinkedNode LastLinkedChild {
127                         get { return null; }
128                         set { }
129                 }
130
131                 public abstract string LocalName { get; }
132
133                 public abstract string Name     { get; }
134
135                 [MonoTODO]
136                 public virtual string NamespaceURI {
137                         get { throw new NotImplementedException (); }
138                 }
139
140                 public virtual XmlNode NextSibling {
141                         get { return null; }
142                 }
143
144                 public abstract XmlNodeType NodeType { get;     }
145
146                 [MonoTODO]
147                 public virtual string OuterXml {
148                         get {
149                                 StringWriter sw = new StringWriter ();
150                                 XmlTextWriter xtw = new XmlTextWriter (sw);
151
152                                 WriteTo(xtw);
153
154                                 return sw.GetStringBuilder().ToString();
155                         }
156                 }
157
158                 public virtual XmlDocument OwnerDocument {
159                         get { return ownerDocument; }
160                 }
161
162                 public virtual XmlNode ParentNode {
163                         get { return parentNode; }
164                 }
165
166                 [MonoTODO]
167                 public virtual string Prefix {
168                         get { throw new NotImplementedException (); }
169                         set { throw new NotImplementedException (); }
170                 }
171
172                 public virtual XmlNode PreviousSibling {
173                         get { return null; }
174                 }
175
176                 public virtual string Value {
177                         get { return null; }
178                         set { throw new InvalidOperationException ("This node does not have a value"); }
179                 }
180
181                 #endregion
182
183                 #region Methods
184
185                 public virtual XmlNode AppendChild (XmlNode newChild)
186                 {
187                         if (NodeType == XmlNodeType.Document
188                             || NodeType == XmlNodeType.Element
189                             || NodeType == XmlNodeType.Attribute) {
190                                 XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
191                                 XmlLinkedNode lastLinkedChild = LastLinkedChild;
192
193                                 newLinkedChild.parentNode = this;
194                                 
195                                 if (lastLinkedChild != null) {
196                                         newLinkedChild.NextLinkedSibling = lastLinkedChild.NextLinkedSibling;
197                                         lastLinkedChild.NextLinkedSibling = newLinkedChild;
198                                 } else
199                                         newLinkedChild.NextLinkedSibling = newLinkedChild;
200                                 
201                                 LastLinkedChild = newLinkedChild;
202
203                                 return newChild;
204                         } else
205                                 throw new InvalidOperationException();
206                 }
207
208                 [MonoTODO]
209                 public virtual XmlNode Clone ()
210                 {
211                         throw new NotImplementedException ();
212                 }
213
214                 public abstract XmlNode CloneNode (bool deep);
215
216                 [MonoTODO]
217                 public XPathNavigator CreateNavigator ()
218                 {
219                         throw new NotImplementedException ();
220                 }
221
222                 public IEnumerator GetEnumerator ()
223                 {
224                         return new XmlNodeListChildren(this).GetEnumerator();
225                 }
226
227                 [MonoTODO]
228                 public virtual string GetNamespaceOfPrefix (string prefix)
229                 {
230                         throw new NotImplementedException ();
231                 }
232
233                 [MonoTODO]
234                 public virtual string GetPrefixOfNamespace (string namespaceURI)
235                 {
236                         throw new NotImplementedException ();
237                 }
238
239                 object ICloneable.Clone ()
240                 {
241                         return Clone ();
242                 }
243
244                 IEnumerator IEnumerable.GetEnumerator ()
245                 {
246                         return GetEnumerator ();
247                 }
248
249                 [MonoTODO]
250                 public virtual XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
251                 {
252                         throw new NotImplementedException ();
253                 }
254
255                 [MonoTODO]
256                 public virtual XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
257                 {
258                         throw new NotImplementedException ();
259                 }
260
261                 [MonoTODO]
262                 public virtual void Normalize ()
263                 {
264                         throw new NotImplementedException ();
265                 }
266
267                 [MonoTODO]
268                 public virtual XmlNode PrependChild (XmlNode newChild)
269                 {
270                         throw new NotImplementedException ();
271                 }
272
273                 public virtual void RemoveAll ()
274                 {
275                         LastLinkedChild = null;
276                 }
277
278                 public virtual XmlNode RemoveChild (XmlNode oldChild)
279                 {
280                         if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute) 
281                         {
282                                 if (IsReadOnly)
283                                         throw new ArgumentException();
284
285                                 if (Object.ReferenceEquals(LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals(LastLinkedChild, oldChild))
286                                         LastLinkedChild = null;
287                                 else {
288                                         XmlLinkedNode oldLinkedChild = (XmlLinkedNode)oldChild;
289                                         XmlLinkedNode beforeLinkedChild = LastLinkedChild;
290                                         
291                                         while (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, LastLinkedChild) && !Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
292                                                 beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
293
294                                         if (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
295                                                 throw new ArgumentException();
296
297                                         beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
298                                         oldLinkedChild.NextLinkedSibling = null;
299                                  }
300
301                                 return oldChild;
302                         } 
303                         else
304                                 throw new ArgumentException();
305                 }
306
307                 [MonoTODO]
308                 public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
309                 {
310                         throw new NotImplementedException ();
311                 }
312
313                 [MonoTODO]
314                 public XmlNodeList SelectNodes (string xpath)
315                 {
316                         throw new NotImplementedException ();
317                 }
318
319                 [MonoTODO]
320                 public XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
321                 {
322                         throw new NotImplementedException ();
323                 }
324
325                 [MonoTODO]
326                 public XmlNode SelectSingleNode (string xpath)
327                 {
328                         throw new NotImplementedException ();
329                 }
330
331                 [MonoTODO]
332                 public XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
333                 {
334                         throw new NotImplementedException ();
335                 }
336
337                 internal void SetParentNode (XmlNode parent)
338                 {
339                         parentNode = parent;
340                 }
341
342                 [MonoTODO]
343                 public virtual bool Supports (string feature, string version)
344                 {
345                         throw new NotImplementedException ();
346                 }
347
348                 public abstract void WriteContentTo (XmlWriter w);
349
350                 public abstract void WriteTo (XmlWriter w);
351
352                 #endregion
353         }
354 }