XmlAttribute.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 XmlNameEntry name;
  44. internal bool isDefault;
  45. #if NET_2_0
  46. private IXmlSchemaInfo schemaInfo;
  47. #endif
  48. #endregion
  49. #region Constructor
  50. protected internal XmlAttribute (
  51. string prefix,
  52. string localName,
  53. string namespaceURI,
  54. XmlDocument doc) : this (prefix, localName, namespaceURI, doc, false, true)
  55. {
  56. }
  57. internal XmlAttribute (
  58. string prefix,
  59. string localName,
  60. string namespaceURI,
  61. XmlDocument doc,
  62. bool atomizedNames, bool checkNamespace) : base (doc)
  63. {
  64. if (prefix == null)
  65. prefix = String.Empty;
  66. if (namespaceURI == null)
  67. namespaceURI = String.Empty;
  68. // Prefix "xml" should be also checked (http://www.w3.org/XML/xml-names-19990114-errata#NE05)
  69. // but MS.NET ignores such case
  70. if (checkNamespace) {
  71. if (prefix == "xmlns" || (prefix == "" && localName == "xmlns"))
  72. if (namespaceURI != XmlNamespaceManager.XmlnsXmlns)
  73. throw new ArgumentException ("Invalid attribute namespace for namespace declaration.");
  74. else if (prefix == "xml" && namespaceURI != XmlNamespaceManager.XmlnsXml)
  75. throw new ArgumentException ("Invalid attribute namespace for namespace declaration.");
  76. }
  77. // There are no means to identify the DOM is namespace-
  78. // aware or not, so we can only check Name validity.
  79. if (prefix != "" && !XmlChar.IsName (prefix))
  80. throw new ArgumentException ("Invalid attribute prefix.");
  81. else if (!XmlChar.IsName (localName))
  82. throw new ArgumentException ("Invalid attribute local name.");
  83. if (!atomizedNames) {
  84. prefix = doc.NameTable.Add (prefix);
  85. localName = doc.NameTable.Add (localName);
  86. namespaceURI = doc.NameTable.Add (namespaceURI);
  87. }
  88. name = doc.NameCache.Add (prefix, localName, namespaceURI, true);
  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 name.LocalName;
  123. }
  124. }
  125. public override string Name {
  126. get {
  127. return name.Prefix != String.Empty ? OwnerDocument.NameTable.Add (name.Prefix + ":" + name.LocalName) : name.LocalName;
  128. }
  129. }
  130. public override string NamespaceURI {
  131. get {
  132. return name.NS;
  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 AttributeOwnerElement; }
  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 (name.Prefix == "xmlns" && value != "xmlns")
  172. throw new ArgumentException ("Cannot bind to the reserved namespace.");
  173. value = OwnerDocument.NameTable.Add (value);
  174. name = OwnerDocument.NameCache.Add (value,
  175. name.LocalName, name.NS, true);
  176. }
  177. get {
  178. return name.Prefix;
  179. }
  180. }
  181. #if NET_2_0
  182. public override IXmlSchemaInfo SchemaInfo {
  183. get { return schemaInfo; }
  184. internal set { schemaInfo = value; }
  185. }
  186. #endif
  187. public virtual bool Specified {
  188. get {
  189. return !isDefault;
  190. }
  191. }
  192. private string BuildChildValue (XmlNodeList list)
  193. {
  194. string ret = String.Empty;
  195. for (int i = 0; i < list.Count; i++) {
  196. if (list [i].NodeType == XmlNodeType.EntityReference)
  197. ret += BuildChildValue (list [i].ChildNodes);
  198. else
  199. ret += list [i].Value;
  200. }
  201. return ret;
  202. }
  203. public override string Value {
  204. get { return BuildChildValue (ChildNodes); }
  205. set {
  206. if (this.IsReadOnly)
  207. throw new ArgumentException ("Attempt to modify a read-only node.");
  208. XmlNode firstChild = FirstChild;
  209. if (firstChild == null)
  210. AppendChild (OwnerDocument.CreateTextNode (value));
  211. else if (FirstChild.NextSibling != null) {
  212. this.RemoveAll ();
  213. AppendChild (OwnerDocument.CreateTextNode (value));
  214. }
  215. else
  216. firstChild.Value = value;
  217. isDefault = false;
  218. }
  219. }
  220. internal override string XmlLang {
  221. get { return OwnerElement != null ? OwnerElement.XmlLang : String.Empty; }
  222. }
  223. internal override XmlSpace XmlSpace {
  224. get { return OwnerElement != null ? OwnerElement.XmlSpace : XmlSpace.None; }
  225. }
  226. #endregion
  227. #region Methods
  228. public override XmlNode CloneNode (bool deep)
  229. {
  230. XmlNode node = new XmlAttribute (name.Prefix, name.LocalName, name.NS,
  231. OwnerDocument, true, false);
  232. if (deep) {
  233. for (int i = 0; i < ChildNodes.Count; i++)
  234. node.AppendChild (ChildNodes [i].CloneNode (deep));
  235. }
  236. if (IsReadOnly)
  237. node.SetReadOnly ();
  238. return node;
  239. }
  240. internal void SetDefault ()
  241. {
  242. isDefault = true;
  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 (name.Prefix, name.LocalName, name.NS);
  252. WriteContentTo (w);
  253. w.WriteEndAttribute ();
  254. }
  255. internal DTDAttributeDefinition GetAttributeDefinition ()
  256. {
  257. if (OwnerElement == null)
  258. return null;
  259. // If it is default, then directly create new attribute.
  260. DTDAttListDeclaration attList = OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD.AttListDecls [OwnerElement.Name] : null;
  261. return attList != null ? attList [Name] : null;
  262. }
  263. #endregion
  264. }
  265. }