1. /*
  2. * Copyright 2001-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: FilterIterator.java,v 1.8 2004/02/16 22:54:59 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.dom;
  20. import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
  21. import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  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.ref.DTMAxisIteratorBase;
  25. /**
  26. * Similar to a CurrentNodeListIterator except that the filter has a
  27. * simpler interface (only needs the node, no position, last, etc.)
  28. * It takes a source iterator and a Filter object and returns nodes
  29. * from the source after filtering them by calling filter.test(node).
  30. * @author Jacek Ambroziak
  31. * @author Santiago Pericas-Geertsen
  32. */
  33. public final class FilterIterator extends DTMAxisIteratorBase {
  34. /**
  35. * Reference to source iterator.
  36. */
  37. private DTMAxisIterator _source;
  38. /**
  39. * Reference to a filter object that to be applied to each node.
  40. */
  41. private final DTMFilter _filter;
  42. /**
  43. * A flag indicating if position is reversed.
  44. */
  45. private final boolean _isReverse;
  46. public FilterIterator(DTMAxisIterator source, DTMFilter filter) {
  47. _source = source;
  48. // System.out.println("FI souce = " + source + " this = " + this);
  49. _filter = filter;
  50. _isReverse = source.isReverse();
  51. }
  52. public boolean isReverse() {
  53. return _isReverse;
  54. }
  55. public void setRestartable(boolean isRestartable) {
  56. _isRestartable = isRestartable;
  57. _source.setRestartable(isRestartable);
  58. }
  59. public DTMAxisIterator cloneIterator() {
  60. try {
  61. final FilterIterator clone = (FilterIterator) super.clone();
  62. clone._source = _source.cloneIterator();
  63. clone._isRestartable = false;
  64. return clone.reset();
  65. }
  66. catch (CloneNotSupportedException e) {
  67. BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
  68. e.toString());
  69. return null;
  70. }
  71. }
  72. public DTMAxisIterator reset() {
  73. _source.reset();
  74. return resetPosition();
  75. }
  76. public int next() {
  77. int node;
  78. while ((node = _source.next()) != END) {
  79. if (_filter.acceptNode(node, DTMFilter.SHOW_ALL) == DTMIterator.FILTER_ACCEPT) {
  80. return returnNode(node);
  81. }
  82. }
  83. return END;
  84. }
  85. public DTMAxisIterator setStartNode(int node) {
  86. if (_isRestartable) {
  87. _source.setStartNode(_startNode = node);
  88. return resetPosition();
  89. }
  90. return this;
  91. }
  92. public void setMark() {
  93. _source.setMark();
  94. }
  95. public void gotoMark() {
  96. _source.gotoMark();
  97. }
  98. }