| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.type.SpecificFunctionType; |
| 11 |
import net.sf.saxon.expr.Callable; |
| 12 |
import net.sf.saxon.expr.ContextItemExpression; |
| 13 |
import net.sf.saxon.expr.Expression; |
| 14 |
import net.sf.saxon.expr.XPathContext; |
| 15 |
import net.sf.saxon.om.*; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.type.FunctionItemType; |
| 18 |
import net.sf.saxon.value.DoubleValue; |
| 19 |
import net.sf.saxon.value.SequenceType; |
| 20 |
import net.sf.saxon.value.StringValue; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class ContextItemAccessorFunction extends ContextAccessorFunction { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public Function bindContext(XPathContext context) throws XPathException { |
| 42 |
final Item ci = context.getContextItem(); |
| 43 |
if (ci == null) { |
| 44 |
Callable callable = (context1, arguments) -> { |
| 45 |
throw new XPathException("Context item for " + |
| 46 |
getFunctionName().getDisplayName() + " is absent", "XPDY0002"); |
| 47 |
}; |
| 48 |
FunctionItemType fit = new SpecificFunctionType(new SequenceType[]{}, SequenceType.ANY_SEQUENCE); |
| 49 |
return new CallableFunction(0, callable, fit); |
| 50 |
} |
| 51 |
ConstantFunction fn = new ConstantFunction(evaluate(ci, context)); |
| 52 |
fn.setDetails(getDetails()); |
| 53 |
fn.setRetainedStaticContext(getRetainedStaticContext()); |
| 54 |
return fn; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public GroundedValue evaluate(Item item, XPathContext context) throws XPathException { |
| 67 |
SystemFunction f = SystemFunction.makeFunction(getDetails().name.getLocalPart(), getRetainedStaticContext(), 1); |
| 68 |
return f.call(context, new Sequence[]{item}).materialize(); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 81 |
|
| 82 |
return evaluate(context.getContextItem(), context); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@Override |
| 94 |
public Expression makeFunctionCall(Expression[] arguments) { |
| 95 |
Expression arg = new ContextItemExpression(); |
| 96 |
return SystemFunction.makeCall(getFunctionName().getLocalPart(), getRetainedStaticContext(), arg); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public Expression makeContextItemExplicit() { |
| 106 |
Expression[] args = new Expression[]{new ContextItemExpression()}; |
| 107 |
return SystemFunction.makeCall(getFunctionName().getLocalPart(), getRetainedStaticContext(), args); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public static class StringAccessor extends ContextItemAccessorFunction { |
| 116 |
|
| 117 |
@Override |
| 118 |
public Expression makeFunctionCall(Expression[] arguments) { |
| 119 |
Expression ci = new ContextItemExpression(); |
| 120 |
Expression sv = SystemFunction.makeCall("string", getRetainedStaticContext(), ci); |
| 121 |
return SystemFunction.makeCall(getFunctionName().getLocalPart(), getRetainedStaticContext(), sv); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public GroundedValue evaluate(Item item, XPathContext context) throws XPathException { |
| 126 |
SystemFunction f = SystemFunction.makeFunction(getDetails().name.getLocalPart(), getRetainedStaticContext(), 1); |
| 127 |
StringValue val = new StringValue(item.getStringValueCS()); |
| 128 |
return f.call(context, new Sequence[]{val}).materialize(); |
| 129 |
} |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public static class Number_0 extends ContextItemAccessorFunction { |
| 139 |
|
| 140 |
@Override |
| 141 |
public Expression makeFunctionCall(Expression[] arguments) { |
| 142 |
Expression ci = new ContextItemExpression(); |
| 143 |
Expression sv = SystemFunction.makeCall("data", getRetainedStaticContext(), ci); |
| 144 |
return SystemFunction.makeCall(getFunctionName().getLocalPart(), getRetainedStaticContext(), sv); |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public GroundedValue evaluate(Item item, XPathContext context) throws XPathException { |
| 149 |
SystemFunction f = SystemFunction.makeFunction(getDetails().name.getLocalPart(), getRetainedStaticContext(), 1); |
| 150 |
AtomicSequence val = item.atomize(); |
| 151 |
switch (val.getLength()) { |
| 152 |
case 0: |
| 153 |
return DoubleValue.NaN; |
| 154 |
case 1: |
| 155 |
return f.call(context, new Sequence[]{val.head()}).materialize(); |
| 156 |
default: |
| 157 |
XPathException err = new XPathException( |
| 158 |
"When number() is called with no arguments, the atomized value of the context node must " + |
| 159 |
"not be a sequence of several atomic values", "XPTY0004"); |
| 160 |
err.setIsTypeError(true); |
| 161 |
throw err; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
} |
| 169 |
|