2003-02-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml / XmlElement.cs
index 3419ab9a7c0757a3b937b2b5a6d134809515f979..6b6fd2838b8e06b11f48d2df56ea53c6d1970595 100644 (file)
-// -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 //
 // System.Xml.XmlElement
 //
 // Author:
-//   Daniel Weber (daniel-weber@austin.rr.com)
+//   Jason Diamond (jason@injektilo.org)
+//   Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
 //
-// (C) 2001 Daniel Weber
+// (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
 {
        public class XmlElement : XmlLinkedNode
        {
-               // Private/Protected internal data structures
-               //===========================================================================
-               private XmlAttributeCollection _attributes;
+               #region Fields
 
-               private string prefix;
+               private XmlAttributeCollection attributes;
                private string localName;
                private string namespaceURI;
+               private string prefix;
+               private bool isEmpty;
 
-               // Public Properties
-               //===========================================================================
+               #endregion
 
-               /// <summary>
-               /// Return the XmlAttributeCollection on the Element
-               /// </summary>
-               public override XmlAttributeCollection Attributes
+               #region Constructor
+
+               protected internal XmlElement (
+                       string prefix, 
+                       string localName, 
+                       string namespaceURI, 
+                       XmlDocument doc) : base (doc)
                {
-                       get
+                       this.prefix = prefix;
+                       this.localName = localName;
+                       this.namespaceURI = namespaceURI;
+
+                       attributes = new XmlAttributeCollection (this);
+
+                       // TODO: Adds default attributes
+                       if(doc.DocumentType != null)
                        {
-                               // TODO - implement Attributes
-                               return _attributes;
                        }
                }
 
-               /// <summary>
-               /// Get/Set the value for this node
-               /// </summary>
-               public override string Value
-               {
-                       get
-                       {
-                               return null;
+               #endregion
+
+               #region Properties
+
+               public override XmlAttributeCollection Attributes {
+                       get { return attributes; }
+               }
+
+               public virtual bool HasAttributes {
+                       get { return attributes.Count > 0; }
+               }
+
+               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));
+                               }
                        }
+               }
 
-                       set
-                       {
-                               // Do nothing, can't set value on XmlElement...
+               public override string InnerXml {
+                       get {
+                               return base.InnerXml;
+                       }
+                       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);
                        }
                }
 
