1 16 19 package org.apache.xpath.axes; 20 21 import org.apache.xml.dtm.Axis; 22 import org.apache.xml.dtm.DTM; 23 import org.apache.xml.dtm.DTMIterator; 24 import org.apache.xpath.Expression; 25 import org.apache.xpath.ExpressionOwner; 26 import org.apache.xpath.XPathVisitor; 27 import org.apache.xpath.compiler.Compiler; 28 import org.apache.xpath.compiler.OpCodes; 29 30 38 public class UnionPathIterator extends LocPathIterator 39 implements Cloneable , DTMIterator, java.io.Serializable , PathComponent 40 { 41 42 45 public UnionPathIterator() 46 { 47 48 super(); 49 50 m_iterators = null; 53 m_exprs = null; 54 } 55 56 63 public void setRoot(int context, Object environment) 64 { 65 super.setRoot(context, environment); 66 67 try 68 { 69 if (null != m_exprs) 70 { 71 int n = m_exprs.length; 72 DTMIterator newIters[] = new DTMIterator[n]; 73 74 for (int i = 0; i < n; i++) 75 { 76 DTMIterator iter = m_exprs[i].asIterator(m_execContext, context); 77 newIters[i] = iter; 78 iter.nextNode(); 79 } 80 m_iterators = newIters; 81 } 82 } 83 catch(Exception e) 84 { 85 throw new org.apache.xml.utils.WrappedRuntimeException(e); 86 } 87 } 88 89 94 public void addIterator(DTMIterator expr) 95 { 96 97 if (null == m_iterators) 100 { 101 m_iterators = new DTMIterator[1]; 102 m_iterators[0] = expr; 103 } 104 else 105 { 106 DTMIterator[] exprs = m_iterators; 107 int len = m_iterators.length; 108 109 m_iterators = new DTMIterator[len + 1]; 110 111 System.arraycopy(exprs, 0, m_iterators, 0, len); 112 113 m_iterators[len] = expr; 114 } 115 expr.nextNode(); 116 if(expr instanceof Expression) 117 ((Expression)expr).exprSetParent(this); 118 } 119 120 127 public void detach() 128 { 129 if(m_allowDetach && null != m_iterators){ 130 int n = m_iterators.length; 131 for(int i = 0; i < n; i++) 132 { 133 m_iterators[i].detach(); 134 } 135 m_iterators = null; 136 } 137 } 138 139 140 152 public UnionPathIterator(Compiler compiler, int opPos) 153 throws javax.xml.transform.TransformerException 154 { 155 156 super(); 157 158 opPos = compiler.getFirstChildPos(opPos); 159 160 loadLocationPaths(compiler, opPos, 0); 161 } 162 163 175 public static LocPathIterator createUnionIterator(Compiler compiler, int opPos) 176 throws javax.xml.transform.TransformerException 177 { 178 UnionPathIterator upi = new UnionPathIterator(compiler, opPos); 183 int nPaths = upi.m_exprs.length; 184 boolean isAllChildIterators = true; 185 for(int i = 0; i < nPaths; i++) 186 { 187 LocPathIterator lpi = upi.m_exprs[i]; 188 189 if(lpi.getAxis() != Axis.CHILD) 190 { 191 isAllChildIterators = false; 192 break; 193 } 194 else 195 { 196 if(HasPositionalPredChecker.check(lpi)) 198 { 199 isAllChildIterators = false; 200 break; 201 } 202 } 203 } 204 if(isAllChildIterators) 205 { 206 UnionChildIterator uci = new UnionChildIterator(); 207 208 for(int i = 0; i < nPaths; i++) 209 { 210 PredicatedNodeTest lpi = upi.m_exprs[i]; 211 uci.addNodeTest(lpi); 215 } 216 return uci; 217 218 } 219 else 220 return upi; 221 } 222 223 227 public int getAnalysisBits() 228 { 229 int bits = 0; 230 231 if (m_exprs != null) 232 { 233 int n = m_exprs.length; 234 235 for (int i = 0; i < n; i++) 236 { 237 int bit = m_exprs[i].getAnalysisBits(); 238 bits |= bit; 239 } 240 } 241 242 return bits; 243 } 244 245 253 private void readObject(java.io.ObjectInputStream stream) 254 throws java.io.IOException , javax.xml.transform.TransformerException 255 { 256 try 257 { 258 stream.defaultReadObject(); 259 m_clones = new IteratorPool(this); 260 } 261 catch (ClassNotFoundException cnfe) 262 { 263 throw new javax.xml.transform.TransformerException (cnfe); 264 } 265 } 266 267 275 public Object clone() throws CloneNotSupportedException 276 { 277 278 UnionPathIterator clone = (UnionPathIterator) super.clone(); 279 291 return clone; 292 } 293 294 295 306 protected LocPathIterator createDTMIterator( 307 Compiler compiler, int opPos) throws javax.xml.transform.TransformerException 308 { 309 LocPathIterator lpi = (LocPathIterator)WalkerFactory.newDTMIterator(compiler, opPos, 310 (compiler.getLocationPathDepth() <= 0)); 311 return lpi; 312 } 313 314 325 protected void loadLocationPaths(Compiler compiler, int opPos, int count) 326 throws javax.xml.transform.TransformerException 327 { 328 329 int steptype = compiler.getOp(opPos); 331 332 if (steptype == OpCodes.OP_LOCATIONPATH) 333 { 334 loadLocationPaths(compiler, compiler.getNextOpPos(opPos), count + 1); 335 336 m_exprs[count] = createDTMIterator(compiler, opPos); 337 m_exprs[count].exprSetParent(this); 338 } 339 else 340 { 341 342 switch (steptype) 345 { 346 case OpCodes.OP_VARIABLE : 347 case OpCodes.OP_EXTFUNCTION : 348 case OpCodes.OP_FUNCTION : 349 case OpCodes.OP_GROUP : 350 loadLocationPaths(compiler, compiler.getNextOpPos(opPos), count + 1); 351 352 WalkingIterator iter = 353 new WalkingIterator(compiler.getNamespaceContext()); 354 iter.exprSetParent(this); 355 356 if(compiler.getLocationPathDepth() <= 0) 357 iter.setIsTopLevel(true); 358 359 iter.m_firstWalker = new org.apache.xpath.axes.FilterExprWalker(iter); 360 361 iter.m_firstWalker.init(compiler, opPos, steptype); 362 363 m_exprs[count] = iter; 364 break; 365 default : 366 m_exprs = new LocPathIterator[count]; 367 } 368 } 369 } 370 371 378 public int nextNode() 379 { 380 if(m_foundLast) 381 return DTM.NULL; 382 383 int earliestNode = DTM.NULL; 386 387 if (null != m_iterators) 388 { 389 int n = m_iterators.length; 390 int iteratorUsed = -1; 391 392 for (int i = 0; i < n; i++) 393 { 394 int node = m_iterators[i].getCurrentNode(); 395 396 if (DTM.NULL == node) 397 continue; 398 else if (DTM.NULL == earliestNode) 399 { 400 iteratorUsed = i; 401 earliestNode = node; 402 } 403 else 404 { 405 if (node == earliestNode) 406 { 407 408 m_iterators[i].nextNode(); 410 } 411 else 412 { 413 DTM dtm = getDTM(node); 414 415 if (dtm.isNodeAfter(node, earliestNode)) 416 { 417 iteratorUsed = i; 418 earliestNode = node; 419 } 420 } 421 } 422 } 423 424 if (DTM.NULL != earliestNode) 425 { 426 m_iterators[iteratorUsed].nextNode(); 427 428 incrementCurrentPos(); 429 } 430 else 431 m_foundLast = true; 432 } 433 434 m_lastFetched = earliestNode; 435 436 return earliestNode; 437 } 438 439 449 public void fixupVariables(java.util.Vector vars, int globalsSize) 450 { 451 for (int i = 0; i < m_exprs.length; i++) 452 { 453 m_exprs[i].fixupVariables(vars, globalsSize); 454 } 455 456 } 457 458 464 protected LocPathIterator[] m_exprs; 465 466 467 473 protected DTMIterator[] m_iterators; 474 475 481 public int getAxis() 482 { 483 return -1; 485 } 486 487 class iterOwner implements ExpressionOwner 488 { 489 int m_index; 490 491 iterOwner(int index) 492 { 493 m_index = index; 494 } 495 496 499 public Expression getExpression() 500 { 501 return m_exprs[m_index]; 502 } 503 504 507 public void setExpression(Expression exp) 508 { 509 510 if(!(exp instanceof LocPathIterator)) 511 { 512 WalkingIterator wi = new WalkingIterator(getPrefixResolver()); 515 FilterExprWalker few = new FilterExprWalker(wi); 516 wi.setFirstWalker(few); 517 few.setInnerExpression(exp); 518 wi.exprSetParent(UnionPathIterator.this); 519 few.exprSetParent(wi); 520 exp.exprSetParent(few); 521 exp = wi; 522 } 523 else 524 exp.exprSetParent(UnionPathIterator.this); 525 m_exprs[m_index] = (LocPathIterator)exp; 526 } 527 528 } 529 530 533 public void callVisitors(ExpressionOwner owner, XPathVisitor visitor) 534 { 535 if(visitor.visitUnionPath(owner, this)) 536 { 537 if(null != m_exprs) 538 { 539 int n = m_exprs.length; 540 for(int i = 0; i < n; i++) 541 { 542 m_exprs[i].callVisitors(new iterOwner(i), visitor); 543 } 544 } 545 } 546 } 547 548 551 public boolean deepEquals(Expression expr) 552 { 553 if (!super.deepEquals(expr)) 554 return false; 555 556 UnionPathIterator upi = (UnionPathIterator) expr; 557 558 if (null != m_exprs) 559 { 560 int n = m_exprs.length; 561 562 if((null == upi.m_exprs) || (upi.m_exprs.length != n)) 563 return false; 564 565 for (int i = 0; i < n; i++) 566 { 567 if(!m_exprs[i].deepEquals(upi.m_exprs[i])) 568 return false; 569 } 570 } 571 else if (null != upi.m_exprs) 572 { 573 return false; 574 } 575 576 return true; 577 } 578 579 580 } 581 | Popular Tags |