2003-10-25 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / System.Xml / XmlAttribute.cs
index 6a86c71ac16e1abf74bad433fa1c37a75f68014b..ba60b74589eabad38c4bcc993d1d1e3e697efcf4 100644 (file)
-// -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 //
 // System.Xml.XmlAttribute
 //
-// Author:
-//   Daniel Weber (daniel-weber@austin.rr.com)
+// Authors:
+//   Jason Diamond (jason@injektilo.org)
+//   Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
+//
+// (C) 2002 Jason Diamond  http://injektilo.org/
+// (C) 2003 Atsushi Enomoto
 //
-// (C) 2001 Daniel Weber
 
 using System;
+using System.Text;
+using System.Xml.XPath;
 
 namespace System.Xml
 {
-       /// <summary>
-       /// Summary description for XmlAttribute.
-       /// </summary>
        public class XmlAttribute : XmlNode
        {
-               // ============  private data structures ==============================
-               private XmlNode FOwnerElement;
-               
-               string FattrName;
-               string FattrValue;
-               
-               //==== Public Properties ====================================================
-
-               /// <summary>
-               /// Returns the local name of the attribute.  For attributes, this is the same as Name
-               /// </summary>
-               public override string LocalName 
+               #region Fields
+
+               private XmlLinkedNode lastChild;
+               private string localName;
+               private string namespaceURI;
+               private string prefix;
+               internal bool isDefault;
+               private XmlElement ownerElement;
+
+               #endregion
+
+               #region Constructor
+
+               protected internal XmlAttribute (
+                       string prefix, 
+                       string localName, 
+                       string namespaceURI,
+                       XmlDocument doc) : base (doc)
                {
-                       get
-                       {
-                               return FattrName;
+                       if (prefix == null)
+                               prefix = String.Empty;
+                       if (namespaceURI == null)
+                               namespaceURI = String.Empty;
+
+                       // Prefix "xml" should be also checked (http://www.w3.org/XML/xml-names-19990114-errata#NE05)
+                       // but MS.NET ignores such case.
+                       if (prefix == "xmlns" || (prefix == "" && localName == "xmlns"))
+                               if (namespaceURI != XmlNamespaceManager.XmlnsXmlns)
+                                       throw new ArgumentException ("Invalid attribute namespace for namespace declaration.");
+                       else if (prefix == "xml" && namespaceURI != XmlNamespaceManager.XmlnsXml)
+                                       throw new ArgumentException ("Invalid attribute namespace for namespace declaration.");
+
+                       // There are no means to identify the DOM is namespace-
+                       // aware or not, so we can only check Name validity.
+                       if (prefix != "" && !XmlChar.IsName (prefix))
+                               throw new ArgumentException ("Invalid attribute prefix.");
+                       else if (!XmlChar.IsName (localName))
+                               throw new ArgumentException ("Invalid attribute local name.");
+
+                       this.prefix = prefix;
+                       this.localName = localName;
+                       this.namespaceURI = namespaceURI;
+               }
+
+               #endregion
+
+               #region Properties
+
+               public override string BaseURI {
+                       get {
+                               return OwnerElement.BaseURI;
                        }
                }
 
-               /// <summary>
-               /// Get the qualified attribute name.  Attributes do not have an associated namespace.
-               /// </summary>
-               public override string Name 
-               { 
-                       get
-                       {
-                               return FattrName;
+               public override string InnerText {
+                       get {
+                               return base.InnerText;
+                       }
+
+                       set {
+                               Value = value;
                        }
                }
 
-               /// <summary>
-               /// Override.  Returns the node type.
-               /// </summary>
-               public override XmlNodeType NodeType 
-               {
-                       get
-                       {
+               public override string InnerXml {
+                       get {
+                               // Not sure why this is an override.  Passing through for now.
+                               return base.InnerXml;
+                       }
+
+                       set {
+                               RemoveAll ();
+                               XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
+                               XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
+                                       OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
+                                       BaseURI, XmlLang, XmlSpace, null);
+                               XmlTextReader xtr = new XmlTextReader (value, XmlNodeType.Attribute, ctx);
+                               xtr.XmlResolver = OwnerDocument.Resolver;
+                               xtr.Read ();
+                               OwnerDocument.ReadAttributeNodeValue (xtr, this);
+                       }
+               }
+
+               public override string LocalName {
+                       get {
+                               return localName;
+                       }
+               }
+
+               public override string Name {
+                       get { 
+                               return prefix != String.Empty ? prefix + ":" + localName : localName; 
+                       }
+               }
+
+               public override string NamespaceURI {
+                       get {
+                               return namespaceURI;
+                       }
+               }
+
+               public override XmlNodeType NodeType {
+                       get {
                                return XmlNodeType.Attribute;
                        }
                }
 
-               /// <summary>
-               /// Retrieve the XmlElement owner of this attribute, or null if attribute not assigned
-               /// </summary>
-               public virtual XmlElement OwnerElement
-               {
-                       get
-                       {
-                               if (FOwnerElement.NodeType == XmlNodeType.Element)
-                                       return FOwnerElement as XmlElement;
-                               else
-                                       return null;
+               internal override XPathNodeType XPathNodeType {
+                       get {
+                               return XPathNodeType.Attribute;
                        }
                }
 
-               /// <summary>
-               /// Get/Set the value for this node
-               /// </summary>
-               public override string Value 
-               {
-                       get
-                       {
-                               return FattrValue;
+               public override XmlDocument OwnerDocument {
+                       get {
+                               return base.OwnerDocument;
+                       }
+               }
+
+               public virtual XmlElement OwnerElement {
+                       get {
+                               return ownerElement;
+                       }
+               }
+
+               public override XmlNode ParentNode {
+                       get {
+                               // It always returns null (by specification).
+                               return null;
+                       }
+               }
+
+               // We gotta do more in the set block here
+               // We need to do the proper tests and throw
+               // the correct Exceptions
+               //
+               // Wrong cases are: (1)check readonly, (2)check character validity,
+               // (3)check format validity, (4)this is attribute and qualifiedName != "xmlns"
+               public override string Prefix {
+                       set {
+                               if (IsReadOnly)
+                                       throw new XmlException ("This node is readonly.");
+                               if (!XmlChar.IsNCName (value))
+                                       throw new ArgumentException ("Specified name is not a valid NCName: " + value);
+
+                               prefix = value;
                        }
                        
-                       set
-                       {
-                               FattrValue = value;
+                       get {
+                               return prefix;
+                       }
+               }
+
+               public virtual bool Specified {
+                       get {
+                               return !isDefault;
                        }
                }
 
-               //============== Public Methods =============================================
+               public override string Value {
+                       get {
+                               XmlNode firstChild = FirstChild;
+                               if (firstChild == null)
+                                       return String.Empty;
+                               return firstChild.Value;
+                       }
 
-               /// <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)
-               {
-                       // TODO - implement XmlAttribute.CloneNode()
-                       throw new NotImplementedException();
+                       set {
+                               XmlNode firstChild = FirstChild;
+                               if (firstChild == null)
+                                       AppendChild (OwnerDocument.CreateTextNode (value));
+                               else if (FirstChild.NextSibling != null) {
+                                       this.RemoveAll ();
+                                       AppendChild (OwnerDocument.CreateTextNode (value));
+                               }
+                               else
+                                       firstChild.Value = value;
+                       }
                }
 
-               /// <summary>
-               /// Saves all children of the current node to the passed writer
-               /// </summary>
-               /// <param name="w"></param>
-               public override void WriteContentTo(XmlWriter w)
-               {
-                       // TODO - implement XmlAttribute.WriteContentsTo(XmlWriter)
-                       throw new NotImplementedException();
+               internal override string XmlLang {
+                       get { return OwnerElement.XmlLang; }
+               }
+
+               internal override XmlSpace XmlSpace {
+                       get { return OwnerElement.XmlSpace; }
                }
 
-               /// <summary>
-               /// Saves the current node to writer w
-               /// </summary>
-               /// <param name="w"></param>
-               public override void WriteTo(XmlWriter w)
+               #endregion
+
+               #region Methods
+
+               public override XmlNode CloneNode (bool deep)
                {
-                       // TODO - implement XmlAttribute.WriteTo(XmlWriter)
-                       throw new NotImplementedException();
+                       XmlNode node = new XmlAttribute (prefix, localName, namespaceURI,
+                                                        OwnerDocument);
+                       if (deep) {
+                               foreach (XmlNode child in this.ChildNodes)
+                                       node.AppendChild (child.CloneNode (deep));
+                       }
+
+                       return node;
                }
 
-               // ============  Internal methods  ====================================
-               internal void setOwnerElement( XmlElement newOwnerElement)
+               internal void SetDefault ()
                {
-                       FOwnerElement = newOwnerElement;
+                       isDefault = true;
                }
 
-               // ============  Constructors =========================================
-               internal XmlAttribute ( XmlDocument aOwner,                     // owner document
-                       string attributeName,                                                   // cannot be ""
-                       string attValue) : base(aOwner)
+               // Parent of XmlAttribute must be null
+               internal void SetOwnerElement (XmlElement el) {
+                       ownerElement = el;
+               }
+
+               public override void WriteContentTo (XmlWriter w)
                {
-                       if (aOwner == null)
-                               throw new ArgumentException("Null OwnerDocument passed to XmlAttribute constructor");
-                       if (attributeName.Length == 0)
-                               throw new ArgumentException("Empty string passed to XmlAttribute constructor");
+                       foreach (XmlNode n in ChildNodes)
+                               n.WriteTo (w);
+               }
 
-                       FOwnerElement = null;
-                       FattrName = attributeName;
-                       FattrValue = attValue;
+               public override void WriteTo (XmlWriter w)
+               {
+                       w.WriteStartAttribute (prefix, localName, namespaceURI);
+                       WriteContentTo (w);
+                       w.WriteEndAttribute ();
                }
-       
 
+               #endregion
+
+               internal override XmlLinkedNode LastLinkedChild {
+                       get { return lastChild; }
+
+                       set { lastChild = value; }
+               }
        }
 }