XmlAttribute.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // System.Xml.XmlAttribute
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. //
  7. // (C) 2002 Jason Diamond http://injektilo.org/
  8. //
  9. using System;
  10. using System.Text;
  11. using System.Xml.XPath;
  12. namespace System.Xml
  13. {
  14. public class XmlAttribute : XmlNode
  15. {
  16. #region Fields
  17. private XmlLinkedNode lastChild;
  18. private string localName;
  19. private string namespaceURI;
  20. private string prefix;
  21. internal bool isDefault;
  22. private XmlElement ownerElement;
  23. #endregion
  24. #region Constructor
  25. [MonoTODO("need to set namespaceURI if prefix is recognized built-in ones like xmlns")]
  26. protected internal XmlAttribute (
  27. string prefix,
  28. string localName,
  29. string namespaceURI,
  30. XmlDocument doc) : base (doc)
  31. {
  32. // What to be recognized is: xml:space, xml:lang, xml:base, and
  33. // xmlns and xmlns:* (when XmlDocument.Namespaces = true only)
  34. this.prefix = prefix;
  35. this.localName = localName;
  36. this.namespaceURI = namespaceURI;
  37. }
  38. #endregion
  39. #region Properties
  40. public override string BaseURI {
  41. get {
  42. return OwnerElement.BaseURI;
  43. }
  44. }
  45. public override string InnerText {
  46. get {
  47. StringBuilder builder = new StringBuilder ();
  48. AppendChildValues (this, builder);
  49. return builder.ToString ();
  50. }
  51. set {
  52. Value = value;
  53. }
  54. }
  55. private void AppendChildValues (XmlNode parent, StringBuilder builder)
  56. {
  57. XmlNode node = parent.FirstChild;
  58. while (node != null) {
  59. builder.Append (node.Value);
  60. AppendChildValues (node, builder);
  61. node = node.NextSibling;
  62. }
  63. }
  64. [MonoTODO ("Setter is incomplete(XmlTextReader.ReadAttribute is incomplete;No resolution for xml:lang/space")]
  65. public override string InnerXml {
  66. get {
  67. // Not sure why this is an override. Passing through for now.
  68. return base.InnerXml;
  69. }
  70. set {
  71. XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
  72. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr, XmlLang, this.XmlSpace);
  73. XmlTextReader xtr = OwnerDocument.ReusableReader;
  74. xtr.Initialize (BaseURI, ctx, new System.IO.StringReader ("'" + value.Replace ("'", "'") + "'"), XmlNodeType.Attribute);
  75. OwnerDocument.ReadAttributeNodeValue (xtr, this);
  76. }
  77. }
  78. public override string LocalName {
  79. get {
  80. return localName;
  81. }
  82. }
  83. public override string Name {
  84. get {
  85. return prefix != String.Empty ? prefix + ":" + localName : localName;
  86. }
  87. }
  88. public override string NamespaceURI {
  89. get {
  90. return namespaceURI;
  91. }
  92. }
  93. public override XmlNodeType NodeType {
  94. get {
  95. return XmlNodeType.Attribute;
  96. }
  97. }
  98. internal override XPathNodeType XPathNodeType {
  99. get {
  100. return XPathNodeType.Attribute;
  101. }
  102. }
  103. public override XmlDocument OwnerDocument {
  104. get {
  105. return base.OwnerDocument;
  106. }
  107. }
  108. public virtual XmlElement OwnerElement {
  109. get {
  110. return ownerElement;
  111. }
  112. }
  113. public override XmlNode ParentNode {
  114. get {
  115. // It always returns null (by specification).
  116. return null;
  117. }
  118. }
  119. [MonoTODO("setter incomplete (name character check, format check, wrong prefix&nsURI)")]
  120. // We gotta do more in the set block here
  121. // We need to do the proper tests and throw
  122. // the correct Exceptions
  123. //
  124. // Wrong cases are: (1)check readonly, (2)check character validity,
  125. // (3)check format validity, (4)this is attribute and qualifiedName != "xmlns"
  126. // (5)when argument is 'xml' or 'xmlns' and namespaceURI doesn't match
  127. public override string Prefix {
  128. set {
  129. if(IsReadOnly)
  130. throw new XmlException ("This node is readonly.");
  131. XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
  132. string nsuri = nsmgr.LookupNamespace (value);
  133. if(nsuri == null)
  134. throw new XmlException ("Namespace URI not found for this prefix");
  135. prefix = value;
  136. }
  137. get {
  138. return prefix;
  139. }
  140. }
  141. [MonoTODO("There are no code which sets 'specified = true', so this logic is without checking.")]
  142. public virtual bool Specified {
  143. get {
  144. return !isDefault;
  145. }
  146. }
  147. public override string Value {
  148. get {
  149. XmlNode firstChild = FirstChild;
  150. if (firstChild == null)
  151. return String.Empty;
  152. return firstChild.Value;
  153. }
  154. set {
  155. XmlNode firstChild = FirstChild;
  156. if (firstChild == null)
  157. AppendChild (OwnerDocument.CreateTextNode (value));
  158. else
  159. firstChild.Value = value;
  160. }
  161. }
  162. internal override string XmlLang {
  163. get { return OwnerElement.XmlLang; }
  164. }
  165. internal override XmlSpace XmlSpace {
  166. get { return OwnerElement.XmlSpace; }
  167. }
  168. #endregion
  169. #region Methods
  170. public override XmlNode CloneNode (bool deep)
  171. {
  172. XmlNode node = new XmlAttribute (prefix, localName, namespaceURI,
  173. OwnerDocument);
  174. if (deep) {
  175. while ((node != null) && (node.HasChildNodes)) {
  176. AppendChild (node.NextSibling.CloneNode (true));
  177. node = node.NextSibling;
  178. }
  179. }
  180. return node;
  181. }
  182. // Parent of XmlAttribute must be null
  183. internal void SetOwnerElement (XmlElement el) {
  184. ownerElement = el;
  185. }
  186. public override void WriteContentTo (XmlWriter w)
  187. {
  188. w.WriteString (Value);
  189. }
  190. public override void WriteTo (XmlWriter w)
  191. {
  192. w.WriteAttributeString (prefix, localName, namespaceURI, Value);
  193. }
  194. #endregion
  195. internal override XmlLinkedNode LastLinkedChild {
  196. get { return lastChild; }
  197. set { lastChild = value; }
  198. }
  199. }
  200. }