XmlAttribute.cs 8.2 KB

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