| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions.registry; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.OperandUsage; |
| 12 |
import net.sf.saxon.expr.StaticContext; |
| 13 |
import net.sf.saxon.expr.StaticProperty; |
| 14 |
import net.sf.saxon.expr.parser.RetainedStaticContext; |
| 15 |
import net.sf.saxon.functions.FunctionLibrary; |
| 16 |
import net.sf.saxon.functions.OptionsParameter; |
| 17 |
import net.sf.saxon.functions.SystemFunction; |
| 18 |
import net.sf.saxon.lib.NamespaceConstant; |
| 19 |
import net.sf.saxon.om.Function; |
| 20 |
import net.sf.saxon.om.Item; |
| 21 |
import net.sf.saxon.om.Sequence; |
| 22 |
import net.sf.saxon.om.StructuredQName; |
| 23 |
import net.sf.saxon.trans.SymbolicName; |
| 24 |
import net.sf.saxon.trans.XPathException; |
| 25 |
import net.sf.saxon.type.ItemType; |
| 26 |
import net.sf.saxon.type.PlainType; |
| 27 |
import net.sf.saxon.value.AtomicValue; |
| 28 |
import net.sf.saxon.value.EmptySequence; |
| 29 |
import net.sf.saxon.value.SequenceType; |
| 30 |
|
| 31 |
import java.util.HashMap; |
| 32 |
import java.util.List; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public abstract class BuiltInFunctionSet implements FunctionLibrary { |
| 38 |
|
| 39 |
public static Sequence<?> EMPTY = EmptySequence.getInstance(); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static final int CORE = 1; |
| 46 |
public static final int XSLT = 2; |
| 47 |
public static final int USE_WHEN = 4; |
| 48 |
public static final int XQUPDATE = 8; |
| 49 |
public static final int XPATH30 = 16; |
| 50 |
public static final int INTERNAL = 32; |
| 51 |
public static final int XSLT30 = 64; |
| 52 |
public static final int XPATH31 = 128; |
| 53 |
public static final int HOF = 256; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public static final int ONE = StaticProperty.ALLOWS_ONE; |
| 60 |
public static final int OPT = StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 61 |
public static final int STAR = StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 62 |
public static final int PLUS = StaticProperty.ALLOWS_ONE_OR_MORE; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static final int AS_ARG0 = 1; |
| 69 |
public static final int AS_PRIM_ARG0 = 2; |
| 70 |
public static final int CITEM = 4; |
| 71 |
public static final int BASE = 8; |
| 72 |
public static final int NS = 16; |
| 73 |
public static final int DCOLL = 32; |
| 74 |
public static final int DLANG = 64; |
| 75 |
public static final int FILTER = 256; |
| 76 |
public static final int LATE = 512; |
| 77 |
public static final int UO = 1024; |
| 78 |
public static final int POSN = 1024*2; |
| 79 |
public static final int LAST = 1024*4; |
| 80 |
public static final int SIDE = 1024*8; |
| 81 |
public static final int CDOC = 1024*16; |
| 82 |
public static final int CARD0 = 1024*32; |
| 83 |
public static final int NEW = 1024*64; |
| 84 |
|
| 85 |
public static final int DEPENDS_ON_STATIC_CONTEXT = BASE | NS | DCOLL; |
| 86 |
public static final int FOCUS = CITEM | POSN | LAST | CDOC; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public static final int INS = 1 << 24; |
| 95 |
public static final int ABS = 1 << 25; |
| 96 |
public static final int TRA = 1 << 26; |
| 97 |
public static final int NAV = 1 << 27; |
| 98 |
|
| 99 |
|
| 100 |
private HashMap<String, Entry> functionTable = new HashMap<>(200); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public final void importFunctionSet(BuiltInFunctionSet importee) { |
| 109 |
if (!importee.getNamespace().equals(getNamespace())) { |
| 110 |
throw new IllegalArgumentException(importee.getNamespace()); |
| 111 |
} |
| 112 |
functionTable.putAll(importee.functionTable); |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public Entry getFunctionDetails(String name, int arity) { |
| 125 |
if (arity == -1) { |
| 126 |
for (int i=0; i<20; i++) { |
| 127 |
Entry found = getFunctionDetails(name, i); |
| 128 |
if (found != null) { |
| 129 |
return found; |
| 130 |
} |
| 131 |
} |
| 132 |
return null; |
| 133 |
} |
| 134 |
String key = name + "#" + arity; |
| 135 |
Entry entry = functionTable.get(key); |
| 136 |
if (entry != null) { |
| 137 |
return entry; |
| 138 |
} |
| 139 |
|
| 140 |
if (name.equals("concat") && arity >= 2 && getNamespace().equals(NamespaceConstant.FN)) { |
| 141 |
key = "concat#-1"; |
| 142 |
entry = functionTable.get(key); |
| 143 |
if (entry != null) { |
| 144 |
return entry; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
return null; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
@Override |
| 181 |
public Expression bind(SymbolicName.F symbolicName, Expression[] staticArgs, StaticContext env, List<String> reasons) { |
| 182 |
StructuredQName functionName = symbolicName.getComponentName(); |
| 183 |
int arity = symbolicName.getArity(); |
| 184 |
String localName = functionName.getLocalPart(); |
| 185 |
if (functionName.hasURI(getNamespace()) && getFunctionDetails(localName, arity) != null) { |
| 186 |
RetainedStaticContext rsc = new RetainedStaticContext(env); |
| 187 |
try { |
| 188 |
SystemFunction fn = makeFunction(localName, arity); |
| 189 |
fn.setRetainedStaticContext(rsc); |
| 190 |
Expression f = fn.makeFunctionCall(staticArgs); |
| 191 |
f.setRetainedStaticContext(rsc); |
| 192 |
return f; |
| 193 |
} catch (XPathException e) { |
| 194 |
reasons.add(e.getMessage()); |
| 195 |
return null; |
| 196 |
} |
| 197 |
} else { |
| 198 |
return null; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
public SystemFunction makeFunction(String name, int arity) throws XPathException { |
| 203 |
Entry entry = getFunctionDetails(name, arity); |
| 204 |
if (entry == null) { |
| 205 |
String diagName = getNamespace().equals(NamespaceConstant.FN) ? |
| 206 |
"System function " + name : |
| 207 |
"Function Q{" + getNamespace() + "}" + name; |
| 208 |
if (getFunctionDetails(name, -1) == null) { |
| 209 |
XPathException err = new XPathException(diagName + "() does not exist or is not available in this environment"); |
| 210 |
err.setErrorCode("XPST0017"); |
| 211 |
err.setIsStaticError(true); |
| 212 |
throw err; |
| 213 |
} else { |
| 214 |
XPathException err = new XPathException(diagName + "() cannot be called with " |
| 215 |
+ pluralArguments(arity)); |
| 216 |
err.setErrorCode("XPST0017"); |
| 217 |
err.setIsStaticError(true); |
| 218 |
throw err; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
Class functionClass = entry.implementationClass; |
| 223 |
SystemFunction f; |
| 224 |
try { |
| 225 |
f = (SystemFunction) functionClass.newInstance(); |
| 226 |
} catch (Exception err) { |
| 227 |
err.printStackTrace(); |
| 228 |
throw new AssertionError("Failed to instantiate system function " + name + " - " + err.getMessage()); |
| 229 |
} |
| 230 |
f.setDetails(entry); |
| 231 |
f.setArity(arity); |
| 232 |
return f; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
private static String pluralArguments(int num) { |
| 243 |
if (num == 0) { |
| 244 |
return "zero arguments"; |
| 245 |
} |
| 246 |
if (num == 1) { |
| 247 |
return "one argument"; |
| 248 |
} |
| 249 |
return num + " arguments"; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
@Override |
| 261 |
public boolean isAvailable(SymbolicName.F symbolicName) { |
| 262 |
StructuredQName qn = symbolicName.getComponentName(); |
| 263 |
return qn.hasURI(getNamespace()) && getFunctionDetails(qn.getLocalPart(), symbolicName.getArity()) != null; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
@Override |
| 274 |
public FunctionLibrary copy() { |
| 275 |
return this; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
@Override |
| 293 |
public Function getFunctionItem(SymbolicName.F symbolicName, StaticContext staticContext) throws XPathException { |
| 294 |
StructuredQName functionName = symbolicName.getComponentName(); |
| 295 |
int arity = symbolicName.getArity(); |
| 296 |
if (functionName.hasURI(getNamespace()) && getFunctionDetails(functionName.getLocalPart(), arity) != null) { |
| 297 |
RetainedStaticContext rsc = staticContext.makeRetainedStaticContext(); |
| 298 |
SystemFunction fn = makeFunction(functionName.getLocalPart(), arity); |
| 299 |
fn.setRetainedStaticContext(rsc); |
| 300 |
return fn; |
| 301 |
} else { |
| 302 |
return null; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
protected Entry register(String name, |
| 322 |
int arity, |
| 323 |
Class<? extends SystemFunction> implementationClass, |
| 324 |
ItemType itemType, |
| 325 |
int cardinality, |
| 326 |
int applicability, |
| 327 |
int properties) { |
| 328 |
Entry e = new Entry(); |
| 329 |
e.name = new StructuredQName(getConventionalPrefix(), getNamespace(), name); |
| 330 |
e.arity = arity; |
| 331 |
e.implementationClass = implementationClass; |
| 332 |
e.itemType = itemType; |
| 333 |
e.cardinality = cardinality; |
| 334 |
e.applicability = applicability; |
| 335 |
e.properties = properties; |
| 336 |
if (e.arity == -1) { |
| 337 |
|
| 338 |
e.argumentTypes = new SequenceType[1]; |
| 339 |
e.resultIfEmpty = new AtomicValue[1]; |
| 340 |
e.usage = new OperandUsage[1]; |
| 341 |
} else { |
| 342 |
e.argumentTypes = new SequenceType[arity]; |
| 343 |
e.resultIfEmpty = (Sequence<?>[])new Sequence[arity]; |
| 344 |
e.usage = new OperandUsage[arity]; |
| 345 |
} |
| 346 |
functionTable.put(name + "#" + arity, e); |
| 347 |
return e; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
protected void registerReducedArityVariants(String key, int min, int max) { |
| 356 |
Entry master = functionTable.get(key); |
| 357 |
for (int arity = min; arity <= max; arity++) { |
| 358 |
Entry e = new Entry(); |
| 359 |
e.name = master.name; |
| 360 |
e.arity = arity; |
| 361 |
e.implementationClass = master.implementationClass; |
| 362 |
e.itemType = master.itemType; |
| 363 |
e.cardinality = master.cardinality; |
| 364 |
e.applicability = master.applicability; |
| 365 |
e.properties = master.properties; |
| 366 |
e.argumentTypes = new SequenceType[arity]; |
| 367 |
e.resultIfEmpty = (Sequence<?>[])new Sequence[arity]; |
| 368 |
e.usage = new OperandUsage[arity]; |
| 369 |
for (int i=0; i<arity; i++) { |
| 370 |
e.argumentTypes[i] = master.argumentTypes[i]; |
| 371 |
e.resultIfEmpty[i] = master.resultIfEmpty[i]; |
| 372 |
e.usage[i] = master.usage[i]; |
| 373 |
} |
| 374 |
functionTable.put(e.name.getLocalPart() + "#" + arity, e); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public String getNamespace() { |
| 386 |
return NamespaceConstant.FN; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
public String getConventionalPrefix() { |
| 395 |
return "fn"; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
public static class Entry implements java.io.Serializable { |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public StructuredQName name; |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
public Class implementationClass; |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
public int arity; |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
public ItemType itemType; |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public int cardinality; |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public int applicability; |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public OperandUsage[] usage; |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
public SequenceType[] argumentTypes; |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
public Sequence<?>[] resultIfEmpty; |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
public int properties; |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
public OptionsParameter optionDetails; |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
public Class streamingAdjunct; |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
public Entry arg(int a, ItemType type, int options, Sequence<?> resultIfEmpty) { |
| 467 |
int cardinality = options & StaticProperty.CARDINALITY_MASK; |
| 468 |
OperandUsage usage = OperandUsage.NAVIGATION; |
| 469 |
if ((options & ABS) != 0) { |
| 470 |
usage = OperandUsage.ABSORPTION; |
| 471 |
} else if ((options & TRA) != 0) { |
| 472 |
usage = OperandUsage.TRANSMISSION; |
| 473 |
} else if ((options & INS) != 0) { |
| 474 |
usage = OperandUsage.INSPECTION; |
| 475 |
} else if (type instanceof PlainType) { |
| 476 |
usage = OperandUsage.ABSORPTION; |
| 477 |
} |
| 478 |
try { |
| 479 |
this.argumentTypes[a] = SequenceType.makeSequenceType(type, cardinality); |
| 480 |
this.resultIfEmpty[a] = resultIfEmpty; |
| 481 |
this.usage[a] = usage; |
| 482 |
} catch (ArrayIndexOutOfBoundsException err) { |
| 483 |
System.err.println("Internal Saxon error: Can't set argument " + a + " of " + name); |
| 484 |
} |
| 485 |
return this; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public Entry optionDetails(OptionsParameter details) { |
| 493 |
this.optionDetails = details; |
| 494 |
return this; |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
public void setStreamingAdjunctClass(Class streamer) { |
| 502 |
this.streamingAdjunct = streamer; |
| 503 |
} |
| 504 |
|
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
} |
| 509 |
|