XmlAttribute.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. protected internal XmlAttribute (
  26. string prefix,
  27. string localName,
  28. string namespaceURI,
  29. XmlDocument doc) : base (doc)
  30. {
  31. // I think prefix "xml" should be checked as same, but
  32. // MS.NET ignores such case.
  33. if (prefix == "xmlns" || (prefix == "" && localName == "xmlns"))
  34. if (namespaceURI != "http://www.w3.org/2000/xmlns/")
  35. throw new ArgumentException ("Invalid attribute namespace for namespace declaration.");
  36. // There are no means to identify the DOM is namespace-
  37. // aware or not, so we can only check Name validity.
  38. Exception ex;
  39. if (prefix != "" && !XmlConstructs.IsValidName (prefix, out ex))
  40. throw ex;
  41. else if (!XmlConstructs.IsValidName (localName, out ex))
  42. throw ex;
  43. this.prefix = prefix;
  44. this.localName = localName;
  45. this.namespaceURI = namespaceURI;
  46. }
  47. #endregion
  48. #region Properties
  49. public override string BaseURI {
  50. get {
  51. return OwnerElement.BaseURI;
  52. }
  53. }
  54. public override string InnerText {
  55. get {
  56. StringBuilder builder = new StringBuilder ();
  57. AppendChildValues (this, builder);
  58. return builder.ToString ();
  59. }
  60. set {
  61. Value = value;
  62. }
  63. }
  64. private void AppendChildValues (XmlNode parent, StringBuilder builder)
  65. {
  66. XmlNode node = parent.FirstChild;
  67. while (node != null) {
  68. builder.Append (node.Value);
  69. AppendChildValues (node, builder);
  70. node = node.NextSibling;
  71. }
  72. }
  73. [MonoTODO ("Setter is incomplete(XmlTextReader.ReadAttribute is incomplete;No resolution for xml:lang/space")]
  74. public override string InnerXml {
  75. get {
  76. // Not sure why this is an override. Passing through for now.
  77. return base.InnerXml;
  78. }
  79. set {
  80. XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
  81. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr, XmlLang, this.XmlSpace);
  82. XmlTextReader xtr = OwnerDocument.ReusableReader;
  83. xtr.Initialize (BaseURI, ctx, new System.IO.StringReader ("'" + value.Replace ("'", "'") + "'"), XmlNodeType.Attribute);
  84. OwnerDocument.ReadAttributeNodeValue (xtr, this);
  85. }
  86. }
  87. public override string LocalName {
  88. get {
  89. return localName;
  90. }
  91. }
  92. public override string Name {
  93. get {
  94. return prefix != String.Empty ? prefix + ":" + localName : localName;
  95. }
  96. }
  97. public override string NamespaceURI {
  98. get {
  99. return namespaceURI;
  100. }
  101. }
  102. public override XmlNodeType NodeType {
  103. get {
  104. return XmlNodeType.Attribute;
  105. }
  106. }
  107. internal protected override XPathNodeType XPathNodeType {
  108. get {
  109. return XPathNodeType.Attribute;
  110. }
  111. }
  112. public override XmlDocument OwnerDocument {
  113. get {
  114. return base.OwnerDocument;
  115. }
  116. }
  117. public virtual XmlElement OwnerElement {
  118. get {
  119. return ownerElement;
  120. }
  121. }
  122. public override XmlNode ParentNode {
  123. get {
  124. // It always returns null (by specification).
  125. return null;
  126. }
  127. }
  128. [MonoTODO("setter incomplete (name character check, format check, wrong prefix&nsURI)")]
  129. // We gotta do more in the set block here
  130. // We need to do the proper tests and throw
  131. // the correct Exceptions
  132. //
  133. // Wrong cases are: (1)check readonly, (2)check character validity,
  134. // (3)check format validity, (4)this is attribute and qualifiedName != "xmlns"
  135. // (5)when argument is 'xml' or 'xmlns' and namespaceURI doesn't match
  136. public override string Prefix {
  137. set {
  138. if (IsReadOnly)
  139. throw new XmlException ("This node is readonly.");
  140. Exception ex;
  141. if (!XmlConstructs.IsValidNCName (value, out ex))
  142. throw ex;
  143. prefix = value;
  144. }
  145. get {
  146. return prefix;
  147. }
  148. }
  149. [MonoTODO("There are no code which sets 'specified = true', so this logic is without checking.")]
  150. public virtual bool Specified {
  151. get {
  152. return !isDefault;
  153. }
  154. }
  155. public override string Value {
  156. get {
  157. XmlNode firstChild = FirstChild;
  158. if (firstChild == null)
  159. return String.Empty;
  160. return firstChild.Value;
  161. }
  162. set {
  163. XmlNode firstChild = FirstChild;
  164. if (firstChild == null)
  165. AppendChild (OwnerDocument.CreateTextNode (value));
  166. else
  167. firstChild.Value = value;
  168. }
  169. }
  170. internal protected override string XmlLang {
  171. get { return OwnerElement.XmlLang; }
  172. }
  173. internal protected override XmlSpace XmlSpace {
  174. get { return OwnerElement.XmlSpace; }
  175. }
  176. #endregion
  177. #region Methods
  178. public override XmlNode CloneNode (bool deep)
  179. {
  180. XmlNode node = new XmlAttribute (prefix, localName, namespaceURI,
  181. OwnerDocument);
  182. if (deep) {
  183. while ((node != null) && (node.HasChildNodes)) {
  184. AppendChild (node.NextSibling.CloneNode (true));
  185. node = node.NextSibling;
  186. }
  187. }
  188. return node;
  189. }
  190. // Parent of XmlAttribute must be null
  191. internal void SetOwnerElement (XmlElement el) {
  192. ownerElement = el;
  193. }
  194. public override void WriteContentTo (XmlWriter w)
  195. {
  196. foreach (XmlNode n in ChildNodes)
  197. n.WriteTo (w);
  198. }
  199. public override void WriteTo (XmlWriter w)
  200. {
  201. w.WriteStartAttribute (prefix, localName, namespaceURI);
  202. WriteContentTo (w);
  203. w.WriteEndAttribute ();
  204. }
  205. #endregion
  206. internal protected override XmlLinkedNode LastLinkedChild {
  207. get { return lastChild; }
  208. set { lastChild = value; }
  209. }
  210. }
  211. }