XmlElement.cs 10 KB

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