XmlAttribute.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. OwnerDocument.ReadAttributeNodeValue (xtr, this);
  83. }
  84. }
  85. public override string LocalName {
  86. get {
  87. return localName;
  88. }
  89. }
  90. public override string Name {
  91. get {
  92. return prefix != String.Empty ? prefix + ":" + localName : localName;
  93. }
  94. }
  95. public override string NamespaceURI {
  96. get {
  97. return namespaceURI;
  98. }
  99. }
  100. public override XmlNodeType NodeType {
  101. get {
  102. return XmlNodeType.Attribute;
  103. }
  104. }
  105. internal override XPathNodeType XPathNodeType {
  106. get {
  107. return XPathNodeType.Attribute;
  108. }
  109. }
  110. public override XmlDocument OwnerDocument {
  111. get {
  112. return base.OwnerDocument;
  113. }
  114. }
  115. public virtual XmlElement OwnerElement {
  116. get {
  117. return ownerElement;
  118. }
  119. }
  120. public override XmlNode ParentNode {
  121. get {
  122. // It always returns null (by specification).
  123. return null;
  124. }
  125. }
  126. [MonoTODO("setter incomplete (wrong prefix&nsURI)")]
  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. // (5)when argument is 'xml' or 'xmlns' and namespaceURI doesn't match
  134. public override string Prefix {
  135. set {
  136. if (IsReadOnly)
  137. throw new XmlException ("This node is readonly.");
  138. if (!XmlChar.IsNCName (value))
  139. throw new ArgumentException ("Specified name is not a valid NCName: " + value);
  140. prefix = value;
  141. }
  142. get {
  143. return prefix;
  144. }
  145. }
  146. public virtual bool Specified {
  147. get {
  148. return !isDefault;
  149. }
  150. }
  151. public override string Value {
  152. get {
  153. XmlNode firstChild = FirstChild;
  154. if (firstChild == null)
  155. return String.Empty;
  156. return firstChild.Value;
  157. }
  158. set {
  159. XmlNode firstChild = FirstChild;
  160. if (firstChild == null)
  161. AppendChild (OwnerDocument.CreateTextNode (value));
  162. else
  163. firstChild.Value = value;
  164. }
  165. }
  166. internal override string XmlLang {
  167. get { return OwnerElement.XmlLang; }
  168. }
  169. internal override XmlSpace XmlSpace {
  170. get { return OwnerElement.XmlSpace; }
  171. }
  172. #endregion
  173. #region Methods
  174. public override XmlNode CloneNode (bool deep)
  175. {
  176. XmlNode node = new XmlAttribute (prefix, localName, namespaceURI,
  177. OwnerDocument);
  178. if (deep) {
  179. foreach (XmlNode child in this.ChildNodes)
  180. node.AppendChild (child.CloneNode (deep));
  181. }
  182. return node;
  183. }
  184. internal void SetDefault ()
  185. {
  186. isDefault = true;
  187. }
  188. // Parent of XmlAttribute must be null
  189. internal void SetOwnerElement (XmlElement el) {
  190. ownerElement = el;
  191. }
  192. public override void WriteContentTo (XmlWriter w)
  193. {
  194. foreach (XmlNode n in ChildNodes)
  195. n.WriteTo (w);
  196. }
  197. public override void WriteTo (XmlWriter w)
  198. {
  199. w.WriteStartAttribute (prefix, localName, namespaceURI);
  200. WriteContentTo (w);
  201. w.WriteEndAttribute ();
  202. }
  203. #endregion
  204. internal override XmlLinkedNode LastLinkedChild {
  205. get { return lastChild; }
  206. set { lastChild = value; }
  207. }
  208. }
  209. }