XmlElement.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. using System;
  12. using System.Collections;
  13. using System.Xml.XPath;
  14. using System.IO;
  15. using System.Text;
  16. namespace System.Xml
  17. {
  18. public class XmlElement : XmlLinkedNode
  19. {
  20. #region Fields
  21. private XmlAttributeCollection attributes;
  22. private string localName;
  23. private string namespaceURI;
  24. private string prefix;
  25. private bool isNotEmpty;
  26. #endregion
  27. #region Constructor
  28. protected internal XmlElement (
  29. string prefix,
  30. string localName,
  31. string namespaceURI,
  32. XmlDocument doc) : base (doc)
  33. {
  34. this.prefix = prefix;
  35. this.localName = localName;
  36. this.namespaceURI = namespaceURI;
  37. attributes = new XmlAttributeCollection (this);
  38. // TODO: Adds default attributes
  39. if(doc.DocumentType != null)
  40. {
  41. }
  42. }
  43. #endregion
  44. #region Properties
  45. public override XmlAttributeCollection Attributes {
  46. get { return attributes; }
  47. }
  48. public virtual bool HasAttributes {
  49. get { return attributes.Count > 0; }
  50. }
  51. public override string InnerText {
  52. get {
  53. return base.InnerText;
  54. }
  55. set {
  56. // Why its behavior (of MS FCL) is different from InnerXml...?
  57. if (FirstChild != null && FirstChild.NodeType == XmlNodeType.Text)
  58. FirstChild.Value = value;
  59. else {
  60. if(FirstChild != null) {
  61. foreach (XmlNode n in ChildNodes)
  62. this.RemoveChild (n);
  63. }
  64. // creates new Text node
  65. AppendChild(OwnerDocument.CreateTextNode(value));
  66. }
  67. }
  68. }
  69. public override string InnerXml {
  70. get {
  71. return base.InnerXml;
  72. }
  73. set {
  74. while (FirstChild != null)
  75. this.RemoveChild (FirstChild);
  76. // I hope there are any well-performance logic...
  77. XmlNameTable nt = this.OwnerDocument.NameTable;
  78. XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
  79. XmlParserContext ctx = new XmlParserContext (nt, nsmgr, XmlLang, this.XmlSpace);
  80. XmlTextReader xmlReader = OwnerDocument.ReusableReader;
  81. xmlReader.Initialize (String.Empty, ctx, new StringReader (value), XmlNodeType.Element);
  82. do {
  83. XmlNode n = OwnerDocument.ReadNode (xmlReader);
  84. if(n == null) break;
  85. AppendChild (n);
  86. } while (true);
  87. }
  88. }
  89. public bool IsEmpty {
  90. get {
  91. return !isNotEmpty && (FirstChild == null);
  92. }
  93. set {
  94. isNotEmpty = !value;
  95. if(value)
  96. RemoveAll();
  97. }
  98. }
  99. public override string LocalName {
  100. get { return localName; }
  101. }
  102. public override string Name {
  103. get {
  104. if (prefix == String.Empty || prefix == null)
  105. return localName;
  106. else
  107. return prefix + ":" + localName;
  108. }
  109. }
  110. public override string NamespaceURI {
  111. get { return namespaceURI; }
  112. }
  113. [MonoTODO]
  114. public override XmlNode NextSibling {
  115. get {
  116. return base.NextSibling;
  117. }
  118. }
  119. public override XmlNodeType NodeType {
  120. get {
  121. return XmlNodeType.Element;
  122. }
  123. }
  124. internal protected override XPathNodeType XPathNodeType {
  125. get {
  126. return XPathNodeType.Element;
  127. }
  128. }
  129. [MonoTODO]
  130. public override XmlDocument OwnerDocument {
  131. get {
  132. return base.OwnerDocument;
  133. }
  134. }
  135. public override string Prefix {
  136. get { return prefix; }
  137. set {
  138. if (IsReadOnly)
  139. throw new XmlException ("This node is readonly.");
  140. Exception ex;
  141. if (!XmlConstructs.IsValidNCName (value, out ex))
  142. throw ex;
  143. prefix = value;
  144. }
  145. }
  146. #endregion
  147. #region Methods
  148. [MonoTODO]
  149. public override XmlNode CloneNode (bool deep)
  150. {
  151. XmlNode node = new XmlElement (prefix, localName, namespaceURI,
  152. OwnerDocument);
  153. for (int i = 0; i < node.Attributes.Count; i++)
  154. node.AppendChild (node.Attributes [i].CloneNode (false));
  155. if (deep) {
  156. while ((node != null) && (node.HasChildNodes)) {
  157. AppendChild (node.NextSibling.CloneNode (true));
  158. node = node.NextSibling;
  159. }
  160. } // shallow cloning
  161. //
  162. // Reminder: Also look into Default attributes.
  163. //
  164. return node;
  165. }
  166. [MonoTODO]
  167. public virtual string GetAttribute (string name)
  168. {
  169. XmlNode attributeNode = Attributes.GetNamedItem (name);
  170. return attributeNode != null ? attributeNode.Value : String.Empty;
  171. }
  172. [MonoTODO]
  173. public virtual string GetAttribute (string localName, string namespaceURI)
  174. {
  175. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  176. return attributeNode != null ? attributeNode.Value : String.Empty;
  177. }
  178. [MonoTODO]
  179. public virtual XmlAttribute GetAttributeNode (string name)
  180. {
  181. XmlNode attributeNode = Attributes.GetNamedItem (name);
  182. return attributeNode != null ? attributeNode as XmlAttribute : null;
  183. }
  184. [MonoTODO]
  185. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  186. {
  187. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  188. return attributeNode != null ? attributeNode as XmlAttribute : null;
  189. }
  190. public virtual XmlNodeList GetElementsByTagName (string name)
  191. {
  192. ArrayList nodeArrayList = new ArrayList ();
  193. this.searchNodesRecursively (this, name, nodeArrayList);
  194. return new XmlNodeArrayList (nodeArrayList);
  195. }
  196. private void searchNodesRecursively (XmlNode argNode, string argName,
  197. ArrayList argArrayList)
  198. {
  199. XmlNodeList xmlNodeList = argNode.ChildNodes;
  200. foreach (XmlNode node in xmlNodeList){
  201. if (node.Name.Equals (argName))
  202. argArrayList.Add (node);
  203. else
  204. this.searchNodesRecursively (node, argName, argArrayList);
  205. }
  206. }
  207. private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI,
  208. ArrayList argArrayList)
  209. {
  210. XmlNodeList xmlNodeList = argNode.ChildNodes;
  211. foreach (XmlNode node in xmlNodeList)
  212. {
  213. if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
  214. argArrayList.Add (node);
  215. else
  216. this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
  217. }
  218. }
  219. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  220. {
  221. ArrayList nodeArrayList = new ArrayList ();
  222. this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
  223. return new XmlNodeArrayList (nodeArrayList);
  224. }
  225. [MonoTODO]
  226. public virtual bool HasAttribute (string name)
  227. {
  228. XmlNode attributeNode = Attributes.GetNamedItem (name);
  229. return attributeNode != null;
  230. }
  231. [MonoTODO]
  232. public virtual bool HasAttribute (string localName, string namespaceURI)
  233. {
  234. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  235. return attributeNode != null;
  236. }
  237. [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
  238. public override void RemoveAll ()
  239. {
  240. // Remove the child nodes.
  241. base.RemoveAll ();
  242. // Remove all attributes.
  243. attributes.RemoveAll ();
  244. }
  245. [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
  246. public virtual void RemoveAllAttributes ()
  247. {
  248. attributes.RemoveAll ();
  249. }
  250. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  251. public virtual void RemoveAttribute (string name)
  252. {
  253. XmlAttribute attr = attributes.GetNamedItem (name) as XmlAttribute;
  254. if (attr != null)
  255. attributes.Remove(attr);
  256. }
  257. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  258. public virtual void RemoveAttribute (string localName, string namespaceURI)
  259. {
  260. XmlAttribute attr = attributes.GetNamedItem(localName, namespaceURI) as XmlAttribute;
  261. if (attr != null)
  262. attributes.Remove(attr);
  263. }
  264. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  265. public virtual XmlNode RemoveAttributeAt (int i)
  266. {
  267. return attributes.RemoveAt (i);
  268. }
  269. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  270. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  271. {
  272. return attributes.Remove(oldAttr);
  273. }
  274. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  275. public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
  276. {
  277. return attributes.Remove(attributes[localName, namespaceURI]);
  278. }
  279. [MonoTODO]
  280. public virtual void SetAttribute (string name, string value)
  281. {
  282. XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
  283. attribute.SetOwnerElement(this);
  284. attribute.Value = value;
  285. Attributes.SetNamedItem (attribute);
  286. }
  287. // [MonoTODO]
  288. public virtual string SetAttribute (string localName, string namespaceURI, string value)
  289. {
  290. XmlAttribute attr = attributes[localName, namespaceURI];
  291. if(attr == null)
  292. {
  293. attr = OwnerDocument.CreateAttribute(localName, namespaceURI);
  294. attr.Value = value;
  295. attributes.SetNamedItem(attr);
  296. }
  297. else
  298. attr.Value = value;
  299. return attr.Value;
  300. }
  301. // [MonoTODO]
  302. public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
  303. {
  304. newAttr.SetOwnerElement(this);
  305. XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
  306. return oldAttr != null ? oldAttr as XmlAttribute : null;
  307. }
  308. public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
  309. {
  310. XmlDocument xmlDoc = this.OwnerDocument;
  311. XmlAttribute xmlAttribute = new XmlAttribute (String.Empty, localName, namespaceURI, xmlDoc);
  312. return this.attributes.Append (xmlAttribute);
  313. }
  314. public override void WriteContentTo (XmlWriter w)
  315. {
  316. foreach(XmlNode childNode in ChildNodes)
  317. childNode.WriteTo(w);
  318. }
  319. [MonoTODO]
  320. public override void WriteTo (XmlWriter w)
  321. {
  322. w.WriteStartElement(Prefix, LocalName, NamespaceURI);
  323. foreach(XmlNode attributeNode in Attributes)
  324. attributeNode.WriteTo(w);
  325. // write namespace declarations(if not exist)
  326. foreach(XmlNode attributeNode in Attributes) {
  327. if(attributeNode.Prefix != null && attributeNode.Prefix != String.Empty &&
  328. w.LookupPrefix(attributeNode.NamespaceURI) != attributeNode.Prefix &&
  329. attributeNode.Prefix != "xmlns")
  330. w.WriteAttributeString("xmlns", attributeNode.Prefix, "http://www.w3.org/2000/xmlns/", attributeNode.NamespaceURI);
  331. }
  332. if (IsEmpty)
  333. w.WriteEndElement ();
  334. else {
  335. WriteContentTo(w);
  336. w.WriteFullEndElement();
  337. }
  338. }
  339. #endregion
  340. }
  341. }