2003-02-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml / XmlElement.cs
index 6925b8f0dead3ff211c268eb35c1b8cbb9ba291a..6b6fd2838b8e06b11f48d2df56ea53c6d1970595 100644 (file)
@@ -1,13 +1,19 @@
 //
-// System.Xml.XmlAttribute
+// System.Xml.XmlElement
 //
 // Author:
 //   Jason Diamond (jason@injektilo.org)
+//   Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
 //
 // (C) 2002 Jason Diamond  http://injektilo.org/
+// (C) 2002 Atsushi Enomoto
 //
 
 using System;
+using System.Collections;
+using System.Xml.XPath;
+using System.IO;
+using System.Text;
 
 namespace System.Xml
 {
@@ -16,10 +22,10 @@ namespace System.Xml
                #region Fields
 
                private XmlAttributeCollection attributes;
-               private XmlLinkedNode lastLinkedChild;
                private string localName;
                private string namespaceURI;
                private string prefix;
+               private bool isEmpty;
 
                #endregion
 
@@ -36,6 +42,11 @@ namespace System.Xml
                        this.namespaceURI = namespaceURI;
 
                        attributes = new XmlAttributeCollection (this);
+
+                       // TODO: Adds default attributes
+                       if(doc.DocumentType != null)
+                       {
+                       }
                }
 
                #endregion
@@ -50,28 +61,59 @@ namespace System.Xml
                        get { return attributes.Count > 0; }
                }
 
-               [MonoTODO ("Setter.")]
+               public override string InnerText {
+                       get {
+                               return base.InnerText;
+                       }
+                       set {
+                               // Why its behavior (of MS FCL) is different from InnerXml...?
+                               if (FirstChild != null && FirstChild.NodeType == XmlNodeType.Text)
+                                       FirstChild.Value = value;
+                               else {
+                                       if(FirstChild != null) {
+                                               foreach (XmlNode n in ChildNodes)
+                                                       this.RemoveChild (n);
+                                       }
+                                       // creates new Text node
+                                       AppendChild(OwnerDocument.CreateTextNode(value));
+                               }
+                       }
+               }
+
                public override string InnerXml {
                        get {
-                               // Not sure why this is an override.  Passing through for now.
                                return base.InnerXml;
                        }
-                       set { throw new NotImplementedException (); }
+                       set {
+                               foreach(XmlNode n in ChildNodes)
+                                       this.RemoveChild(n);
+
+                               // I hope there are any well-performance logic...
+                               XmlNameTable nt = this.OwnerDocument.NameTable;
+                               XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
+                               XmlParserContext ctx = new XmlParserContext (nt, nsmgr, XmlLang, this.XmlSpace);
+                               XmlTextReader xmlReader = OwnerDocument.ReusableReader;
+                               xmlReader.SetReaderContext (String.Empty, ctx);
+                               xmlReader.SetReaderFragment (new StringReader (value), XmlNodeType.Element);
+
+                               do {
+                                       XmlNode n = OwnerDocument.ReadNode (xmlReader);
+                                       if(n == null) break;
+                                       AppendChild (n);
+                               } while (true);
+                       }
                }
 
-               [MonoTODO]
                public bool IsEmpty {
-                       get { throw new NotImplementedException (); }
+                       get { return isEmpty; }
 
-                       set { throw new NotImplementedException (); }
+                       set {
+                               if(value)
+                                       RemoveAll();
+                               isEmpty = value;
+                       }
                }
 
-               internal override XmlLinkedNode LastLinkedChild {
-                       get { return lastLinkedChild; }
-
-                       set { lastLinkedChild = value; }
-               }
-               
                public override string LocalName {
                        get { return localName; }
                }
@@ -99,6 +141,12 @@ namespace System.Xml
                        }
                }
 
+               internal override XPathNodeType XPathNodeType {
+                       get {
+                               return XPathNodeType.Element;
+                       }
+               }
+
                [MonoTODO]
                public override XmlDocument OwnerDocument {
                        get { 
@@ -107,9 +155,8 @@ namespace System.Xml
                }
 
                public override string Prefix {
-                       get { 
-                               return prefix; 
-                       }
+                       get { return prefix; }
+                       set { prefix = value; }
                }
 
                #endregion
@@ -166,16 +213,43 @@ namespace System.Xml
                        return attributeNode != null ? attributeNode as XmlAttribute : null;
                }
 
