1 16 19 package org.apache.xpath.axes; 20 21 import org.apache.xml.dtm.DTM; 22 import org.apache.xml.dtm.DTMAxisIterator; 23 import org.apache.xml.dtm.DTMFilter; 24 import org.apache.xml.dtm.DTMIterator; 25 import org.apache.xpath.Expression; 26 import org.apache.xpath.XPathContext; 27 import org.apache.xpath.compiler.Compiler; 28 29 35 public class OneStepIterator extends ChildTestIterator 36 { 37 38 protected int m_axis = -1; 39 40 41 protected DTMAxisIterator m_iterator; 42 43 52 OneStepIterator(Compiler compiler, int opPos, int analysis) 53 throws javax.xml.transform.TransformerException 54 { 55 super(compiler, opPos, analysis); 56 int firstStepPos = compiler.getFirstChildPos(opPos); 57 58 m_axis = WalkerFactory.getAxisFromStep(compiler, firstStepPos); 59 60 } 61 62 63 71 public OneStepIterator(DTMAxisIterator iterator, int axis) 72 throws javax.xml.transform.TransformerException 73 { 74 super(null); 75 76 m_iterator = iterator; 77 m_axis = axis; 78 int whatToShow = DTMFilter.SHOW_ALL; 79 initNodeTest(whatToShow); 80 } 81 82 89 public void setRoot(int context, Object environment) 90 { 91 super.setRoot(context, environment); 92 if(m_axis > -1) 93 m_iterator = m_cdtm.getAxisIterator(m_axis); 94 m_iterator.setStartNode(m_context); 95 } 96 97 104 public void detach() 105 { 106 if(m_allowDetach) 107 { 108 if(m_axis > -1) 109 m_iterator = null; 110 111 super.detach(); 113 } 114 } 115 116 119 protected int getNextNode() 120 { 121 return m_lastFetched = m_iterator.next(); 122 } 123 124 131 public Object clone() throws CloneNotSupportedException 132 { 133 135 OneStepIterator clone = (OneStepIterator) super.clone(); 136 137 if(m_iterator != null) 138 { 139 clone.m_iterator = m_iterator.cloneIterator(); 140 } 141 return clone; 142 } 143 144 152 public DTMIterator cloneWithReset() throws CloneNotSupportedException 153 { 154 155 OneStepIterator clone = (OneStepIterator) super.cloneWithReset(); 156 clone.m_iterator = m_iterator; 157 158 return clone; 159 } 160 161 162 163 168 public boolean isReverseAxes() 169 { 170 return m_iterator.isReverse(); 171 } 172 173 184 protected int getProximityPosition(int predicateIndex) 185 { 186 if(!isReverseAxes()) 187 return super.getProximityPosition(predicateIndex); 188 189 if(predicateIndex < 0) 193 return -1; 194 195 if (m_proximityPositions[predicateIndex] <= 0) 196 { 197 XPathContext xctxt = getXPathContext(); 198 try 199 { 200 OneStepIterator clone = (OneStepIterator) this.clone(); 201 202 int root = getRoot(); 203 xctxt.pushCurrentNode(root); 204 clone.setRoot(root, xctxt); 205 206 clone.m_predCount = predicateIndex; 208 209 int count = 1; 211 int next; 212 213 while (DTM.NULL != (next = clone.nextNode())) 214 { 215 count++; 216 } 217 218 m_proximityPositions[predicateIndex] += count; 219 } 220 catch (CloneNotSupportedException cnse) 221 { 222 223 } 225 finally 226 { 227 xctxt.popCurrentNode(); 228 } 229 } 230 231 return m_proximityPositions[predicateIndex]; 232 } 233 234 240 public int getLength() 241 { 242 if(!isReverseAxes()) 243 return super.getLength(); 244 245 boolean isPredicateTest = (this == m_execContext.getSubContextList()); 247 248 int predCount = getPredicateCount(); 250 251 if (-1 != m_length && isPredicateTest && m_predicateIndex < 1) 255 return m_length; 256 257 int count = 0; 258 259 XPathContext xctxt = getXPathContext(); 260 try 261 { 262 OneStepIterator clone = (OneStepIterator) this.cloneWithReset(); 263 264 int root = getRoot(); 265 xctxt.pushCurrentNode(root); 266 clone.setRoot(root, xctxt); 267 268 clone.m_predCount = m_predicateIndex; 269 270 int next; 271 272 while (DTM.NULL != (next = clone.nextNode())) 273 { 274 count++; 275 } 276 } 277 catch (CloneNotSupportedException cnse) 278 { 279 } 281 finally 282 { 283 xctxt.popCurrentNode(); 284 } 285 if (isPredicateTest && m_predicateIndex < 1) 286 m_length = count; 287 288 return count; 289 } 290 291 296 protected void countProximityPosition(int i) 297 { 298 if(!isReverseAxes()) 299 super.countProximityPosition(i); 300 else if (i < m_proximityPositions.length) 301 m_proximityPositions[i]--; 302 } 303 304 307 public void reset() 308 { 309 310 super.reset(); 311 if(null != m_iterator) 312 m_iterator.reset(); 313 } 314 315 321 public int getAxis() 322 { 323 return m_axis; 324 } 325 326 329 public boolean deepEquals(Expression expr) 330 { 331 if(!super.deepEquals(expr)) 332 return false; 333 334 if(m_axis != ((OneStepIterator)expr).m_axis) 335 return false; 336 337 return true; 338 } 339 340 341 } 342 | Popular Tags |