| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.instruct.OriginalFunction; |
| 11 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 12 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 13 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 14 |
import net.sf.saxon.functions.SystemFunction; |
| 15 |
import net.sf.saxon.om.Function; |
| 16 |
import net.sf.saxon.om.Item; |
| 17 |
import net.sf.saxon.om.Sequence; |
| 18 |
import net.sf.saxon.om.StructuredQName; |
| 19 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.type.ItemType; |
| 22 |
import net.sf.saxon.type.UType; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class StaticFunctionCall extends FunctionCall implements Callable { |
| 31 |
|
| 32 |
private Function target; |
| 33 |
|
| 34 |
public StaticFunctionCall(Function target, Expression[] arguments) { |
| 35 |
if (target.getArity() != arguments.length) { |
| 36 |
throw new IllegalArgumentException("Function call to " + target.getFunctionName() + " with wrong number of arguments (" + arguments.length + ")"); |
| 37 |
} |
| 38 |
this.target = target; |
| 39 |
setOperanda(arguments, target.getOperandRoles()); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public Function getTargetFunction() { |
| 49 |
return target; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
@Override |
| 60 |
public Function getTargetFunction(XPathContext context) { |
| 61 |
return getTargetFunction(); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
@Override |
| 70 |
public StructuredQName getFunctionName() { |
| 71 |
return target.getFunctionName(); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public boolean isCallOn(Class<? extends SystemFunction> function) { |
| 82 |
return function.isAssignableFrom(target.getClass()); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@Override |
| 94 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 95 |
checkFunctionCall(target, visitor); |
| 96 |
return super.typeCheck(visitor, contextInfo); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
@Override |
| 106 |
public Expression copy(RebindingMap rebindings) { |
| 107 |
Expression[] args = new Expression[getArity()]; |
| 108 |
for (int i=0; i<args.length; i++) { |
| 109 |
args[i] = getArg(i).copy(rebindings); |
| 110 |
} |
| 111 |
return new StaticFunctionCall(target, args); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
@Override |
| 122 |
protected int computeCardinality() { |
| 123 |
return target.getFunctionItemType().getResultType().getCardinality(); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
@Override |
| 138 |
public ItemType getItemType() { |
| 139 |
return target.getFunctionItemType().getResultType().getPrimaryType(); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
@Override |
| 150 |
public UType getStaticUType(UType contextItemType) { |
| 151 |
UType result = getItemType().getUType(); |
| 152 |
for (Operand o : operands()) { |
| 153 |
if (o.getUsage() == OperandUsage.TRANSMISSION) { |
| 154 |
result = result.intersection(o.getChildExpression().getStaticUType(contextItemType)); |
| 155 |
} |
| 156 |
} |
| 157 |
return result; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public Sequence<?> call(XPathContext context, Sequence<?>[] arguments) throws XPathException { |
| 181 |
return target.call(context, arguments); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
@Override |
| 192 |
public String getExpressionName() { |
| 193 |
return "staticFunctionCall"; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
@Override |
| 203 |
public void export(ExpressionPresenter out) throws XPathException { |
| 204 |
if (target instanceof OriginalFunction) { |
| 205 |
ExpressionPresenter.ExportOptions options = (ExpressionPresenter.ExportOptions) out.getOptions(); |
| 206 |
OriginalFunction pf = (OriginalFunction) target; |
| 207 |
out.startElement("origFC", this); |
| 208 |
out.emitAttribute("name", pf.getFunctionName()); |
| 209 |
out.emitAttribute("pack", options.packageMap.get(pf.getComponent().getContainingPackage()) + ""); |
| 210 |
|
| 211 |
for (Operand o : operands()) { |
| 212 |
o.getChildExpression().export(out); |
| 213 |
} |
| 214 |
out.endElement(); |
| 215 |
} else if (target instanceof UnionCastableFunction) { |
| 216 |
|
| 217 |
out.startElement("castable", this); |
| 218 |
out.emitAttribute("emptiable", ((UnionCastableFunction) target).isAllowEmpty() ? "1" : "0"); |
| 219 |
out.emitAttribute("as", ((UnionCastableFunction) target).getTargetType().toExportString()); |
| 220 |
for (Operand o : operands()) { |
| 221 |
o.getChildExpression().export(out); |
| 222 |
} |
| 223 |
out.endElement(); |
| 224 |
} else if (target instanceof ListCastableFunction) { |
| 225 |
|
| 226 |
out.startElement("castable", this); |
| 227 |
out.emitAttribute("emptiable", ((ListCastableFunction) target).isAllowEmpty() ? "1" : "0"); |
| 228 |
out.emitAttribute("as", ((ListCastableFunction) target).getTargetType().getStructuredQName()); |
| 229 |
for (Operand o : operands()) { |
| 230 |
o.getChildExpression().export(out); |
| 231 |
} |
| 232 |
out.endElement(); |
| 233 |
} else if (target instanceof UnionConstructorFunction) { |
| 234 |
|
| 235 |
out.startElement("cast", this); |
| 236 |
out.emitAttribute("emptiable", ((UnionConstructorFunction) target).isAllowEmpty() ? "1" : "0"); |
| 237 |
out.emitAttribute("as", ((UnionConstructorFunction) target).getTargetType().toExportString()); |
| 238 |
for (Operand o : operands()) { |
| 239 |
o.getChildExpression().export(out); |
| 240 |
} |
| 241 |
out.endElement(); |
| 242 |
} else if (target instanceof ListConstructorFunction) { |
| 243 |
|
| 244 |
out.startElement("cast", this); |
| 245 |
out.emitAttribute("emptiable", ((ListConstructorFunction) target).isAllowEmpty() ? "1" : "0"); |
| 246 |
out.emitAttribute("as", ((ListConstructorFunction) target).getTargetType().getStructuredQName()); |
| 247 |
for (Operand o : operands()) { |
| 248 |
o.getChildExpression().export(out); |
| 249 |
} |
| 250 |
out.endElement(); |
| 251 |
} else { |
| 252 |
super.export(out); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
} |
| 258 |
|