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: DTMNodeList.java,v 1.11 2004/02/16 23:06:11 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.dtm.ref;
  20. import com.sun.org.apache.xml.internal.dtm.DTM;
  21. import com.sun.org.apache.xml.internal.dtm.DTMIterator;
  22. import org.w3c.dom.Node;
  23. /**
  24. * <code>DTMNodeList</code> gives us an implementation of the DOM's
  25. * NodeList interface wrapped around a DTM Iterator. The author
  26. * considers this something of an abominations, since NodeList was not
  27. * intended to be a general purpose "list of nodes" API and is
  28. * generally considered by the DOM WG to have be a mistake... but I'm
  29. * told that some of the XPath/XSLT folks say they must have this
  30. * solution.
  31. *
  32. * Please note that this is not necessarily equivlaent to a DOM
  33. * NodeList operating over the same document. In particular:
  34. * <ul>
  35. *
  36. * <li>If there are several Text nodes in logical succession (ie,
  37. * across CDATASection and EntityReference boundaries), we will return
  38. * only the first; the caller is responsible for stepping through
  39. * them.
  40. * (%REVIEW% Provide a convenience routine here to assist, pending
  41. * proposed DOM Level 3 getAdjacentText() operation?) </li>
  42. *
  43. * <li>Since the whole XPath/XSLT architecture assumes that the source
  44. * document is not altered while we're working with it, we do not
  45. * promise to implement the DOM NodeList's "live view" response to
  46. * document mutation. </li>
  47. *
  48. * </ul>
  49. *
  50. * <p>State: In progress!!</p>
  51. * */
  52. public class DTMNodeList extends DTMNodeListBase {
  53. private DTMIterator m_iter;
  54. //================================================================
  55. // Methods unique to this class
  56. private DTMNodeList() {
  57. }
  58. /**
  59. * Public constructor: Wrap a DTMNodeList around an existing
  60. * and preconfigured DTMIterator
  61. *
  62. * WARNING: THIS HAS THE SIDE EFFECT OF ISSUING setShouldCacheNodes(true)
  63. * AGAINST THE DTMIterator.
  64. *
  65. */
  66. public DTMNodeList(DTMIterator dtmIterator) {
  67. if (dtmIterator != null) {
  68. int pos = dtmIterator.getCurrentPos();
  69. try {
  70. m_iter=(DTMIterator)dtmIterator.cloneWithReset();
  71. } catch(CloneNotSupportedException cnse) {
  72. m_iter = dtmIterator;
  73. }
  74. m_iter.setShouldCacheNodes(true);
  75. m_iter.runTo(-1);
  76. m_iter.setCurrentPos(pos);
  77. }
  78. }
  79. /**
  80. * Access the wrapped DTMIterator. I'm not sure whether anyone will
  81. * need this or not, but let's write it and think about it.
  82. *
  83. */
  84. public DTMIterator getDTMIterator() {
  85. return m_iter;
  86. }
  87. //================================================================
  88. // org.w3c.dom.NodeList API follows
  89. /**
  90. * Returns the <code>index</code>th item in the collection. If
  91. * <code>index</code> is greater than or equal to the number of nodes in
  92. * the list, this returns <code>null</code>.
  93. * @param indexIndex into the collection.
  94. * @return The node at the <code>index</code>th position in the
  95. * <code>NodeList</code>, or <code>null</code> if that is not a valid
  96. * index.
  97. */
  98. public Node item(int index)
  99. {
  100. if (m_iter != null) {
  101. int handle=m_iter.item(index);
  102. if (handle == DTM.NULL) {
  103. return null;
  104. }
  105. return m_iter.getDTM(handle).getNode(handle);
  106. } else {
  107. return null;
  108. }
  109. }
  110. /**
  111. * The number of nodes in the list. The range of valid child node indices
  112. * is 0 to <code>length-1</code> inclusive.
  113. */
  114. public int getLength() {
  115. return (m_iter != null) ? m_iter.getLength() : 0;
  116. }
  117. }