-               [MonoTODO]
                public virtual XmlNodeList GetElementsByTagName (string name)
                {
-                       throw new NotImplementedException ();
+                       ArrayList nodeArrayList = new ArrayList ();
+                       this.searchNodesRecursively (this, name, nodeArrayList);
+                       return new XmlNodeArrayList (nodeArrayList);
+               }
+
+               private void searchNodesRecursively (XmlNode argNode, string argName, 
+                       ArrayList argArrayList)
+               {
+                       XmlNodeList xmlNodeList = argNode.ChildNodes;
+                       foreach (XmlNode node in xmlNodeList){
+                               if (node.Name.Equals (argName))
+                                       argArrayList.Add (node);
+                               else    
+                                       this.searchNodesRecursively (node, argName, argArrayList);
+                       }
+               }
+
+               private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI, 
+                       ArrayList argArrayList)
+               {
+                       XmlNodeList xmlNodeList = argNode.ChildNodes;
+                       foreach (XmlNode node in xmlNodeList)
+                       {
+                               if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
+                                       argArrayList.Add (node);
+                               else    
+                                       this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
+                       }
                }
 
-               [MonoTODO]
                public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       ArrayList nodeArrayList = new ArrayList ();
+                       this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
+                       return new XmlNodeArrayList (nodeArrayList);
                }
 
                [MonoTODO]
@@ -188,10 +262,11 @@ namespace System.Xml
                [MonoTODO]
                public virtual bool HasAttribute (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
+                       return attributeNode != null;
                }
 
-               [MonoTODO ("Don't remove default attributes.")]
+               [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
                public override void RemoveAll ()
                {
                        // Remove the child nodes.
@@ -201,69 +276,79 @@ namespace System.Xml
                        attributes.RemoveAll ();
                }
 
-               [MonoTODO]
+               [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
                public virtual void RemoveAllAttributes ()
                {
-                       throw new NotImplementedException ();
+                       attributes.RemoveAll ();
                }
 
-               [MonoTODO]
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
                public virtual void RemoveAttribute (string name)
                {
-                       throw new NotImplementedException ();
+                       attributes.Remove((XmlAttribute)attributes.GetNamedItem(name));
                }
 
-               [MonoTODO]
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
                public virtual void RemoveAttribute (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       attributes.Remove((XmlAttribute)attributes.GetNamedItem(localName, namespaceURI));
                }
 
-               [MonoTODO]
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
                public virtual XmlNode RemoveAttributeAt (int i)
                {
-                       throw new NotImplementedException ();
+                       return attributes.Remove(attributes[i]);
                }
 
-               [MonoTODO]
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
                public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
                {
-                       throw new NotImplementedException ();
+                       return attributes.Remove(oldAttr);
                }
 
-               [MonoTODO]
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
                public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       return attributes.Remove(attributes[localName, namespaceURI]);
                }
 
                [MonoTODO]
                public virtual void SetAttribute (string name, string value)
                {
                        XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
-                       attribute.SetParentNode (this);
+                       attribute.SetOwnerElement(this);
                        attribute.Value = value;
                        Attributes.SetNamedItem (attribute);
                }
 
-               [MonoTODO]
+//             [MonoTODO]
                public virtual string SetAttribute (string localName, string namespaceURI, string value)
                {
-                       throw new NotImplementedException ();
+                       XmlAttribute attr = attributes[localName, namespaceURI];
+                       if(attr == null)
+                       {
+                               attr = OwnerDocument.CreateAttribute(localName, namespaceURI);
+                               attr.Value = value;
+                               attributes.SetNamedItem(attr);
+                       }
+                       else
+                               attr.Value = value;
+                       return attr.Value;
                }
 
-               [MonoTODO]
+//             [MonoTODO]
                public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
                {
-                       newAttr.SetParentNode(this);
+                       newAttr.SetOwnerElement(this);
                        XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
                        return oldAttr != null ? oldAttr as XmlAttribute : null;
                }
 
-               [MonoTODO]
                public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       XmlDocument xmlDoc = this.OwnerDocument;
+                       XmlAttribute xmlAttribute = new XmlAttribute (String.Empty, localName, namespaceURI, xmlDoc);
+                       return this.attributes.Append (xmlAttribute);
                }
 
                public override void WriteContentTo (XmlWriter w)
@@ -272,13 +357,22 @@ namespace System.Xml
                                childNode.WriteTo(w);
                }
 
+               [MonoTODO]
                public override void WriteTo (XmlWriter w)
                {
-                       w.WriteStartElement(LocalName);
+                       w.WriteStartElement(Prefix, LocalName, NamespaceURI);
 
                        foreach(XmlNode attributeNode in Attributes)
                                attributeNode.WriteTo(w);
 
+                       // write namespace declarations(if not exist)
+                       foreach(XmlNode attributeNode in Attributes) {
+                               if(attributeNode.Prefix != null && attributeNode.Prefix != String.Empty &&\r
+                                       w.LookupPrefix(attributeNode.Prefix) != attributeNode.NamespaceURI &&\r
+                                       attributeNode.Prefix != "xmlns")\r
+                                       w.WriteAttributeString("xmlns", attributeNode.Prefix, "http://www.w3.org/2000/xmlns/", attributeNode.NamespaceURI);
+                       }
+
                        WriteContentTo(w);
 
                        w.WriteEndElement();