XmlElement.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 isEmpty;
  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. foreach(XmlNode n in ChildNodes)
  57. {
  58. this.RemoveChild(n);
  59. }
  60. // creates new Text node
  61. AppendChild(OwnerDocument.CreateTextNode(value));
  62. }
  63. }
  64. [MonoTODO ("Setter is immature")]
  65. public override string InnerXml {
  66. get {
  67. // Not sure why this is an override. Passing through for now.
  68. return base.InnerXml;
  69. }
  70. set {
  71. foreach(XmlNode n in ChildNodes)
  72. {
  73. this.RemoveChild(n);
  74. }
  75. // I hope there are any well-performance logic...
  76. XmlNameTable nt = this.OwnerDocument.NameTable;
  77. XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
  78. XmlParserContext ctx = new XmlParserContext (nt, nsmgr, XmlLang, this.XmlSpace);
  79. XmlTextReader xmlReader = OwnerDocument.ReusableReader;
  80. xmlReader.SetReaderContext (String.Empty, ctx);
  81. xmlReader.SetReaderFragment (new StringReader (value), XmlNodeType.Element);
  82. this.ConstructDOM (xmlReader, this);
  83. }
  84. }
  85. public bool IsEmpty {
  86. get { return isEmpty; }
  87. set {
  88. if(value) {
  89. RemoveAll();
  90. }
  91. isEmpty = value;
  92. }
  93. }
  94. public override string LocalName {
  95. get { return localName; }
  96. }
  97. public override string Name {
  98. get {
  99. return prefix != String.Empty ? prefix + ":" + localName : localName;
  100. }
  101. }
  102. public override string NamespaceURI {
  103. get { return namespaceURI; }
  104. }
  105. [MonoTODO]
  106. public override XmlNode NextSibling {
  107. get {
  108. return base.NextSibling;
  109. }
  110. }
  111. public override XmlNodeType NodeType {
  112. get {
  113. return XmlNodeType.Element;
  114. }
  115. }
  116. internal override XPathNodeType XPathNodeType {
  117. get {
  118. return XPathNodeType.Element;
  119. }
  120. }
  121. [MonoTODO]
  122. public override XmlDocument OwnerDocument {
  123. get {
  124. return base.OwnerDocument;
  125. }
  126. }
  127. public override string Prefix {
  128. get { return prefix; }
  129. set { prefix = value; }
  130. }
  131. #endregion
  132. #region Methods
  133. [MonoTODO]
  134. public override XmlNode CloneNode (bool deep)
  135. {
  136. XmlNode node = new XmlElement (prefix, localName, namespaceURI,
  137. OwnerDocument);
  138. for (int i = 0; i < node.Attributes.Count; i++)
  139. node.AppendChild (node.Attributes [i].CloneNode (false));
  140. if (deep) {
  141. while ((node != null) && (node.HasChildNodes)) {
  142. AppendChild (node.NextSibling.CloneNode (true));
  143. node = node.NextSibling;
  144. }
  145. } // shallow cloning
  146. //
  147. // Reminder: Also look into Default attributes.
  148. //
  149. return node;
  150. }
  151. [MonoTODO]
  152. public virtual string GetAttribute (string name)
  153. {
  154. XmlNode attributeNode = Attributes.GetNamedItem (name);
  155. return attributeNode != null ? attributeNode.Value : String.Empty;
  156. }
  157. [MonoTODO]
  158. public virtual string GetAttribute (string localName, string namespaceURI)
  159. {
  160. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  161. return attributeNode != null ? attributeNode.Value : String.Empty;
  162. }
  163. [MonoTODO]
  164. public virtual XmlAttribute GetAttributeNode (string name)
  165. {
  166. XmlNode attributeNode = Attributes.GetNamedItem (name);
  167. return attributeNode != null ? attributeNode as XmlAttribute : null;
  168. }
  169. [MonoTODO]
  170. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  171. {
  172. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  173. return attributeNode != null ? attributeNode as XmlAttribute : null;
  174. }
  175. public virtual XmlNodeList GetElementsByTagName (string name)
  176. {
  177. ArrayList nodeArrayList = new ArrayList ();
  178. this.searchNodesRecursively (this, name, nodeArrayList);
  179. return new XmlNodeArrayList (nodeArrayList);
  180. }
  181. private void searchNodesRecursively (XmlNode argNode, string argName,
  182. ArrayList argArrayList)
  183. {
  184. XmlNodeList xmlNodeList = argNode.ChildNodes;
  185. foreach (XmlNode node in xmlNodeList){
  186. if (node.Name.Equals (argName))
  187. argArrayList.Add (node);
  188. else
  189. this.searchNodesRecursively (node, argName, argArrayList);
  190. }
  191. }
  192. private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI,
  193. ArrayList argArrayList)
  194. {
  195. XmlNodeList xmlNodeList = argNode.ChildNodes;
  196. foreach (XmlNode node in xmlNodeList)
  197. {
  198. if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
  199. argArrayList.Add (node);
  200. else
  201. this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
  202. }
  203. }
  204. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  205. {
  206. ArrayList nodeArrayList = new ArrayList ();
  207. this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
  208. return new XmlNodeArrayList (nodeArrayList);
  209. }
  210. [MonoTODO]
  211. public virtual bool HasAttribute (string name)
  212. {
  213. XmlNode attributeNode = Attributes.GetNamedItem (name);
  214. return attributeNode != null;
  215. }
  216. [MonoTODO]
  217. public virtual bool HasAttribute (string localName, string namespaceURI)
  218. {
  219. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  220. return attributeNode != null;
  221. }
  222. [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
  223. public override void RemoveAll ()
  224. {
  225. // Remove the child nodes.
  226. base.RemoveAll ();
  227. // Remove all attributes.
  228. attributes.RemoveAll ();
  229. }
  230. [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
  231. public virtual void RemoveAllAttributes ()
  232. {
  233. attributes.RemoveAll ();
  234. }
  235. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  236. public virtual void RemoveAttribute (string name)
  237. {
  238. attributes.Remove((XmlAttribute)attributes.GetNamedItem(name));
  239. }
  240. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  241. public virtual void RemoveAttribute (string localName, string namespaceURI)
  242. {
  243. attributes.Remove((XmlAttribute)attributes.GetNamedItem(localName, namespaceURI));
  244. }
  245. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  246. public virtual XmlNode RemoveAttributeAt (int i)
  247. {
  248. return attributes.Remove(attributes[i]);
  249. }
  250. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  251. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  252. {
  253. return attributes.Remove(oldAttr);
  254. }
  255. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  256. public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
  257. {
  258. return attributes.Remove(attributes[localName, namespaceURI]);
  259. }
  260. [MonoTODO]
  261. public virtual void SetAttribute (string name, string value)
  262. {
  263. XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
  264. attribute.SetOwnerElement(this);
  265. attribute.Value = value;
  266. Attributes.SetNamedItem (attribute);
  267. }
  268. // [MonoTODO]
  269. public virtual string SetAttribute (string localName, string namespaceURI, string value)
  270. {
  271. XmlAttribute attr = attributes[localName, namespaceURI];
  272. if(attr == null)
  273. {
  274. attr = OwnerDocument.CreateAttribute(localName, namespaceURI);
  275. attr.Value = value;
  276. attributes.SetNamedItem(attr);
  277. }
  278. else
  279. attr.Value = value;
  280. return attr.Value;
  281. }
  282. // [MonoTODO]
  283. public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
  284. {
  285. newAttr.SetOwnerElement(this);
  286. XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
  287. return oldAttr != null ? oldAttr as XmlAttribute : null;
  288. }
  289. public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
  290. {
  291. XmlDocument xmlDoc = this.OwnerDocument;
  292. XmlAttribute xmlAttribute = new XmlAttribute (String.Empty, localName, namespaceURI, xmlDoc);
  293. return this.attributes.Append (xmlAttribute);
  294. }
  295. public override void WriteContentTo (XmlWriter w)
  296. {
  297. foreach(XmlNode childNode in ChildNodes)
  298. childNode.WriteTo(w);
  299. }
  300. [MonoTODO("indenting feature is incomplete.")]
  301. public override void WriteTo (XmlWriter w)
  302. {
  303. w.WriteStartElement(Prefix, LocalName, NamespaceURI);
  304. // write namespace declarations(if not exist)
  305. if(Prefix != null && Prefix != String.Empty && w.LookupPrefix(Prefix) != NamespaceURI)
  306. w.WriteAttributeString("xmlns", Prefix, "http://www.w3.org/2000/xmlns/", NamespaceURI);
  307. foreach(XmlNode attributeNode in Attributes)
  308. {
  309. attributeNode.WriteTo(w);
  310. // write namespace declarations(if not exist)
  311. if(attributeNode.Prefix != null && attributeNode.Prefix != String.Empty &&
  312. w.LookupPrefix(attributeNode.Prefix) != attributeNode.NamespaceURI &&
  313. attributeNode.Prefix != "xmlns")
  314. w.WriteAttributeString("xmlns", attributeNode.Prefix, "http://www.w3.org/2000/xmlns/", attributeNode.NamespaceURI);
  315. }
  316. WriteContentTo(w);
  317. w.WriteEndElement();
  318. }
  319. #endregion
  320. }
  321. }