2002-04-12 Duncan Mak <duncan@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml / XmlDocument.cs
index c2bd395ca80cd8526e62273039ccc4d5a7abfe41..5a6cc0827e28bbbe1c8bf3faeb89538375a64077 100644 (file)
@@ -9,19 +9,22 @@
 using System;
 using System.IO;
 using System.Xml.XPath;
+using System.Diagnostics;
 
 namespace System.Xml
 {
-       public delegate void XmlNodeChangedEventHandler (XmlNodeChangedEventArgs args);
-
        public class XmlDocument : XmlNode
        {
+               #region Fields
+
+               XmlLinkedNode lastLinkedChild;
+               XmlNameTable nameTable;
+
+               #endregion
+
                #region Constructors
 
-               public XmlDocument () : base (null)
-               {
-                       FOwnerDocument = this;
-               }
+               public XmlDocument () : base (null) { }
 
                [MonoTODO]
                protected internal XmlDocument (XmlImplementation imp) : base (null)
@@ -29,10 +32,9 @@ namespace System.Xml
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
-               public XmlDocument (NameTable nt) : base (null)
+               public XmlDocument (XmlNameTable nt) : base (null)
                {
-                       throw new NotImplementedException ();
+                       nameTable = nt;
                }
 
                #endregion
@@ -57,9 +59,7 @@ namespace System.Xml
 
                [MonoTODO]
                public override string BaseURI {
-                       get {
-                               throw new NotImplementedException();
-                       }
+                       get { throw new NotImplementedException(); }
                }
 
                public XmlElement DocumentElement {
@@ -78,82 +78,66 @@ namespace System.Xml
 
                [MonoTODO]
                public virtual XmlDocumentType DocumentType {
-                       get {
-                               throw new NotImplementedException();
-                       }
+                       get { throw new NotImplementedException(); }
                }
 
                [MonoTODO]
                public XmlImplementation Implementation {
-                       get {
-                               throw new NotImplementedException();
-                       }
+                       get { throw new NotImplementedException(); }
                }
 
-               [MonoTODO]
+               [MonoTODO ("Setter.")]
                public override string InnerXml {
                        get {
-                               throw new NotImplementedException();
-                       }
-
-                       set {
-                               throw new NotImplementedException();
+                               // Not sure why this is an override.  Passing through for now.
+                               return base.InnerXml;
                        }
+                       set { throw new NotImplementedException(); }
                }
 
                public override bool IsReadOnly {
-                       get { 
-                               return false; 
-                       }
+                       get { return false; }
                }
 
-               public override string LocalName {
-                       get 
-                               return "#document"; 
+               internal override XmlLinkedNode LastLinkedChild {
+                       get     {
+                               return lastLinkedChild;
                        }
+
+                       set {
+                               lastLinkedChild = value;
+                       }
+               }
+               
+               public override string LocalName {
+                       get { return "#document"; }
                }
 
                public override string Name {
-                       get { 
-                               return "#document"; 
-                       }
+                       get { return "#document"; }
                }
 
-               [MonoTODO]
                public XmlNameTable NameTable {
-                       get {
-                               throw new NotImplementedException();
-                       }
+                       get { return nameTable; }
                }
 
                public override XmlNodeType NodeType {
-                       get { 
-                               return XmlNodeType.Document; 
-                       }
+                       get { return XmlNodeType.Document; }
                }
 
                public override XmlDocument OwnerDocument {
-                       get { 
-                               return null; 
-                       }
+                       get { return null; }
                }
 
                [MonoTODO]
                public bool PreserveWhitespace {
-                       get {
-                               throw new NotImplementedException();
-                       }
-
-                       set {
-                               throw new NotImplementedException();
-                       }
+                       get { throw new NotImplementedException(); }
+                       set { throw new NotImplementedException(); }
                }
 
                [MonoTODO]
-               public XmlResolver XmlResolver {
-                       set {
-                               throw new NotImplementedException();
-                       }
+               public virtual XmlResolver XmlResolver {
+                       set { throw new NotImplementedException(); }
                }
 
                #endregion
@@ -166,50 +150,37 @@ namespace System.Xml
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
                public XmlAttribute CreateAttribute (string name)
                {
-                       int indexOfColon = name.IndexOf (':');
-                       
-                       if (indexOfColon == -1)
-                               return CreateAttribute (String.Empty, name, String.Empty);
-
-                       string prefix = name.Substring (0, indexOfColon);
-                       string localName = name.Substring (indexOfColon + 1);
-
-                       return CreateAttribute (prefix, localName, String.Empty);
+                       return CreateAttribute (name, String.Empty);
                }
 
-               [MonoTODO]
                public XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI)
                {
-                       int indexOfColon = qualifiedName.IndexOf (':');
-                       
-                       if (indexOfColon == -1)
-                               return CreateAttribute (String.Empty, qualifiedName, String.Empty);
+                       string prefix;
+                       string localName;
 
-                       string prefix = qualifiedName.Substring (0, indexOfColon);
-                       string localName = qualifiedName.Substring (indexOfColon + 1);
+                       ParseName (qualifiedName, out prefix, out localName);
 
-                       return CreateAttribute (prefix, localName, String.Empty);
+                       return CreateAttribute (prefix, localName, namespaceURI);
                }
 
-               [MonoTODO]
                public virtual XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI)
                {
+                       if ((localName == null) || (localName == String.Empty))
+                               throw new ArgumentException ("The attribute local name cannot be empty.");
+
                        return new XmlAttribute (prefix, localName, namespaceURI, this);
                }
 
-               [MonoTODO]
                public virtual XmlCDataSection CreateCDataSection (string data)
                {
-                       throw new NotImplementedException ();
+                       return new XmlCDataSection (data, this);
                }
 
-               [MonoTODO]
                public virtual XmlComment CreateComment (string data)
                {
-                       throw new NotImplementedException ();
+                       return new XmlComment(data, this);
                }
 
                [MonoTODO]
@@ -224,41 +195,25 @@ namespace System.Xml
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
-               public virtual XmlDocumentType CreateDocumentType (
-                       string name,
-                       string publicId,
-                       string systemId,
-                       string internalSubset)
+               public virtual XmlDocumentType CreateDocumentType (string name, string publicId,
+                                                                  string systemId, string internalSubset)
                {
-                       throw new NotImplementedException ();
+                       return new XmlDocumentType (name, publicId, systemId, internalSubset, this);
                }
 
                public XmlElement CreateElement (string name)
                {
-                       int indexOfColon = name.IndexOf (':');
-                       
-                       if (indexOfColon == -1)
-                               return CreateElement (String.Empty, name, String.Empty);
-
-                       string prefix = name.Substring (0, indexOfColon);
-                       string localName = name.Substring (indexOfColon + 1);
-
-                       return CreateElement (prefix, localName, String.Empty);
+                       return CreateElement (name, String.Empty);
                }
 
-               [MonoTODO]
                public XmlElement CreateElement (
                        string qualifiedName, 
                        string namespaceURI)
                {
-                       int indexOfColon = qualifiedName.IndexOf (':');
-                       
-                       if (indexOfColon == -1)
-                               return CreateElement (String.Empty, qualifiedName, namespaceURI);
+                       string prefix;
+                       string localName;
 
-                       string prefix = qualifiedName.Substring (0, indexOfColon);
-                       string localName = qualifiedName.Substring (indexOfColon + 1);
+                       ParseName (qualifiedName, out prefix, out localName);
 
                        return CreateElement (prefix, localName, namespaceURI);
                }
@@ -268,6 +223,9 @@ namespace System.Xml
                        string localName,
                        string namespaceURI)
                {
+                       if ((localName == null) || (localName == String.Empty))
+                               throw new ArgumentException ("The local name for elements or attributes cannot be null or an empty string.");
+
                        return new XmlElement (prefix, localName, namespaceURI, this);
                }
 
@@ -283,46 +241,67 @@ namespace System.Xml
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
                public virtual XmlNode CreateNode (
                        string nodeTypeString,
                        string name,
                        string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       return CreateNode (GetNodeTypeFromString (nodeTypeString), name, namespaceURI);
                }
 
-               [MonoTODO]
                public virtual XmlNode CreateNode (
                        XmlNodeType type,
                        string name,
                        string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       string prefix = null;
+                       string localName = name;
+
+                       if ((type == XmlNodeType.Attribute) || (type == XmlNodeType.Element) || (type == XmlNodeType.EntityReference))
+                               ParseName (name, out prefix, out localName);
+                       
+                       return CreateNode (type, prefix, localName, namespaceURI);
                }
 
-               [MonoTODO]
                public virtual XmlNode CreateNode (
                        XmlNodeType type,
                        string prefix,
                        string name,
                        string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       switch (type) {
+                               case XmlNodeType.Attribute: return CreateAttribute (prefix, name, namespaceURI);
+                               case XmlNodeType.CDATA: return CreateCDataSection (null);
+                               case XmlNodeType.Comment: return CreateComment (null);
+                               case XmlNodeType.Document: return new XmlDocument (); // TODO - test to see which constructor to use, i.e. use existing NameTable or not.
+                               case XmlNodeType.DocumentFragment: return CreateDocumentFragment ();
+                               case XmlNodeType.DocumentType: return CreateDocumentType (null, null, null, null);
+                               case XmlNodeType.Element: return CreateElement (prefix, name, namespaceURI);
+                               case XmlNodeType.EntityReference: return CreateEntityReference (null);
+                               case XmlNodeType.ProcessingInstruction: return CreateProcessingInstruction (null, null);
+                               case XmlNodeType.SignificantWhitespace: return CreateSignificantWhitespace (String.Empty);
+                               case XmlNodeType.Text: return CreateTextNode (null);
+                               case XmlNodeType.Whitespace: return CreateWhitespace (String.Empty);
+                               case XmlNodeType.XmlDeclaration: return CreateXmlDeclaration ("1.0", null, null);
+                               default: throw new ArgumentOutOfRangeException(String.Format("{0}\nParameter name: {1}",
+                                                        "Specified argument was out of the range of valid values", type.ToString ()));
+                       }
                }
 
-               [MonoTODO]
                public virtual XmlProcessingInstruction CreateProcessingInstruction (
                        string target,
                        string data)
                {
-                       throw new NotImplementedException();
+                       return new XmlProcessingInstruction (target, data, this);
                }
 
-               [MonoTODO]
-               public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text        )
+               public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
                {
-                       throw new NotImplementedException ();
+                       foreach (char c in text)
+                               if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
+                                   throw new ArgumentException ("Invalid whitespace characters.");
+                        
+                       return new XmlSignificantWhitespace (text, this);
                }
 
                public virtual XmlText CreateTextNode (string text)
@@ -330,19 +309,25 @@ namespace System.Xml
                        return new XmlText (text, this);
                }
 
