XmlAttribute.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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, IHasXmlChildNode
  41. {
  42. #region Fields
  43. private XmlNameEntry name;
  44. internal bool isDefault;
  45. XmlLinkedNode lastLinkedChild;
  46. #if NET_2_0
  47. private IXmlSchemaInfo schemaInfo;
  48. #endif
  49. #endregion
  50. #region Constructor
  51. protected internal XmlAttribute (
  52. string prefix,
  53. string localName,
  54. string namespaceURI,
  55. XmlDocument doc) : this (prefix, localName, namespaceURI, doc, false, true)
  56. {
  57. }
  58. internal XmlAttribute (
  59. string prefix,
  60. string localName,
  61. string namespaceURI,
  62. XmlDocument doc,
  63. bool atomizedNames, bool checkNamespace) : base (doc)
  64. {
  65. if (!atomizedNames) {
  66. if (prefix == null)
  67. prefix = String.Empty;
  68. if (namespaceURI == null)
  69. namespaceURI = String.Empty;
  70. }
  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. if (!atomizedNames) {
  81. // There are no means to identify the DOM is
  82. // namespace-aware or not, so we can only
  83. // check Name validity.
  84. if (prefix != "" && !XmlChar.IsName (prefix))
  85. throw new ArgumentException ("Invalid attribute prefix.");
  86. else if (!XmlChar.IsName (localName))
  87. throw new ArgumentException ("Invalid attribute local name.");
  88. prefix = doc.NameTable.Add (prefix);
  89. localName = doc.NameTable.Add (localName);
  90. namespaceURI = doc.NameTable.Add (namespaceURI);
  91. }
  92. name = doc.NameCache.Add (prefix, localName, namespaceURI, true);
  93. }
  94. #endregion
  95. #region Properties
  96. XmlLinkedNode IHasXmlChildNode.LastLinkedChild {
  97. get { return lastLinkedChild; }
  98. set { lastLinkedChild = value; }
  99. }
  100. public override string BaseURI {
  101. get { return OwnerElement != null ? OwnerElement.BaseURI : String.Empty; }
  102. }
  103. public override string InnerText {
  104. #if !(NET_2_0)
  105. get {
  106. return base.InnerText;
  107. }
  108. #endif
  109. set {
  110. Value = value;
  111. }
  112. }
  113. public override string InnerXml {
  114. #if !(NET_2_0)
  115. get {
  116. // Not sure why this is an override. Passing through for now.
  117. return base.InnerXml;
  118. }
  119. #endif
  120. set {
  121. RemoveAll ();
  122. XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
  123. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  124. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  125. BaseURI, XmlLang, XmlSpace, null);
  126. XmlTextReader xtr = new XmlTextReader (value, XmlNodeType.Attribute, ctx);
  127. xtr.XmlResolver = OwnerDocument.Resolver;
  128. xtr.Read ();
  129. OwnerDocument.ReadAttributeNodeValue (xtr, this);
  130. }
  131. }
  132. public override string LocalName {
  133. get {
  134. return name.LocalName;
  135. }
  136. }
  137. public override string Name {
  138. get { return name.GetPrefixedName (OwnerDocument.NameCache); }
  139. }
  140. public override string NamespaceURI {
  141. get {
  142. return name.NS;
  143. }
  144. }
  145. public override XmlNodeType NodeType {
  146. get {
  147. return XmlNodeType.Attribute;
  148. }
  149. }
  150. internal override XPathNodeType XPathNodeType {
  151. get {
  152. return XPathNodeType.Attribute;
  153. }
  154. }
  155. public override XmlDocument OwnerDocument {
  156. get {
  157. return base.OwnerDocument;
  158. }
  159. }
  160. public virtual XmlElement OwnerElement {
  161. get { return AttributeOwnerElement; }
  162. }
  163. public override XmlNode ParentNode {
  164. get {
  165. // It always returns null (by specification).
  166. return null;
  167. }
  168. }
  169. // We gotta do more in the set block here
  170. // We need to do the proper tests and throw
  171. // the correct Exceptions
  172. //
  173. // Wrong cases are: (1)check readonly, (2)check character validity,
  174. // (3)check format validity, (4)this is attribute and qualifiedName != "xmlns"
  175. public override string Prefix {
  176. set {
  177. if (IsReadOnly)
  178. throw new XmlException ("This node is readonly.");
  179. if (name.Prefix == "xmlns" && value != "xmlns")
  180. throw new ArgumentException ("Cannot bind to the reserved namespace.");
  181. value = OwnerDocument.NameTable.Add (value);
  182. name = OwnerDocument.NameCache.Add (value,
  183. name.LocalName, name.NS, true);
  184. }
  185. get {
  186. return name.Prefix;
  187. }
  188. }
  189. #if NET_2_0
  190. public override IXmlSchemaInfo SchemaInfo {
  191. get { return schemaInfo; }
  192. internal set { schemaInfo = value; }
  193. }
  194. #endif
  195. public virtual bool Specified {
  196. get {
  197. return !isDefault;
  198. }
  199. }
  200. public override string Value {
  201. get { return InnerText; }
  202. set {
  203. if (this.IsReadOnly)
  204. throw new ArgumentException ("Attempt to modify a read-only node.");
  205. OwnerDocument.CheckIdTableUpdate (this, InnerText, value);
  206. XmlNode textChild = FirstChild as XmlCharacterData;
  207. if (textChild == null) {
  208. this.RemoveAll ();
  209. AppendChild (OwnerDocument.CreateTextNode (value), false);
  210. }
  211. else if (FirstChild.NextSibling != null) {
  212. this.RemoveAll ();
  213. AppendChild (OwnerDocument.CreateTextNode (value), false);
  214. }
  215. else
  216. textChild.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. #if NET_2_0
  229. public override XmlNode AppendChild (XmlNode child)
  230. {
  231. return base.AppendChild (child);
  232. }
  233. public override XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
  234. {
  235. return base.InsertBefore (newChild, refChild);
  236. }
  237. public override XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
  238. {
  239. return base.InsertAfter (newChild, refChild);
  240. }
  241. public override XmlNode PrependChild (XmlNode node)
  242. {
  243. return base.PrependChild (node);
  244. }
  245. public override XmlNode RemoveChild (XmlNode node)
  246. {
  247. return base.RemoveChild (node);
  248. }
  249. public override XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
  250. {
  251. return base.ReplaceChild (newChild, oldChild);
  252. }
  253. #endif
  254. public override XmlNode CloneNode (bool deep)
  255. {
  256. XmlNode node = new XmlAttribute (name.Prefix, name.LocalName, name.NS,
  257. OwnerDocument, true, false);
  258. if (deep) {
  259. for (XmlNode n = FirstChild; n != null; n = n.NextSibling)
  260. node.AppendChild (n.CloneNode (deep), false);
  261. }
  262. return node;
  263. }
  264. internal void SetDefault ()
  265. {
  266. isDefault = true;
  267. }
  268. public override void WriteContentTo (XmlWriter w)
  269. {
  270. for (XmlNode n = FirstChild; n != null; n = n.NextSibling)
  271. n.WriteTo (w);
  272. }
  273. public override void WriteTo (XmlWriter w)
  274. {
  275. if (isDefault)
  276. return; // Write nothing.
  277. w.WriteStartAttribute (name.Prefix, name.LocalName, name.NS);
  278. WriteContentTo (w);
  279. w.WriteEndAttribute ();
  280. }
  281. internal DTDAttributeDefinition GetAttributeDefinition ()
  282. {
  283. if (OwnerElement == null)
  284. return null;
  285. // If it is default, then directly create new attribute.
  286. DTDAttListDeclaration attList = OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD.AttListDecls [OwnerElement.Name] : null;
  287. return attList != null ? attList [Name] : null;
  288. }
  289. #endregion
  290. }
  291. }