XmlNode.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. //
  2. // System.Xml.XmlNode
  3. //
  4. // Author:
  5. // Kral Ferch <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // (C) 2002 Kral Ferch
  9. // (C) 2002 Atsushi Enomoto
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.IO;
  14. using System.Text;
  15. using System.Xml.XPath;
  16. namespace System.Xml
  17. {
  18. public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
  19. {
  20. #region Fields
  21. XmlDocument ownerDocument;
  22. XmlNode parentNode;
  23. #endregion
  24. #region Constructors
  25. internal XmlNode (XmlDocument ownerDocument)
  26. {
  27. this.ownerDocument = ownerDocument;
  28. }
  29. #endregion
  30. #region Properties
  31. public virtual XmlAttributeCollection Attributes {
  32. get { return null; }
  33. }
  34. public virtual string BaseURI {
  35. get {
  36. // Isn't it conformant to W3C XML Base Recommendation?
  37. // As far as I tested, there are not...
  38. return (ParentNode != null) ? ParentNode.BaseURI : OwnerDocument.BaseURI;
  39. }
  40. }
  41. public virtual XmlNodeList ChildNodes {
  42. get {
  43. return new XmlNodeListChildren (this);
  44. }
  45. }
  46. public virtual XmlNode FirstChild {
  47. get {
  48. if (LastChild != null) {
  49. return LastLinkedChild.NextLinkedSibling;
  50. }
  51. else {
  52. return null;
  53. }
  54. }
  55. }
  56. public virtual bool HasChildNodes {
  57. get { return LastChild != null; }
  58. }
  59. [MonoTODO("confirm whether this way is right for each not-overriden types.")]
  60. public virtual string InnerText {
  61. get {
  62. StringBuilder builder = new StringBuilder ();
  63. AppendChildValues (this, builder);
  64. return builder.ToString ();
  65. }
  66. set { throw new NotImplementedException (); }
  67. }
  68. private void AppendChildValues (XmlNode parent, StringBuilder builder)
  69. {
  70. XmlNode node = parent.FirstChild;
  71. while (node != null) {
  72. if (node.NodeType == XmlNodeType.Text)
  73. builder.Append (node.Value);
  74. AppendChildValues (node, builder);
  75. node = node.NextSibling;
  76. }
  77. }
  78. [MonoTODO("Setter.")]
  79. public virtual string InnerXml {
  80. get {
  81. StringWriter sw = new StringWriter ();
  82. XmlTextWriter xtw = new XmlTextWriter (sw);
  83. WriteContentTo (xtw);
  84. return sw.GetStringBuilder ().ToString ();
  85. }
  86. set { throw new NotImplementedException (); }
  87. }
  88. public virtual bool IsReadOnly {
  89. get { return false; }
  90. }
  91. [System.Runtime.CompilerServices.IndexerName("Item")]
  92. public virtual XmlElement this [string name] {
  93. get {
  94. foreach (XmlNode node in ChildNodes) {
  95. if ((node.NodeType == XmlNodeType.Element) &&
  96. (node.Name == name)) {
  97. return (XmlElement) node;
  98. }
  99. }
  100. return null;
  101. }
  102. }
  103. [System.Runtime.CompilerServices.IndexerName("Item")]
  104. public virtual XmlElement this [string localname, string ns] {
  105. get {
  106. foreach (XmlNode node in ChildNodes) {
  107. if ((node.NodeType == XmlNodeType.Element) &&
  108. (node.LocalName == localname) &&
  109. (node.NamespaceURI == ns)) {
  110. return (XmlElement) node;
  111. }
  112. }
  113. return null;
  114. }
  115. }
  116. public virtual XmlNode LastChild {
  117. get { return LastLinkedChild; }
  118. }
  119. internal virtual XmlLinkedNode LastLinkedChild {
  120. get { return null; }
  121. set { }
  122. }
  123. public abstract string LocalName { get; }
  124. public abstract string Name { get; }
  125. public virtual string NamespaceURI {
  126. get { return String.Empty; }
  127. }
  128. public virtual XmlNode NextSibling {
  129. get { return null; }
  130. }
  131. public abstract XmlNodeType NodeType { get; }
  132. internal virtual XPathNodeType XPathNodeType {
  133. get {
  134. return (XPathNodeType) (-1);
  135. }
  136. }
  137. public virtual string OuterXml {
  138. get {
  139. StringWriter sw = new StringWriter ();
  140. XmlTextWriter xtw = new XmlTextWriter (sw);
  141. WriteTo (xtw);
  142. return sw.GetStringBuilder ().ToString ();
  143. }
  144. }
  145. public virtual XmlDocument OwnerDocument {
  146. get { return ownerDocument; }
  147. }
  148. public virtual XmlNode ParentNode {
  149. get { return parentNode; }
  150. }
  151. public virtual string Prefix {
  152. get { return String.Empty; }
  153. set {}
  154. }
  155. public virtual XmlNode PreviousSibling {
  156. get { return null; }
  157. }
  158. public virtual string Value {
  159. get { return null; }
  160. set { throw new InvalidOperationException ("This node does not have a value"); }
  161. }
  162. internal virtual string XmlLang {
  163. get {
  164. if(Attributes != null)
  165. foreach(XmlAttribute attr in Attributes)
  166. if(attr.Name == "xml:lang")
  167. return attr.Value;
  168. return (ParentNode != null) ? ParentNode.XmlLang : OwnerDocument.XmlLang;
  169. }
  170. }
  171. internal virtual XmlSpace XmlSpace {
  172. get {
  173. if(Attributes != null) {
  174. foreach(XmlAttribute attr in Attributes) {
  175. if(attr.Name == "xml:space") {
  176. switch(attr.Value) {
  177. case "preserve": return XmlSpace.Preserve;
  178. case "default": return XmlSpace.Default;
  179. }
  180. break;
  181. }
  182. }
  183. }
  184. return (ParentNode != null) ? ParentNode.XmlSpace : OwnerDocument.XmlSpace;
  185. }
  186. }
  187. #endregion
  188. #region Methods
  189. public virtual XmlNode AppendChild (XmlNode newChild)
  190. {
  191. // I assume that AppendChild(n) equals to InsertAfter(n, this.LastChild) or InsertBefore(n, null)
  192. return InsertBefore (newChild, null);
  193. // Below are formerly used logic.
  194. /* XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
  195. if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute || NodeType == XmlNodeType.DocumentFragment) {
  196. if (IsReadOnly)
  197. throw new ArgumentException ("The specified node is readonly.");
  198. if (newChild.OwnerDocument != ownerDoc)
  199. throw new ArgumentException ("Can't append a node created by another document.");
  200. // checking validity finished. then appending...
  201. ownerDoc.onNodeInserting (newChild, this);
  202. if(newChild.ParentNode != null)
  203. newChild.ParentNode.RemoveChild(newChild);
  204. if(newChild.NodeType == XmlNodeType.DocumentFragment)
  205. {
  206. int x = newChild.ChildNodes.Count;
  207. for(int i=0; i<x; i++)
  208. {
  209. // When this logic became to remove children in order, then index will have never to increments.
  210. XmlNode n = newChild.ChildNodes [0];
  211. this.AppendChild(n); // recursively invokes events. (It is compatible with MS implementation.)
  212. }
  213. }
  214. else
  215. {
  216. XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
  217. XmlLinkedNode lastLinkedChild = LastLinkedChild;
  218. newLinkedChild.parentNode = this;
  219. if (lastLinkedChild != null)
  220. {
  221. newLinkedChild.NextLinkedSibling = lastLinkedChild.NextLinkedSibling;
  222. lastLinkedChild.NextLinkedSibling = newLinkedChild;
  223. }
  224. else
  225. newLinkedChild.NextLinkedSibling = newLinkedChild;
  226. LastLinkedChild = newLinkedChild;
  227. ownerDoc.onNodeInserted (newChild, newChild.ParentNode);
  228. }
  229. return newChild;
  230. } else
  231. throw new InvalidOperationException();
  232. */ }
  233. public virtual XmlNode Clone ()
  234. {
  235. // By MS document, it is equivalent to CloneNode(true).
  236. return this.CloneNode (true);
  237. }
  238. public abstract XmlNode CloneNode (bool deep);
  239. [MonoTODO]
  240. public XPathNavigator CreateNavigator ()
  241. {
  242. return new XmlDocumentNavigator (this);
  243. }
  244. public IEnumerator GetEnumerator ()
  245. {
  246. return new XmlNodeListChildren (this).GetEnumerator ();
  247. }
  248. [MonoTODO("performance problem.")]
  249. public virtual string GetNamespaceOfPrefix (string prefix)
  250. {
  251. XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
  252. return nsmgr.LookupNamespace (prefix);
  253. }
  254. [MonoTODO("performance problem.")]
  255. public virtual string GetPrefixOfNamespace (string namespaceURI)
  256. {
  257. XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
  258. return nsmgr.LookupPrefix (namespaceURI);
  259. }
  260. object ICloneable.Clone ()
  261. {
  262. return Clone ();
  263. }
  264. IEnumerator IEnumerable.GetEnumerator ()
  265. {
  266. return GetEnumerator ();
  267. }
  268. public virtual XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
  269. {
  270. // I assume that insertAfter(n1, n2) equals to InsertBefore(n1, n2.PreviousSibling).
  271. // I took this way because rather than calling InsertAfter() from InsertBefore()
  272. // because current implementation of 'NextSibling' looks faster than 'PreviousSibling'.
  273. XmlNode argNode = null;
  274. if(refChild != null)
  275. argNode = refChild.NextSibling;
  276. else if(ChildNodes.Count > 0)
  277. argNode = FirstChild;
  278. return InsertBefore (newChild, argNode);
  279. }
  280. [MonoTODO("If inserted node is entity reference, then check conforming entity. Wait for DTD implementation.")]
  281. public virtual XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
  282. {
  283. XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
  284. if (NodeType == XmlNodeType.Document ||
  285. NodeType == XmlNodeType.Element ||
  286. NodeType == XmlNodeType.Attribute ||
  287. NodeType == XmlNodeType.DocumentFragment) {
  288. if (IsReadOnly)
  289. throw new ArgumentException ("The specified node is readonly.");
  290. if (newChild.OwnerDocument != ownerDoc)
  291. throw new ArgumentException ("Can't append a node created by another document.");
  292. if (refChild != null && newChild.OwnerDocument != refChild.OwnerDocument)
  293. throw new ArgumentException ("argument nodes are on the different documents.");
  294. if (refChild != null && this == ownerDoc &&
  295. ownerDoc.DocumentElement != null &&
  296. (newChild is XmlElement ||
  297. newChild is XmlCharacterData ||
  298. newChild is XmlEntityReference))
  299. throw new XmlException ("cannot insert this node to this position.");
  300. // checking validity finished. then appending...
  301. ownerDoc.onNodeInserting (newChild, this);
  302. if(newChild.ParentNode != null)
  303. newChild.ParentNode.RemoveChild (newChild);
  304. if(newChild.NodeType == XmlNodeType.DocumentFragment) {
  305. int x = newChild.ChildNodes.Count;
  306. for(int i=0; i<x; i++) {
  307. XmlNode n = newChild.ChildNodes [0];
  308. this.InsertBefore (n, refChild); // recursively invokes events. (It is compatible with MS implementation.)
  309. }
  310. }
  311. else {
  312. XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
  313. XmlLinkedNode lastLinkedChild = LastLinkedChild;
  314. newLinkedChild.parentNode = this;
  315. if(refChild == null) {
  316. // append last, so:
  317. // * set nextSibling of previous lastchild to newChild
  318. // * set lastchild = newChild
  319. // * set next of newChild to firstChild
  320. if(LastLinkedChild != null) {
  321. XmlLinkedNode formerFirst = FirstChild as XmlLinkedNode;
  322. LastLinkedChild.NextLinkedSibling = newLinkedChild;
  323. LastLinkedChild = newLinkedChild;
  324. newLinkedChild.NextLinkedSibling = formerFirst;
  325. }
  326. else {
  327. LastLinkedChild = newLinkedChild;
  328. LastLinkedChild.NextLinkedSibling = newLinkedChild; // FirstChild
  329. }
  330. }
  331. else {
  332. // append not last, so:
  333. // * if newchild is first, then set next of lastchild is newChild.
  334. // otherwise, set next of previous sibling to newChild
  335. // * set next of newChild to refChild
  336. XmlLinkedNode prev = refChild.PreviousSibling as XmlLinkedNode;
  337. if(prev == null)
  338. LastLinkedChild.NextLinkedSibling = newLinkedChild;
  339. else
  340. prev.NextLinkedSibling = newLinkedChild;
  341. newLinkedChild.NextLinkedSibling = refChild as XmlLinkedNode;
  342. }
  343. ownerDoc.onNodeInserted (newChild, newChild.ParentNode);
  344. }
  345. return newChild;
  346. }
  347. else
  348. throw new InvalidOperationException ();
  349. }
  350. [MonoTODO]
  351. public virtual void Normalize ()
  352. {
  353. throw new NotImplementedException ();
  354. }
  355. public virtual XmlNode PrependChild (XmlNode newChild)
  356. {
  357. return InsertAfter (newChild, null);
  358. }
  359. public virtual void RemoveAll ()
  360. {
  361. XmlNode next = null;
  362. for (XmlNode node = FirstChild; node != null; node = next) {
  363. next = node.NextSibling;
  364. RemoveChild (node);
  365. }
  366. }
  367. public virtual XmlNode RemoveChild (XmlNode oldChild)
  368. {
  369. XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
  370. if(oldChild.ParentNode != this)
  371. throw new XmlException ("specified child is not child of this node.");
  372. ownerDoc.onNodeRemoving (oldChild, oldChild.ParentNode);
  373. if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute || NodeType == XmlNodeType.DocumentFragment) {
  374. if (IsReadOnly)
  375. throw new ArgumentException ();
  376. if (Object.ReferenceEquals (LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals (LastLinkedChild, oldChild))
  377. LastLinkedChild = null;
  378. else {
  379. XmlLinkedNode oldLinkedChild = (XmlLinkedNode)oldChild;
  380. XmlLinkedNode beforeLinkedChild = LastLinkedChild;
  381. while (!Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, LastLinkedChild) && !Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
  382. beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
  383. if (!Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
  384. throw new ArgumentException ();
  385. beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
  386. oldLinkedChild.NextLinkedSibling = null;
  387. }
  388. ownerDoc.onNodeRemoved (oldChild, oldChild.ParentNode);
  389. oldChild.parentNode = null; // clear parent 'after' above logic.
  390. return oldChild;
  391. }
  392. else
  393. throw new ArgumentException ();
  394. }
  395. [MonoTODO]
  396. public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
  397. {
  398. throw new NotImplementedException ();
  399. }
  400. public XmlNodeList SelectNodes (string xpath)
  401. {
  402. return SelectNodes (xpath, null);
  403. }
  404. [MonoTODO]
  405. public XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
  406. {
  407. XPathNavigator nav = CreateNavigator ();
  408. XPathExpression expr = nav.Compile (xpath);
  409. if (nsmgr != null)
  410. expr.SetContext (nsmgr);
  411. XPathNodeIterator iter = nav.Select (expr);
  412. ArrayList rgNodes = new ArrayList ();
  413. while (iter.MoveNext ())
  414. {
  415. rgNodes.Add (((XmlDocumentNavigator) iter.Current).Node);
  416. }
  417. return new XmlNodeArrayList (rgNodes);
  418. }
  419. public XmlNode SelectSingleNode (string xpath)
  420. {
  421. return SelectSingleNode (xpath, null);
  422. }
  423. [MonoTODO]
  424. public XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
  425. {
  426. XPathNavigator nav = CreateNavigator ();
  427. XPathExpression expr = nav.Compile (xpath);
  428. if (nsmgr != null)
  429. expr.SetContext (nsmgr);
  430. XPathNodeIterator iter = nav.Select (expr);
  431. if (!iter.MoveNext ())
  432. return null;
  433. return ((XmlDocumentNavigator) iter.Current).Node;
  434. }
  435. internal void SetParentNode (XmlNode parent)
  436. {
  437. parentNode = parent;
  438. }
  439. [MonoTODO]
  440. public virtual bool Supports (string feature, string version)
  441. {
  442. throw new NotImplementedException ();
  443. }
  444. public abstract void WriteContentTo (XmlWriter w);
  445. public abstract void WriteTo (XmlWriter w);
  446. // It parses this and all the ancestor elements,
  447. // find 'xmlns' declarations, stores and then return them.
  448. // TODO: tests
  449. internal XmlNamespaceManager ConstructNamespaceManager ()
  450. {
  451. XmlDocument doc = this is XmlDocument ? (XmlDocument)this : this.OwnerDocument;
  452. XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
  453. XmlElement el = null;
  454. switch(this.NodeType) {
  455. case XmlNodeType.Attribute:
  456. el = ((XmlAttribute)this).OwnerElement;
  457. break;
  458. case XmlNodeType.Element:
  459. el = this as XmlElement;
  460. break;
  461. default:
  462. el = this.ParentNode as XmlElement;
  463. break;
  464. }
  465. while(el != null) {
  466. foreach(XmlAttribute attr in el.Attributes) {
  467. if(attr.Prefix == "xmlns" || (attr.Name == "xmlns" && attr.Prefix == String.Empty)) {
  468. if(nsmgr.LookupNamespace (attr.LocalName) == null )
  469. nsmgr.AddNamespace (attr.LocalName, attr.Value);
  470. }
  471. }
  472. // When reached to document, then it will set null value :)
  473. el = el.ParentNode as XmlElement;
  474. }
  475. return nsmgr;
  476. }
  477. #endregion
  478. }
  479. }