1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: NodeSetDTM.java,v 1.15 2004/02/17 04:30:02 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal;
  20. import com.sun.org.apache.xalan.internal.res.XSLMessages;
  21. import com.sun.org.apache.xml.internal.dtm.DTM;
  22. import com.sun.org.apache.xml.internal.dtm.DTMFilter;
  23. import com.sun.org.apache.xml.internal.dtm.DTMIterator;
  24. import com.sun.org.apache.xml.internal.dtm.DTMManager;
  25. import com.sun.org.apache.xml.internal.utils.NodeVector;
  26. import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
  27. import org.w3c.dom.Node;
  28. import org.w3c.dom.NodeList;
  29. import org.w3c.dom.traversal.NodeIterator;
  30. /**
  31. * <p>The NodeSetDTM class can act as either a NodeVector,
  32. * NodeList, or NodeIterator. However, in order for it to
  33. * act as a NodeVector or NodeList, it's required that
  34. * setShouldCacheNodes(true) be called before the first
  35. * nextNode() is called, in order that nodes can be added
  36. * as they are fetched. Derived classes that implement iterators
  37. * must override runTo(int index), in order that they may
  38. * run the iteration to the given index. </p>
  39. *
  40. * <p>Note that we directly implement the DOM's NodeIterator
  41. * interface. We do not emulate all the behavior of the
  42. * standard NodeIterator. In particular, we do not guarantee
  43. * to present a "live view" of the document ... but in XSLT,
  44. * the source document should never be mutated, so this should
  45. * never be an issue.</p>
  46. *
  47. * <p>Thought: Should NodeSetDTM really implement NodeList and NodeIterator,
  48. * or should there be specific subclasses of it which do so? The
  49. * advantage of doing it all here is that all NodeSetDTMs will respond
  50. * to the same calls; the disadvantage is that some of them may return
  51. * less-than-enlightening results when you do so.</p>
  52. * @xsl.usage advanced
  53. */
  54. public class NodeSetDTM extends NodeVector
  55. implements /* NodeList, NodeIterator, */ DTMIterator,
  56. Cloneable
  57. {
  58. /**
  59. * Create an empty nodelist.
  60. */
  61. public NodeSetDTM(DTMManager dtmManager)
  62. {
  63. super();
  64. m_manager = dtmManager;
  65. }
  66. /**
  67. * Create an empty, using the given block size.
  68. *
  69. * @param blocksize Size of blocks to allocate
  70. * @param dummy pass zero for right now...
  71. */
  72. public NodeSetDTM(int blocksize, int dummy, DTMManager dtmManager)
  73. {
  74. super(blocksize);
  75. m_manager = dtmManager;
  76. }
  77. // %TBD%
  78. // /**
  79. // * Create a NodeSetDTM, and copy the members of the
  80. // * given nodelist into it.
  81. // *
  82. // * @param nodelist List of Nodes to be made members of the new set.
  83. // */
  84. // public NodeSetDTM(NodeList nodelist)
  85. // {
  86. //
  87. // super();
  88. //
  89. // addNodes(nodelist);
  90. // }
  91. /**
  92. * Create a NodeSetDTM, and copy the members of the
  93. * given NodeSetDTM into it.
  94. *
  95. * @param nodelist Set of Nodes to be made members of the new set.
  96. */
  97. public NodeSetDTM(NodeSetDTM nodelist)
  98. {
  99. super();
  100. m_manager = nodelist.getDTMManager();
  101. m_root = nodelist.getRoot();
  102. addNodes((DTMIterator) nodelist);
  103. }
  104. /**
  105. * Create a NodeSetDTM, and copy the members of the
  106. * given DTMIterator into it.
  107. *
  108. * @param ni Iterator which yields Nodes to be made members of the new set.
  109. */
  110. public NodeSetDTM(DTMIterator ni)
  111. {
  112. super();
  113. m_manager = ni.getDTMManager();
  114. m_root = ni.getRoot();
  115. addNodes(ni);
  116. }
  117. /**
  118. * Create a NodeSetDTM, and copy the members of the
  119. * given DTMIterator into it.
  120. *
  121. * @param ni Iterator which yields Nodes to be made members of the new set.
  122. */
  123. public NodeSetDTM(NodeIterator iterator, XPathContext xctxt)
  124. {
  125. super();
  126. Node node;
  127. m_manager = xctxt.getDTMManager();
  128. while (null != (node = iterator.nextNode()))
  129. {
  130. int handle = xctxt.getDTMHandleFromNode(node);
  131. addNodeInDocOrder(handle, xctxt);
  132. }
  133. }
  134. /**
  135. * Create a NodeSetDTM, and copy the members of the
  136. * given DTMIterator into it.
  137. *
  138. * @param ni Iterator which yields Nodes to be made members of the new set.
  139. */
  140. public NodeSetDTM(NodeList nodeList, XPathContext xctxt)
  141. {
  142. super();
  143. m_manager = xctxt.getDTMManager();
  144. int n = nodeList.getLength();
  145. for (int i = 0; i < n; i++)
  146. {
  147. Node node = nodeList.item(i);
  148. int handle = xctxt.getDTMHandleFromNode(node);
  149. // Do not reorder or strip duplicate nodes from the given DOM nodelist
  150. addNode(handle); // addNodeInDocOrder(handle, xctxt);
  151. }
  152. }
  153. /**
  154. * Create a NodeSetDTM which contains the given Node.
  155. *
  156. * @param node Single node to be added to the new set.
  157. */
  158. public NodeSetDTM(int node, DTMManager dtmManager)
  159. {
  160. super();
  161. m_manager = dtmManager;
  162. addNode(node);
  163. }
  164. /**
  165. * Set the environment in which this iterator operates, which should provide:
  166. * a node (the context node... same value as "root" defined below)
  167. * a pair of non-zero positive integers (the context position and the context size)
  168. * a set of variable bindings
  169. * a function library
  170. * the set of namespace declarations in scope for the expression.
  171. *
  172. * <p>At this time the exact implementation of this environment is application
  173. * dependent. Probably a proper interface will be created fairly soon.</p>
  174. *
  175. * @param environment The environment object.
  176. */
  177. public void setEnvironment(Object environment)
  178. {
  179. // no-op
  180. }
  181. /**
  182. * @return The root node of the Iterator, as specified when it was created.
  183. * For non-Iterator NodeSetDTMs, this will be null.
  184. */
  185. public int getRoot()
  186. {
  187. if(DTM.NULL == m_root)
  188. {
  189. if(size() > 0)
  190. return item(0);
  191. else
  192. return DTM.NULL;
  193. }
  194. else
  195. return m_root;
  196. }
  197. /**
  198. * Initialize the context values for this expression
  199. * after it is cloned.
  200. *
  201. * @param execContext The XPath runtime context for this
  202. * transformation.
  203. */
  204. public void setRoot(int context, Object environment)
  205. {
  206. // no-op, I guess... (-sb)
  207. }
  208. /**
  209. * Clone this NodeSetDTM.
  210. * At this time, we only expect this to be used with LocPathIterators;
  211. * it may not work with other kinds of NodeSetDTMs.
  212. *
  213. * @return a new NodeSetDTM of the same type, having the same state...
  214. * though unless overridden in the subclasses, it may not copy all
  215. * the state information.
  216. *
  217. * @throws CloneNotSupportedException if this subclass of NodeSetDTM
  218. * does not support the clone() operation.
  219. */
  220. public Object clone() throws CloneNotSupportedException
  221. {
  222. NodeSetDTM clone = (NodeSetDTM) super.clone();
  223. return clone;
  224. }
  225. /**
  226. * Get a cloned Iterator, and reset its state to the beginning of the
  227. * iteration.
  228. *
  229. * @return a new NodeSetDTM of the same type, having the same state...
  230. * except that the reset() operation has been called.
  231. *
  232. * @throws CloneNotSupportedException if this subclass of NodeSetDTM
  233. * does not support the clone() operation.
  234. */
  235. public DTMIterator cloneWithReset() throws CloneNotSupportedException
  236. {
  237. NodeSetDTM clone = (NodeSetDTM) clone();
  238. clone.reset();
  239. return clone;
  240. }
  241. /**
  242. * Reset the iterator. May have no effect on non-iterator Nodesets.
  243. */
  244. public void reset()
  245. {
  246. m_next = 0;
  247. }
  248. /**
  249. * This attribute determines which node types are presented via the
  250. * iterator. The available set of constants is defined in the
  251. * <code>DTMFilter</code> interface. For NodeSetDTMs, the mask has been
  252. * hardcoded to show all nodes except EntityReference nodes, which have
  253. * no equivalent in the XPath data model.
  254. *
  255. * @return integer used as a bit-array, containing flags defined in
  256. * the DOM's DTMFilter class. The value will be
  257. * <code>SHOW_ALL & ~SHOW_ENTITY_REFERENCE</code>, meaning that
  258. * only entity references are suppressed.
  259. */
  260. public int getWhatToShow()
  261. {
  262. return DTMFilter.SHOW_ALL & ~DTMFilter.SHOW_ENTITY_REFERENCE;
  263. }
  264. /**
  265. * The filter object used to screen nodes. Filters are applied to
  266. * further reduce (and restructure) the DTMIterator's view of the
  267. * document. In our case, we will be using hardcoded filters built
  268. * into our iterators... but getFilter() is part of the DOM's
  269. * DTMIterator interface, so we have to support it.
  270. *
  271. * @return null, which is slightly misleading. True, there is no
  272. * user-written filter object, but in fact we are doing some very
  273. * sophisticated custom filtering. A DOM purist might suggest
  274. * returning a placeholder object just to indicate that this is
  275. * not going to return all nodes selected by whatToShow.
  276. */
  277. public DTMFilter getFilter()
  278. {
  279. return null;
  280. }
  281. /**
  282. * The value of this flag determines whether the children of entity
  283. * reference nodes are visible to the iterator. If false, they will be
  284. * skipped over.
  285. * <br> To produce a view of the document that has entity references
  286. * expanded and does not expose the entity reference node itself, use the
  287. * whatToShow flags to hide the entity reference node and set
  288. * expandEntityReferences to true when creating the iterator. To produce
  289. * a view of the document that has entity reference nodes but no entity
  290. * expansion, use the whatToShow flags to show the entity reference node
  291. * and set expandEntityReferences to false.
  292. *
  293. * @return true for all iterators based on NodeSetDTM, meaning that the
  294. * contents of EntityRefrence nodes may be returned (though whatToShow
  295. * says that the EntityReferences themselves are not shown.)
  296. */
  297. public boolean getExpandEntityReferences()
  298. {
  299. return true;
  300. }
  301. /**
  302. * Get an instance of a DTM that "owns" a node handle. Since a node
  303. * iterator may be passed without a DTMManager, this allows the
  304. * caller to easily get the DTM using just the iterator.
  305. *
  306. * @param nodeHandle the nodeHandle.
  307. *
  308. * @return a non-null DTM reference.
  309. */
  310. public DTM getDTM(int nodeHandle)
  311. {
  312. return m_manager.getDTM(nodeHandle);
  313. }
  314. /* An instance of the DTMManager. */
  315. DTMManager m_manager;
  316. /**
  317. * Get an instance of the DTMManager. Since a node
  318. * iterator may be passed without a DTMManager, this allows the
  319. * caller to easily get the DTMManager using just the iterator.
  320. *
  321. * @return a non-null DTMManager reference.
  322. */
  323. public DTMManager getDTMManager()
  324. {
  325. return m_manager;
  326. }
  327. /**
  328. * Returns the next node in the set and advances the position of the
  329. * iterator in the set. After a DTMIterator is created, the first call
  330. * to nextNode() returns the first node in the set.
  331. * @return The next <code>Node</code> in the set being iterated over, or
  332. * <code>DTM.NULL</code> if there are no more members in that set.
  333. * @throws DOMException
  334. * INVALID_STATE_ERR: Raised if this method is called after the
  335. * <code>detach</code> method was invoked.
  336. */
  337. public int nextNode()
  338. {
  339. if ((m_next) < this.size())
  340. {
  341. int next = this.elementAt(m_next);
  342. m_next++;
  343. return next;
  344. }
  345. else
  346. return DTM.NULL;
  347. }
  348. /**
  349. * Returns the previous node in the set and moves the position of the
  350. * iterator backwards in the set.
  351. * @return The previous <code>Node</code> in the set being iterated over,
  352. * or<code>DTM.NULL</code> if there are no more members in that set.
  353. * @throws DOMException
  354. * INVALID_STATE_ERR: Raised if this method is called after the
  355. * <code>detach</code> method was invoked.
  356. * @throws RuntimeException thrown if this NodeSetDTM is not of
  357. * a cached type, and hence doesn't know what the previous node was.
  358. */
  359. public int previousNode()
  360. {
  361. if (!m_cacheNodes)
  362. throw new RuntimeException(
  363. XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_CANNOT_ITERATE, null)); //"This NodeSetDTM can not iterate to a previous node!");
  364. if ((m_next - 1) > 0)
  365. {
  366. m_next--;
  367. return this.elementAt(m_next);
  368. }
  369. else
  370. return DTM.NULL;
  371. }
  372. /**
  373. * Detaches the iterator from the set which it iterated over, releasing
  374. * any computational resources and placing the iterator in the INVALID
  375. * state. After<code>detach</code> has been invoked, calls to
  376. * <code>nextNode</code> or<code>previousNode</code> will raise the
  377. * exception INVALID_STATE_ERR.
  378. * <p>
  379. * This operation is a no-op in NodeSetDTM, and will not cause
  380. * INVALID_STATE_ERR to be raised by later operations.
  381. * </p>
  382. */
  383. public void detach(){}
  384. /**
  385. * Specify if it's OK for detach to release the iterator for reuse.
  386. *
  387. * @param allowRelease true if it is OK for detach to release this iterator
  388. * for pooling.
  389. */
  390. public void allowDetachToRelease(boolean allowRelease)
  391. {
  392. // no action for right now.
  393. }
  394. /**
  395. * Tells if this NodeSetDTM is "fresh", in other words, if
  396. * the first nextNode() that is called will return the
  397. * first node in the set.
  398. *
  399. * @return true if nextNode() would return the first node in the set,
  400. * false if it would return a later one.
  401. */
  402. public boolean isFresh()
  403. {
  404. return (m_next == 0);
  405. }
  406. /**
  407. * If an index is requested, NodeSetDTM will call this method
  408. * to run the iterator to the index. By default this sets
  409. * m_next to the index. If the index argument is -1, this
  410. * signals that the iterator should be run to the end.
  411. *
  412. * @param index Position to advance (or retreat) to, with
  413. * 0 requesting the reset ("fresh") position and -1 (or indeed
  414. * any out-of-bounds value) requesting the final position.
  415. * @throws RuntimeException thrown if this NodeSetDTM is not
  416. * one of the types which supports indexing/counting.
  417. */
  418. public void runTo(int index)
  419. {
  420. if (!m_cacheNodes)
  421. throw new RuntimeException(
  422. XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_CANNOT_INDEX, null)); //"This NodeSetDTM can not do indexing or counting functions!");
  423. if ((index >= 0) && (m_next < m_firstFree))
  424. m_next = index;
  425. else
  426. m_next = m_firstFree - 1;
  427. }
  428. /**
  429. * Returns the <code>index</code>th item in the collection. If
  430. * <code>index</code> is greater than or equal to the number of nodes in
  431. * the list, this returns <code>null</code>.
  432. *
  433. * TODO: What happens if index is out of range?
  434. *
  435. * @param index Index into the collection.
  436. * @return The node at the <code>index</code>th position in the
  437. * <code>NodeList</code>, or <code>null</code> if that is not a valid
  438. * index.
  439. */
  440. public int item(int index)
  441. {
  442. runTo(index);
  443. return this.elementAt(index);
  444. }
  445. /**
  446. * The number of nodes in the list. The range of valid child node indices is
  447. * 0 to <code>length-1</code> inclusive. Note that this operation requires
  448. * finding all the matching nodes, which may defeat attempts to defer
  449. * that work.
  450. *
  451. * @return integer indicating how many nodes are represented by this list.
  452. */
  453. public int getLength()
  454. {
  455. runTo(-1);
  456. return this.size();
  457. }
  458. /**
  459. * Add a node to the NodeSetDTM. Not all types of NodeSetDTMs support this
  460. * operation
  461. *
  462. * @param n Node to be added
  463. * @throws RuntimeException thrown if this NodeSetDTM is not of
  464. * a mutable type.
  465. */
  466. public void addNode(int n)
  467. {
  468. if (!m_mutable)
  469. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  470. this.addElement(n);
  471. }
  472. /**
  473. * Insert a node at a given position.
  474. *
  475. * @param n Node to be added
  476. * @param pos Offset at which the node is to be inserted,
  477. * with 0 being the first position.
  478. * @throws RuntimeException thrown if this NodeSetDTM is not of
  479. * a mutable type.
  480. */
  481. public void insertNode(int n, int pos)
  482. {
  483. if (!m_mutable)
  484. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  485. insertElementAt(n, pos);
  486. }
  487. /**
  488. * Remove a node.
  489. *
  490. * @param n Node to be added
  491. * @throws RuntimeException thrown if this NodeSetDTM is not of
  492. * a mutable type.
  493. */
  494. public void removeNode(int n)
  495. {
  496. if (!m_mutable)
  497. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  498. this.removeElement(n);
  499. }
  500. // %TBD%
  501. // /**
  502. // * Copy NodeList members into this nodelist, adding in
  503. // * document order. If a node is null, don't add it.
  504. // *
  505. // * @param nodelist List of nodes which should now be referenced by
  506. // * this NodeSetDTM.
  507. // * @throws RuntimeException thrown if this NodeSetDTM is not of
  508. // * a mutable type.
  509. // */
  510. // public void addNodes(NodeList nodelist)
  511. // {
  512. //
  513. // if (!m_mutable)
  514. // throw new RuntimeException("This NodeSetDTM is not mutable!");
  515. //
  516. // if (null != nodelist) // defensive to fix a bug that Sanjiva reported.
  517. // {
  518. // int nChildren = nodelist.getLength();
  519. //
  520. // for (int i = 0; i < nChildren; i++)
  521. // {
  522. // int obj = nodelist.item(i);
  523. //
  524. // if (null != obj)
  525. // {
  526. // addElement(obj);
  527. // }
  528. // }
  529. // }
  530. //
  531. // // checkDups();
  532. // }
  533. // %TBD%
  534. // /**
  535. // * <p>Copy NodeList members into this nodelist, adding in
  536. // * document order. Only genuine node references will be copied;
  537. // * nulls appearing in the source NodeSetDTM will
  538. // * not be added to this one. </p>
  539. // *
  540. // * <p> In case you're wondering why this function is needed: NodeSetDTM
  541. // * implements both DTMIterator and NodeList. If this method isn't
  542. // * provided, Java can't decide which of those to use when addNodes()
  543. // * is invoked. Providing the more-explicit match avoids that
  544. // * ambiguity.)</p>
  545. // *
  546. // * @param ns NodeSetDTM whose members should be merged into this NodeSetDTM.
  547. // * @throws RuntimeException thrown if this NodeSetDTM is not of
  548. // * a mutable type.
  549. // */
  550. // public void addNodes(NodeSetDTM ns)
  551. // {
  552. //
  553. // if (!m_mutable)
  554. // throw new RuntimeException("This NodeSetDTM is not mutable!");
  555. //
  556. // addNodes((DTMIterator) ns);
  557. // }
  558. /**
  559. * Copy NodeList members into this nodelist, adding in
  560. * document order. Null references are not added.
  561. *
  562. * @param iterator DTMIterator which yields the nodes to be added.
  563. * @throws RuntimeException thrown if this NodeSetDTM is not of
  564. * a mutable type.
  565. */
  566. public void addNodes(DTMIterator iterator)
  567. {
  568. if (!m_mutable)
  569. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  570. if (null != iterator) // defensive to fix a bug that Sanjiva reported.
  571. {
  572. int obj;
  573. while (DTM.NULL != (obj = iterator.nextNode()))
  574. {
  575. addElement(obj);
  576. }
  577. }
  578. // checkDups();
  579. }
  580. // %TBD%
  581. // /**
  582. // * Copy NodeList members into this nodelist, adding in
  583. // * document order. If a node is null, don't add it.
  584. // *
  585. // * @param nodelist List of nodes to be added
  586. // * @param support The XPath runtime context.
  587. // * @throws RuntimeException thrown if this NodeSetDTM is not of
  588. // * a mutable type.
  589. // */
  590. // public void addNodesInDocOrder(NodeList nodelist, XPathContext support)
  591. // {
  592. //
  593. // if (!m_mutable)
  594. // throw new RuntimeException("This NodeSetDTM is not mutable!");
  595. //
  596. // int nChildren = nodelist.getLength();
  597. //
  598. // for (int i = 0; i < nChildren; i++)
  599. // {
  600. // int node = nodelist.item(i);
  601. //
  602. // if (null != node)
  603. // {
  604. // addNodeInDocOrder(node, support);
  605. // }
  606. // }
  607. // }
  608. /**
  609. * Copy NodeList members into this nodelist, adding in
  610. * document order. If a node is null, don't add it.
  611. *
  612. * @param iterator DTMIterator which yields the nodes to be added.
  613. * @param support The XPath runtime context.
  614. * @throws RuntimeException thrown if this NodeSetDTM is not of
  615. * a mutable type.
  616. */
  617. public void addNodesInDocOrder(DTMIterator iterator, XPathContext support)
  618. {
  619. if (!m_mutable)
  620. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  621. int node;
  622. while (DTM.NULL != (node = iterator.nextNode()))
  623. {
  624. addNodeInDocOrder(node, support);
  625. }
  626. }
  627. // %TBD%
  628. // /**
  629. // * Add the node list to this node set in document order.
  630. // *
  631. // * @param start index.
  632. // * @param end index.
  633. // * @param testIndex index.
  634. // * @param nodelist The nodelist to add.
  635. // * @param support The XPath runtime context.
  636. // *
  637. // * @return false always.
  638. // * @throws RuntimeException thrown if this NodeSetDTM is not of
  639. // * a mutable type.
  640. // */
  641. // private boolean addNodesInDocOrder(int start, int end, int testIndex,
  642. // NodeList nodelist, XPathContext support)
  643. // {
  644. //
  645. // if (!m_mutable)
  646. // throw new RuntimeException("This NodeSetDTM is not mutable!");
  647. //
  648. // boolean foundit = false;
  649. // int i;
  650. // int node = nodelist.item(testIndex);
  651. //
  652. // for (i = end; i >= start; i--)
  653. // {
  654. // int child = elementAt(i);
  655. //
  656. // if (child == node)
  657. // {
  658. // i = -2; // Duplicate, suppress insert
  659. //
  660. // break;
  661. // }
  662. //
  663. // if (!support.getDOMHelper().isNodeAfter(node, child))
  664. // {
  665. // insertElementAt(node, i + 1);
  666. //
  667. // testIndex--;
  668. //
  669. // if (testIndex > 0)
  670. // {
  671. // boolean foundPrev = addNodesInDocOrder(0, i, testIndex, nodelist,
  672. // support);
  673. //
  674. // if (!foundPrev)
  675. // {
  676. // addNodesInDocOrder(i, size() - 1, testIndex, nodelist, support);
  677. // }
  678. // }
  679. //
  680. // break;
  681. // }
  682. // }
  683. //
  684. // if (i == -1)
  685. // {
  686. // insertElementAt(node, 0);
  687. // }
  688. //
  689. // return foundit;
  690. // }
  691. /**
  692. * Add the node into a vector of nodes where it should occur in
  693. * document order.
  694. * @param v Vector of nodes, presumably containing Nodes
  695. * @param obj Node object.
  696. *
  697. * @param node The node to be added.
  698. * @param test true if we should test for doc order
  699. * @param support The XPath runtime context.
  700. * @return insertIndex.
  701. * @throws RuntimeException thrown if this NodeSetDTM is not of
  702. * a mutable type.
  703. */
  704. public int addNodeInDocOrder(int node, boolean test, XPathContext support)
  705. {
  706. if (!m_mutable)
  707. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  708. int insertIndex = -1;
  709. if (test)
  710. {
  711. // This needs to do a binary search, but a binary search
  712. // is somewhat tough because the sequence test involves
  713. // two nodes.
  714. int size = size(), i;
  715. for (i = size - 1; i >= 0; i--)
  716. {
  717. int child = elementAt(i);
  718. if (child == node)
  719. {
  720. i = -2; // Duplicate, suppress insert
  721. break;
  722. }
  723. DTM dtm = support.getDTM(node);
  724. if (!dtm.isNodeAfter(node, child))
  725. {
  726. break;
  727. }
  728. }
  729. if (i != -2)
  730. {
  731. insertIndex = i + 1;
  732. insertElementAt(node, insertIndex);
  733. }
  734. }
  735. else
  736. {
  737. insertIndex = this.size();
  738. boolean foundit = false;
  739. for (int i = 0; i < insertIndex; i++)
  740. {
  741. if (i == node)
  742. {
  743. foundit = true;
  744. break;
  745. }
  746. }
  747. if (!foundit)
  748. addElement(node);
  749. }
  750. // checkDups();
  751. return insertIndex;
  752. } // end addNodeInDocOrder(Vector v, Object obj)
  753. /**
  754. * Add the node into a vector of nodes where it should occur in
  755. * document order.
  756. * @param v Vector of nodes, presumably containing Nodes
  757. * @param obj Node object.
  758. *
  759. * @param node The node to be added.
  760. * @param support The XPath runtime context.
  761. *
  762. * @return The index where it was inserted.
  763. * @throws RuntimeException thrown if this NodeSetDTM is not of
  764. * a mutable type.
  765. */
  766. public int addNodeInDocOrder(int node, XPathContext support)
  767. {
  768. if (!m_mutable)
  769. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  770. return addNodeInDocOrder(node, true, support);
  771. } // end addNodeInDocOrder(Vector v, Object obj)
  772. /**
  773. * Get the length of the list.
  774. *
  775. * @return The size of this node set.
  776. */
  777. public int size()
  778. {
  779. return super.size();
  780. }
  781. /**
  782. * Append a Node onto the vector.
  783. *
  784. * @param value The node to be added.
  785. * @throws RuntimeException thrown if this NodeSetDTM is not of
  786. * a mutable type.
  787. */
  788. public void addElement(int value)
  789. {
  790. if (!m_mutable)
  791. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  792. super.addElement(value);
  793. }
  794. /**
  795. * Inserts the specified node in this vector at the specified index.
  796. * Each component in this vector with an index greater or equal to
  797. * the specified index is shifted upward to have an index one greater
  798. * than the value it had previously.
  799. *
  800. * @param value The node to be inserted.
  801. * @param at The index where the insert should occur.
  802. * @throws RuntimeException thrown if this NodeSetDTM is not of
  803. * a mutable type.
  804. */
  805. public void insertElementAt(int value, int at)
  806. {
  807. if (!m_mutable)
  808. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  809. super.insertElementAt(value, at);
  810. }
  811. /**
  812. * Append the nodes to the list.
  813. *
  814. * @param nodes The nodes to be appended to this node set.
  815. * @throws RuntimeException thrown if this NodeSetDTM is not of
  816. * a mutable type.
  817. */
  818. public void appendNodes(NodeVector nodes)
  819. {
  820. if (!m_mutable)
  821. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  822. super.appendNodes(nodes);
  823. }
  824. /**
  825. * Inserts the specified node in this vector at the specified index.
  826. * Each component in this vector with an index greater or equal to
  827. * the specified index is shifted upward to have an index one greater
  828. * than the value it had previously.
  829. * @throws RuntimeException thrown if this NodeSetDTM is not of
  830. * a mutable type.
  831. */
  832. public void removeAllElements()
  833. {
  834. if (!m_mutable)
  835. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  836. super.removeAllElements();
  837. }
  838. /**
  839. * Removes the first occurrence of the argument from this vector.
  840. * If the object is found in this vector, each component in the vector
  841. * with an index greater or equal to the object's index is shifted
  842. * downward to have an index one smaller than the value it had
  843. * previously.
  844. *
  845. * @param s The node to be removed.
  846. *
  847. * @return True if the node was successfully removed
  848. * @throws RuntimeException thrown if this NodeSetDTM is not of
  849. * a mutable type.
  850. */
  851. public boolean removeElement(int s)
  852. {
  853. if (!m_mutable)
  854. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  855. return super.removeElement(s);
  856. }
  857. /**
  858. * Deletes the component at the specified index. Each component in
  859. * this vector with an index greater or equal to the specified
  860. * index is shifted downward to have an index one smaller than
  861. * the value it had previously.
  862. *
  863. * @param i The index of the node to be removed.
  864. * @throws RuntimeException thrown if this NodeSetDTM is not of
  865. * a mutable type.
  866. */
  867. public void removeElementAt(int i)
  868. {
  869. if (!m_mutable)
  870. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  871. super.removeElementAt(i);
  872. }
  873. /**
  874. * Sets the component at the specified index of this vector to be the
  875. * specified object. The previous component at that position is discarded.
  876. *
  877. * The index must be a value greater than or equal to 0 and less
  878. * than the current size of the vector.
  879. *
  880. * @param node The node to be set.
  881. * @param index The index of the node to be replaced.
  882. * @throws RuntimeException thrown if this NodeSetDTM is not of
  883. * a mutable type.
  884. */
  885. public void setElementAt(int node, int index)
  886. {
  887. if (!m_mutable)
  888. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  889. super.setElementAt(node, index);
  890. }
  891. /**
  892. * Same as setElementAt.
  893. *
  894. * @param node The node to be set.
  895. * @param index The index of the node to be replaced.
  896. * @throws RuntimeException thrown if this NodeSetDTM is not of
  897. * a mutable type.
  898. */
  899. public void setItem(int node, int index)
  900. {
  901. if (!m_mutable)
  902. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  903. super.setElementAt(node, index);
  904. }
  905. /**
  906. * Get the nth element.
  907. *
  908. * @param i The index of the requested node.
  909. *
  910. * @return Node at specified index.
  911. */
  912. public int elementAt(int i)
  913. {
  914. runTo(i);
  915. return super.elementAt(i);
  916. }
  917. /**
  918. * Tell if the table contains the given node.
  919. *
  920. * @param s Node to look for
  921. *
  922. * @return True if the given node was found.
  923. */
  924. public boolean contains(int s)
  925. {
  926. runTo(-1);
  927. return super.contains(s);
  928. }
  929. /**
  930. * Searches for the first occurence of the given argument,
  931. * beginning the search at index, and testing for equality
  932. * using the equals method.
  933. *
  934. * @param elem Node to look for
  935. * @param index Index of where to start the search
  936. * @return the index of the first occurrence of the object
  937. * argument in this vector at position index or later in the
  938. * vector; returns -1 if the object is not found.
  939. */
  940. public int indexOf(int elem, int index)
  941. {
  942. runTo(-1);
  943. return super.indexOf(elem, index);
  944. }
  945. /**
  946. * Searches for the first occurence of the given argument,
  947. * beginning the search at index, and testing for equality
  948. * using the equals method.
  949. *
  950. * @param elem Node to look for
  951. * @return the index of the first occurrence of the object
  952. * argument in this vector at position index or later in the
  953. * vector; returns -1 if the object is not found.
  954. */
  955. public int indexOf(int elem)
  956. {
  957. runTo(-1);
  958. return super.indexOf(elem);
  959. }
  960. /** If this node is being used as an iterator, the next index that nextNode()
  961. * will return. */
  962. transient protected int m_next = 0;
  963. /**
  964. * Get the current position, which is one less than
  965. * the next nextNode() call will retrieve. i.e. if
  966. * you call getCurrentPos() and the return is 0, the next
  967. * fetch will take place at index 1.
  968. *
  969. * @return The the current position index.
  970. */
  971. public int getCurrentPos()
  972. {
  973. return m_next;
  974. }
  975. /**
  976. * Set the current position in the node set.
  977. * @param i Must be a valid index.
  978. * @throws RuntimeException thrown if this NodeSetDTM is not of
  979. * a cached type, and thus doesn't permit indexed access.
  980. */
  981. public void setCurrentPos(int i)
  982. {
  983. if (!m_cacheNodes)
  984. throw new RuntimeException(
  985. XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_CANNOT_INDEX, null)); //"This NodeSetDTM can not do indexing or counting functions!");
  986. m_next = i;
  987. }
  988. /**
  989. * Return the last fetched node. Needed to support the UnionPathIterator.
  990. *
  991. * @return the last fetched node.
  992. * @throws RuntimeException thrown if this NodeSetDTM is not of
  993. * a cached type, and thus doesn't permit indexed access.
  994. */
  995. public int getCurrentNode()
  996. {
  997. if (!m_cacheNodes)
  998. throw new RuntimeException(
  999. "This NodeSetDTM can not do indexing or counting functions!");
  1000. int saved = m_next;
  1001. // because nextNode always increments
  1002. // But watch out for copy29, where the root iterator didn't
  1003. // have nextNode called on it.
  1004. int current = (m_next > 0) ? m_next-1 : m_next;
  1005. int n = (current < m_firstFree) ? elementAt(current) : DTM.NULL;
  1006. m_next = saved; // HACK: I think this is a bit of a hack. -sb
  1007. return n;
  1008. }
  1009. /** True if this list can be mutated. */
  1010. transient protected boolean m_mutable = true;
  1011. /** True if this list is cached.
  1012. * @serial */
  1013. transient protected boolean m_cacheNodes = true;
  1014. /** The root of the iteration, if available. */
  1015. protected int m_root = DTM.NULL;
  1016. /**
  1017. * Get whether or not this is a cached node set.
  1018. *
  1019. *
  1020. * @return True if this list is cached.
  1021. */
  1022. public boolean getShouldCacheNodes()
  1023. {
  1024. return m_cacheNodes;
  1025. }
  1026. /**
  1027. * If setShouldCacheNodes(true) is called, then nodes will
  1028. * be cached. They are not cached by default. This switch must
  1029. * be set before the first call to nextNode is made, to ensure
  1030. * that all nodes are cached.
  1031. *
  1032. * @param b true if this node set should be cached.
  1033. * @throws RuntimeException thrown if an attempt is made to
  1034. * request caching after we've already begun stepping through the
  1035. * nodes in this set.
  1036. */
  1037. public void setShouldCacheNodes(boolean b)
  1038. {
  1039. if (!isFresh())
  1040. throw new RuntimeException(
  1041. XSLMessages.createXPATHMessage(XPATHErrorResources.ER_CANNOT_CALL_SETSHOULDCACHENODE, null)); //"Can not call setShouldCacheNodes after nextNode has been called!");
  1042. m_cacheNodes = b;
  1043. m_mutable = true;
  1044. }
  1045. /**
  1046. * Tells if this iterator can have nodes added to it or set via
  1047. * the <code>setItem(int node, int index)</code> method.
  1048. *
  1049. * @return True if the nodelist can be mutated.
  1050. */
  1051. public boolean isMutable()
  1052. {
  1053. return m_mutable;
  1054. }
  1055. transient private int m_last = 0;
  1056. public int getLast()
  1057. {
  1058. return m_last;
  1059. }
  1060. public void setLast(int last)
  1061. {
  1062. m_last = last;
  1063. }
  1064. /**
  1065. * Returns true if all the nodes in the iteration well be returned in document
  1066. * order.
  1067. *
  1068. * @return true as a default.
  1069. */
  1070. public boolean isDocOrdered()
  1071. {
  1072. return true;
  1073. }
  1074. /**
  1075. * Returns the axis being iterated, if it is known.
  1076. *
  1077. * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
  1078. * types.
  1079. */
  1080. public int getAxis()
  1081. {
  1082. return -1;
  1083. }
  1084. }