XmlAttribute.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Text;
  33. using System.Xml.XPath;
  34. using Mono.Xml;
  35. namespace System.Xml
  36. {
  37. public class XmlAttribute : XmlNode
  38. {
  39. #region Fields
  40. private string localName;
  41. private string namespaceURI;
  42. private string prefix;
  43. internal bool isDefault;
  44. private XmlElement ownerElement;
  45. #endregion
  46. #region Constructor
  47. protected internal XmlAttribute (
  48. string prefix,
  49. string localName,
  50. string namespaceURI,
  51. XmlDocument doc) : this (prefix, localName, namespaceURI, doc, false, true)
  52. {
  53. }
  54. internal XmlAttribute (
  55. string prefix,
  56. string localName,
  57. string namespaceURI,
  58. XmlDocument doc,
  59. bool atomizedNames, bool checkNamespace) : base (doc)
  60. {
  61. if (prefix == null)
  62. prefix = String.Empty;
  63. if (namespaceURI == null)
  64. namespaceURI = String.Empty;
  65. // Prefix "xml" should be also checked (http://www.w3.org/XML/xml-names-19990114-errata#NE05)
  66. // but MS.NET ignores such case
  67. if (checkNamespace) {
  68. if (prefix == "xmlns" || (prefix == "" && localName == "xmlns"))
  69. if (namespaceURI != XmlNamespaceManager.XmlnsXmlns)
  70. throw new ArgumentException ("Invalid attribute namespace for namespace declaration.");
  71. else if (prefix == "xml" && namespaceURI != XmlNamespaceManager.XmlnsXml)
  72. throw new ArgumentException ("Invalid attribute namespace for namespace declaration.");
  73. }
  74. // There are no means to identify the DOM is namespace-
  75. // aware or not, so we can only check Name validity.
  76. if (prefix != "" && !XmlChar.IsName (prefix))
  77. throw new ArgumentException ("Invalid attribute prefix.");
  78. else if (!XmlChar.IsName (localName))
  79. throw new ArgumentException ("Invalid attribute local name.");
  80. if (atomizedNames) {
  81. this.prefix = prefix;
  82. this.localName = localName;
  83. this.namespaceURI = namespaceURI;
  84. } else {
  85. this.prefix = doc.NameTable.Add (prefix);
  86. this.localName = doc.NameTable.Add (localName);
  87. this.namespaceURI = doc.NameTable.Add (namespaceURI);
  88. }
  89. }
  90. #endregion
  91. #region Properties
  92. public override string BaseURI {
  93. get { return OwnerElement != null ? OwnerElement.BaseURI : String.Empty; }
  94. }
  95. public override string InnerText {
  96. get {
  97. return base.InnerText;
  98. }
  99. set {
  100. Value = value;
  101. }
  102. }
  103. public override string InnerXml {
  104. get {
  105. // Not sure why this is an override. Passing through for now.
  106. return base.InnerXml;
  107. }
  108. set {
  109. RemoveAll ();
  110. XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
  111. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  112. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  113. BaseURI, XmlLang, XmlSpace, null);
  114. XmlTextReader xtr = new XmlTextReader (value, XmlNodeType.Attribute, ctx);
  115. xtr.XmlResolver = OwnerDocument.Resolver;
  116. xtr.Read ();
  117. OwnerDocument.ReadAttributeNodeValue (xtr, this);
  118. }
  119. }
  120. public override string LocalName {
  121. get {
  122. return localName;
  123. }
  124. }
  125. public override string Name {
  126. get {
  127. return prefix != String.Empty ? prefix + ":" + localName : localName;
  128. }
  129. }
  130. public override string NamespaceURI {
  131. get {
  132. return namespaceURI;
  133. }
  134. }
  135. public override XmlNodeType NodeType {
  136. get {
  137. return XmlNodeType.Attribute;
  138. }
  139. }
  140. internal override XPathNodeType XPathNodeType {
  141. get {
  142. return XPathNodeType.Attribute;
  143. }
  144. }
  145. public override XmlDocument OwnerDocument {
  146. get {
  147. return base.OwnerDocument;
  148. }
  149. }
  150. public virtual XmlElement OwnerElement {
  151. get { return ownerElement; }
  152. }
  153. public override XmlNode ParentNode {
  154. get {
  155. // It always returns null (by specification).
  156. return null;
  157. }
  158. }
  159. // We gotta do more in the set block here
  160. // We need to do the proper tests and throw
  161. // the correct Exceptions
  162. //
  163. // Wrong cases are: (1)check readonly, (2)check character validity,
  164. // (3)check format validity, (4)this is attribute and qualifiedName != "xmlns"
  165. public override string Prefix {
  166. set {
  167. if (IsReadOnly)
  168. throw new XmlException ("This node is readonly.");
  169. if (!XmlChar.IsNCName (value))
  170. throw new ArgumentException ("Specified name is not a valid NCName: " + value);
  171. if (prefix == "xmlns" && value != "xmlns")
  172. throw new ArgumentException ("Cannot bind to the reserved namespace.");
  173. prefix = OwnerDocument.NameTable.Add (value);
  174. }
  175. get {
  176. return prefix;
  177. }
  178. }
  179. public virtual bool Specified {
  180. get {
  181. return !isDefault;
  182. }
  183. }
  184. private string BuildChildValue (XmlNodeList list)
  185. {
  186. string ret = String.Empty;
  187. for (int i = 0; i < list.Count; i++) {
  188. if (list [i].NodeType == XmlNodeType.EntityReference)
  189. ret += BuildChildValue (list [i].ChildNodes);
  190. else
  191. ret += list [i].Value;
  192. }
  193. return ret;
  194. }
  195. public override string Value {
  196. get { return BuildChildValue (ChildNodes); }
  197. set {
  198. if (this.IsReadOnly)
  199. throw new ArgumentException ("Attempt to modify a read-only node.");
  200. XmlNode firstChild = FirstChild;
  201. if (firstChild == null)
  202. AppendChild (OwnerDocument.CreateTextNode (value));
  203. else if (FirstChild.NextSibling != null) {
  204. this.RemoveAll ();
  205. AppendChild (OwnerDocument.CreateTextNode (value));
  206. }
  207. else
  208. firstChild.Value = value;
  209. isDefault = false;
  210. }
  211. }
  212. internal override string XmlLang {
  213. get { return OwnerElement != null ? OwnerElement.XmlLang : String.Empty; }
  214. }
  215. internal override XmlSpace XmlSpace {
  216. get { return OwnerElement != null ? OwnerElement.XmlSpace : XmlSpace.None; }
  217. }
  218. #endregion
  219. #region Methods
  220. public override XmlNode CloneNode (bool deep)
  221. {
  222. XmlNode node = new XmlAttribute (prefix, localName, namespaceURI,
  223. OwnerDocument, true, false);
  224. if (deep) {
  225. for (int i = 0; i < ChildNodes.Count; i++)
  226. node.AppendChild (ChildNodes [i].CloneNode (deep));
  227. }
  228. if (IsReadOnly)
  229. node.SetReadOnly ();
  230. return node;
  231. }
  232. internal void SetDefault ()
  233. {
  234. isDefault = true;
  235. }
  236. // Parent of XmlAttribute must be null
  237. internal void SetOwnerElement (XmlElement el) {
  238. ownerElement = el;
  239. }
  240. public override void WriteContentTo (XmlWriter w)
  241. {
  242. for (int i = 0; i < ChildNodes.Count; i++)
  243. ChildNodes [i].WriteTo (w);
  244. }
  245. public override void WriteTo (XmlWriter w)
  246. {
  247. w.WriteStartAttribute (prefix, localName, namespaceURI);
  248. WriteContentTo (w);
  249. w.WriteEndAttribute ();
  250. }
  251. internal DTDAttributeDefinition GetAttributeDefinition ()
  252. {
  253. if (OwnerElement == null)
  254. return null;
  255. // If it is default, then directly create new attribute.
  256. DTDAttListDeclaration attList = OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD.AttListDecls [OwnerElement.Name] : null;
  257. return attList != null ? attList [Name] : null;
  258. }
  259. #endregion
  260. }
  261. }