XmlAttribute.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. if (prefix != "" && !XmlChar.IsName (prefix))
  47. throw new ArgumentException ("Invalid attribute prefix.");
  48. else if (!XmlChar.IsName (localName))
  49. throw new ArgumentException ("Invalid attribute local name.");
  50. this.prefix = prefix;
  51. this.localName = localName;
  52. this.namespaceURI = namespaceURI;
  53. }
  54. #endregion
  55. #region Properties
  56. public override string BaseURI {
  57. get {
  58. return OwnerElement.BaseURI;
  59. }
  60. }
  61. public override string InnerText {
  62. get {
  63. return base.InnerText;
  64. }
  65. set {
  66. Value = value;
  67. }
  68. }
  69. public override string InnerXml {
  70. get {
  71. // Not sure why this is an override. Passing through for now.
  72. return base.InnerXml;
  73. }
  74. set {
  75. RemoveAll ();
  76. XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
  77. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  78. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  79. BaseURI, XmlLang, XmlSpace, null);
  80. XmlTextReader xtr = new XmlTextReader (value, XmlNodeType.Attribute, ctx);
  81. xtr.XmlResolver = OwnerDocument.Resolver;
  82. xtr.Read ();
  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. // We gotta do more in the set block here
  128. // We need to do the proper tests and throw
  129. // the correct Exceptions
  130. //
  131. // Wrong cases are: (1)check readonly, (2)check character validity,
  132. // (3)check format validity, (4)this is attribute and qualifiedName != "xmlns"
  133. public override string Prefix {
  134. set {
  135. if (IsReadOnly)
  136. throw new XmlException ("This node is readonly.");
  137. if (!XmlChar.IsNCName (value))
  138. throw new ArgumentException ("Specified name is not a valid NCName: " + value);
  139. prefix = value;
  140. }
  141. get {
  142. return prefix;
  143. }
  144. }
  145. public virtual bool Specified {
  146. get {
  147. return !isDefault;
  148. }
  149. }
  150. public override string Value {
  151. get {
  152. XmlNode firstChild = FirstChild;
  153. if (firstChild == null)
  154. return String.Empty;
  155. return firstChild.Value;
  156. }
  157. set {
  158. XmlNode firstChild = FirstChild;
  159. if (firstChild == null)
  160. AppendChild (OwnerDocument.CreateTextNode (value));
  161. else
  162. firstChild.Value = value;
  163. }
  164. }
  165. internal override string XmlLang {
  166. get { return OwnerElement.XmlLang; }
  167. }
  168. internal override XmlSpace XmlSpace {
  169. get { return OwnerElement.XmlSpace; }
  170. }
  171. #endregion
  172. #region Methods
  173. public override XmlNode CloneNode (bool deep)
  174. {
  175. XmlNode node = new XmlAttribute (prefix, localName, namespaceURI,
  176. OwnerDocument);
  177. if (deep) {
  178. foreach (XmlNode child in this.ChildNodes)
  179. node.AppendChild (child.CloneNode (deep));
  180. }
  181. return node;
  182. }
  183. internal void SetDefault ()
  184. {
  185. isDefault = true;
  186. }
  187. // Parent of XmlAttribute must be null
  188. internal void SetOwnerElement (XmlElement el) {
  189. ownerElement = el;
  190. }
  191. public override void WriteContentTo (XmlWriter w)
  192. {
  193. foreach (XmlNode n in ChildNodes)
  194. n.WriteTo (w);
  195. }
  196. public override void WriteTo (XmlWriter w)
  197. {
  198. w.WriteStartAttribute (prefix, localName, namespaceURI);
  199. WriteContentTo (w);
  200. w.WriteEndAttribute ();
  201. }
  202. #endregion
  203. internal override XmlLinkedNode LastLinkedChild {
  204. get { return lastChild; }
  205. set { lastChild = value; }
  206. }
  207. }
  208. }