XmlElement.cs 11 KB

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