XmlAttribute.cs 6.0 KB

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