| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
import net.sf.saxon.Configuration; |
| 13 |
import net.sf.saxon.expr.*; |
| 14 |
import net.sf.saxon.expr.parser.*; |
| 15 |
import net.sf.saxon.lib.ExtensionFunctionCall; |
| 16 |
import net.sf.saxon.lib.ExtensionFunctionDefinition; |
| 17 |
import net.sf.saxon.om.*; |
| 18 |
import net.sf.saxon.pattern.AnyNodeTest; |
| 19 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 20 |
import net.sf.saxon.trans.SaxonErrorCode; |
| 21 |
import net.sf.saxon.trans.XPathException; |
| 22 |
import net.sf.saxon.type.AnyItemType; |
| 23 |
import net.sf.saxon.type.ItemType; |
| 24 |
import net.sf.saxon.type.TypeHierarchy; |
| 25 |
import net.sf.saxon.value.SequenceType; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class IntegratedFunctionCall extends FunctionCall implements Callable { |
| 32 |
|
| 33 |
private StructuredQName name; |
| 34 |
private ExtensionFunctionCall function; |
| 35 |
private SequenceType resultType = SequenceType.ANY_SEQUENCE; |
| 36 |
private int state = 0; |
| 37 |
|
| 38 |
public IntegratedFunctionCall(StructuredQName name, ExtensionFunctionCall function) { |
| 39 |
this.name = name; |
| 40 |
this.function = function; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public void setResultType(SequenceType resultType) { |
| 49 |
this.resultType = resultType; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
@Override |
| 58 |
public StructuredQName getFunctionName() { |
| 59 |
return name; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
@Override |
| 69 |
public Function getTargetFunction(XPathContext context) { |
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public ExtensionFunctionCall getFunction() { |
| 80 |
return function; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public void checkArguments( ExpressionVisitor visitor) throws XPathException { |
| 97 |
ExtensionFunctionDefinition definition = function.getDefinition(); |
| 98 |
checkArgumentCount(definition.getMinimumNumberOfArguments(), definition.getMaximumNumberOfArguments()); |
| 99 |
final int args = getArity(); |
| 100 |
SequenceType[] declaredArgumentTypes = definition.getArgumentTypes(); |
| 101 |
if (declaredArgumentTypes == null || (args != 0 && declaredArgumentTypes.length == 0)) { |
| 102 |
throw new XPathException("Integrated function " + getDisplayName() + |
| 103 |
" failed to declare its argument types"); |
| 104 |
} |
| 105 |
SequenceType[] actualArgumentTypes = new SequenceType[args]; |
| 106 |
TypeChecker tc = visitor.getConfiguration().getTypeChecker(false); |
| 107 |
for (int i = 0; i < args; i++) { |
| 108 |
setArg(i, tc.staticTypeCheck( |
| 109 |
getArg(i), |
| 110 |
i < declaredArgumentTypes.length ? |
| 111 |
declaredArgumentTypes[i] : |
| 112 |
declaredArgumentTypes[declaredArgumentTypes.length - 1], |
| 113 |
new RoleDiagnostic(RoleDiagnostic.FUNCTION, getFunctionName().getDisplayName(), i), |
| 114 |
visitor)); |
| 115 |
|
| 116 |
actualArgumentTypes[i] = SequenceType.makeSequenceType( |
| 117 |
getArg(i).getItemType(), |
| 118 |
getArg(i).getCardinality()); |
| 119 |
} |
| 120 |
resultType = definition.getResultType(actualArgumentTypes); |
| 121 |
if (state == 0) { |
| 122 |
function.supplyStaticContext(visitor.getStaticContext(), 0, getArguments()); |
| 123 |
} |
| 124 |
state++; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 133 |
Expression exp = super.typeCheck(visitor, contextInfo); |
| 134 |
if (exp instanceof IntegratedFunctionCall) { |
| 135 |
Expression exp2 = ((IntegratedFunctionCall) exp).function.rewrite(visitor.getStaticContext(), getArguments()); |
| 136 |
if (exp2 == null) { |
| 137 |
return exp; |
| 138 |
} else { |
| 139 |
ExpressionTool.copyLocationInfo(this, exp2); |
| 140 |
return exp2.simplify().typeCheck(visitor, contextInfo).optimize(visitor, contextInfo); |
| 141 |
} |
| 142 |
} |
| 143 |
return exp; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public Expression preEvaluate(ExpressionVisitor visitor) { |
| 155 |
return this; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public ItemType getItemType() { |
| 172 |
return resultType.getPrimaryType(); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
protected int computeCardinality() { |
| 184 |
return resultType.getCardinality(); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public int getIntrinsicDependencies() { |
| 198 |
ExtensionFunctionDefinition definition = function.getDefinition(); |
| 199 |
return definition.dependsOnFocus() ? StaticProperty.DEPENDS_ON_FOCUS : 0; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
protected int computeSpecialProperties() { |
| 212 |
ExtensionFunctionDefinition definition = function.getDefinition(); |
| 213 |
return definition.hasSideEffects() ? StaticProperty.HAS_SIDE_EFFECTS : StaticProperty.NO_NODES_NEWLY_CREATED; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public Expression copy(RebindingMap rebindings) { |
| 225 |
ExtensionFunctionCall newCall = function.getDefinition().makeCallExpression(); |
| 226 |
newCall.setDefinition(function.getDefinition()); |
| 227 |
function.copyLocalData(newCall); |
| 228 |
IntegratedFunctionCall copy = new IntegratedFunctionCall(getFunctionName(), newCall); |
| 229 |
Expression[] args = new Expression[getArity()]; |
| 230 |
for (int i = 0; i < args.length; i++) { |
| 231 |
args[i] = getArg(i).copy(rebindings); |
| 232 |
} |
| 233 |
copy.setArguments(args); |
| 234 |
copy.resultType = resultType; |
| 235 |
copy.state = state; |
| 236 |
ExpressionTool.copyLocationInfo(this, copy); |
| 237 |
return copy; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public void export(ExpressionPresenter out) throws XPathException { |
| 246 |
out.startElement("ifCall", this); |
| 247 |
out.emitAttribute("name", getFunctionName()); |
| 248 |
out.emitAttribute("type", resultType.toExportString()); |
| 249 |
for (Operand o : operands()) { |
| 250 |
o.getChildExpression().export(out); |
| 251 |
} |
| 252 |
out.endElement(); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public SequenceIterator<?> iterate(final XPathContext context) throws XPathException { |
| 273 |
ExtensionFunctionDefinition definition = function.getDefinition(); |
| 274 |
Sequence[] argValues = new Sequence[getArity()]; |
| 275 |
for (int i = 0; i < argValues.length; i++) { |
| 276 |
argValues[i] = SequenceTool.toLazySequence(getArg(i).iterate(context)); |
| 277 |
} |
| 278 |
final RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.FUNCTION_RESULT, getFunctionName().getDisplayName(), 0); |
| 279 |
final Configuration config = context.getConfiguration(); |
| 280 |
final TypeHierarchy th = config.getTypeHierarchy(); |
| 281 |
|
| 282 |
SequenceIterator<?> result; |
| 283 |
try { |
| 284 |
result = function.call(context, argValues).iterate(); |
| 285 |
} catch (XPathException e) { |
| 286 |
e.maybeSetLocation(getLocation()); |
| 287 |
throw e; |
| 288 |
} |
| 289 |
if (!definition.trustResultType()) { |
| 290 |
int card = resultType.getCardinality(); |
| 291 |
if (card != StaticProperty.ALLOWS_ZERO_OR_MORE) { |
| 292 |
result = new CardinalityCheckingIterator<>(result, card, role, getLocation()); |
| 293 |
} |
| 294 |
final ItemType type = resultType.getPrimaryType(); |
| 295 |
if (type != AnyItemType.getInstance()) { |
| 296 |
result = new ItemMappingIterator<>(result, |
| 297 |
(ItemMappingFunction<Item<?>, Item<?>>) item -> { |
| 298 |
if (!type.matches(item, th)) { |
| 299 |
String msg = role.composeErrorMessage(type, item, th); |
| 300 |
XPathException err = new XPathException(msg, "XPTY0004"); |
| 301 |
err.setLocation(getLocation()); |
| 302 |
throw err; |
| 303 |
} |
| 304 |
return item; |
| 305 |
}, true); |
| 306 |
} |
| 307 |
if (th.relationship(type, AnyNodeTest.getInstance()) != TypeHierarchy.DISJOINT) { |
| 308 |
result = new ItemMappingIterator<>( |
| 309 |
result, |
| 310 |
new ConfigurationCheckingFunction(context.getConfiguration()), true); |
| 311 |
} |
| 312 |
} |
| 313 |
return result; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public boolean effectiveBooleanValue(XPathContext context) throws XPathException { |
| 329 |
Sequence[] argValues = new Sequence[getArity()]; |
| 330 |
for (int i = 0; i < argValues.length; i++) { |
| 331 |
argValues[i] = SequenceTool.toLazySequence(getArg(i).iterate(context)); |
| 332 |
} |
| 333 |
try { |
| 334 |
return function.effectiveBooleanValue(context, argValues); |
| 335 |
} catch (XPathException e) { |
| 336 |
e.maybeSetLocation(getLocation()); |
| 337 |
throw e; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 342 |
return function.call(context, arguments); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public static class ConfigurationCheckingFunction implements ItemMappingFunction<Item<?>, Item<?>> { |
| 352 |
|
| 353 |
private Configuration config; |
| 354 |
|
| 355 |
public ConfigurationCheckingFunction(Configuration config) { |
| 356 |
this.config = config; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public Item mapItem(Item item) throws XPathException { |
| 367 |
if (item instanceof NodeInfo && !config.isCompatible(((NodeInfo) item).getConfiguration())) { |
| 368 |
throw new XPathException( |
| 369 |
"Node returned by extension function was created with an incompatible Configuration", SaxonErrorCode.SXXP0004); |
| 370 |
} |
| 371 |
return item; |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
|