XmlElement.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 = new XmlTextReader (value, XmlNodeType.Element, ctx);
  81. do {
  82. XmlNode n = OwnerDocument.ReadNode (xmlReader);
  83. if(n == null) break;
  84. AppendChild (n);
  85. } while (true);
  86. }
  87. }
  88. public bool IsEmpty {
  89. get {
  90. return !isNotEmpty && (FirstChild == null);
  91. }
  92. set {
  93. isNotEmpty = !value;
  94. if(value)
  95. RemoveAll();
  96. }
  97. }
  98. public override string LocalName {
  99. get { return localName; }
  100. }
  101. public override string Name {
  102. get {
  103. if (prefix == String.Empty || prefix == null)
  104. return localName;
  105. else
  106. return prefix + ":" + localName;
  107. }
  108. }
  109. public override string NamespaceURI {
  110. get { return namespaceURI; }
  111. }
  112. // Why is this override?
  113. public override XmlNode NextSibling {
  114. get {
  115. return base.NextSibling;
  116. }
  117. }
  118. public override XmlNodeType NodeType {
  119. get {
  120. return XmlNodeType.Element;
  121. }
  122. }
  123. internal override XPathNodeType XPathNodeType {
  124. get {
  125. return XPathNodeType.Element;
  126. }
  127. }
  128. [MonoTODO]
  129. public override XmlDocument OwnerDocument {
  130. get {
  131. return base.OwnerDocument;
  132. }
  133. }
  134. public override string Prefix {
  135. get { return prefix; }
  136. set {
  137. if (IsReadOnly)
  138. throw new XmlException ("This node is readonly.");
  139. Exception ex;
  140. if (!XmlConstructs.IsValidNCName (value, out ex))
  141. throw ex;
  142. prefix = value;
  143. }
  144. }
  145. #endregion
  146. #region Methods
  147. [MonoTODO]
  148. public override XmlNode CloneNode (bool deep)
  149. {
  150. XmlNode node = new XmlElement (prefix, localName, namespaceURI,
  151. OwnerDocument);
  152. for (int i = 0; i < node.Attributes.Count; i++)
  153. node.AppendChild (node.Attributes [i].CloneNode (false));
  154. if (deep) {
  155. while ((node != null) && (node.HasChildNodes)) {
  156. AppendChild (node.NextSibling.CloneNode (true));
  157. node = node.NextSibling;
  158. }
  159. } // shallow cloning
  160. //
  161. // Reminder: Also look into Default attributes.
  162. //
  163. return node;
  164. }
  165. [MonoTODO]
  166. public virtual string GetAttribute (string name)
  167. {
  168. XmlNode attributeNode = Attributes.GetNamedItem (name);
  169. return attributeNode != null ? attributeNode.Value : String.Empty;
  170. }
  171. [MonoTODO]
  172. public virtual string GetAttribute (string localName, string namespaceURI)
  173. {
  174. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  175. return attributeNode != null ? attributeNode.Value : String.Empty;
  176. }
  177. [MonoTODO]
  178. public virtual XmlAttribute GetAttributeNode (string name)
  179. {
  180. XmlNode attributeNode = Attributes.GetNamedItem (name);
  181. return attributeNode != null ? attributeNode as XmlAttribute : null;
  182. }
  183. [MonoTODO]
  184. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  185. {
  186. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  187. return attributeNode != null ? attributeNode as XmlAttribute : null;
  188. }
  189. public virtual XmlNodeList GetElementsByTagName (string name)
  190. {
  191. ArrayList nodeArrayList = new ArrayList ();
  192. this.searchNodesRecursively (this, name, nodeArrayList);
  193. return new XmlNodeArrayList (nodeArrayList);
  194. }
  195. private void searchNodesRecursively (XmlNode argNode, string argName,
  196. ArrayList argArrayList)
  197. {
  198. XmlNodeList xmlNodeList = argNode.ChildNodes;
  199. foreach (XmlNode node in xmlNodeList){
  200. if (node.Name.Equals (argName))
  201. argArrayList.Add (node);
  202. else
  203. this.searchNodesRecursively (node, argName, argArrayList);
  204. }
  205. }
  206. private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI,
  207. ArrayList argArrayList)
  208. {
  209. XmlNodeList xmlNodeList = argNode.ChildNodes;
  210. foreach (XmlNode node in xmlNodeList)
  211. {
  212. if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
  213. argArrayList.Add (node);
  214. else
  215. this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
  216. }
  217. }
  218. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  219. {
  220. ArrayList nodeArrayList = new ArrayList ();
  221. this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
  222. return new XmlNodeArrayList (nodeArrayList);
  223. }
  224. [MonoTODO]
  225. public virtual bool HasAttribute (string name)
  226. {
  227. XmlNode attributeNode = Attributes.GetNamedItem (name);
  228. return attributeNode != null;
  229. }
  230. [MonoTODO]
  231. public virtual bool HasAttribute (string localName, string namespaceURI)
  232. {
  233. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  234. return attributeNode != null;
  235. }
  236. [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
  237. public override void RemoveAll ()
  238. {
  239. // Remove the child nodes.
  240. base.RemoveAll ();
  241. // Remove all attributes.
  242. attributes.RemoveAll ();
  243. }
  244. [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
  245. public virtual void RemoveAllAttributes ()
  246. {
  247. attributes.RemoveAll ();
  248. }
  249. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  250. public virtual void RemoveAttribute (string name)
  251. {
  252. XmlAttribute attr = attributes.GetNamedItem (name) as XmlAttribute;
  253. if (attr != null)
  254. attributes.Remove(attr);
  255. }
  256. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  257. public virtual void RemoveAttribute (string localName, string namespaceURI)
  258. {
  259. XmlAttribute attr = attributes.GetNamedItem(localName, namespaceURI) as XmlAttribute;
  260. if (attr != null)
  261. attributes.Remove(attr);
  262. }
  263. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  264. public virtual XmlNode RemoveAttributeAt (int i)
  265. {
  266. return attributes.RemoveAt (i);
  267. }
  268. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  269. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  270. {
  271. return attributes.Remove(oldAttr);
  272. }
  273. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  274. public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
  275. {
  276. return attributes.Remove(attributes[localName, namespaceURI]);
  277. }
  278. [MonoTODO]
  279. public virtual void SetAttribute (string name, string value)
  280. {
  281. XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
  282. attribute.SetOwnerElement(this);
  283. attribute.Value = value;
  284. Attributes.SetNamedItem (attribute);
  285. }
  286. // [MonoTODO]
  287. public virtual string SetAttribute (string localName, string namespaceURI, string value)
  288. {
  289. XmlAttribute attr = attributes[localName, namespaceURI];
  290. if(attr == null)
  291. {
  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. // [MonoTODO]
  301. public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
  302. {
  303. newAttr.SetOwnerElement(this);
  304. XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
  305. return oldAttr != null ? oldAttr as XmlAttribute : null;
  306. }
  307. public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
  308. {
  309. XmlDocument xmlDoc = this.OwnerDocument;
  310. XmlAttribute xmlAttribute = new XmlAttribute (String.Empty, localName, namespaceURI, xmlDoc);
  311. return this.attributes.Append (xmlAttribute);
  312. }
  313. public override void WriteContentTo (XmlWriter w)
  314. {
  315. foreach(XmlNode childNode in ChildNodes)
  316. childNode.WriteTo(w);
  317. }
  318. [MonoTODO]
  319. public override void WriteTo (XmlWriter w)
  320. {
  321. w.WriteStartElement(Prefix, LocalName, NamespaceURI);
  322. foreach(XmlNode attributeNode in Attributes)
  323. attributeNode.WriteTo(w);
  324. // write namespace declarations(if not exist)
  325. foreach(XmlNode attributeNode in Attributes) {
  326. if(attributeNode.Prefix != null && attributeNode.Prefix != String.Empty &&
  327. w.LookupPrefix(attributeNode.NamespaceURI) != attributeNode.Prefix &&
  328. attributeNode.Prefix != "xmlns")
  329. w.WriteAttributeString("xmlns", attributeNode.Prefix, "http://www.w3.org/2000/xmlns/", attributeNode.NamespaceURI);
  330. }
  331. if (IsEmpty)
  332. w.WriteEndElement ();
  333. else {
  334. WriteContentTo(w);
  335. w.WriteFullEndElement();
  336. }
  337. }
  338. #endregion
  339. }
  340. }