| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.XPathContext; |
| 12 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 13 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 14 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 15 |
import net.sf.saxon.expr.parser.RoleDiagnostic; |
| 16 |
import net.sf.saxon.functions.registry.BuiltInFunctionSet; |
| 17 |
import net.sf.saxon.ma.arrays.ArrayFunctionSet; |
| 18 |
import net.sf.saxon.ma.arrays.ArrayItem; |
| 19 |
import net.sf.saxon.ma.arrays.ArrayItemType; |
| 20 |
import net.sf.saxon.ma.arrays.SquareArrayConstructor; |
| 21 |
import net.sf.saxon.ma.map.MapFunctionSet; |
| 22 |
import net.sf.saxon.ma.map.MapType; |
| 23 |
import net.sf.saxon.om.*; |
| 24 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 25 |
import net.sf.saxon.trans.XPathException; |
| 26 |
import net.sf.saxon.type.*; |
| 27 |
import net.sf.saxon.value.SequenceType; |
| 28 |
|
| 29 |
import java.util.Properties; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class ApplyFn extends SystemFunction { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
private String dynamicFunctionCall; |
| 41 |
|
| 42 |
public ApplyFn() { |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public void setDynamicFunctionCall(String fnExpr) { |
| 51 |
dynamicFunctionCall = fnExpr; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public boolean isDynamicFunctionCall() { |
| 61 |
return dynamicFunctionCall != null; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
@Override |
| 71 |
public ItemType getResultItemType(Expression[] args) { |
| 72 |
|
| 73 |
ItemType fnType = args[0].getItemType(); |
| 74 |
if (fnType instanceof MapType) { |
| 75 |
return ((MapType)fnType).getValueType().getPrimaryType(); |
| 76 |
} else if (fnType instanceof ArrayItemType) { |
| 77 |
return ((ArrayItemType) fnType).getMemberType().getPrimaryType(); |
| 78 |
} else if (fnType instanceof FunctionItemType) { |
| 79 |
return ((FunctionItemType) fnType).getResultType().getPrimaryType(); |
| 80 |
} else if (fnType instanceof AnyFunctionType) { |
| 81 |
return AnyItemType.getInstance(); |
| 82 |
} else { |
| 83 |
return AnyItemType.getInstance(); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException { |
| 89 |
if (arguments.length == 2 && arguments[1] instanceof SquareArrayConstructor) { |
| 90 |
Expression target = arguments[0]; |
| 91 |
if (target.getItemType() instanceof MapType) { |
| 92 |
|
| 93 |
|
| 94 |
return makeGetCall(visitor, MapFunctionSet.getInstance(), contextInfo, arguments); |
| 95 |
} else if (target.getItemType() instanceof ArrayItemType) { |
| 96 |
|
| 97 |
return makeGetCall(visitor, ArrayFunctionSet.getInstance(), contextInfo, arguments); |
| 98 |
} |
| 99 |
} |
| 100 |
return null; |
| 101 |
} |
| 102 |
|
| 103 |
private Expression makeGetCall(ExpressionVisitor visitor, BuiltInFunctionSet fnSet, ContextItemStaticInfo contextInfo, Expression[] arguments) throws XPathException { |
| 104 |
Expression target = arguments[0]; |
| 105 |
Expression key = ((SquareArrayConstructor)arguments[1]).getOperanda().getOperand(0).getChildExpression(); |
| 106 |
Expression getter = fnSet.makeFunction("get", 2).makeFunctionCall(target, key); |
| 107 |
getter.setRetainedStaticContext(target.getRetainedStaticContext()); |
| 108 |
return getter.typeCheck(visitor, contextInfo); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 124 |
final Function function = (Function) arguments[0].head(); |
| 125 |
ArrayItem args = (ArrayItem)arguments[1].head(); |
| 126 |
|
| 127 |
if (function.getArity() != args.arrayLength()) { |
| 128 |
String errorCode = isDynamicFunctionCall() ? "XPTY0004" : "FOAP0001"; |
| 129 |
XPathException err = new XPathException( |
| 130 |
"Number of arguments required for dynamic call to " + function.getDescription() + " is " + function.getArity() + |
| 131 |
"; number supplied = " + args.arrayLength(), errorCode); |
| 132 |
err.setIsTypeError(isDynamicFunctionCall()); |
| 133 |
err.setXPathContext(context); |
| 134 |
throw err; |
| 135 |
} |
| 136 |
TypeHierarchy th = context.getConfiguration().getTypeHierarchy(); |
| 137 |
FunctionItemType fit = function.getFunctionItemType(); |
| 138 |
Sequence[] argArray = new Sequence[args.arrayLength()]; |
| 139 |
if (fit == AnyFunctionType.ANY_FUNCTION) { |
| 140 |
for (int i = 0; i < argArray.length; i++) { |
| 141 |
argArray[i] = args.get(i); |
| 142 |
} |
| 143 |
} else { |
| 144 |
for (int i = 0; i < argArray.length; i++) { |
| 145 |
SequenceType expected = fit.getArgumentTypes()[i]; |
| 146 |
RoleDiagnostic role; |
| 147 |
if (isDynamicFunctionCall()) { |
| 148 |
role = new RoleDiagnostic(RoleDiagnostic.FUNCTION, "result of " + dynamicFunctionCall, i); |
| 149 |
} else { |
| 150 |
role = new RoleDiagnostic(RoleDiagnostic.FUNCTION, "fn:apply", i + 1); |
| 151 |
} |
| 152 |
Sequence<?> converted = th.applyFunctionConversionRules( |
| 153 |
args.get(i), expected, role, ExplicitLocation.UNKNOWN_LOCATION); |
| 154 |
argArray[i] = converted.materialize(); |
| 155 |
} |
| 156 |
} |
| 157 |
Sequence<?> rawResult = dynamicCall(function, context, argArray); |
| 158 |
if (function.isTrustedResultType()) { |
| 159 |
|
| 160 |
return rawResult; |
| 161 |
} else { |
| 162 |
|
| 163 |
RoleDiagnostic resultRole = new RoleDiagnostic(RoleDiagnostic.FUNCTION_RESULT, "fn:apply", -1); |
| 164 |
return th.applyFunctionConversionRules( |
| 165 |
rawResult, fit.getResultType(), resultRole, ExplicitLocation.UNKNOWN_LOCATION); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public void exportAttributes(ExpressionPresenter out) { |
| 171 |
out.emitAttribute("dyn", dynamicFunctionCall); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
@Override |
| 181 |
public void importAttributes(Properties attributes) { |
| 182 |
dynamicFunctionCall = attributes.getProperty("dyn"); |
| 183 |
} |
| 184 |
} |
| 185 |
|