XmlAttribute.cs 6.1 KB

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