| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 11 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 12 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 13 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 14 |
import net.sf.saxon.om.GroundedValue; |
| 15 |
import net.sf.saxon.om.Item; |
| 16 |
import net.sf.saxon.om.SequenceIterator; |
| 17 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.type.ItemType; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class TailExpression extends UnaryExpression { |
| 26 |
|
| 27 |
int start; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public TailExpression(Expression base, int start) { |
| 39 |
super(base); |
| 40 |
this.start = start; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 45 |
getOperand().optimize(visitor, contextInfo); |
| 46 |
if (getBaseExpression() instanceof Literal) { |
| 47 |
GroundedValue<?> value = |
| 48 |
iterate(visitor.getStaticContext().makeEarlyEvaluationContext()).materialize(); |
| 49 |
return Literal.makeLiteral(value, this); |
| 50 |
} |
| 51 |
return this; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public Expression copy(RebindingMap rebindings) { |
| 63 |
TailExpression exp = new TailExpression(getBaseExpression().copy(rebindings), start); |
| 64 |
ExpressionTool.copyLocationInfo(this, exp); |
| 65 |
return exp; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
@Override |
| 77 |
public int getImplementationMethod() { |
| 78 |
return ITERATE_METHOD; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
public ItemType getItemType() { |
| 83 |
return getBaseExpression().getItemType(); |
| 84 |
} |
| 85 |
|
| 86 |
public int computeCardinality() { |
| 87 |
return getBaseExpression().getCardinality() | StaticProperty.ALLOWS_ZERO; |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
protected OperandRole getOperandRole() { |
| 92 |
return OperandRole.SAME_FOCUS_ACTION; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public int getStart() { |
| 102 |
return start; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public boolean equals(Object other) { |
| 113 |
return other instanceof TailExpression && |
| 114 |
getBaseExpression().isEqual(((TailExpression)other).getBaseExpression()) && |
| 115 |
start == ((TailExpression) other).start; |
| 116 |
} |
| 117 |
|
| 118 |
public int computeHashCode() { |
| 119 |
return super.computeHashCode() ^ start; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
@Override |
| 129 |
public String getStreamerName() { |
| 130 |
return "TailExpression"; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 135 |
SequenceIterator<?> baseIter = getBaseExpression().iterate(context); |
| 136 |
return TailIterator.make(baseIter, start); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
@Override |
| 147 |
public String getExpressionName() { |
| 148 |
return "tail"; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public void export(ExpressionPresenter destination) throws XPathException { |
| 159 |
destination.startElement("tail", this); |
| 160 |
destination.emitAttribute("start", start + ""); |
| 161 |
getBaseExpression().export(destination); |
| 162 |
destination.endElement(); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
@Override |
| 174 |
public String toString() { |
| 175 |
if (start == 2) { |
| 176 |
return "tail(" + getBaseExpression() + ")"; |
| 177 |
} else { |
| 178 |
return ExpressionTool.parenthesize(getBaseExpression()) + "[position() ge " + start + "]"; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
@Override |
| 188 |
public String toShortString() { |
| 189 |
if (start == 2) { |
| 190 |
return "tail(" + getBaseExpression().toShortString() + ")"; |
| 191 |
} else { |
| 192 |
return getBaseExpression().toShortString() + "[position() ge " + start + "]"; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|