XmlNode.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. // -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2. //
  3. // System.Xml.XmlNode
  4. //
  5. // Author:
  6. // Daniel Weber ([email protected])
  7. //
  8. // (C) 2001 Daniel Weber
  9. using System;
  10. using System.Collections;
  11. using System.Xml.XPath;
  12. namespace System.Xml
  13. {
  14. public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
  15. {
  16. //======= Private data members ==============================================
  17. private XmlNodeListAsArrayList _childNodes;
  18. protected XmlDocument FOwnerDocument;
  19. protected XmlNode _parent;
  20. /// <summary>
  21. /// Return a clone of this node
  22. /// </summary>
  23. /// <returns></returns>
  24. public virtual object Clone()
  25. {
  26. // TODO - implement XmlNode.Clone() as object
  27. throw new NotImplementedException("object XmlNode.Clone() not implmented");
  28. }
  29. // ============ Properties ============================
  30. /// <summary>
  31. /// Get the XmlAttributeCollection representing the attributes
  32. /// on the node type. Returns null if the node type is not XmlElement.
  33. /// </summary>
  34. public virtual XmlAttributeCollection Attributes
  35. {
  36. get
  37. {
  38. return null;
  39. }
  40. }
  41. /// <summary>
  42. /// Return the base Uniform Resource Indicator (URI) used to resolve
  43. /// this node, or String.Empty.
  44. /// </summary>
  45. public virtual string BaseURI
  46. {
  47. get
  48. {
  49. // TODO - implement XmlNode.BaseURI {get;}
  50. throw new NotImplementedException("XmlNode.BaseURI not implemented");
  51. }
  52. }
  53. /// <summary>
  54. /// Return all child nodes of this node. If there are no children,
  55. /// return an empty XmlNodeList;
  56. /// </summary>
  57. public virtual XmlNodeList ChildNodes
  58. {
  59. get
  60. {
  61. if (_childNodes == null)
  62. _childNodes = new XmlNodeListAsArrayList();
  63. return _childNodes as XmlNodeList;
  64. }
  65. }
  66. /// <summary>
  67. /// Return first child node as XmlNode or null
  68. /// if the node has no children
  69. /// </summary>
  70. public virtual XmlNode FirstChild
  71. {
  72. get
  73. {
  74. if (ChildNodes.Count == 0)
  75. return null;
  76. else
  77. return ChildNodes[0];
  78. }
  79. }
  80. /// <summary>
  81. /// Return true if the node has children
  82. /// </summary>
  83. public virtual bool HasChildNodes
  84. {
  85. get
  86. {
  87. if (_childNodes.Count == 0)
  88. return true;
  89. else
  90. return false;
  91. }
  92. }
  93. /// <summary>
  94. /// Get or Set the concatenated values of node and children
  95. /// </summary>
  96. public virtual string InnerText
  97. {
  98. get
  99. {
  100. // TODO - implement set InnerText()
  101. throw new NotImplementedException();
  102. }
  103. set
  104. {
  105. // TODO - implement set InnerText()
  106. throw new NotImplementedException();
  107. }
  108. }
  109. /// <summary>
  110. /// Get/Set the XML representing just the child nodes of this node
  111. /// </summary>
  112. public virtual string InnerXml
  113. {
  114. get
  115. {
  116. // TODO - implement set InnerXml()
  117. throw new NotImplementedException();
  118. }
  119. set
  120. {
  121. // TODO - implement set InnerXml()
  122. throw new NotImplementedException();
  123. }
  124. }
  125. /// <summary>
  126. /// Property Get - true if node is read-only
  127. /// </summary>
  128. public virtual bool IsReadOnly
  129. {
  130. get
  131. {
  132. return OwnerDocument.IsReadOnly;
  133. }
  134. }
  135. /// <summary>
  136. /// Return the child element named [string]. Returns XmlElement
  137. /// Indexer for XmlNode class.
  138. /// </summary>
  139. [System.Runtime.CompilerServices.CSharp.IndexerName("Item")]
  140. public virtual XmlElement this [String index]
  141. {
  142. get
  143. {
  144. // TODO - implement XmlNode.Item(string)
  145. throw new NotImplementedException();
  146. }
  147. }
  148. /// <summary>
  149. /// Get the last child node, or null if there are no nodes
  150. /// </summary>
  151. public virtual XmlNode LastChild
  152. {
  153. get
  154. {
  155. if (_childNodes.Count == 0)
  156. return null;
  157. else
  158. return _childNodes.Item(_childNodes.Count - 1);
  159. }
  160. }
  161. /// <summary>
  162. /// Returns the local name of the node with qualifiers removed
  163. /// LocalName of ns:elementName = "elementName"
  164. /// </summary>
  165. public abstract string LocalName {get;}
  166. /// <summary>
  167. /// Get the qualified node name
  168. /// derived classes must implement as behavior varies
  169. /// by tag type.
  170. /// </summary>
  171. public abstract string Name { get; }
  172. /// <summary>
  173. /// Get the namespace URI or String.Empty if none
  174. /// </summary>
  175. public virtual string NamespaceURI
  176. {
  177. get
  178. {
  179. // TODO - implement Namespace URI, or determine abstractness
  180. throw new NotImplementedException("XmlNode.NamespaceURI not implemented");
  181. }
  182. }
  183. /// <summary>
  184. /// Get the node immediatelly following this node, or null
  185. /// </summary>
  186. public virtual XmlNode NextSibling
  187. {
  188. get
  189. {
  190. if (_parent != null)
  191. {
  192. XmlNodeListAsArrayList children = _parent.ChildNodes as XmlNodeListAsArrayList;
  193. int ourIndex = children.data.IndexOf(this);
  194. return children[ourIndex + 1];
  195. }
  196. else
  197. return null;
  198. }
  199. }
  200. public virtual XmlNodeType NodeType
  201. {
  202. get
  203. {
  204. return XmlNodeType.None;
  205. }
  206. }
  207. /// <summary>
  208. /// Return the string representing this node and all it's children
  209. /// </summary>
  210. public virtual string OuterXml
  211. {
  212. get
  213. {
  214. // TODO - implement OuterXml {get;}
  215. throw new NotImplementedException();
  216. }
  217. }
  218. /// <summary>
  219. /// Return owning document.
  220. /// If this nodeType is a document, return null
  221. /// </summary>
  222. public virtual XmlDocument OwnerDocument
  223. {
  224. get
  225. {
  226. return FOwnerDocument;
  227. }
  228. }
  229. /// <summary>
  230. /// Returns the parent node, or null
  231. /// Return value depends on superclass node type
  232. /// </summary>
  233. public virtual XmlNode ParentNode
  234. {
  235. get
  236. {
  237. return _parent;
  238. }
  239. }
  240. /// <summary>
  241. /// set/get the namespace prefix for this node, or
  242. /// string.empty if it does not exist
  243. /// </summary>
  244. public virtual string Prefix
  245. {
  246. get
  247. {
  248. // TODO - implement Prefix {get;}
  249. throw new NotImplementedException();
  250. }
  251. set
  252. {
  253. // TODO - implement Prefix {set;}
  254. throw new NotImplementedException();
  255. }
  256. }
  257. /// <summary>
  258. /// The preceding XmlNode or null
  259. /// </summary>
  260. public virtual XmlNode PreviousSibling {
  261. get
  262. {
  263. if (_parent != null)
  264. {
  265. XmlNodeListAsArrayList children = _parent.ChildNodes as XmlNodeListAsArrayList;
  266. int ourIndex = children.data.IndexOf(this);
  267. return children[ourIndex - 1];
  268. }
  269. else
  270. return null;
  271. }
  272. }
  273. /// <summary>
  274. /// Get/Set the value for this node
  275. /// </summary>
  276. public virtual string Value
  277. {
  278. get
  279. {
  280. // TODO - implement Value {get;}
  281. throw new NotImplementedException();
  282. }
  283. set
  284. {
  285. // TODO - implement Value {set;}
  286. throw new NotImplementedException();
  287. }
  288. }
  289. //======= Methods ==========================
  290. /// <summary>
  291. /// Appends the specified node to the end of the child node list
  292. /// </summary>
  293. /// <param name="newChild"></param>
  294. /// <returns></returns>
  295. public virtual XmlNode AppendChild (XmlNode newChild)
  296. {
  297. return InsertBefore(newChild, null);
  298. }
  299. /// <summary>
  300. /// Return a clone of the node
  301. /// </summary>
  302. /// <param name="deep">Make copy of all children</param>
  303. /// <returns>Cloned node</returns>
  304. public abstract XmlNode CloneNode( bool deep);
  305. /// <summary>
  306. /// Return an XPathNavigator for navigating this node
  307. /// </summary>
  308. /// <returns></returns>
  309. public System.Xml.XPath.XPathNavigator CreateNavigator()
  310. {
  311. // TODO - implement CreateNavigator()
  312. throw new NotImplementedException();
  313. }
  314. /// <summary>
  315. /// Provide support for "for each"
  316. /// </summary>
  317. /// <returns></returns>
  318. public IEnumerator GetEnumerator()
  319. {
  320. return _childNodes.data.GetEnumerator();
  321. }
  322. /// <summary>
  323. /// Look up the closest namespace for this node that is in scope for the given prefix
  324. /// </summary>
  325. /// <param name="prefix"></param>
  326. /// <returns>Namespace URI</returns>
  327. public virtual string GetNamespaceOfPrefix(string prefix)
  328. {
  329. // TODO - implement GetNamespaceOfPrefix()
  330. throw new NotImplementedException();
  331. }
  332. /// <summary>
  333. /// Get the closest xmlns declaration for the given namespace URI that is in scope.
  334. /// Returns the prefix defined in that declaration.
  335. /// </summary>
  336. /// <param name="namespaceURI"></param>
  337. /// <returns></returns>
  338. public virtual string GetPrefixOfNamespace(string namespaceURI)
  339. {
  340. // TODO - implement GetPrefixOfNamespace
  341. throw new NotImplementedException();
  342. }
  343. /// <summary>
  344. /// Insert newChild directly after the reference node.
  345. /// If refChild is null, newChild is inserted at the beginning of childnodes.
  346. /// If newChild is a document fragment, all nodes are inserted after refChild.
  347. /// If newChild is already in the tree, it is first removed.
  348. /// </summary>
  349. /// <exception cref="ArgumentException">NewChild was created from differant document.
  350. /// RefChild not a child of this node or null.
  351. /// Node is read-only</exception>
  352. /// <exception cref="InvalidOperationException">Node is of type that does not have children
  353. /// Node to insert is an ancestor of this node.</exception>
  354. /// <param name="newChild">Child node to insert.</param>
  355. /// <param name="refChild">Reference node to insert after</param>
  356. /// <returns></returns>
  357. public virtual XmlNode InsertAfter(XmlNode newChild, XmlNode refChild)
  358. {
  359. // Checks parent not ancestor, arguments valid, etc. Throws exception on error
  360. InsertionCheck(newChild, refChild);
  361. // Scan the node list, looking for refChild and seeing if newChild is in the list
  362. // Note that if refNode is null (prepend), we don't want to do the .Equals(null)
  363. XmlNode retval = null;
  364. int refNodeIndex = -1;
  365. for (int i = 0; i < _childNodes.Count; i++)
  366. {
  367. XmlNode e = _childNodes.data[i] as XmlNode;
  368. if (e.Equals(newChild))
  369. {
  370. retval = e;
  371. FOwnerDocument.onNodeRemoving(newChild, newChild.ParentNode);
  372. _childNodes.data.RemoveAt(i);
  373. newChild.setParent(null);
  374. FOwnerDocument.onNodeRemoved(newChild, null);
  375. break;
  376. }
  377. if ( (refChild != null ) & ( e.Equals(refChild) ) )
  378. {
  379. refNodeIndex = i;
  380. if (retval != null)
  381. break;
  382. }
  383. }
  384. if ( ( refNodeIndex == -1 ) & (refChild != null) )
  385. throw new ArgumentException("Reference node not found (and not null) in call to XmlNode.InsertAfter()");
  386. FOwnerDocument.onNodeInserting(newChild, this);
  387. if (refChild == null)
  388. refNodeIndex = 0;
  389. else
  390. refNodeIndex++; // insert after reference...
  391. if (newChild.NodeType == XmlNodeType.DocumentFragment)
  392. {
  393. // Insert all children, starting from refNodeIndex (0,1,2...n)
  394. for (int i = 0; i < newChild.ChildNodes.Count; i++)
  395. {
  396. XmlNode e = newChild.ChildNodes[i] as XmlNode;
  397. FOwnerDocument.onNodeInserting(e, this);
  398. _childNodes.data.Insert(refNodeIndex, newChild.ChildNodes[i]);
  399. e.setParent(this);
  400. FOwnerDocument.onNodeInserted(newChild, this);
  401. refNodeIndex ++;
  402. }
  403. }
  404. else
  405. {
  406. FOwnerDocument.onNodeInserting(newChild, this);
  407. _childNodes.data.Insert(refNodeIndex, newChild);
  408. newChild.setParent(this);
  409. FOwnerDocument.onNodeInserted(newChild, this);
  410. }
  411. return retval;
  412. }
  413. /// <summary>
  414. /// Insert newChild directly before the reference node.
  415. /// </summary>
  416. /// <param name="newChild"></param>
  417. /// <param name="refChild"></param>
  418. /// <returns></returns>
  419. public virtual XmlNode InsertBefore(XmlNode newChild, XmlNode refChild)
  420. {
  421. // Checks parent not ancestor, arguments valid, etc. Throws exception on error
  422. InsertionCheck(newChild, refChild);
  423. // Scan the node list, looking for refChild and seeing if newChild is in the list
  424. XmlNode retval = null;
  425. int refNodeIndex = -1;
  426. for (int i = 0; i < _childNodes.Count; i++)
  427. {
  428. XmlNode e = _childNodes.data[i] as XmlNode;
  429. if (e.Equals(newChild))
  430. {
  431. retval = e;
  432. FOwnerDocument.onNodeRemoving(newChild, newChild.ParentNode);
  433. _childNodes.data.RemoveAt(i);
  434. newChild.setParent(null);
  435. FOwnerDocument.onNodeRemoved(newChild, null);
  436. break;
  437. }
  438. if ( (refChild != null ) & ( e.Equals(refChild) ) )
  439. {
  440. refNodeIndex = i;
  441. if (retval != null)
  442. break;
  443. }
  444. }
  445. if ( ( refNodeIndex == -1 ) & (refChild != null) )
  446. throw new ArgumentException("Reference node not found (and not null) in call to XmlNode.InsertAfter()");
  447. if (refChild == null)
  448. refNodeIndex = _childNodes.Count;
  449. if (newChild.NodeType == XmlNodeType.DocumentFragment)
  450. {
  451. // Insert all children, starting from refNodeIndex (0,1,2...n)
  452. for (int i = 0; i < newChild.ChildNodes.Count; i++)
  453. {
  454. XmlNode e = newChild.ChildNodes[i] as XmlNode;
  455. FOwnerDocument.onNodeInserting(e, this);
  456. _childNodes.data.Insert(refNodeIndex, newChild.ChildNodes[i]);
  457. e.setParent(this);
  458. FOwnerDocument.onNodeInserted(newChild, this);
  459. refNodeIndex ++;
  460. }
  461. }
  462. else
  463. {
  464. FOwnerDocument.onNodeInserting(newChild, this);
  465. _childNodes.data.Insert(refNodeIndex, newChild);
  466. newChild.setParent(this);
  467. FOwnerDocument.onNodeInserted(newChild, this);
  468. }
  469. return retval;
  470. }
  471. /// <summary>
  472. /// Put all nodes under this node in "normal" form
  473. /// Whatever that means...
  474. /// </summary>
  475. public virtual void Normalize()
  476. {
  477. // TODO - Implement Normalize()
  478. throw new NotImplementedException();
  479. }
  480. /// <summary>
  481. /// Add the specified child to the beginning of the child node list
  482. /// </summary>
  483. /// <param name="newChild">Node to add</param>
  484. /// <returns>The node added</returns>
  485. public virtual XmlNode PrependChild(XmlNode newChild)
  486. {
  487. return InsertAfter(newChild, null);
  488. }
  489. /// <summary>
  490. /// Remove all children and attributes
  491. /// </summary>
  492. public virtual void RemoveAll()
  493. {
  494. if (_childNodes == null)
  495. return;
  496. else
  497. {
  498. // Remove in order, 0..n
  499. while (_childNodes.Count > 0)
  500. {
  501. XmlNode e = _childNodes[0];
  502. FOwnerDocument.onNodeRemoving(e, this);
  503. e.setParent(null);
  504. _childNodes.data.RemoveAt(0);
  505. FOwnerDocument.onNodeRemoved(e, null);
  506. }
  507. }
  508. }
  509. /// <summary>
  510. /// Remove specified child node
  511. /// </summary>
  512. /// <param name="oldChild"></param>
  513. /// <returns>Removed node</returns>
  514. public virtual XmlNode RemoveChild(XmlNode oldChild)
  515. {
  516. // TODO - implement RemoveChild(oldChild)
  517. throw new NotImplementedException();
  518. }
  519. /// <summary>
  520. /// Select a list of nodes matching the xpath
  521. /// </summary>
  522. /// <param name="xpath"></param>
  523. /// <returns>matching nodes</returns>
  524. public XmlNodeList SelectNodes( string xpath)
  525. {
  526. // TODO - imlement SelectNodes(xpath)
  527. throw new NotImplementedException();
  528. }
  529. /// <summary>
  530. /// Select a list of nodes matching the xpath. Any prefixes are resolved
  531. /// using the passed namespace manager
  532. /// </summary>
  533. /// <param name="xpath"></param>
  534. /// <param name="nsmgr"></param>
  535. /// <returns></returns>
  536. public XmlNodeList SelectNodes(string xpath, XmlNamespaceManager nsmgr)
  537. {
  538. // TODO - implement SelectNodes(xpath, nsmgr)
  539. throw new NotImplementedException();
  540. }
  541. /// <summary>
  542. /// Selects the first node that matches xpath
  543. /// </summary>
  544. /// <param name="?"></param>
  545. /// <returns></returns>
  546. public XmlNode SelectSingleNode(string xpatch)
  547. {
  548. // TODO - implement SelectSingeNode(xpath)
  549. throw new NotImplementedException();
  550. }
  551. /// <summary>
  552. /// Returns the first node that matches xpath
  553. /// Uses the passed namespace manager to resolve namespace URI's
  554. /// </summary>
  555. /// <param name="xpath"></param>
  556. /// <param name="nsmgr"></param>
  557. /// <returns></returns>
  558. public XmlNode SelectSingleNode(string xpath, XmlNamespaceManager nsmgr)
  559. {
  560. // Implement SelectSingleNode(xpath, nsmgr)
  561. throw new NotImplementedException();
  562. }
  563. /// <summary>
  564. /// Tests if the DOM implementation supports the passed feature
  565. /// </summary>
  566. /// <param name="feature"></param>
  567. /// <param name="version"></param>
  568. /// <returns></returns>
  569. public virtual bool Supports(string feature, string version)
  570. {
  571. //TODO - implement Supports(feature, version)
  572. throw new NotImplementedException();
  573. }
  574. /// <summary>
  575. /// Returns a string representation of the current node and it's children
  576. /// </summary>
  577. /// <returns></returns>
  578. public override string ToString()
  579. {
  580. // TODO - implement ToString()
  581. throw new NotImplementedException();
  582. }
  583. /// <summary>
  584. /// Saves all children of the current node to the passed writer
  585. /// </summary>
  586. /// <param name="w"></param>
  587. public abstract void WriteContentTo(XmlWriter w);
  588. /// <summary>
  589. /// Saves the current node to writer w
  590. /// </summary>
  591. /// <param name="w"></param>
  592. public abstract void WriteTo(XmlWriter w);
  593. //======= Internal methods ===============================================
  594. /// <summary>
  595. /// accessor {set;} for parentNode only visible internally.
  596. /// </summary>
  597. /// <param name="newParent">new parent node.</param>
  598. internal void setParent( XmlNode newParent)
  599. {
  600. if (newParent.OwnerDocument.Equals( FOwnerDocument) )
  601. _parent = newParent;
  602. else
  603. throw new ArgumentException("New parent node owner does not match");
  604. }
  605. //======= Protected methods ==============================================
  606. //======= Private Methods ===================================================
  607. /// <summary>
  608. /// Helper function to perform checks required before insrting a node.
  609. /// Throws applicable exceptions on error.
  610. /// </summary>
  611. /// <param name="newChild"></param>
  612. /// <param name="refChild"></param>
  613. private void InsertionCheck( XmlNode newChild, XmlNode refChild)
  614. {
  615. if (newChild == null)
  616. throw new ArgumentNullException("Null newNode passed to InsertAfter()");
  617. if (newChild.Equals(this))
  618. throw new ArgumentException("Cannot insert node onto itself");
  619. if (! FOwnerDocument.Equals( newChild.OwnerDocument) )
  620. throw new ArgumentException("Reference node has different owner document than this node");
  621. if ( FOwnerDocument.IsReadOnly )
  622. throw new ArgumentException("Operation not supported - tree is read-only");
  623. //Check that insert node is not in our path to the root
  624. XmlNode curParent = _parent;
  625. while ( (curParent != null) & (! FOwnerDocument.Equals(curParent) ))
  626. {
  627. if (curParent.Equals(newChild) )
  628. throw new ArgumentException("Cannot insert ancestor a node");
  629. curParent = curParent.ParentNode;
  630. }
  631. }
  632. // Constructors
  633. //===========================================================================
  634. //When we're first created, we won't know parent, etc.
  635. internal XmlNode( XmlDocument aOwnerDoc )
  636. {
  637. // Don't create childnodes object, since not all derived classes have children
  638. FOwnerDocument = aOwnerDoc;
  639. }
  640. } // XmlNode
  641. } // using namespace System.Xml