-               [MonoTODO]
                public virtual XmlWhitespace CreateWhitespace (string text)
                {
-                       throw new NotImplementedException ();
+                       foreach (char c in text)
+                               if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
+                                   throw new ArgumentException ("Invalid whitespace characters.");
+                        
+                       return new XmlWhitespace (text, this);
                }
 
-               [MonoTODO]
-               public virtual XmlDeclaration CreateXmlDeclaration (
-                       string version,
-                       string encoding,
-                       string standalone)
+               public virtual XmlDeclaration CreateXmlDeclaration (string version, string encoding,
+                                                                   string standalone)
                {
-                       throw new NotImplementedException();
+                       if (version != "1.0")
+                               throw new ArgumentException ("version string is not correct.");
+
+                       if  ((standalone != null) && !((standalone == "yes") || (standalone == "no")))
+                               throw new ArgumentException ("standalone string is not correct.");
+                       
+                       return new XmlDeclaration (version, encoding, standalone, this);
                }
 
                [MonoTODO]
@@ -363,6 +348,26 @@ namespace System.Xml
                        throw new NotImplementedException();
                }
 
+               private XmlNodeType GetNodeTypeFromString (string nodeTypeString)
+               {
+                       switch (nodeTypeString) {
+                               case "attribute": return XmlNodeType.Attribute;
+                               case "cdatasection": return XmlNodeType.CDATA;
+                               case "comment": return XmlNodeType.Comment;
+                               case "document": return XmlNodeType.Document;
+                               case "documentfragment": return XmlNodeType.DocumentFragment;
+                               case "documenttype": return XmlNodeType.DocumentType;
+                               case "element": return XmlNodeType.Element;
+                               case "entityreference": return XmlNodeType.EntityReference;
+                               case "processinginstruction": return XmlNodeType.ProcessingInstruction;
+                               case "significantwhitespace": return XmlNodeType.SignificantWhitespace;
+                               case "text": return XmlNodeType.Text;
+                               case "whitespace": return XmlNodeType.Whitespace;
+                               default:
+                                       throw new ArgumentException(String.Format("The string doesn't represent any node type : {0}.", nodeTypeString));
+                       }
+               }
+
                [MonoTODO]
                public virtual XmlNode ImportNode (XmlNode node, bool deep)
                {
@@ -396,33 +401,51 @@ namespace System.Xml
                        RemoveAll ();
 
                        XmlNode currentNode = this;
+                       XmlNode newNode;
 
                        while (xmlReader.Read ()) 
                        {
                                switch (xmlReader.NodeType) {
 
+                               case XmlNodeType.CDATA:
+                                       newNode = CreateCDataSection(xmlReader.Value);
+                                       currentNode.AppendChild (newNode);
+                                       break;
+
+                               case XmlNodeType.Comment:
+                                       newNode = CreateComment (xmlReader.Value);
+                                       currentNode.AppendChild (newNode);
+                                       break;
+
                                case XmlNodeType.Element:
-                                       XmlElement element = CreateElement (xmlReader.Name, xmlReader.LocalName, xmlReader.NamespaceURI);
+                                       XmlElement element = CreateElement (xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
                                        currentNode.AppendChild (element);
 
                                        // set the element's attributes.
                                        while (xmlReader.MoveToNextAttribute ())
                                                element.SetAttribute (xmlReader.Name, xmlReader.Value);
 
+                                       xmlReader.MoveToElement ();
+
                                        // if this element isn't empty, push it onto our "stack".
                                        if (!xmlReader.IsEmptyElement)
                                                currentNode = element;
 
                                        break;
 
-                               case XmlNodeType.Text:
-                                       XmlText text = CreateTextNode (xmlReader.Value);
-                                       currentNode.AppendChild (text);
-                                       break;
-
                                case XmlNodeType.EndElement:
                                        currentNode = currentNode.ParentNode;
                                        break;
+
+                               case XmlNodeType.ProcessingInstruction:
+                                       newNode = CreateProcessingInstruction (xmlReader.Name, xmlReader.Value);
+                                       currentNode.AppendChild (newNode);
+                                       break;
+
+                               case XmlNodeType.Text:
+                                       newNode = CreateTextNode (xmlReader.Value);
+                                       currentNode.AppendChild (newNode);
+                                       break;
                                }
                        }
                }
@@ -433,88 +456,106 @@ namespace System.Xml
                        Load (xmlReader);
                }
 
