XmlElement.cs 11 KB

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