| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Callable; |
| 11 |
import net.sf.saxon.expr.Expression; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 14 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 15 |
import net.sf.saxon.om.FocusIterator; |
| 16 |
import net.sf.saxon.om.Function; |
| 17 |
import net.sf.saxon.om.Sequence; |
| 18 |
import net.sf.saxon.trans.SymbolicName; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.type.ErrorType; |
| 21 |
import net.sf.saxon.value.Int64Value; |
| 22 |
import net.sf.saxon.value.IntegerValue; |
| 23 |
|
| 24 |
|
| 25 |
public abstract class PositionAndLast extends ContextAccessorFunction { |
| 26 |
|
| 27 |
private boolean contextPossiblyUndefined = true; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
@Override |
| 37 |
public int getNetCost() { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
@Override |
| 52 |
public Function bindContext(XPathContext context) { |
| 53 |
Int64Value value; |
| 54 |
try { |
| 55 |
value = evaluateItem(context); |
| 56 |
} catch (final XPathException e) { |
| 57 |
|
| 58 |
SymbolicName.F name = new SymbolicName.F(getFunctionName(), getArity()); |
| 59 |
Callable callable = (context1, arguments) -> { |
| 60 |
throw e; |
| 61 |
}; |
| 62 |
return new CallableFunction(name, callable, getFunctionItemType()); |
| 63 |
} |
| 64 |
ConstantFunction fn = new ConstantFunction(value); |
| 65 |
fn.setDetails(getDetails()); |
| 66 |
fn.setRetainedStaticContext(getRetainedStaticContext()); |
| 67 |
return fn; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
@Override |
| 84 |
public IntegerValue[] getIntegerBounds() { |
| 85 |
return new IntegerValue[]{Int64Value.PLUS_ONE, Expression.MAX_SEQUENCE_LENGTH}; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void supplyTypeInformation(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression[] arguments) throws XPathException { |
| 90 |
super.supplyTypeInformation(visitor, contextInfo, arguments); |
| 91 |
if (contextInfo.getItemType() == ErrorType.getInstance()) { |
| 92 |
XPathException err = new XPathException("The context item is absent at this point"); |
| 93 |
err.setErrorCode("XPDY0002"); |
| 94 |
throw err; |
| 95 |
} else { |
| 96 |
contextPossiblyUndefined = contextInfo.isPossiblyAbsent(); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public boolean isContextPossiblyUndefined() { |
| 107 |
return contextPossiblyUndefined; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public abstract Int64Value evaluateItem(XPathContext c) throws XPathException; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public IntegerValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 126 |
return evaluateItem(context); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
public static class Position extends PositionAndLast { |
| 131 |
public Int64Value evaluateItem(XPathContext c) throws XPathException { |
| 132 |
FocusIterator currentIterator = c.getCurrentIterator(); |
| 133 |
if (currentIterator == null) { |
| 134 |
XPathException e = new XPathException("The context item is absent, so position() is undefined"); |
| 135 |
e.setXPathContext(c); |
| 136 |
e.setErrorCode("XPDY0002"); |
| 137 |
throw e; |
| 138 |
} |
| 139 |
return Int64Value.makeIntegerValue(currentIterator.position()); |
| 140 |
} |
| 141 |
|
| 142 |
public String getCompilerName() { |
| 143 |
return "PositionCompiler"; |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
public static class Last extends PositionAndLast { |
| 149 |
public Int64Value evaluateItem(XPathContext c) throws XPathException { |
| 150 |
return Int64Value.makeIntegerValue(c.getLast()); |
| 151 |
} |
| 152 |
|
| 153 |
public String getCompilerName() { |
| 154 |
return "LastCompiler"; |
| 155 |
} |
| 156 |
|
| 157 |
public String getStreamerName() { |
| 158 |
return "Last"; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|