-               // Implement abstract methods of XmlNode
-               //=====================================================================
-               /// <summary>
-               /// Remove all children and attributes.  If
-               /// </summary>
-               public override void RemoveAll()
-               {
-                       // Remove all child nodes
-                       base.RemoveAll();
+               public bool IsEmpty {
+                       get { return isEmpty; }
 
-                       // Remove all attributes
-                       _attributes.RemoveAll();
+                       set {
+                               if(value)
+                                       RemoveAll();
+                               isEmpty = value;
+                       }
+               }
+
+               public override string LocalName {
+                       get { return localName; }
+               }
+
+               public override string Name {
+                       get { 
+                               return prefix != String.Empty ? prefix + ":" + localName : localName; 
+                       }
+               }
 
-                       // If we have any default attributes, add them back in with the
-                       //      appropriate namespace, baseURI, name, localName
-                       // TODO - implement adding default attributes back in XmlElement.RemoveAll()
+               public override string NamespaceURI {
+                       get { return namespaceURI; }
                }
 
-               /// <summary>
-               /// Return a clone of the node
-               /// </summary>
-               /// <param name="deep">Make copy of all children</param>
-               /// <returns>Cloned node</returns>
-               public override XmlNode CloneNode( bool deep)
+               [MonoTODO]
+               public override XmlNode NextSibling {
+                       get { 
+                               return base.NextSibling; 
+                       }
+               }
+
+               public override XmlNodeType NodeType {
+                       get { 
+                               return XmlNodeType.Element; 
+                       }
+               }
+
+               internal override XPathNodeType XPathNodeType {
+                       get {
+                               return XPathNodeType.Element;
+                       }
+               }
+
+               [MonoTODO]
+               public override XmlDocument OwnerDocument {
+                       get { 
+                               return base.OwnerDocument; 
+                       }
+               }
+
+               public override string Prefix {
+                       get { return prefix; }
+                       set { prefix = value; }
+               }
+
+               #endregion
+
+               #region Methods
+               
+               [MonoTODO]
+               public override XmlNode CloneNode (bool deep)
                {
-                       // TODO - implement CloneNode()
-                       throw new NotImplementedException();
+                       XmlNode node =  new XmlElement (prefix, localName, namespaceURI,
+                                                       OwnerDocument);
+
+                       for (int i = 0; i < node.Attributes.Count; i++)
+                               node.AppendChild (node.Attributes [i].CloneNode (false));
+                       
+                       if (deep) {
+                               while ((node != null) && (node.HasChildNodes)) {                                        
+                                       AppendChild (node.NextSibling.CloneNode (true));
+                                       node = node.NextSibling;
+                               }
+                       } // shallow cloning
+                               
+                       //
+                       // Reminder: Also look into Default attributes.
+                       //
+                       return node;
                }
 
-               /// <summary>
-               /// Saves all children of the current node to the passed writer
-               /// </summary>
-               /// <param name="w"></param>
-               public override void WriteContentTo(XmlWriter w)
+               [MonoTODO]
+               public virtual string GetAttribute (string name)
                {
-                       // TODO - implement WriteContentsTo(XmlWriter)
-                       throw new NotImplementedException();
+                       XmlNode attributeNode = Attributes.GetNamedItem (name);
+                       return attributeNode != null ? attributeNode.Value : String.Empty;
                }
 
-               /// <summary>
-               /// Saves the current node to writer w
-               /// </summary>
-               /// <param name="w"></param>
-               public override void WriteTo(XmlWriter w)
+               [MonoTODO]
+               public virtual string GetAttribute (string localName, string namespaceURI)
                {
-                       // TODO - implement WriteTo(XmlWriter)
-                       throw new NotImplementedException();
+                       XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
+                       return attributeNode != null ? attributeNode.Value : String.Empty;
                }
 
-               /// <summary>
-               /// Returns the local name of the node with qualifiers removed
-               /// LocalName of ns:elementName = "elementName"
-               /// </summary>
-               public override string LocalName
+               [MonoTODO]
+               public virtual XmlAttribute GetAttributeNode (string name)
                {
-                       get {
-                               return localName;
-                       }
+                       XmlNode attributeNode = Attributes.GetNamedItem (name);
+                       return attributeNode != null ? attributeNode as XmlAttribute : null;
                }
 
+               [MonoTODO]
+               public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
+               {
+                       XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
+                       return attributeNode != null ? attributeNode as XmlAttribute : null;
+               }
 
-               /// <summary>
-               /// Get the qualified node name
-               /// derived classes must implement as behavior varies
-               /// by tag type.
-               /// </summary>
-               public override string Name
+               public virtual XmlNodeList GetElementsByTagName (string name)
                {
-                       get
-                       {
-                               return prefix != String.Empty ? prefix + ":" + localName : localName;
-                       }
+                       ArrayList nodeArrayList = new ArrayList ();
+                       this.searchNodesRecursively (this, name, nodeArrayList);
+                       return new XmlNodeArrayList (nodeArrayList);
                }
 
-               public override string NamespaceURI
+               private void searchNodesRecursively (XmlNode argNode, string argName, 
+                       ArrayList argArrayList)
                {
-                       get
-                       {
-                               return namespaceURI;
+                       XmlNodeList xmlNodeList = argNode.ChildNodes;
+                       foreach (XmlNode node in xmlNodeList){
+                               if (node.Name.Equals (argName))
+                                       argArrayList.Add (node);
+                               else    
+                                       this.searchNodesRecursively (node, argName, argArrayList);
                        }
                }
 
-               public override XmlNodeType NodeType
+               private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI, 
+                       ArrayList argArrayList)
                {
-                       get
+                       XmlNodeList xmlNodeList = argNode.ChildNodes;
+                       foreach (XmlNode node in xmlNodeList)
                        {
-                               return XmlNodeType.Element;
+                               if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
+                                       argArrayList.Add (node);
+                               else    
+                                       this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
                        }
                }
 
-               public override string Prefix
+               public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
+               {
+                       ArrayList nodeArrayList = new ArrayList ();
+                       this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
+                       return new XmlNodeArrayList (nodeArrayList);
+               }
+
+               [MonoTODO]
+               public virtual bool HasAttribute (string name)
                {
-                       get
+                       XmlNode attributeNode = Attributes.GetNamedItem (name);
+                       return attributeNode != null;
+               }
+
+               [MonoTODO]
+               public virtual bool HasAttribute (string localName, string namespaceURI)
+               {
+                       XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
+                       return attributeNode != null;
+               }
+
+               [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
+               public override void RemoveAll ()
+               {
+                       // Remove the child nodes.
+                       base.RemoveAll ();
+
+                       // Remove all attributes.
+                       attributes.RemoveAll ();
+               }
+
+               [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
+               public virtual void RemoveAllAttributes ()
+               {
+                       attributes.RemoveAll ();
+               }
+
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
+               public virtual void RemoveAttribute (string name)
+               {
+                       attributes.Remove((XmlAttribute)attributes.GetNamedItem(name));
+               }
+
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
+               public virtual void RemoveAttribute (string localName, string namespaceURI)
+               {
+                       attributes.Remove((XmlAttribute)attributes.GetNamedItem(localName, namespaceURI));
+               }
+
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
+               public virtual XmlNode RemoveAttributeAt (int i)
+               {
+                       return attributes.Remove(attributes[i]);
+               }
+
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
+               public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
+               {
+                       return attributes.Remove(oldAttr);
+               }
+
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
+               public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
+               {
+                       return attributes.Remove(attributes[localName, namespaceURI]);
+               }
+
+               [MonoTODO]
+               public virtual void SetAttribute (string name, string value)
+               {
+                       XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
+                       attribute.SetOwnerElement(this);
+                       attribute.Value = value;
+                       Attributes.SetNamedItem (attribute);
+               }
+
+//             [MonoTODO]
+               public virtual string SetAttribute (string localName, string namespaceURI, string value)
+               {
+                       XmlAttribute attr = attributes[localName, namespaceURI];
+                       if(attr == null)
                        {
-                               return prefix;
+                               attr = OwnerDocument.CreateAttribute(localName, namespaceURI);
+                               attr.Value = value;
+                               attributes.SetNamedItem(attr);
                        }
+                       else
+                               attr.Value = value;
+                       return attr.Value;
                }
 
+//             [MonoTODO]
+               public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
+               {
+                       newAttr.SetOwnerElement(this);
+                       XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
+                       return oldAttr != null ? oldAttr as XmlAttribute : null;
+               }
 
-               // ============= Internal calls =============================================
-
-               // Constructors
-               // ==========================================================================
-               protected internal XmlElement(string prefix, string localName, string namespaceURI, XmlDocument doc) : base(doc)
+               public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
                {
-                       this.prefix = prefix;
-                       this.localName = localName;
-                       this.namespaceURI = namespaceURI;
+                       XmlDocument xmlDoc = this.OwnerDocument;
+                       XmlAttribute xmlAttribute = new XmlAttribute (String.Empty, localName, namespaceURI, xmlDoc);
+                       return this.attributes.Append (xmlAttribute);
+               }
 
-                       _attributes = new XmlAttributeCollection(doc, this, null);
+               public override void WriteContentTo (XmlWriter w)
+               {
+                       foreach(XmlNode childNode in ChildNodes)
+                               childNode.WriteTo(w);
                }
 
+               [MonoTODO]
+               public override void WriteTo (XmlWriter w)
+               {
+                       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();
+               }
 
-               } // class
-       }  //namespace
+               #endregion
+       }
+}