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: DTMChildIterNodeList.java,v 1.3 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 org.w3c.dom.Node;
  22. /**
  23. * <code>DTMNodeList</code> gives us an implementation of the DOM's
  24. * NodeList interface wrapped around a DTM Iterator. The author
  25. * considers this something of an abominations, since NodeList was not
  26. * intended to be a general purpose "list of nodes" API and is
  27. * generally considered by the DOM WG to have be a mistake... but I'm
  28. * told that some of the XPath/XSLT folks say they must have this
  29. * solution.
  30. *
  31. * Please note that this is not necessarily equivlaent to a DOM
  32. * NodeList operating over the same document. In particular:
  33. * <ul>
  34. *
  35. * <li>If there are several Text nodes in logical succession (ie,
  36. * across CDATASection and EntityReference boundaries), we will return
  37. * only the first; the caller is responsible for stepping through
  38. * them.
  39. * (%REVIEW% Provide a convenience routine here to assist, pending
  40. * proposed DOM Level 3 getAdjacentText() operation?) </li>
  41. *
  42. * <li>Since the whole XPath/XSLT architecture assumes that the source
  43. * document is not altered while we're working with it, we do not
  44. * promise to implement the DOM NodeList's "live view" response to
  45. * document mutation. </li>
  46. *
  47. * </ul>
  48. *
  49. * <p>State: In progress!!</p>
  50. * */
  51. public class DTMChildIterNodeList extends DTMNodeListBase {
  52. private int m_firstChild;
  53. private DTM m_parentDTM;
  54. //================================================================
  55. // Methods unique to this class
  56. private DTMChildIterNodeList() {
  57. }
  58. /**
  59. * Public constructor: Create a NodeList to support
  60. * DTMNodeProxy.getChildren().
  61. *
  62. * Unfortunately AxisIterators and DTMIterators don't share an API,
  63. * so I can't use the existing Axis.CHILD iterator. Rather than
  64. * create Yet Another Class, let's set up a special case of this
  65. * one.
  66. *
  67. * @param parentDTM The DTM containing this node
  68. * @param parentHandle DTM node-handle integer
  69. *
  70. */
  71. public DTMChildIterNodeList(DTM parentDTM,int parentHandle) {
  72. m_parentDTM=parentDTM;
  73. m_firstChild=parentDTM.getFirstChild(parentHandle);
  74. }
  75. //================================================================
  76. // org.w3c.dom.NodeList API follows
  77. /**
  78. * Returns the <code>index</code>th item in the collection. If
  79. * <code>index</code> is greater than or equal to the number of nodes in
  80. * the list, this returns <code>null</code>.
  81. * @param indexIndex into the collection.
  82. * @return The node at the <code>index</code>th position in the
  83. * <code>NodeList</code>, or <code>null</code> if that is not a valid
  84. * index.
  85. */
  86. public Node item(int index) {
  87. int handle=m_firstChild;
  88. while(--index>=0 && handle!=DTM.NULL) {
  89. handle=m_parentDTM.getNextSibling(handle);
  90. }
  91. if (handle == DTM.NULL) {
  92. return null;
  93. }
  94. return m_parentDTM.getNode(handle);
  95. }
  96. /**
  97. * The number of nodes in the list. The range of valid child node indices
  98. * is 0 to <code>length-1</code> inclusive.
  99. */
  100. public int getLength() {
  101. int count=0;
  102. for (int handle=m_firstChild;
  103. handle!=DTM.NULL;
  104. handle=m_parentDTM.getNextSibling(handle)) {
  105. ++count;
  106. }
  107. return count;
  108. }
  109. }