1 package net.sf.saxon.expr; 2 3 import net.sf.saxon.om.ArrayIterator; 4 import net.sf.saxon.om.Item; 5 import net.sf.saxon.om.NamePool; 6 import net.sf.saxon.om.SequenceIterator; 7 import net.sf.saxon.trans.XPathException; 8 import net.sf.saxon.type.ItemType; 9 10 import java.io.PrintStream ; 11 import java.util.Iterator ; 12 13 17 public class TailExpression extends ComputedExpression { 18 19 Expression base; 20 int start; 23 29 30 public TailExpression(Expression base, int start) { 31 this.base = base; 32 this.start = start; 33 adoptChildExpression(base); 34 } 35 36 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 37 base = base.typeCheck(env, contextItemType); 38 return this; 39 } 40 41 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 42 base = base.optimize(opt, env, contextItemType); 43 return this; 44 } 45 46 public Expression promote(PromotionOffer offer) throws XPathException { 47 Expression exp = offer.accept(this); 48 if (exp != null) { 49 return exp; 50 } else { 51 if (offer.action != PromotionOffer.UNORDERED) { 52 base = doPromotion(base, offer); 53 } 54 return this; 55 } 56 } 57 58 public int computeSpecialProperties() { 59 return base.getSpecialProperties(); 60 } 61 62 public ItemType getItemType() { 63 return base.getItemType(); 64 } 65 66 public int computeCardinality() { 67 return base.getCardinality() | StaticProperty.ALLOWS_ZERO; 68 } 69 70 public Iterator iterateSubExpressions() { 71 return new MonoIterator(base); 72 } 73 74 public Expression getBaseExpression() { 75 return base; 76 } 77 78 public int getStart() { 79 return start; 80 } 81 82 public boolean equals(Object other) { 83 return other instanceof TailExpression && 84 base.equals(((TailExpression)other).base) && 85 start == ((TailExpression)other).start; 86 } 87 88 public int hashCode() { 89 return base.hashCode(); 90 } 91 92 public SequenceIterator iterate(XPathContext context) throws XPathException { 93 SequenceIterator baseIter = base.iterate(context); 94 if (baseIter instanceof ArrayIterator) { 95 return ((ArrayIterator)baseIter).makeSliceIterator(start, Integer.MAX_VALUE); 96 } else { 97 return new TailIterator(baseIter, start); 98 } 99 } 100 101 public void display(int level, NamePool pool, PrintStream out) { 102 out.println(ExpressionTool.indent(level) + "tail " + start); 103 base.display(level+1, pool, out); 104 } 105 106 public static class TailIterator implements SequenceIterator { 107 108 private SequenceIterator base; 109 private int start; 110 111 public TailIterator(SequenceIterator base, int start) throws XPathException { 112 this.base = base; 113 this.start = start; 114 115 for (int i=0; i < start-1; i++) { 117 base.next(); 118 } 119 } 120 121 public Item next() throws XPathException { 122 return base.next(); 123 } 124 125 public Item current() { 126 return base.current(); 127 } 128 129 public int position() { 130 return base.position() - start + 1; 131 } 132 133 public SequenceIterator getAnother() throws XPathException { 134 return new TailIterator(base.getAnother(), start); 135 } 136 137 146 147 public int getProperties() { 148 return 0; 149 } 150 } 151 } 152 153 | Popular Tags |