XmlAttribute.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 {
  139. return name.Prefix != String.Empty ? OwnerDocument.NameTable.Add (name.Prefix + ":" + name.LocalName) : name.LocalName;
  140. }
  141. }
  142. public override string NamespaceURI {
  143. get {
  144. return name.NS;
  145. }
  146. }
  147. public override XmlNodeType NodeType {
  148. get {
  149. return XmlNodeType.Attribute;
  150. }
  151. }
  152. internal override XPathNodeType XPathNodeType {
  153. get {
  154. return XPathNodeType.Attribute;
  155. }
  156. }
  157. public override XmlDocument OwnerDocument {
  158. get {
  159. return base.OwnerDocument;
  160. }
  161. }
  162. public virtual XmlElement OwnerElement {
  163. get { return AttributeOwnerElement; }
  164. }
  165. public override XmlNode ParentNode {
  166. get {
  167. // It always returns null (by specification).
  168. return null;
  169. }
  170. }
  171. // We gotta do more in the set block here
  172. // We need to do the proper tests and throw
  173. // the correct Exceptions
  174. //
  175. // Wrong cases are: (1)check readonly, (2)check character validity,
  176. // (3)check format validity, (4)this is attribute and qualifiedName != "xmlns"
  177. public override string Prefix {
  178. set {
  179. if (IsReadOnly)
  180. throw new XmlException ("This node is readonly.");
  181. if (name.Prefix == "xmlns" && value != "xmlns")
  182. throw new ArgumentException ("Cannot bind to the reserved namespace.");
  183. value = OwnerDocument.NameTable.Add (value);
  184. name = OwnerDocument.NameCache.Add (value,
  185. name.LocalName, name.NS, true);
  186. }
  187. get {
  188. return name.Prefix;
  189. }
  190. }
  191. #if NET_2_0
  192. public override IXmlSchemaInfo SchemaInfo {
  193. get { return schemaInfo; }
  194. internal set { schemaInfo = value; }
  195. }
  196. #endif
  197. public virtual bool Specified {
  198. get {
  199. return !isDefault;
  200. }
  201. }
  202. public override string Value {
  203. get { return InnerText; }
  204. set {
  205. if (this.IsReadOnly)
  206. throw new ArgumentException ("Attempt to modify a read-only node.");
  207. OwnerDocument.CheckIdTableUpdate (this, InnerText, value);
  208. XmlNode textChild = FirstChild as XmlCharacterData;
  209. if (textChild == null) {
  210. this.RemoveAll ();
  211. AppendChild (OwnerDocument.CreateTextNode (value), false);
  212. }
  213. else if (FirstChild.NextSibling != null) {
  214. this.RemoveAll ();
  215. AppendChild (OwnerDocument.CreateTextNode (value), false);
  216. }
  217. else
  218. textChild.Value = value;
  219. isDefault = false;
  220. }
  221. }
  222. internal override string XmlLang {
  223. get { return OwnerElement != null ? OwnerElement.XmlLang : String.Empty; }
  224. }
  225. internal override XmlSpace XmlSpace {
  226. get { return OwnerElement != null ? OwnerElement.XmlSpace : XmlSpace.None; }
  227. }
  228. #endregion
  229. #region Methods
  230. #if NET_2_0
  231. public override XmlNode AppendChild (XmlNode child)
  232. {
  233. return base.AppendChild (child);
  234. }
  235. public override XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
  236. {
  237. return base.InsertBefore (newChild, refChild);
  238. }
  239. public override XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
  240. {
  241. return base.InsertAfter (newChild, refChild);
  242. }
  243. public override XmlNode PrependChild (XmlNode node)
  244. {
  245. return base.PrependChild (node);
  246. }
  247. public override XmlNode RemoveChild (XmlNode node)
  248. {
  249. return base.RemoveChild (node);
  250. }
  251. public override XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
  252. {
  253. return base.ReplaceChild (newChild, oldChild);
  254. }
  255. #endif
  256. public override XmlNode CloneNode (bool deep)
  257. {
  258. XmlNode node = new XmlAttribute (name.Prefix, name.LocalName, name.NS,
  259. OwnerDocument, true, false);
  260. if (deep) {
  261. for (XmlNode n = FirstChild; n != null; n = n.NextSibling)
  262. node.AppendChild (n.CloneNode (deep), false);
  263. }
  264. return node;
  265. }
  266. internal void SetDefault ()
  267. {
  268. isDefault = true;
  269. }
  270. public override void WriteContentTo (XmlWriter w)
  271. {
  272. for (XmlNode n = FirstChild; n != null; n = n.NextSibling)
  273. n.WriteTo (w);
  274. }
  275. public override void WriteTo (XmlWriter w)
  276. {
  277. if (isDefault)
  278. return; // Write nothing.
  279. w.WriteStartAttribute (name.Prefix, name.LocalName, name.NS);
  280. WriteContentTo (w);
  281. w.WriteEndAttribute ();
  282. }
  283. internal DTDAttributeDefinition GetAttributeDefinition ()
  284. {
  285. if (OwnerElement == null)
  286. return null;
  287. // If it is default, then directly create new attribute.
  288. DTDAttListDeclaration attList = OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD.AttListDecls [OwnerElement.Name] : null;
  289. return attList != null ? attList [Name] : null;
  290. }
  291. #endregion
  292. }
  293. }