XmlElement.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. //
  2. // System.Xml.XmlElement
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C) 2002 Jason Diamond http://injektilo.org/
  9. // (C) 2002 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.Collections;
  33. using System.Xml.XPath;
  34. using System.IO;
  35. using System.Text;
  36. using Mono.Xml;
  37. namespace System.Xml
  38. {
  39. public class XmlElement : XmlLinkedNode
  40. {
  41. #region Fields
  42. private XmlAttributeCollection attributes;
  43. private string localName;
  44. private string namespaceURI;
  45. private string prefix;
  46. private bool isNotEmpty;
  47. #endregion
  48. #region Constructor
  49. protected internal XmlElement (
  50. string prefix,
  51. string localName,
  52. string namespaceURI,
  53. XmlDocument doc) : this (prefix, localName, namespaceURI, doc, false)
  54. {
  55. }
  56. internal XmlElement (
  57. string prefix,
  58. string localName,
  59. string namespaceURI,
  60. XmlDocument doc,
  61. bool atomizedNames) : base (doc)
  62. {
  63. XmlConvert.VerifyName (localName);
  64. if (atomizedNames) {
  65. this.prefix = prefix;
  66. this.localName = localName;
  67. this.namespaceURI = namespaceURI;
  68. } else {
  69. this.prefix = doc.NameTable.Add (prefix);
  70. this.localName = doc.NameTable.Add (localName);
  71. this.namespaceURI = doc.NameTable.Add (namespaceURI);
  72. }
  73. attributes = new XmlAttributeCollection (this);
  74. if(doc.DocumentType != null)
  75. {
  76. DTDAttListDeclaration attlist = doc.DocumentType.DTD.AttListDecls [localName];
  77. if (attlist != null) {
  78. for (int i = 0; i < attlist.Definitions.Count; i++) {
  79. DTDAttributeDefinition def = attlist [i];
  80. if (def.DefaultValue != null) {
  81. SetAttribute (def.Name, def.DefaultValue);
  82. attributes [def.Name].SetDefault ();
  83. }
  84. }
  85. }
  86. }
  87. }
  88. #endregion
  89. #region Properties
  90. public override XmlAttributeCollection Attributes {
  91. get { return attributes; }
  92. }
  93. public virtual bool HasAttributes {
  94. get { return attributes.Count > 0; }
  95. }
  96. public override string InnerText {
  97. get {
  98. return base.InnerText;
  99. }
  100. set {
  101. // Why its behavior (of MS FCL) is different from InnerXml...?
  102. if (FirstChild != null && FirstChild.NodeType == XmlNodeType.Text)
  103. FirstChild.Value = value;
  104. else {
  105. if (FirstChild != null) {
  106. for (int i = 0; i < ChildNodes.Count; i++)
  107. this.RemoveChild (ChildNodes [i]);
  108. }
  109. // creates new Text node
  110. AppendChild (OwnerDocument.CreateTextNode (value));
  111. }
  112. }
  113. }
  114. public override string InnerXml {
  115. get {
  116. return base.InnerXml;
  117. }
  118. set {
  119. while (FirstChild != null)
  120. this.RemoveChild (FirstChild);
  121. XmlNameTable nt = this.OwnerDocument.NameTable;
  122. XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
  123. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  124. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  125. BaseURI, XmlLang, XmlSpace, null);
  126. XmlTextReader xmlReader = new XmlTextReader (value, XmlNodeType.Element, ctx);
  127. xmlReader.XmlResolver = OwnerDocument.Resolver;
  128. do {
  129. XmlNode n = OwnerDocument.ReadNode (xmlReader);
  130. if(n == null) break;
  131. AppendChild (n);
  132. } while (true);
  133. }
  134. }
  135. public bool IsEmpty {
  136. get {
  137. return !isNotEmpty && (FirstChild == null);
  138. }
  139. set {
  140. isNotEmpty = !value;
  141. if(value) {
  142. while (FirstChild != null)
  143. RemoveChild (FirstChild);
  144. }
  145. }
  146. }
  147. public override string LocalName {
  148. get { return localName; }
  149. }
  150. public override string Name {
  151. get {
  152. if (prefix == String.Empty || prefix == null)
  153. return localName;
  154. else
  155. return prefix + ":" + localName;
  156. }
  157. }
  158. public override string NamespaceURI {
  159. get { return namespaceURI; }
  160. }
  161. // Why is this override?
  162. public override XmlNode NextSibling {
  163. get {
  164. return base.NextSibling;
  165. }
  166. }
  167. public override XmlNodeType NodeType {
  168. get {
  169. return XmlNodeType.Element;
  170. }
  171. }
  172. internal override XPathNodeType XPathNodeType {
  173. get {
  174. return XPathNodeType.Element;
  175. }
  176. }
  177. public override XmlDocument OwnerDocument {
  178. get {
  179. return base.OwnerDocument;
  180. }
  181. }
  182. public override string Prefix {
  183. get { return prefix; }
  184. set {
  185. if (IsReadOnly)
  186. throw new XmlException ("This node is readonly.");
  187. if (!XmlChar.IsNCName (value))
  188. throw new ArgumentException ("Specified name is not a valid NCName: " + value);
  189. prefix = OwnerDocument.NameTable.Add (value);
  190. }
  191. }
  192. #endregion
  193. #region Methods
  194. public override XmlNode CloneNode (bool deep)
  195. {
  196. XmlElement node = new XmlElement (
  197. prefix, localName, namespaceURI, OwnerDocument, true);
  198. for (int i = 0; i < Attributes.Count; i++)
  199. node.SetAttributeNode ((XmlAttribute)
  200. Attributes [i].CloneNode (true));
  201. if (deep) {
  202. for (int i = 0; i < ChildNodes.Count; i++)
  203. node.AppendChild (ChildNodes [i].CloneNode (true));
  204. }
  205. if (IsReadOnly)
  206. node.SetReadOnly ();
  207. return node;
  208. }
  209. public virtual string GetAttribute (string name)
  210. {
  211. XmlNode attributeNode = Attributes.GetNamedItem (name);
  212. return attributeNode != null ? attributeNode.Value : String.Empty;
  213. }
  214. public virtual string GetAttribute (string localName, string namespaceURI)
  215. {
  216. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  217. return attributeNode != null ? attributeNode.Value : String.Empty;
  218. }
  219. public virtual XmlAttribute GetAttributeNode (string name)
  220. {
  221. XmlNode attributeNode = Attributes.GetNamedItem (name);
  222. return attributeNode != null ? attributeNode as XmlAttribute : null;
  223. }
  224. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  225. {
  226. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  227. return attributeNode != null ? attributeNode as XmlAttribute : null;
  228. }
  229. public virtual XmlNodeList GetElementsByTagName (string name)
  230. {
  231. ArrayList nodeArrayList = new ArrayList ();
  232. this.SearchDescendantElements (name, name == "*", nodeArrayList);
  233. return new XmlNodeArrayList (nodeArrayList);
  234. }
  235. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  236. {
  237. ArrayList nodeArrayList = new ArrayList ();
  238. this.SearchDescendantElements (localName, localName == "*", namespaceURI, namespaceURI == "*", nodeArrayList);
  239. return new XmlNodeArrayList (nodeArrayList);
  240. }
  241. public virtual bool HasAttribute (string name)
  242. {
  243. XmlNode attributeNode = Attributes.GetNamedItem (name);
  244. return attributeNode != null;
  245. }
  246. public virtual bool HasAttribute (string localName, string namespaceURI)
  247. {
  248. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  249. return attributeNode != null;
  250. }
  251. public override void RemoveAll ()
  252. {
  253. // Remove all attributes and child nodes.
  254. base.RemoveAll ();
  255. }
  256. public virtual void RemoveAllAttributes ()
  257. {
  258. attributes.RemoveAll ();
  259. }
  260. public virtual void RemoveAttribute (string name)
  261. {
  262. XmlAttribute attr = attributes.GetNamedItem (name) as XmlAttribute;
  263. if (attr != null)
  264. attributes.Remove(attr);
  265. }
  266. public virtual void RemoveAttribute (string localName, string namespaceURI)
  267. {
  268. XmlAttribute attr = attributes.GetNamedItem(localName, namespaceURI) as XmlAttribute;
  269. if (attr != null)
  270. attributes.Remove(attr);
  271. }
  272. public virtual XmlNode RemoveAttributeAt (int i)
  273. {
  274. return attributes.RemoveAt (i);
  275. }
  276. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  277. {
  278. return attributes.Remove(oldAttr);
  279. }
  280. public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
  281. {
  282. return attributes.Remove(attributes[localName, namespaceURI]);
  283. }
  284. public virtual void SetAttribute (string name, string value)
  285. {
  286. XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
  287. attribute.Value = value;
  288. Attributes.SetNamedItem (attribute);
  289. }
  290. public virtual string SetAttribute (string localName, string namespaceURI, string value)
  291. {
  292. XmlAttribute attr = attributes [localName, namespaceURI];
  293. if (attr == null) {
  294. attr = OwnerDocument.CreateAttribute (localName, namespaceURI);
  295. attr.Value = value;
  296. attributes.SetNamedItem (attr);
  297. }
  298. else
  299. attr.Value = value;
  300. return attr.Value;
  301. }
  302. public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
  303. {
  304. if (newAttr.OwnerElement != null)
  305. throw new InvalidOperationException (
  306. "Specified attribute is already an attribute of another element.");
  307. XmlAttribute ret = Attributes.SetNamedItem (newAttr) as XmlAttribute;
  308. return ret == newAttr ? null : ret;
  309. }
  310. public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
  311. {
  312. // Note that this constraint is only for this method.
  313. // SetAttribute() allows prefixed name.
  314. XmlConvert.VerifyNCName (localName);
  315. return Attributes.Append (OwnerDocument.CreateAttribute (String.Empty, localName, namespaceURI, false, true));
  316. }
  317. public override void WriteContentTo (XmlWriter w)
  318. {
  319. int count = ChildNodes.Count;
  320. for (int i = 0; i < count; i++)
  321. ChildNodes [i].WriteTo (w);
  322. }
  323. public override void WriteTo (XmlWriter w)
  324. {
  325. w.WriteStartElement (NamespaceURI == null || NamespaceURI.Length == 0 ? String.Empty : Prefix, LocalName, NamespaceURI);
  326. for (int i = 0; i < Attributes.Count; i++)
  327. if (Attributes [i].Specified)
  328. Attributes [i].WriteTo(w);
  329. if (IsEmpty)
  330. w.WriteEndElement ();
  331. else {
  332. WriteContentTo (w);
  333. w.WriteFullEndElement ();
  334. }
  335. }
  336. #endregion
  337. }
  338. }