d8b0e3451b6c61467123b592d27990d0dc09ff29
[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                 protected 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                 [MonoTODO]
177                 public virtual string Value {
178                         get { throw new NotImplementedException (); }
179                         set { throw new NotImplementedException (); }
180                 }
181
182                 #endregion
183
184                 #region Methods
185
186                 public virtual XmlNode AppendChild (XmlNode newChild)
187                 {
188                         if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute) {
189                                 XmlLinkedNode newLinkedChild = (XmlLinkedNode)newChild;
190                                 XmlLinkedNode lastLinkedChild = LastLinkedChild;
191
192                                 newLinkedChild.parentNode = this;
193                                 
194                                 if (lastLinkedChild != null) {
195                                         newLinkedChild.NextLinkedSibling = lastLinkedChild.NextLinkedSibling;
196                                         lastLinkedChild.NextLinkedSibling = newLinkedChild;
197                                 } else
198                                         newLinkedChild.NextLinkedSibling = newLinkedChild;
199                                 
200                                 LastLinkedChild = newLinkedChild;
201
202                                 return newChild;
203                         } else
204                                 throw new InvalidOperationException();
205                 }
206
207                 [MonoTODO]
208                 public virtual XmlNode Clone ()
209                 {
210                         throw new NotImplementedException ();
211                 }
212
213                 public abstract XmlNode CloneNode (bool deep);
214
215                 [MonoTODO]
216                 public XPathNavigator CreateNavigator ()
217                 {
218                         throw new NotImplementedException ();
219                 }
220
221                 public IEnumerator GetEnumerator ()
222                 {
223                         return new XmlNodeListChildren(this).GetEnumerator();
224                 }
225
226                 [MonoTODO]
227                 public virtual string GetNamespaceOfPrefix (string prefix)
228                 {
229                         throw new NotImplementedException ();
230                 }
231
232                 [MonoTODO]
233                 public virtual string GetPrefixOfNamespace (string namespaceURI)
234                 {
235                         throw new NotImplementedException ();
236                 }
237
238                 object ICloneable.Clone ()
239                 {
240                         return Clone ();
241                 }
242
243                 IEnumerator IEnumerable.GetEnumerator ()
244                 {
245                         return GetEnumerator ();
246                 }
247
248                 [MonoTODO]
249                 public virtual XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
250                 {
251                         throw new NotImplementedException ();
252                 }
253
254                 [MonoTODO]
255                 public virtual XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
256                 {
257                         throw new NotImplementedException ();
258                 }
259
260                 [MonoTODO]
261                 public virtual void Normalize ()
262                 {
263                         throw new NotImplementedException ();
264                 }
265
266                 [MonoTODO]
267                 public virtual XmlNode PrependChild (XmlNode newChild)
268                 {
269                         throw new NotImplementedException ();
270                 }
271
272                 public virtual void RemoveAll ()
273                 {
274                         LastLinkedChild = null;
275                 }
276
277                 public virtual XmlNode RemoveChild (XmlNode oldChild)
278                 {
279                         if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute) 
280                         {
281                                 if (IsReadOnly)
282                                         throw new ArgumentException();
283
284                                 if (Object.ReferenceEquals(LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals(LastLinkedChild, oldChild))
285                                         LastLinkedChild = null;
286                                 else {
287                                         XmlLinkedNode oldLinkedChild = (XmlLinkedNode)oldChild;
288                                         XmlLinkedNode beforeLinkedChild = LastLinkedChild;
289                                         
290                                         while (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, LastLinkedChild) && !Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
291                                                 beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
292
293                                         if (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
294                                                 throw new ArgumentException();
295
296                                         beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
297                                         oldLinkedChild.NextLinkedSibling = null;
298                                  }
299
300                                 return oldChild;
301                         } 
302                         else
303                                 throw new ArgumentException();
304                 }
305
306                 [MonoTODO]
307                 public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
308                 {
309                         throw new NotImplementedException ();
310                 }
311
312                 [MonoTODO]
313                 public virtual XmlNodeList SelectNodes (string xpath)
314                 {
315                         throw new NotImplementedException ();
316                 }
317
318                 [MonoTODO]
319                 public virtual XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
320                 {
321                         throw new NotImplementedException ();
322                 }
323
324                 [MonoTODO]
325                 public virtual XmlNode SelectSingleNode (string xpath)
326                 {
327                         throw new NotImplementedException ();
328                 }
329
330                 [MonoTODO]
331                 public virtual XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
332                 {
333                         throw new NotImplementedException ();
334                 }
335
336                 internal void SetParentNode (XmlNode parent)
337                 {
338                         parentNode = parent;
339                 }
340
341                 [MonoTODO]
342                 public virtual bool Supports (string feature, string version)
343                 {
344                         throw new NotImplementedException ();
345                 }
346
347                 public abstract void WriteContentTo (XmlWriter w);
348
349                 public abstract void WriteTo (XmlWriter w);
350
351                 #endregion
352         }
353 }