XmlAttribute.cs 6.6 KB

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