-               [MonoTODO]
-               public virtual XmlNode ReadNode(XmlReader reader)
+               internal void onNodeChanged (XmlNode node, XmlNode Parent)
                {
-                       throw new NotImplementedException ();
+                       if (NodeChanged != null)
+                               NodeInserted (node, new XmlNodeChangedEventArgs
+                                       (XmlNodeChangedAction.Change,
+                                       node, Parent, Parent));
                }
 
-               [MonoTODO]
-               public virtual void Save(Stream outStream)
+               internal void onNodeChanging(XmlNode node, XmlNode Parent)
                {
-                       throw new NotImplementedException ();
+                       if (NodeInserting != null)
+                               NodeChanging (node, new XmlNodeChangedEventArgs
+                                       (XmlNodeChangedAction.Change,
+                                       node, Parent, Parent));
                }
 
-               [MonoTODO]
-               public virtual void Save (string filename)
+               internal void onNodeInserted (XmlNode node, XmlNode newParent)
                {
-                       throw new NotImplementedException ();
+                       if (NodeInserted != null)
+                               NodeInserted (node, new XmlNodeChangedEventArgs
+                                       (XmlNodeChangedAction.Insert,
+                                       node, null, newParent));
                }
 
-               [MonoTODO]
-               public virtual void Save (TextWriter writer)
+               internal void onNodeInserting (XmlNode node, XmlNode newParent)
                {
-                       throw new NotImplementedException ();
+                       if (NodeInserting != null)
+                               NodeInserting (node, new XmlNodeChangedEventArgs
+                                       (XmlNodeChangedAction.Insert,
+                                       node, null, newParent));
                }
 
