XmlAttribute.cs 5.6 KB

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