2
0

XmlAttribute.cs 5.7 KB

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