-               [MonoTODO]
-               public virtual void Save (XmlWriter writer)
+               internal void onNodeRemoved (XmlNode node, XmlNode oldParent)
                {
-                       throw new NotImplementedException ();
+                       if (NodeRemoved != null)
+                               NodeRemoved (node, new XmlNodeChangedEventArgs
+                                       (XmlNodeChangedAction.Remove,
+                                       node, oldParent, null));
                }
 
-               [MonoTODO]
-               public override void WriteContentTo (XmlWriter xw)
+               internal void onNodeRemoving (XmlNode node, XmlNode oldParent)
                {
-                       throw new NotImplementedException ();
+                       if (NodeRemoving != null)
+                               NodeRemoving (node, new XmlNodeChangedEventArgs
+                                       (XmlNodeChangedAction.Remove,
+                                       node, oldParent, null));
+               }
+
+               private void ParseName (string name, out string prefix, out string localName)
+               {
+                       int indexOfColon = name.IndexOf (':');
+                       
+                       if (indexOfColon != -1) {
+                               prefix = name.Substring (0, indexOfColon);
+                               localName = name.Substring (indexOfColon + 1);
+                       } else {
+                               prefix = "";
+                               localName = name;
+                       }
                }
 
                [MonoTODO]
-               public override void WriteTo (XmlWriter w)
+               public virtual XmlNode ReadNode(XmlReader reader)
                {
                        throw new NotImplementedException ();
                }
 
