2 // System.Xml.XmlAttribute
5 // Jason Diamond (jason@injektilo.org)
7 // (C) 2002 Jason Diamond http://injektilo.org/
12 using System.Xml.XPath;
16 public class XmlAttribute : XmlNode
20 private XmlLinkedNode lastChild;
21 private string localName;
22 private string namespaceURI;
23 private string prefix;
24 internal bool isDefault;
25 private XmlElement ownerElement;
31 [MonoTODO("need to set namespaceURI if prefix is recognized built-in ones like xmlns")]
32 protected internal XmlAttribute (
36 XmlDocument doc) : base (doc)
38 // What to be recognized is: xml:space, xml:lang, xml:base, and
39 // xmlns and xmlns:* (when XmlDocument.Namespaces = true only)
41 this.localName = localName;
42 this.namespaceURI = namespaceURI;
49 public override string BaseURI {
51 return OwnerElement.BaseURI;
55 public override string InnerText {
57 StringBuilder builder = new StringBuilder ();
58 AppendChildValues (this, builder);
59 return builder.ToString ();
67 private void AppendChildValues (XmlNode parent, StringBuilder builder)
69 XmlNode node = parent.FirstChild;
71 while (node != null) {
72 builder.Append (node.Value);
73 AppendChildValues (node, builder);
74 node = node.NextSibling;
78 [MonoTODO ("Setter is incomplete(XmlTextReader.ReadAttribute is incomplete;No resolution for xml:lang/space")]
79 public override string InnerXml {
81 // Not sure why this is an override. Passing through for now.
86 XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
87 XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr, XmlLang, this.XmlSpace);
88 XmlTextReader xtr = OwnerDocument.ReusableReader;
89 xtr.Initialize (BaseURI, ctx, new System.IO.StringReader ("'" + value.Replace ("'", "'") + "'"), XmlNodeType.Attribute);
91 OwnerDocument.ReadAttributeNodeValue (xtr, this);
95 public override string LocalName {
101 public override string Name {
103 return prefix != String.Empty ? prefix + ":" + localName : localName;
107 public override string NamespaceURI {
113 public override XmlNodeType NodeType {
115 return XmlNodeType.Attribute;
119 internal override XPathNodeType XPathNodeType {
121 return XPathNodeType.Attribute;
125 public override XmlDocument OwnerDocument {
127 return base.OwnerDocument;
131 public virtual XmlElement OwnerElement {
137 public override XmlNode ParentNode {
139 // It always returns null (by specification).
144 [MonoTODO("setter incomplete (name character check, format check, wrong prefix&nsURI)")]
145 // We gotta do more in the set block here
146 // We need to do the proper tests and throw
147 // the correct Exceptions
149 // Wrong cases are: (1)check readonly, (2)check character validity,
150 // (3)check format validity, (4)this is attribute and qualifiedName != "xmlns"
151 // (5)when argument is 'xml' or 'xmlns' and namespaceURI doesn't match
152 public override string Prefix {
155 throw new XmlException ("This node is readonly.");
157 XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
158 string nsuri = nsmgr.LookupNamespace (value);
160 throw new XmlException ("Namespace URI not found for this prefix");
170 [MonoTODO("There are no code which sets 'specified = true', so this logic is without checking.")]
171 public virtual bool Specified {
177 public override string Value {
179 XmlNode firstChild = FirstChild;
180 if (firstChild == null)
182 return firstChild.Value;
186 XmlNode firstChild = FirstChild;
187 if (firstChild == null)
188 AppendChild (OwnerDocument.CreateTextNode (value));
190 firstChild.Value = value;
194 internal override string XmlLang {
195 get { return OwnerElement.XmlLang; }
198 internal override XmlSpace XmlSpace {
199 get { return OwnerElement.XmlSpace; }
206 public override XmlNode CloneNode (bool deep)
208 XmlNode node = new XmlAttribute (prefix, localName, namespaceURI,
211 while ((node != null) && (node.HasChildNodes)) {
212 AppendChild (node.NextSibling.CloneNode (true));
213 node = node.NextSibling;
220 // Parent of XmlAttribute must be null
221 internal void SetOwnerElement (XmlElement el) {
225 public override void WriteContentTo (XmlWriter w)
227 foreach (XmlNode n in ChildNodes)
231 public override void WriteTo (XmlWriter w)
233 w.WriteStartAttribute (prefix, localName, namespaceURI);
235 w.WriteEndAttribute ();
240 internal override XmlLinkedNode LastLinkedChild {
241 get { return lastChild; }
243 set { lastChild = value; }