2003-03-19 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / System.Xml / XmlDocument.cs
1 //
2 // System.Xml.XmlDocument
3 //
4 // Authors:
5 //   Daniel Weber (daniel-weber@austin.rr.com)
6 //   Kral Ferch <kral_ferch@hotmail.com>
7 //   Jason Diamond <jason@injektilo.org>
8 //   Miguel de Icaza (miguel@ximian.com)
9 //   Duncan Mak (duncan@ximian.com)
10 //   Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
11 //
12 // (C) 2001 Daniel Weber
13 // (C) 2002 Kral Ferch, Jason Diamond, Miguel de Icaza, Duncan Mak,
14 //   Atsushi Enomoto
15 //
16
17 using System;
18 using System.IO;
19 using System.Text;
20 using System.Xml.XPath;
21 using System.Diagnostics;
22 using System.Collections;
23 using Mono.Xml;
24 using Mono.Xml.Native;
25
26 namespace System.Xml
27 {
28         public class XmlDocument : XmlNode
29         {
30                 #region Fields
31
32                 XmlLinkedNode lastLinkedChild;
33                 XmlNameTable nameTable;
34                 string baseURI = String.Empty;
35                 XmlImplementation implementation;
36                 bool preserveWhitespace = false;
37                 WeakReference reusableXmlTextReader;
38
39                 #endregion
40
41                 #region Constructors
42
43                 public XmlDocument () : this (null, null)
44                 {
45                 }
46
47                 protected internal XmlDocument (XmlImplementation imp) : this (imp, null)
48                 {
49                 }
50
51                 public XmlDocument (XmlNameTable nt) : this (null, nt)
52                 {
53                 }
54
55                 XmlDocument (XmlImplementation impl, XmlNameTable nt) : base (null)
56                 {
57                         implementation = (impl != null) ? impl : new XmlImplementation ();
58                         nameTable = (nt != null) ? nt : implementation.internalNameTable;
59                         AddDefaultNameTableKeys ();
60                 }
61                 #endregion
62
63                 #region Events
64
65                 public event XmlNodeChangedEventHandler NodeChanged;
66
67                 public event XmlNodeChangedEventHandler NodeChanging;
68
69                 public event XmlNodeChangedEventHandler NodeInserted;
70
71                 public event XmlNodeChangedEventHandler NodeInserting;
72
73                 public event XmlNodeChangedEventHandler NodeRemoved;
74
75                 public event XmlNodeChangedEventHandler NodeRemoving;
76
77                 #endregion
78
79                 #region Properties
80
81                 public override string BaseURI {
82                         get {
83                                 return baseURI;
84                         }
85                 }
86
87                 // Used to read 'InnerXml's for its descendants at any place.
88                 internal XmlTextReader ReusableReader {
89                         get {
90                                 if(reusableXmlTextReader == null)
91                                         reusableXmlTextReader = new WeakReference (null);
92                                 if(!reusableXmlTextReader.IsAlive) {
93                                         XmlTextReader reader = new XmlTextReader ((TextReader)null);
94                                         reusableXmlTextReader.Target = reader;
95                                 }
96                                 return (XmlTextReader)reusableXmlTextReader.Target;
97                         }
98                 }
99
100                 public XmlElement DocumentElement {
101                         get {
102                                 XmlNode node = FirstChild;
103
104                                 while (node != null) {
105                                         if (node is XmlElement)
106                                                 break;
107                                         node = node.NextSibling;
108                                 }
109
110                                 return node != null ? node as XmlElement : null;
111                         }
112                 }
113
114                 [MonoTODO("It doesn't have internal subset object model.")]
115                 public virtual XmlDocumentType DocumentType {
116                         get {
117                                 foreach(XmlNode n in this.ChildNodes) {
118                                         if(n.NodeType == XmlNodeType.DocumentType)
119                                                 return (XmlDocumentType)n;
120                                 }
121                                 return null;
122                         }
123                 }
124
125                 public XmlImplementation Implementation {
126                         get { return implementation; }
127                 }
128
129                 public override string InnerXml {
130                         get {
131                                 return base.InnerXml;
132                         }
133                         set {   // reason for overriding
134                                 this.LoadXml (value);
135                         }
136                 }
137
138                 public override bool IsReadOnly {
139                         get { return false; }
140                 }
141
142                 internal override XmlLinkedNode LastLinkedChild {
143                         get     {
144                                 return lastLinkedChild;
145                         }
146
147                         set {
148                                 lastLinkedChild = value;
149                         }
150                 }
151                 
152                 public override string LocalName {
153                         get { return "#document"; }
154                 }
155
156                 public override string Name {
157                         get { return "#document"; }
158                 }
159
160                 public XmlNameTable NameTable {
161                         get { return nameTable; }
162                 }
163
164                 public override XmlNodeType NodeType {
165                         get { return XmlNodeType.Document; }
166                 }
167
168                 internal override XPathNodeType XPathNodeType {
169                         get {
170                                 return XPathNodeType.Root;
171                         }
172                 }
173
174                 public override XmlDocument OwnerDocument {
175                         get { return null; }
176                 }
177
178                 public bool PreserveWhitespace {
179                         get { return preserveWhitespace; }
180                         set { preserveWhitespace = value; }
181                 }
182
183                 internal override string XmlLang {
184                         get { return String.Empty; }
185                 }
186
187                 [MonoTODO]
188                 public virtual XmlResolver XmlResolver {
189                         set { throw new NotImplementedException (); }
190                 }
191
192                 internal override XmlSpace XmlSpace {
193                         get {
194                                 return XmlSpace.None;
195                         }
196                 }
197
198                 #endregion
199
200                 #region Methods
201
202                 [MonoTODO("Should BaseURI be cloned?")]
203                 public override XmlNode CloneNode (bool deep)
204                 {
205                         XmlDocument doc = implementation.CreateDocument ();
206                         doc.PreserveWhitespace = PreserveWhitespace;    // required?
207                         if(deep)
208                         {
209                                 foreach(XmlNode n in ChildNodes)
210                                         doc.AppendChild (doc.ImportNode (n, deep));
211                         }
212                         return doc;
213                 }
214
215                 public XmlAttribute CreateAttribute (string name)
216                 {
217                         return CreateAttribute (name, String.Empty);
218                 }
219
220                 public XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI)
221                 {
222                         string prefix;
223                         string localName;
224
225                         ParseName (qualifiedName, out prefix, out localName);
226
227                         return CreateAttribute (prefix, localName, namespaceURI);
228                 }
229
230                 public virtual XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI)
231                 {
232                         if ((localName == null) || (localName == String.Empty))
233                                 throw new ArgumentException ("The attribute local name cannot be empty.");
234
235                         return new XmlAttribute (prefix, localName, namespaceURI, this);
236                 }
237
238                 public virtual XmlCDataSection CreateCDataSection (string data)
239                 {
240                         return new XmlCDataSection (data, this);
241                 }
242
243                 public virtual XmlComment CreateComment (string data)
244                 {
245                         return new XmlComment (data, this);
246                 }
247
248                 [MonoTODO]
249                 protected internal virtual XmlAttribute CreateDefaultAttribute (string prefix, string localName, string namespaceURI)
250                 {
251                         throw new NotImplementedException ();
252                 }
253
254                 public virtual XmlDocumentFragment CreateDocumentFragment ()
255                 {
256                         return new XmlDocumentFragment (this);
257                 }
258
259                 public virtual XmlDocumentType CreateDocumentType (string name, string publicId,
260                                                                    string systemId, string internalSubset)
261                 {
262                         return new XmlDocumentType (name, publicId, systemId, internalSubset, this);
263                 }
264
265                 public XmlElement CreateElement (string name)
266                 {
267                         return CreateElement (name, String.Empty);
268                 }
269
270                 public XmlElement CreateElement (
271                         string qualifiedName, 
272                         string namespaceURI)
273                 {
274                         string prefix;
275                         string localName;
276
277                         ParseName (qualifiedName, out prefix, out localName);
278                         
279                         return CreateElement (prefix, localName, namespaceURI);
280                 }
281
282                 public virtual XmlElement CreateElement (
283                         string prefix,
284                         string localName,
285                         string namespaceURI)
286                 {
287                         if ((localName == null) || (localName == String.Empty))
288                                 throw new ArgumentException ("The local name for elements or attributes cannot be null or an empty string.");
289                         CheckName (localName);
290                         return new XmlElement (prefix != null ? prefix : String.Empty, localName, namespaceURI != null ? namespaceURI : String.Empty, this);
291                 }
292
293                 public virtual XmlEntityReference CreateEntityReference (string name)
294                 {
295                         return new XmlEntityReference (name, this);
296                 }
297
298                 [MonoTODO]
299                 internal protected virtual XPathNavigator CreateNavigator (XmlNode node)
300                 {
301                         throw new NotImplementedException ();
302                 }
303
304                 public virtual XmlNode CreateNode (
305                         string nodeTypeString,
306                         string name,
307                         string namespaceURI)
308                 {
309                         return CreateNode (GetNodeTypeFromString (nodeTypeString), name, namespaceURI);
310                 }
311
312                 public virtual XmlNode CreateNode (
313                         XmlNodeType type,
314                         string name,
315                         string namespaceURI)
316                 {
317                         string prefix = null;
318                         string localName = name;
319
320                         if ((type == XmlNodeType.Attribute) || (type == XmlNodeType.Element) || (type == XmlNodeType.EntityReference))
321                                 ParseName (name, out prefix, out localName);
322                         
323                         return CreateNode (type, prefix, localName, namespaceURI);
324                 }
325
326                 public virtual XmlNode CreateNode (
327                         XmlNodeType type,
328                         string prefix,
329                         string name,
330                         string namespaceURI)
331                 {
332                         switch (type) {
333                                 case XmlNodeType.Attribute: return CreateAttribute (prefix, name, namespaceURI);
334                                 case XmlNodeType.CDATA: return CreateCDataSection (null);
335                                 case XmlNodeType.Comment: return CreateComment (null);
336                                 case XmlNodeType.Document: return new XmlDocument (); // TODO - test to see which constructor to use, i.e. use existing NameTable or not.
337                                 case XmlNodeType.DocumentFragment: return CreateDocumentFragment ();
338                                 case XmlNodeType.DocumentType: return CreateDocumentType (null, null, null, null);
339                                 case XmlNodeType.Element: return CreateElement (prefix, name, namespaceURI);
340                                 case XmlNodeType.EntityReference: return CreateEntityReference (null);
341                                 case XmlNodeType.ProcessingInstruction: return CreateProcessingInstruction (null, null);
342                                 case XmlNodeType.SignificantWhitespace: return CreateSignificantWhitespace (String.Empty);
343                                 case XmlNodeType.Text: return CreateTextNode (null);
344                                 case XmlNodeType.Whitespace: return CreateWhitespace (String.Empty);
345                                 case XmlNodeType.XmlDeclaration: return CreateXmlDeclaration ("1.0", null, null);
346                                 default: throw new ArgumentOutOfRangeException(String.Format("{0}\nParameter name: {1}",
347                                                          "Specified argument was out of the range of valid values", type.ToString ()));
348                         }
349                 }
350
351                 public virtual XmlProcessingInstruction CreateProcessingInstruction (
352                         string target,
353                         string data)
354                 {
355                         return new XmlProcessingInstruction (target, data, this);
356                 }
357
358                 public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
359                 {
360                         foreach (char c in text)
361                                 if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
362                                     throw new ArgumentException ("Invalid whitespace characters.");
363                          
364                         return new XmlSignificantWhitespace (text, this);
365                 }
366
367                 public virtual XmlText CreateTextNode (string text)
368                 {
369                         return new XmlText (text, this);
370                 }
371
372                 public virtual XmlWhitespace CreateWhitespace (string text)
373                 {
374                         foreach (char c in text)
375                                 if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
376                                     throw new ArgumentException ("Invalid whitespace characters.");
377                          
378                         return new XmlWhitespace (text, this);
379                 }
380
381                 public virtual XmlDeclaration CreateXmlDeclaration (string version, string encoding,
382                                                                     string standalone)
383                 {
384                         if (version != "1.0")
385                                 throw new ArgumentException ("version string is not correct.");
386
387                         if  ((standalone != null && standalone != String.Empty) && !((standalone == "yes") || (standalone == "no")))
388                                 throw new ArgumentException ("standalone string is not correct.");
389
390                         return new XmlDeclaration (version, encoding, standalone, this);
391                 }
392
393                 [MonoTODO]
394                 public virtual XmlElement GetElementById (string elementId)
395                 {
396                         throw new NotImplementedException ();
397                 }
398
399                 public virtual XmlNodeList GetElementsByTagName (string name)
400                 {
401                         ArrayList nodeArrayList = new ArrayList ();
402                         this.searchNodesRecursively (this, name, nodeArrayList);
403                         return new XmlNodeArrayList (nodeArrayList);
404                 }
405
406                 private void searchNodesRecursively (XmlNode argNode, string argName, 
407                         ArrayList argArrayList)
408                 {
409                         XmlNodeList xmlNodeList = argNode.ChildNodes;
410                         foreach (XmlNode node in xmlNodeList){
411                                 if (node.Name.Equals (argName))
412                                         argArrayList.Add (node);
413                                 else    
414                                         this.searchNodesRecursively (node, argName, argArrayList);
415                         }
416                 }
417
418                 private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI, 
419                         ArrayList argArrayList)
420                 {
421                         XmlNodeList xmlNodeList = argNode.ChildNodes;
422                         foreach (XmlNode node in xmlNodeList){
423                                 if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
424                                         argArrayList.Add (node);
425                                 else    
426                                         this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
427                         }
428                 }
429
430                 public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
431                 {
432                         ArrayList nodeArrayList = new ArrayList ();
433                         this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
434                         return new XmlNodeArrayList (nodeArrayList);
435                 }
436
437                 private XmlNodeType GetNodeTypeFromString (string nodeTypeString)
438                 {
439                         switch (nodeTypeString) {
440                                 case "attribute": return XmlNodeType.Attribute;
441                                 case "cdatasection": return XmlNodeType.CDATA;
442                                 case "comment": return XmlNodeType.Comment;
443                                 case "document": return XmlNodeType.Document;
444                                 case "documentfragment": return XmlNodeType.DocumentFragment;
445                                 case "documenttype": return XmlNodeType.DocumentType;
446                                 case "element": return XmlNodeType.Element;
447                                 case "entityreference": return XmlNodeType.EntityReference;
448                                 case "processinginstruction": return XmlNodeType.ProcessingInstruction;
449                                 case "significantwhitespace": return XmlNodeType.SignificantWhitespace;
450                                 case "text": return XmlNodeType.Text;
451                                 case "whitespace": return XmlNodeType.Whitespace;
452                                 default:
453                                         throw new ArgumentException(String.Format("The string doesn't represent any node type : {0}.", nodeTypeString));
454                         }
455                 }
456
457                 [MonoTODO("default attributes (of imported doc); Entity; Notation")]
458                 public virtual XmlNode ImportNode (XmlNode node, bool deep)
459                 {
460                         switch(node.NodeType)
461                         {
462                                 case XmlNodeType.Attribute:
463                                         {
464                                                 XmlAttribute src_att = node as XmlAttribute;
465                                                 XmlAttribute dst_att = this.CreateAttribute (src_att.Prefix, src_att.LocalName, src_att.NamespaceURI);
466                                                 dst_att.Value = src_att.Value;  // always explicitly specified (whether source is specified or not)
467                                                 return dst_att;
468                                         }
469
470                                 case XmlNodeType.CDATA:
471                                         return this.CreateCDataSection (node.Value);
472
473                                 case XmlNodeType.Comment:
474                                         return this.CreateComment (node.Value);
475
476                                 case XmlNodeType.Document:
477                                         throw new XmlException ("Document cannot be imported.");
478
479                                 case XmlNodeType.DocumentFragment:
480                                         {
481                                                 XmlDocumentFragment df = this.CreateDocumentFragment ();
482                                                 if(deep)
483                                                 {
484                                                         foreach(XmlNode n in node.ChildNodes)
485                                                         {
486                                                                 df.AppendChild (this.ImportNode (n, deep));
487                                                         }
488                                                 }
489                                                 return df;
490                                         }
491
492                                 case XmlNodeType.DocumentType:
493                                         throw new XmlException ("DocumentType cannot be imported.");
494
495                                 case XmlNodeType.Element:
496                                         {
497                                                 XmlElement src = (XmlElement)node;
498                                                 XmlElement dst = this.CreateElement (src.Prefix, src.LocalName, src.NamespaceURI);
499                                                 foreach(XmlAttribute attr in src.Attributes)
500                                                 {
501                                                         if(attr.Specified)      // copies only specified attributes
502                                                                 dst.SetAttributeNode ((XmlAttribute)this.ImportNode (attr, deep));
503                                                         if(DocumentType != null)
504                                                         {
505                                                                 // TODO: create default attribute values
506                                                         }
507                                                 }
508                                                 if(deep)
509                                                 {
510                                                         foreach(XmlNode n in src.ChildNodes)
511                                                                 dst.AppendChild (this.ImportNode (n, deep));
512                                                 }
513                                                 return dst;
514                                         }
515
516                                 case XmlNodeType.EndElement:
517                                         throw new XmlException ("Illegal ImportNode call for NodeType.EndElement");
518                                 case XmlNodeType.EndEntity:
519                                         throw new XmlException ("Illegal ImportNode call for NodeType.EndEntity");
520
521                                 case XmlNodeType.Entity:
522                                         throw new NotImplementedException ();   // TODO
523
524                                 case XmlNodeType.EntityReference:
525                                         return this.CreateEntityReference (node.Name);
526
527                                 case XmlNodeType.None:
528                                         throw new XmlException ("Illegal ImportNode call for NodeType.None");
529
530                                 case XmlNodeType.Notation:
531                                         throw new NotImplementedException ();   // TODO
532
533                                 case XmlNodeType.ProcessingInstruction:
534                                         XmlProcessingInstruction pi = node as XmlProcessingInstruction;
535                                         return this.CreateProcessingInstruction (pi.Target, pi.Data);
536
537                                 case XmlNodeType.SignificantWhitespace:
538                                         return this.CreateSignificantWhitespace (node.Value);
539
540                                 case XmlNodeType.Text:
541                                         return this.CreateTextNode (node.Value);
542
543                                 case XmlNodeType.Whitespace:
544                                         return this.CreateWhitespace (node.Value);
545
546                                 case XmlNodeType.XmlDeclaration:
547                                         XmlDeclaration srcDecl = node as XmlDeclaration;
548                                         return this.CreateXmlDeclaration (srcDecl.Version, srcDecl.Encoding, srcDecl.Standalone);
549
550                                 default:
551                                         throw new NotImplementedException ();
552                         }
553                 }
554
555                 public virtual void Load (Stream inStream)
556                 {
557                         Load (new XmlTextReader (new XmlStreamReader (inStream)));
558                 }
559
560                 public virtual void Load (string filename)
561                 {
562                         Load (new XmlTextReader (filename));
563                 }
564
565                 public virtual void Load (TextReader txtReader)
566                 {
567                         Load (new XmlTextReader (txtReader));
568                 }
569
570                 public virtual void Load (XmlReader xmlReader)
571                 {
572                         // Reset our document
573                         // For now this just means removing all our children but later this
574                         // may turn out o need to call a private method that resets other things
575                         // like properties we have, etc.
576                         RemoveAll ();
577
578                         // create all contents with use of ReadNode()
579                         do {
580                                 XmlNode n = ReadNode (xmlReader);
581                                 if(n == null) break;
582                                 AppendChild (n);
583                         } while (true);
584                 }
585
586                 public virtual void LoadXml (string xml)
587                 {
588                         XmlReader xmlReader = new XmlTextReader (new StringReader (xml));
589                         Load (xmlReader);
590                 }
591
592                 internal void onNodeChanged (XmlNode node, XmlNode Parent)
593                 {
594                         if (NodeChanged != null)
595                                 NodeChanged (node, new XmlNodeChangedEventArgs
596                                         (XmlNodeChangedAction.Change,
597                                         node, Parent, Parent));
598                 }
599
600                 internal void onNodeChanging(XmlNode node, XmlNode Parent)
601                 {
602                         if (NodeChanging != null)
603                                 NodeChanging (node, new XmlNodeChangedEventArgs
604                                         (XmlNodeChangedAction.Change,
605                                         node, Parent, Parent));
606                 }
607
608                 internal void onNodeInserted (XmlNode node, XmlNode newParent)
609                 {
610                         if (NodeInserted != null)
611                                 NodeInserted (node, new XmlNodeChangedEventArgs
612                                         (XmlNodeChangedAction.Insert,
613                                         node, null, newParent));
614                 }
615
616                 internal void onNodeInserting (XmlNode node, XmlNode newParent)
617                 {
618                         if (NodeInserting != null)
619                                 NodeInserting (node, new XmlNodeChangedEventArgs
620                                         (XmlNodeChangedAction.Insert,
621                                         node, null, newParent));
622                 }
623
624                 internal void onNodeRemoved (XmlNode node, XmlNode oldParent)
625                 {
626                         if (NodeRemoved != null)
627                                 NodeRemoved (node, new XmlNodeChangedEventArgs
628                                         (XmlNodeChangedAction.Remove,
629                                         node, oldParent, null));
630                 }
631
632                 internal void onNodeRemoving (XmlNode node, XmlNode oldParent)
633                 {
634                         if (NodeRemoving != null)
635                                 NodeRemoving (node, new XmlNodeChangedEventArgs
636                                         (XmlNodeChangedAction.Remove,
637                                         node, oldParent, null));
638                 }
639
640                 private void ParseName (string name, out string prefix, out string localName)
641                 {
642                         int indexOfColon = name.IndexOf (':');
643                         
644                         if (indexOfColon != -1) {
645                                 prefix = name.Substring (0, indexOfColon);
646                                 localName = name.Substring (indexOfColon + 1);
647                         } else {
648                                 prefix = "";
649                                 localName = name;
650                         }
651                 }
652
653                 // Checks that Element's name is valid
654                 private void CheckName (String name)
655                 {
656                         // TODO: others validations?
657                         if (name.IndexOf (" ") >= 0)
658                                 throw new XmlException ("The ' ' characted cannot be included in a name");
659                 }
660
661                 // Reads XmlReader and creates Attribute Node.
662                 private XmlAttribute ReadAttributeNode(XmlReader reader)
663                 {
664                         if(reader.NodeType == XmlNodeType.Element)
665                                 reader.MoveToFirstAttribute ();
666                         else if(reader.NodeType != XmlNodeType.Attribute)
667                                 throw new InvalidOperationException ("bad position to read attribute.");
668                         XmlAttribute attribute = CreateAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
669                         ReadAttributeNodeValue (reader, attribute);
670                         return attribute;
671                 }
672
673                 // Reads attribute from XmlReader and then creates attribute value children. XmlAttribute also uses this.
674                 internal void ReadAttributeNodeValue(XmlReader reader, XmlAttribute attribute)
675                 {
676                         while(reader.ReadAttributeValue ()) {
677                                 if(reader.NodeType == XmlNodeType.EntityReference)
678                                         // FIXME: if DocumentType is available, then try to resolve it.
679                                         attribute.AppendChild (CreateEntityReference (reader.Name));
680                                 // FIXME: else if(NodeType == EndEntity) -- reset BaseURI and so on -- ;
681                                 else
682                                         // (IMHO) Children of Attribute is likely restricted to Text and EntityReference.
683                                         attribute.AppendChild (CreateTextNode (reader.Value));
684                         }
685                 }
686
687                 [MonoTODO ("Child of entity is not simple Value string;Get prefix of NotationDecl")]
688                 public virtual XmlNode ReadNode(XmlReader reader)
689                 {
690                         XmlNode resultNode = null;
691                         XmlNode newNode = null;
692                         XmlNode currentNode = null;
693
694                         if (reader.ReadState == ReadState.Initial)
695                                 reader.Read ();
696
697                         int startDepth = reader.Depth;
698                         bool ignoredWhitespace;
699                         bool reachedEOF = false;
700
701                         do {
702                                 ignoredWhitespace = false;
703                                 if (reader.NodeType == XmlNodeType.None)
704                                         if (reachedEOF)
705                                                 throw new Exception ("XML Reader reached to end while reading node.");
706                                         else
707                                                 reachedEOF = true;
708                                 switch (reader.NodeType) {
709
710                                 case XmlNodeType.Attribute:
711                                         newNode = ReadAttributeNode (reader);
712                                         break;
713
714                                 case XmlNodeType.CDATA:
715                                         newNode = CreateCDataSection (reader.Value);
716                                         if(currentNode != null)
717                                                 currentNode.AppendChild (newNode);
718                                         break;
719
720                                 case XmlNodeType.Comment:
721                                         newNode = CreateComment (reader.Value);
722                                         if(currentNode != null)
723                                                 currentNode.AppendChild (newNode);
724                                         break;
725
726                                 case XmlNodeType.Element:
727                                         XmlElement element = CreateElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
728                                         element.IsEmpty = reader.IsEmptyElement;
729                                         if(currentNode != null)
730                                                 currentNode.AppendChild (element);
731                                         else
732                                                 resultNode = element;
733
734                                         // set the element's attributes.
735                                         while (reader.MoveToNextAttribute ()) {
736                                                 element.SetAttributeNode (ReadAttributeNode (reader));
737                                         }
738
739                                         reader.MoveToElement ();
740
741                                         if (!reader.IsEmptyElement)
742                                                 currentNode = element;
743
744                                         break;
745
746                                 case XmlNodeType.EndElement:
747                                         if(currentNode == null || currentNode.Name != reader.Name)
748                                                 throw new XmlException ("mismatch end tag.");
749                                         currentNode = currentNode.ParentNode;
750                                         break;
751
752                                 case XmlNodeType.EndEntity:
753                                         break;  // no operation
754
755                                 case XmlNodeType.ProcessingInstruction:
756                                         newNode = CreateProcessingInstruction (reader.Name, reader.Value);
757                                         if(currentNode != null)
758                                                 currentNode.AppendChild (newNode);
759                                         break;
760
761                                 case XmlNodeType.Text:
762                                         newNode = CreateTextNode (reader.Value);
763                                         if(currentNode != null)
764                                                 currentNode.AppendChild (newNode);
765                                         break;
766
767                                 case XmlNodeType.XmlDeclaration:
768                                         // empty strings are dummy, then gives over setting value contents to setter.
769                                         newNode = CreateXmlDeclaration ("1.0" , String.Empty, String.Empty);
770                                         ((XmlDeclaration)newNode).Value = reader.Value;
771                                         if(currentNode != null)
772                                                 throw new XmlException ("XmlDeclaration at invalid position.");
773                                         break;
774
775                                 case XmlNodeType.DocumentType:
776                                         // hack ;-)
777                                         XmlTextReader xtReader = reader as XmlTextReader;
778                                         if(xtReader == null) {
779                                                 xtReader = new XmlTextReader (reader.ReadOuterXml (),
780                                                         XmlNodeType.DocumentType,
781                                                         new XmlParserContext (NameTable, ConstructNamespaceManager(), XmlLang, XmlSpace));
782                                                 xtReader.Read ();
783                                         }
784                                         newNode = ReadDoctypeNode (xtReader);
785                                         if(currentNode != null)
786                                                 throw new XmlException ("XmlDocumentType at invalid position.");
787                                         break;
788
789                                 case XmlNodeType.EntityReference:
790                                         newNode = CreateEntityReference (reader.Name);
791                                         if(currentNode != null)
792                                                 currentNode.AppendChild (newNode);
793                                         break;
794
795                                 case XmlNodeType.SignificantWhitespace:
796                                         newNode = CreateSignificantWhitespace (reader.Value);
797                                         if(currentNode != null)
798                                                 currentNode.AppendChild (newNode);
799                                         break;
800
801                                 case XmlNodeType.Whitespace:
802                                         if(PreserveWhitespace) {
803                                                 newNode = CreateWhitespace (reader.Value);
804                                                 if(currentNode != null)
805                                                         currentNode.AppendChild (newNode);
806                                         }
807                                         else
808                                                 ignoredWhitespace = true;
809                                         break;
810                                 }
811                                 reader.Read ();
812                         } while (ignoredWhitespace || reader.Depth > startDepth ||
813                                 (reader.Depth == startDepth && reader.NodeType == XmlNodeType.EndElement));
814                         return resultNode != null ? resultNode : newNode;
815                 }
816
817                 private XmlDocumentType ReadDoctypeNode (XmlTextReader xtReader)
818                 {
819                         XmlDocumentType doctype = CreateDocumentType (xtReader.Name,
820                                 xtReader.GetAttribute ("PUBLIC"),
821                                 xtReader.GetAttribute ("SYSTEM"),
822                                 xtReader.Value);
823                         DTDObjectModel subset = xtReader.currentSubset;
824                         foreach (DTDEntityDeclaration decl in subset.EntityDecls.Values) {
825                                 XmlNode n = new XmlEntity (decl.Name, decl.NotationName,
826                                         decl.PublicId, decl.SystemId, this);
827                                 // FIXME: Value is more complex, similar to Attribute.
828                                 n.insertBeforeIntern (this.CreateTextNode (decl.EntityValue), null);
829                                 doctype.entities.Nodes.Add (n);
830                         }
831                         foreach (DTDNotationDeclaration decl in subset.NotationDecls.Values) {
832                                 XmlNode n = new XmlNotation (decl.LocalName, decl.Prefix,
833                                         decl.PublicId, decl.SystemId, this);
834                                 doctype.notations.Nodes.Add (n);
835                         }
836                         foreach (DTDElementDeclaration decl in subset.ElementDecls.Values) {
837                                 doctype.elementDecls.Add (decl.Name, decl.Clone ());
838                         }
839                         foreach (DTDAttListDeclaration decl in subset.AttListDecls.Values) {
840                                 doctype.attListDecls.Add (decl.Name, decl.Clone ());
841                         }
842                         return doctype;
843                 }
844
845                 public virtual void Save(Stream outStream)
846                 {
847                         XmlTextWriter xmlWriter = new XmlTextWriter (outStream, Encoding.UTF8);
848                         xmlWriter.Formatting = Formatting.Indented;
849                         WriteContentTo (xmlWriter);
850                         xmlWriter.Close ();
851                 }
852
853                 public virtual void Save (string filename)
854                 {
855                         XmlTextWriter xmlWriter = new XmlTextWriter (filename, Encoding.UTF8);
856                         xmlWriter.Formatting = Formatting.Indented;
857                         WriteContentTo (xmlWriter);
858                         xmlWriter.Close ();
859                 }
860
861                 [MonoTODO]
862                 public virtual void Save (TextWriter writer)
863                 {
864                         XmlTextWriter xmlWriter = new XmlTextWriter (writer);
865                         xmlWriter.Formatting = Formatting.Indented;
866                         WriteContentTo (xmlWriter);
867                         xmlWriter.Flush ();
868                 }
869
870                 public virtual void Save (XmlWriter xmlWriter)
871                 {
872                         //
873                         // This should preserve white space if PreserveWhiteSpace is true
874                         //
875                         WriteContentTo (xmlWriter);
876                         xmlWriter.Flush ();
877                 }
878
879                 public override void WriteContentTo (XmlWriter w)
880                 {
881                         foreach(XmlNode childNode in ChildNodes) {
882                                 childNode.WriteTo (w);
883                         }
884                 }
885
886                 public override void WriteTo (XmlWriter w)
887                 {
888                         WriteContentTo (w);
889                 }
890
891                 private void AddDefaultNameTableKeys ()
892                 {
893                         // The following keys are default of MS .NET Framework
894                         nameTable.Add ("#text");
895                         nameTable.Add ("xml");
896                         nameTable.Add ("xmlns");
897                         nameTable.Add ("#entity");
898                         nameTable.Add ("#document-fragment");
899                         nameTable.Add ("#comment");
900                         nameTable.Add ("space");
901                         nameTable.Add ("id");
902                         nameTable.Add ("#whitespace");
903                         nameTable.Add ("http://www.w3.org/2000/xmlns/");
904                         nameTable.Add ("#cdata-section");
905                         nameTable.Add ("lang");
906                         nameTable.Add ("#document");
907                         nameTable.Add ("#significant-whitespace");
908                 }
909                 #endregion
910         }
911 }