XmlAttribute.cs 6.1 KB

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