-               internal void onNodeChanging(XmlNode node, XmlNode Parent)
+               [MonoTODO]
+               public virtual void Save(Stream outStream)
                {
-                       if (NodeInserting != null)
-                               NodeChanging( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Change,
-                                       node, Parent, Parent));
+                       throw new NotImplementedException ();
                }
 
-               internal void onNodeChanged(XmlNode node, XmlNode Parent)
+               [MonoTODO]
+               public virtual void Save (string filename)
                {
-                       if (NodeChanged != null)
-                               NodeInserted( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Change,
-                                       node, Parent, Parent));
+                       throw new NotImplementedException ();
                }
 
-               internal void onNodeInserting(XmlNode node, XmlNode newParent)
+               [MonoTODO]
+               public virtual void Save (TextWriter writer)
                {
-                       if (NodeInserting != null)
-                               NodeInserting( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Insert,
-                                       node, null, newParent));
+                       throw new NotImplementedException ();
                }
 
-               internal void onNodeInserted(XmlNode node, XmlNode newParent)
+               [MonoTODO]
+               public virtual void Save (XmlWriter writer)
                {
-                       if (NodeInserted != null)
-                               NodeInserted( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Insert,
-                                       node, null, newParent));
+                       throw new NotImplementedException ();
                }
 
-               internal void onNodeRemoving(XmlNode node, XmlNode oldParent)
+               public override void WriteContentTo (XmlWriter w)
                {
-                       if (NodeRemoving != null)
-                               NodeRemoving(new XmlNodeChangedEventArgs(XmlNodeChangedAction.Remove,
-                                       node, oldParent, null));
+                       foreach(XmlNode childNode in ChildNodes)
+                               childNode.WriteTo(w);
                }
 
-               internal void onNodeRemoved(XmlNode node, XmlNode oldParent)
+               public override void WriteTo (XmlWriter w)
                {
-                       if (NodeRemoved != null)
-                               NodeRemoved(new XmlNodeChangedEventArgs(XmlNodeChangedAction.Remove,
-                                       node, oldParent, null));
+                       WriteContentTo(w);
                }
 
                #endregion