X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2FSystem.XML%2FSystem.Xml%2FXmlElement.cs;h=8ba8f30946c13a807193383095dbdfd97e9d1da3;hb=879d8fbdcd1e6a59972d878a74760a0d67055062;hp=670b514c9f307be8c1bd3bf17123480fb868102b;hpb=686b51d1a6a598292492b4e2420a55f0fca5c400;p=mono.git diff --git a/mcs/class/System.XML/System.Xml/XmlElement.cs b/mcs/class/System.XML/System.Xml/XmlElement.cs index 670b514c9f3..8ba8f30946c 100644 --- a/mcs/class/System.XML/System.Xml/XmlElement.cs +++ b/mcs/class/System.XML/System.Xml/XmlElement.cs @@ -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 @@ -43,65 +54,68 @@ namespace System.Xml #region Properties public override XmlAttributeCollection Attributes { - get { - return attributes; - } + get { return attributes; } } public virtual bool HasAttributes { - get { - return attributes.Count > 0; - } + get { return attributes.Count > 0; } } - [MonoTODO] public override string InnerText { - get { - throw new NotImplementedException (); + get { + return base.InnerText; } - - set { - throw new NotImplementedException (); + set { + foreach(XmlNode n in ChildNodes) + { + this.RemoveChild(n); + } + // creates new Text node + AppendChild(OwnerDocument.CreateTextNode(value)); } } - [MonoTODO] + [MonoTODO ("Setter is immature")] public override string InnerXml { - get { - throw new NotImplementedException (); - } - - set { - throw new NotImplementedException (); + get { + // Not sure why this is an override. Passing through for now. + return base.InnerXml; } - } - - [MonoTODO] - public bool IsEmpty { - get { - throw new NotImplementedException (); - } - - 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); } } - internal override XmlLinkedNode LastLinkedChild { - get { - return lastLinkedChild; - } + public bool IsEmpty { + get { return isEmpty; } set { - lastLinkedChild = value; + if(value) { + RemoveAll(); + } + isEmpty = value; } } - - public override string LocalName - { - get { - return localName; - } + + public override string LocalName { + get { return localName; } } public override string Name { @@ -111,9 +125,7 @@ namespace System.Xml } public override string NamespaceURI { - get { - return namespaceURI; - } + get { return namespaceURI; } } [MonoTODO] @@ -129,6 +141,12 @@ namespace System.Xml } } + internal override XPathNodeType XPathNodeType { + get { + return XPathNodeType.Element; + } + } + [MonoTODO] public override XmlDocument OwnerDocument { get { @@ -137,19 +155,34 @@ namespace System.Xml } public override string Prefix { - get { - return prefix; - } + get { return prefix; } + set { prefix = value; } } #endregion #region Methods - + [MonoTODO] public override XmlNode CloneNode (bool deep) { - 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; } [MonoTODO] @@ -162,46 +195,78 @@ namespace System.Xml [MonoTODO] public virtual string GetAttribute (string localName, string namespaceURI) { - throw new NotImplementedException (); + XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI); + return attributeNode != null ? attributeNode.Value : String.Empty; } [MonoTODO] public virtual XmlAttribute GetAttributeNode (string name) { - throw new NotImplementedException (); + XmlNode attributeNode = Attributes.GetNamedItem (name); + return attributeNode != null ? attributeNode as XmlAttribute : null; } [MonoTODO] public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI) { - throw new NotImplementedException (); + XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI); + 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] public virtual bool HasAttribute (string name) { - throw new NotImplementedException (); + XmlNode attributeNode = Attributes.GetNamedItem (name); + return attributeNode != null; } [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. @@ -211,79 +276,109 @@ 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.SetOwnerElement (this); + attribute.SetOwnerElement(this); attribute.Value = value; Attributes.SetNamedItem (attribute); } - [MonoTODO] - public virtual void SetAttribute (string localName, string namespaceURI, string value) +// [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) { - throw new NotImplementedException (); + 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); } - [MonoTODO] public override void WriteContentTo (XmlWriter w) { - throw new NotImplementedException (); + foreach(XmlNode childNode in ChildNodes) + childNode.WriteTo(w); } - [MonoTODO] + [MonoTODO("indenting feature is incomplete.")] public override void WriteTo (XmlWriter w) { - throw new NotImplementedException (); + w.WriteStartElement(Prefix, LocalName, NamespaceURI); + + // write namespace declarations(if not exist) + if(Prefix != null && Prefix != String.Empty && w.LookupPrefix(Prefix) != NamespaceURI) + w.WriteAttributeString("xmlns", Prefix, "http://www.w3.org/2000/xmlns/", NamespaceURI); + + foreach(XmlNode attributeNode in Attributes) + { + attributeNode.WriteTo(w); + // write namespace declarations(if not exist) + if(attributeNode.Prefix != null && attributeNode.Prefix != String.Empty && + w.LookupPrefix(attributeNode.Prefix) != attributeNode.NamespaceURI && + attributeNode.Prefix != "xmlns") + w.WriteAttributeString("xmlns", attributeNode.Prefix, "http://www.w3.org/2000/xmlns/", attributeNode.NamespaceURI); + } + + WriteContentTo(w); + + w.WriteEndElement(); } #endregion