XmlAttribute.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #if !(NET_2_0)
  97. get {
  98. return base.InnerText;
  99. }
  100. #endif
  101. set {
  102. Value = value;
  103. }
  104. }
  105. public override string InnerXml {
  106. #if !(NET_2_0)
  107. get {
  108. // Not sure why this is an override. Passing through for now.
  109. return base.InnerXml;
  110. }
  111. #endif
  112. set {
  113. RemoveAll ();
  114. XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
  115. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  116. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  117. BaseURI, XmlLang, XmlSpace, null);
  118. XmlTextReader xtr = new XmlTextReader (value, XmlNodeType.Attribute, ctx);
  119. xtr.XmlResolver = OwnerDocument.Resolver;
  120. xtr.Read ();
  121. OwnerDocument.ReadAttributeNodeValue (xtr, this);
  122. }
  123. }
  124. public override string LocalName {
  125. get {
  126. return name.LocalName;
  127. }
  128. }
  129. public override string Name {
  130. get {
  131. return name.Prefix != String.Empty ? OwnerDocument.NameTable.Add (name.Prefix + ":" + name.LocalName) : name.LocalName;
  132. }
  133. }
  134. public override string NamespaceURI {
  135. get {
  136. return name.NS;
  137. }
  138. }
  139. public override XmlNodeType NodeType {
  140. get {
  141. return XmlNodeType.Attribute;
  142. }
  143. }
  144. internal override XPathNodeType XPathNodeType {
  145. get {
  146. return XPathNodeType.Attribute;
  147. }
  148. }
  149. public override XmlDocument OwnerDocument {
  150. get {
  151. return base.OwnerDocument;
  152. }
  153. }
  154. public virtual XmlElement OwnerElement {
  155. get { return AttributeOwnerElement; }
  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 (name.Prefix == "xmlns" && value != "xmlns")
  174. throw new ArgumentException ("Cannot bind to the reserved namespace.");
  175. value = OwnerDocument.NameTable.Add (value);
  176. name = OwnerDocument.NameCache.Add (value,
  177. name.LocalName, name.NS, true);
  178. }
  179. get {
  180. return name.Prefix;
  181. }
  182. }
  183. #if NET_2_0
  184. public override IXmlSchemaInfo SchemaInfo {
  185. get { return schemaInfo; }
  186. internal set { schemaInfo = value; }
  187. }
  188. #endif
  189. public virtual bool Specified {
  190. get {
  191. return !isDefault;
  192. }
  193. }
  194. public override string Value {
  195. get { return InnerText; }
  196. set {
  197. if (this.IsReadOnly)
  198. throw new ArgumentException ("Attempt to modify a read-only node.");
  199. OwnerDocument.CheckIdTableUpdate (this, InnerText, value);
  200. XmlNode textChild = FirstChild as XmlCharacterData;
  201. if (textChild == null) {
  202. this.RemoveAll ();
  203. AppendChild (OwnerDocument.CreateTextNode (value));
  204. }
  205. else if (FirstChild.NextSibling != null) {
  206. this.RemoveAll ();
  207. AppendChild (OwnerDocument.CreateTextNode (value));
  208. }
  209. else
  210. textChild.Value = value;
  211. isDefault = false;
  212. }
  213. }
  214. internal override string XmlLang {
  215. get { return OwnerElement != null ? OwnerElement.XmlLang : String.Empty; }
  216. }
  217. internal override XmlSpace XmlSpace {
  218. get { return OwnerElement != null ? OwnerElement.XmlSpace : XmlSpace.None; }
  219. }
  220. #endregion
  221. #region Methods
  222. #if NET_2_0
  223. public override XmlNode AppendChild (XmlNode child)
  224. {
  225. return base.AppendChild (child);
  226. }
  227. public override XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
  228. {
  229. return base.InsertBefore (newChild, refChild);
  230. }
  231. public override XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
  232. {
  233. return base.InsertAfter (newChild, refChild);
  234. }
  235. public override XmlNode PrependChild (XmlNode node)
  236. {
  237. return base.PrependChild (node);
  238. }
  239. public override XmlNode RemoveChild (XmlNode node)
  240. {
  241. return base.RemoveChild (node);
  242. }
  243. public override XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
  244. {
  245. return base.ReplaceChild (newChild, oldChild);
  246. }
  247. #endif
  248. public override XmlNode CloneNode (bool deep)
  249. {
  250. XmlNode node = new XmlAttribute (name.Prefix, name.LocalName, name.NS,
  251. OwnerDocument, true, false);
  252. if (deep) {
  253. for (int i = 0; i < ChildNodes.Count; i++)
  254. node.AppendChild (ChildNodes [i].CloneNode (deep));
  255. }
  256. return node;
  257. }
  258. internal void SetDefault ()
  259. {
  260. isDefault = true;
  261. }
  262. public override void WriteContentTo (XmlWriter w)
  263. {
  264. for (int i = 0; i < ChildNodes.Count; i++)
  265. ChildNodes [i].WriteTo (w);
  266. }
  267. public override void WriteTo (XmlWriter w)
  268. {
  269. if (isDefault)
  270. return; // Write nothing.
  271. w.WriteStartAttribute (name.Prefix, name.LocalName, name.NS);
  272. WriteContentTo (w);
  273. w.WriteEndAttribute ();
  274. }
  275. internal DTDAttributeDefinition GetAttributeDefinition ()
  276. {
  277. if (OwnerElement == null)
  278. return null;
  279. // If it is default, then directly create new attribute.
  280. DTDAttListDeclaration attList = OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD.AttListDecls [OwnerElement.Name] : null;
  281. return attList != null ? attList [Name] : null;
  282. }
  283. #endregion
  284. }
  285. }