1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.dom; 21 22 import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; 23 import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary; 24 import com.sun.org.apache.xalan.internal.xsltc.util.IntegerArray; 25 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator; 26 import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIteratorBase; 27 28 41 42 public final class CurrentNodeListIterator extends DTMAxisIteratorBase { 43 46 private boolean _docOrder; 47 48 51 private DTMAxisIterator _source; 52 53 56 private final CurrentNodeListFilter _filter; 57 58 61 private IntegerArray _nodes = new IntegerArray(); 62 63 66 private int _currentIndex; 67 68 71 private final int _currentNode; 72 73 76 private AbstractTranslet _translet; 77 78 public CurrentNodeListIterator(DTMAxisIterator source, 79 CurrentNodeListFilter filter, 80 int currentNode, 81 AbstractTranslet translet) 82 { 83 this(source, !source.isReverse(), filter, currentNode, translet); 84 } 85 86 public CurrentNodeListIterator(DTMAxisIterator source, boolean docOrder, 87 CurrentNodeListFilter filter, 88 int currentNode, 89 AbstractTranslet translet) 90 { 91 _source = source; 92 _filter = filter; 93 _translet = translet; 94 _docOrder = docOrder; 95 _currentNode = currentNode; 96 } 97 98 public DTMAxisIterator forceNaturalOrder() { 99 _docOrder = true; 100 return this; 101 } 102 103 public void setRestartable(boolean isRestartable) { 104 _isRestartable = isRestartable; 105 _source.setRestartable(isRestartable); 106 } 107 108 public boolean isReverse() { 109 return !_docOrder; 110 } 111 112 public DTMAxisIterator cloneIterator() { 113 try { 114 final CurrentNodeListIterator clone = 115 (CurrentNodeListIterator) super.clone(); 116 clone._nodes = (IntegerArray) _nodes.clone(); 117 clone._source = _source.cloneIterator(); 118 clone._isRestartable = false; 119 return clone.reset(); 120 } 121 catch (CloneNotSupportedException e) { 122 BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR, 123 e.toString()); 124 return null; 125 } 126 } 127 128 public DTMAxisIterator reset() { 129 _currentIndex = 0; 130 return resetPosition(); 131 } 132 133 public int next() { 134 final int last = _nodes.cardinality(); 135 final int currentNode = _currentNode; 136 final AbstractTranslet translet = _translet; 137 138 for (int index = _currentIndex; index < last; ) { 139 final int position = _docOrder ? index + 1 : last - index; 140 final int node = _nodes.at(index++); 142 if (_filter.test(node, position, last, currentNode, translet, 143 this)) { 144 _currentIndex = index; 145 return returnNode(node); 146 } 147 } 148 return END; 149 } 150 151 public DTMAxisIterator setStartNode(int node) { 152 if (_isRestartable) { 153 _source.setStartNode(_startNode = node); 154 155 _nodes.clear(); 156 while ((node = _source.next()) != END) { 157 _nodes.add(node); 158 } 159 _currentIndex = 0; 160 resetPosition(); 161 } 162 return this; 163 } 164 165 public int getLast() { 166 if (_last == -1) { 167 _last = computePositionOfLast(); 168 } 169 return _last; 170 } 171 172 public void setMark() { 173 _markedNode = _currentIndex; 174 } 175 176 public void gotoMark() { 177 _currentIndex = _markedNode; 178 } 179 180 private int computePositionOfLast() { 181 final int last = _nodes.cardinality(); 182 final int currNode = _currentNode; 183 final AbstractTranslet translet = _translet; 184 185 int lastPosition = _position; 186 for (int index = _currentIndex; index < last; ) { 187 final int position = _docOrder ? index + 1 : last - index; 188 int nodeIndex = _nodes.at(index++); 190 if (_filter.test(nodeIndex, position, last, currNode, translet, 191 this)) { 192 lastPosition++; 193 } 194 } 195 return lastPosition; 196 } 197 } 198 | Popular Tags |