XmlNode.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. //
  2. // System.Xml.XmlNode
  3. //
  4. // Author:
  5. // Kral Ferch <[email protected]>
  6. //
  7. // (C) 2002 Kral Ferch
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Text;
  13. using System.Xml.XPath;
  14. namespace System.Xml
  15. {
  16. public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
  17. {
  18. #region Fields
  19. XmlDocument ownerDocument;
  20. XmlNode parentNode;
  21. #endregion
  22. #region Constructors
  23. internal XmlNode (XmlDocument ownerDocument)
  24. {
  25. this.ownerDocument = ownerDocument;
  26. }
  27. #endregion
  28. #region Properties
  29. public virtual XmlAttributeCollection Attributes
  30. {
  31. get { return null; }
  32. }
  33. [MonoTODO]
  34. public virtual string BaseURI
  35. {
  36. get { throw new NotImplementedException (); }
  37. }
  38. public virtual XmlNodeList ChildNodes {
  39. get {
  40. return new XmlNodeListChildren(this);
  41. }
  42. }
  43. public virtual XmlNode FirstChild {
  44. get {
  45. if (LastChild != null) {
  46. return LastLinkedChild.NextLinkedSibling;
  47. }
  48. else {
  49. return null;
  50. }
  51. }
  52. }
  53. public virtual bool HasChildNodes {
  54. get { return LastChild != null; }
  55. }
  56. [MonoTODO]
  57. public virtual string InnerText {
  58. get {
  59. StringBuilder builder = new StringBuilder ();
  60. AppendChildValues (this, builder);
  61. return builder.ToString ();
  62. }
  63. set { throw new NotImplementedException (); }
  64. }
  65. private void AppendChildValues(XmlNode parent, StringBuilder builder)
  66. {
  67. XmlNode node = parent.FirstChild;
  68. while (node != null) {
  69. builder.Append (node.Value);
  70. AppendChildValues (node, builder);
  71. node = node.NextSibling;
  72. }
  73. }
  74. [MonoTODO("Setter.")]
  75. public virtual string InnerXml {
  76. get {
  77. StringWriter sw = new StringWriter ();
  78. XmlTextWriter xtw = new XmlTextWriter (sw);
  79. WriteContentTo(xtw);
  80. return sw.GetStringBuilder().ToString();
  81. }
  82. set { throw new NotImplementedException (); }
  83. }
  84. public virtual bool IsReadOnly {
  85. get { return false; }
  86. }
  87. [System.Runtime.CompilerServices.IndexerName("Item")]
  88. public virtual XmlElement this [string name] {
  89. get {
  90. foreach (XmlNode node in ChildNodes) {
  91. if ((node.NodeType == XmlNodeType.Element) &&
  92. (node.Name == name)) {
  93. return (XmlElement) node;
  94. }
  95. }
  96. return null;
  97. }
  98. }
  99. [System.Runtime.CompilerServices.IndexerName("Item")]
  100. public virtual XmlElement this [string localname, string ns] {
  101. get {
  102. foreach (XmlNode node in ChildNodes) {
  103. if ((node.NodeType == XmlNodeType.Element) &&
  104. (node.LocalName == localname) &&
  105. (node.NamespaceURI == ns)) {
  106. return (XmlElement) node;
  107. }
  108. }
  109. return null;
  110. }
  111. }
  112. public virtual XmlNode LastChild {
  113. get { return LastLinkedChild; }
  114. }
  115. internal virtual XmlLinkedNode LastLinkedChild {
  116. get { return null; }
  117. set { }
  118. }
  119. public abstract string LocalName { get; }
  120. public abstract string Name { get; }
  121. public virtual string NamespaceURI {
  122. get { return String.Empty; }
  123. }
  124. public virtual XmlNode NextSibling {
  125. get { return null; }
  126. }
  127. public abstract XmlNodeType NodeType { get; }
  128. public virtual string OuterXml {
  129. get {
  130. StringWriter sw = new StringWriter ();
  131. XmlTextWriter xtw = new XmlTextWriter (sw);
  132. WriteTo(xtw);
  133. return sw.GetStringBuilder().ToString();
  134. }
  135. }
  136. public virtual XmlDocument OwnerDocument {
  137. get { return ownerDocument; }
  138. }
  139. public virtual XmlNode ParentNode {
  140. get { return parentNode; }
  141. }
  142. public virtual string Prefix {
  143. get { return String.Empty; }
  144. set {}
  145. }
  146. public virtual XmlNode PreviousSibling {
  147. get { return null; }
  148. }
  149. public virtual string Value {
  150. get { return null; }
  151. set { throw new InvalidOperationException ("This node does not have a value"); }
  152. }
  153. #endregion
  154. #region Methods
  155. public virtual XmlNode AppendChild (XmlNode newChild)
  156. {
  157. if (NodeType == XmlNodeType.Document
  158. || NodeType == XmlNodeType.Element
  159. || NodeType == XmlNodeType.Attribute) {
  160. XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
  161. XmlLinkedNode lastLinkedChild = LastLinkedChild;
  162. newLinkedChild.parentNode = this;
  163. if (lastLinkedChild != null) {
  164. newLinkedChild.NextLinkedSibling = lastLinkedChild.NextLinkedSibling;
  165. lastLinkedChild.NextLinkedSibling = newLinkedChild;
  166. } else
  167. newLinkedChild.NextLinkedSibling = newLinkedChild;
  168. LastLinkedChild = newLinkedChild;
  169. return newChild;
  170. } else
  171. throw new InvalidOperationException();
  172. }
  173. [MonoTODO]
  174. public virtual XmlNode Clone ()
  175. {
  176. throw new NotImplementedException ();
  177. }
  178. public abstract XmlNode CloneNode (bool deep);
  179. [MonoTODO]
  180. public virtual XPathNavigator CreateNavigator ()
  181. {
  182. return new XmlDocumentNavigator(this);
  183. }
  184. public IEnumerator GetEnumerator ()
  185. {
  186. return new XmlNodeListChildren(this).GetEnumerator();
  187. }
  188. [MonoTODO]
  189. public virtual string GetNamespaceOfPrefix (string prefix)
  190. {
  191. throw new NotImplementedException ();
  192. }
  193. [MonoTODO]
  194. public virtual string GetPrefixOfNamespace (string namespaceURI)
  195. {
  196. throw new NotImplementedException ();
  197. }
  198. object ICloneable.Clone ()
  199. {
  200. return Clone ();
  201. }
  202. IEnumerator IEnumerable.GetEnumerator ()
  203. {
  204. return GetEnumerator ();
  205. }
  206. [MonoTODO]
  207. public virtual XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
  208. {
  209. throw new NotImplementedException ();
  210. }
  211. [MonoTODO]
  212. public virtual XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
  213. {
  214. throw new NotImplementedException ();
  215. }
  216. [MonoTODO]
  217. public virtual void Normalize ()
  218. {
  219. throw new NotImplementedException ();
  220. }
  221. [MonoTODO]
  222. public virtual XmlNode PrependChild (XmlNode newChild)
  223. {
  224. throw new NotImplementedException ();
  225. }
  226. public virtual void RemoveAll ()
  227. {
  228. LastLinkedChild = null;
  229. }
  230. public virtual XmlNode RemoveChild (XmlNode oldChild)
  231. {
  232. if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute)
  233. {
  234. if (IsReadOnly)
  235. throw new ArgumentException();
  236. if (Object.ReferenceEquals(LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals(LastLinkedChild, oldChild))
  237. LastLinkedChild = null;
  238. else {
  239. XmlLinkedNode oldLinkedChild = (XmlLinkedNode)oldChild;
  240. XmlLinkedNode beforeLinkedChild = LastLinkedChild;
  241. while (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, LastLinkedChild) && !Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
  242. beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
  243. if (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
  244. throw new ArgumentException();
  245. beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
  246. oldLinkedChild.NextLinkedSibling = null;
  247. }
  248. return oldChild;
  249. }
  250. else
  251. throw new ArgumentException();
  252. }
  253. [MonoTODO]
  254. public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
  255. {
  256. throw new NotImplementedException ();
  257. }
  258. [MonoTODO]
  259. public XmlNodeList SelectNodes (string xpath)
  260. {
  261. throw new NotImplementedException ();
  262. }
  263. [MonoTODO]
  264. public XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
  265. {
  266. throw new NotImplementedException ();
  267. }
  268. [MonoTODO]
  269. public XmlNode SelectSingleNode (string xpath)
  270. {
  271. throw new NotImplementedException ();
  272. }
  273. [MonoTODO]
  274. public XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
  275. {
  276. throw new NotImplementedException ();
  277. }
  278. internal void SetParentNode (XmlNode parent)
  279. {
  280. parentNode = parent;
  281. }
  282. [MonoTODO]
  283. public virtual bool Supports (string feature, string version)
  284. {
  285. throw new NotImplementedException ();
  286. }
  287. public abstract void WriteContentTo (XmlWriter w);
  288. public abstract void WriteTo (XmlWriter w);
  289. #endregion
  290. }
  291. }