XmlAttribute.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 {
  94. return OwnerElement.BaseURI;
  95. }
  96. }
  97. public override string InnerText {
  98. get {
  99. return base.InnerText;
  100. }
  101. set {
  102. Value = value;
  103. }
  104. }
  105. public override string InnerXml {
  106. get {
  107. // Not sure why this is an override. Passing through for now.
  108. return base.InnerXml;
  109. }
  110. set {
  111. RemoveAll ();
  112. XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
  113. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  114. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  115. BaseURI, XmlLang, XmlSpace, null);
  116. XmlTextReader xtr = new XmlTextReader (value, XmlNodeType.Attribute, ctx);
  117. xtr.XmlResolver = OwnerDocument.Resolver;
  118. xtr.Read ();
  119. OwnerDocument.ReadAttributeNodeValue (xtr, this);
  120. }
  121. }
  122. public override string LocalName {
  123. get {
  124. return localName;
  125. }
  126. }
  127. public override string Name {
  128. get {
  129. return prefix != String.Empty ? prefix + ":" + localName : localName;
  130. }
  131. }
  132. public override string NamespaceURI {
  133. get {
  134. return namespaceURI;
  135. }
  136. }
  137. public override XmlNodeType NodeType {
  138. get {
  139. return XmlNodeType.Attribute;
  140. }
  141. }
  142. internal override XPathNodeType XPathNodeType {
  143. get {
  144. return XPathNodeType.Attribute;
  145. }
  146. }
  147. public override XmlDocument OwnerDocument {
  148. get {
  149. return base.OwnerDocument;
  150. }
  151. }
  152. public virtual XmlElement OwnerElement {
  153. get {
  154. return ownerElement;
  155. }
  156. }
  157. public override XmlNode ParentNode {
  158. get {
  159. // It always returns null (by specification).
  160. return null;
  161. }
  162. }
  163. // We gotta do more in the set block here
  164. // We need to do the proper tests and throw
  165. // the correct Exceptions
  166. //
  167. // Wrong cases are: (1)check readonly, (2)check character validity,
  168. // (3)check format validity, (4)this is attribute and qualifiedName != "xmlns"
  169. public override string Prefix {
  170. set {
  171. if (IsReadOnly)
  172. throw new XmlException ("This node is readonly.");
  173. if (!XmlChar.IsNCName (value))
  174. throw new ArgumentException ("Specified name is not a valid NCName: " + value);
  175. if (prefix == "xmlns" && value != "xmlns")
  176. throw new ArgumentException ("Cannot bind to the reserved namespace.");
  177. prefix = OwnerDocument.NameTable.Add (value);
  178. }
  179. get {
  180. return prefix;
  181. }
  182. }
  183. public virtual bool Specified {
  184. get {
  185. return !isDefault;
  186. }
  187. }
  188. private string BuildChildValue (XmlNodeList list)
  189. {
  190. string ret = String.Empty;
  191. for (int i = 0; i < list.Count; i++) {
  192. if (list [i].NodeType == XmlNodeType.EntityReference)
  193. ret += BuildChildValue (list [i].ChildNodes);
  194. else
  195. ret += list [i].Value;
  196. }
  197. return ret;
  198. }
  199. public override string Value {
  200. get { return BuildChildValue (ChildNodes); }
  201. set {
  202. if (this.IsReadOnly)
  203. throw new ArgumentException ("Attempt to modify a read-only node.");
  204. XmlNode firstChild = FirstChild;
  205. if (firstChild == null)
  206. AppendChild (OwnerDocument.CreateTextNode (value));
  207. else if (FirstChild.NextSibling != null) {
  208. this.RemoveAll ();
  209. AppendChild (OwnerDocument.CreateTextNode (value));
  210. }
  211. else
  212. firstChild.Value = value;
  213. isDefault = false;
  214. }
  215. }
  216. internal override string XmlLang {
  217. get { return OwnerElement.XmlLang; }
  218. }
  219. internal override XmlSpace XmlSpace {
  220. get { return OwnerElement.XmlSpace; }
  221. }
  222. #endregion
  223. #region Methods
  224. public override XmlNode CloneNode (bool deep)
  225. {
  226. XmlNode node = new XmlAttribute (prefix, localName, namespaceURI,
  227. OwnerDocument, true, false);
  228. if (deep) {
  229. for (int i = 0; i < ChildNodes.Count; i++)
  230. node.AppendChild (ChildNodes [i].CloneNode (deep));
  231. }
  232. if (IsReadOnly)
  233. node.SetReadOnly ();
  234. return node;
  235. }
  236. internal void SetDefault ()
  237. {
  238. isDefault = true;
  239. }
  240. // Parent of XmlAttribute must be null
  241. internal void SetOwnerElement (XmlElement el) {
  242. ownerElement = el;
  243. }
  244. public override void WriteContentTo (XmlWriter w)
  245. {
  246. for (int i = 0; i < ChildNodes.Count; i++)
  247. ChildNodes [i].WriteTo (w);
  248. }
  249. public override void WriteTo (XmlWriter w)
  250. {
  251. w.WriteStartAttribute (prefix, localName, namespaceURI);
  252. WriteContentTo (w);
  253. w.WriteEndAttribute ();
  254. }
  255. internal DTDAttributeDefinition GetAttributeDefinition ()
  256. {
  257. // If it is default, then directly create new attribute.
  258. DTDAttListDeclaration attList = OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD.AttListDecls [OwnerElement.Name] : null;
  259. return attList != null ? attList [Name] : null;
  260. }
  261. #endregion
  262. }
  263. }