XmlElement.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 (ChildNodes != null && ChildNodes.Count == 1 && FirstChild.NodeType == XmlNodeType.Text)
  103. FirstChild.Value = value;
  104. else {
  105. while (FirstChild != null)
  106. this.RemoveChild (FirstChild);
  107. // creates new Text node
  108. AppendChild (OwnerDocument.CreateTextNode (value));
  109. }
  110. }
  111. }
  112. public override string InnerXml {
  113. get {
  114. return base.InnerXml;
  115. }
  116. set {
  117. while (FirstChild != null)
  118. this.RemoveChild (FirstChild);
  119. XmlNameTable nt = this.OwnerDocument.NameTable;
  120. XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
  121. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  122. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  123. BaseURI, XmlLang, XmlSpace, null);
  124. XmlTextReader xmlReader = new XmlTextReader (value, XmlNodeType.Element, ctx);
  125. xmlReader.XmlResolver = OwnerDocument.Resolver;
  126. do {
  127. XmlNode n = OwnerDocument.ReadNode (xmlReader);
  128. if(n == null) break;
  129. AppendChild (n);
  130. } while (true);
  131. }
  132. }
  133. public bool IsEmpty {
  134. get {
  135. return !isNotEmpty && (FirstChild == null);
  136. }
  137. set {
  138. isNotEmpty = !value;
  139. if(value) {
  140. while (FirstChild != null)
  141. RemoveChild (FirstChild);
  142. }
  143. }
  144. }
  145. public override string LocalName {
  146. get { return localName; }
  147. }
  148. public override string Name {
  149. get {
  150. if (prefix == String.Empty || prefix == null)
  151. return localName;
  152. else
  153. return prefix + ":" + localName;
  154. }
  155. }
  156. public override string NamespaceURI {
  157. get { return namespaceURI; }
  158. }
  159. // Why is this override?
  160. public override XmlNode NextSibling {
  161. get {
  162. return base.NextSibling;
  163. }
  164. }
  165. public override XmlNodeType NodeType {
  166. get {
  167. return XmlNodeType.Element;
  168. }
  169. }
  170. internal override XPathNodeType XPathNodeType {
  171. get {
  172. return XPathNodeType.Element;
  173. }
  174. }
  175. public override XmlDocument OwnerDocument {
  176. get {
  177. return base.OwnerDocument;
  178. }
  179. }
  180. public override string Prefix {
  181. get { return prefix; }
  182. set {
  183. if (IsReadOnly)
  184. throw new XmlException ("This node is readonly.");
  185. if (!XmlChar.IsNCName (value))
  186. throw new ArgumentException ("Specified name is not a valid NCName: " + value);
  187. prefix = OwnerDocument.NameTable.Add (value);
  188. }
  189. }
  190. #endregion
  191. #region Methods
  192. public override XmlNode CloneNode (bool deep)
  193. {
  194. XmlElement node = new XmlElement (
  195. prefix, localName, namespaceURI, OwnerDocument, true);
  196. for (int i = 0; i < Attributes.Count; i++)
  197. node.SetAttributeNode ((XmlAttribute)
  198. Attributes [i].CloneNode (true));
  199. if (deep) {
  200. for (int i = 0; i < ChildNodes.Count; i++)
  201. node.AppendChild (ChildNodes [i].CloneNode (true));
  202. }
  203. if (IsReadOnly)
  204. node.SetReadOnly ();
  205. return node;
  206. }
  207. public virtual string GetAttribute (string name)
  208. {
  209. XmlNode attributeNode = Attributes.GetNamedItem (name);
  210. return attributeNode != null ? attributeNode.Value : String.Empty;
  211. }
  212. public virtual string GetAttribute (string localName, string namespaceURI)
  213. {
  214. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  215. return attributeNode != null ? attributeNode.Value : String.Empty;
  216. }
  217. public virtual XmlAttribute GetAttributeNode (string name)
  218. {
  219. XmlNode attributeNode = Attributes.GetNamedItem (name);
  220. return attributeNode != null ? attributeNode as XmlAttribute : null;
  221. }
  222. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  223. {
  224. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  225. return attributeNode != null ? attributeNode as XmlAttribute : null;
  226. }
  227. public virtual XmlNodeList GetElementsByTagName (string name)
  228. {
  229. ArrayList nodeArrayList = new ArrayList ();
  230. this.SearchDescendantElements (name, name == "*", nodeArrayList);
  231. return new XmlNodeArrayList (nodeArrayList);
  232. }
  233. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  234. {
  235. ArrayList nodeArrayList = new ArrayList ();
  236. this.SearchDescendantElements (localName, localName == "*", namespaceURI, namespaceURI == "*", nodeArrayList);
  237. return new XmlNodeArrayList (nodeArrayList);
  238. }
  239. public virtual bool HasAttribute (string name)
  240. {
  241. XmlNode attributeNode = Attributes.GetNamedItem (name);
  242. return attributeNode != null;
  243. }
  244. public virtual bool HasAttribute (string localName, string namespaceURI)
  245. {
  246. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  247. return attributeNode != null;
  248. }
  249. public override void RemoveAll ()
  250. {
  251. // Remove all attributes and child nodes.
  252. base.RemoveAll ();
  253. }
  254. public virtual void RemoveAllAttributes ()
  255. {
  256. attributes.RemoveAll ();
  257. }
  258. public virtual void RemoveAttribute (string name)
  259. {
  260. XmlAttribute attr = attributes.GetNamedItem (name) as XmlAttribute;
  261. if (attr != null)
  262. attributes.Remove(attr);
  263. }
  264. public virtual void RemoveAttribute (string localName, string namespaceURI)
  265. {
  266. XmlAttribute attr = attributes.GetNamedItem(localName, namespaceURI) as XmlAttribute;
  267. if (attr != null)
  268. attributes.Remove(attr);
  269. }
  270. public virtual XmlNode RemoveAttributeAt (int i)
  271. {
  272. return attributes.RemoveAt (i);
  273. }
  274. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  275. {
  276. return attributes.Remove(oldAttr);
  277. }
  278. public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
  279. {
  280. return attributes.Remove(attributes[localName, namespaceURI]);
  281. }
  282. public virtual void SetAttribute (string name, string value)
  283. {
  284. XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
  285. attribute.Value = value;
  286. Attributes.SetNamedItem (attribute);
  287. }
  288. public virtual string SetAttribute (string localName, string namespaceURI, string value)
  289. {
  290. XmlAttribute attr = attributes [localName, namespaceURI];
  291. if (attr == null) {
  292. attr = OwnerDocument.CreateAttribute (localName, namespaceURI);
  293. attr.Value = value;
  294. attributes.SetNamedItem (attr);
  295. }
  296. else
  297. attr.Value = value;
  298. return attr.Value;
  299. }
  300. public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
  301. {
  302. if (newAttr.OwnerElement != null)
  303. throw new InvalidOperationException (
  304. "Specified attribute is already an attribute of another element.");
  305. XmlAttribute ret = Attributes.SetNamedItem (newAttr) as XmlAttribute;
  306. return ret == newAttr ? null : ret;
  307. }
  308. public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
  309. {
  310. // Note that this constraint is only for this method.
  311. // SetAttribute() allows prefixed name.
  312. XmlConvert.VerifyNCName (localName);
  313. return Attributes.Append (OwnerDocument.CreateAttribute (String.Empty, localName, namespaceURI, false, true));
  314. }
  315. public override void WriteContentTo (XmlWriter w)
  316. {
  317. int count = ChildNodes.Count;
  318. for (int i = 0; i < count; i++)
  319. ChildNodes [i].WriteTo (w);
  320. }
  321. public override void WriteTo (XmlWriter w)
  322. {
  323. w.WriteStartElement (NamespaceURI == null || NamespaceURI.Length == 0 ? String.Empty : Prefix, LocalName, NamespaceURI);
  324. for (int i = 0; i < Attributes.Count; i++)
  325. if (Attributes [i].Specified)
  326. Attributes [i].WriteTo(w);
  327. if (IsEmpty)
  328. w.WriteEndElement ();
  329. else {
  330. WriteContentTo (w);
  331. w.WriteFullEndElement ();
  332. }
  333. }
  334. #endregion
  335. }
  336. }