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.SetReaderContext (BaseURI, ctx);
90 xtr.SetReaderFragment (new System.IO.StringReader ("'" + value.Replace ("'", "'") + "'"), XmlNodeType.Attribute);
91 xtr.ReadAttributeValue ();
96 public override string LocalName {
102 public override string Name {
104 return prefix != String.Empty ? prefix + ":" + localName : localName;
108 public override string NamespaceURI {
114 public override XmlNodeType NodeType {
116 return XmlNodeType.Attribute;
120 internal override XPathNodeType XPathNodeType {
122 return XPathNodeType.Attribute;
126 public override XmlDocument OwnerDocument {
128 return base.OwnerDocument;
132 public virtual XmlElement OwnerElement {
138 public override XmlNode ParentNode {
140 // It always returns null (by specification).
145 [MonoTODO("setter incomplete (name character check, format check, wrong prefix&nsURI)")]
146 // We gotta do more in the set block here
147 // We need to do the proper tests and throw
148 // the correct Exceptions
150 // Wrong cases are: (1)check readonly, (2)check character validity,
151 // (3)check format validity, (4)this is attribute and qualifiedName != "xmlns"
152 // (5)when argument is 'xml' or 'xmlns' and namespaceURI doesn't match
153 public override string Prefix {
156 throw new XmlException ("This node is readonly.");
158 XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
159 string nsuri = nsmgr.LookupNamespace (value);
161 throw new XmlException ("Namespace URI not found for this prefix");
171 [MonoTODO("There are no code which sets 'specified = true', so this logic is without checking.")]
172 public virtual bool Specified {
178 public override string Value {
180 XmlNode firstChild = FirstChild;
181 if (firstChild == null)
183 return firstChild.Value;
187 XmlNode firstChild = FirstChild;
188 if (firstChild == null)
189 AppendChild (OwnerDocument.CreateTextNode (value));
191 firstChild.Value = value;
195 internal override string XmlLang {
196 get { return OwnerElement.XmlLang; }
199 internal override XmlSpace XmlSpace {
200 get { return OwnerElement.XmlSpace; }
207 public override XmlNode CloneNode (bool deep)
209 XmlNode node = new XmlAttribute (prefix, localName, namespaceURI,
212 while ((node != null) && (node.HasChildNodes)) {
213 AppendChild (node.NextSibling.CloneNode (true));
214 node = node.NextSibling;
221 // Parent of XmlAttribute must be null
222 internal void SetOwnerElement (XmlElement el) {
226 public override void WriteContentTo (XmlWriter w)
228 w.WriteString (Value);
231 public override void WriteTo (XmlWriter w)
233 w.WriteAttributeString (prefix, localName, namespaceURI, Value);
238 internal override XmlLinkedNode LastLinkedChild {
239 get { return lastChild; }
241 set { lastChild = value; }