| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.query; |
| 9 |
|
| 10 |
import com.saxonica.functions.hof.UnresolvedXQueryFunctionItem; |
| 11 |
import com.saxonica.functions.hof.UserFunctionReference; |
| 12 |
import net.sf.saxon.Configuration; |
| 13 |
import net.sf.saxon.expr.*; |
| 14 |
import net.sf.saxon.expr.instruct.UserFunction; |
| 15 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 16 |
import net.sf.saxon.functions.FunctionLibrary; |
| 17 |
import net.sf.saxon.om.*; |
| 18 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 19 |
import net.sf.saxon.trans.SymbolicName; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.type.FunctionItemType; |
| 22 |
import net.sf.saxon.type.SpecificFunctionType; |
| 23 |
|
| 24 |
import java.util.HashMap; |
| 25 |
import java.util.Iterator; |
| 26 |
import java.util.List; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class XQueryFunctionLibrary implements FunctionLibrary, XQueryFunctionBinder { |
| 36 |
|
| 37 |
private Configuration config; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private HashMap<SymbolicName, XQueryFunction> functions = |
| 43 |
new HashMap<>(20); |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public XQueryFunctionLibrary(Configuration config) { |
| 52 |
this.config = config; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public void setConfiguration(Configuration config) { |
| 62 |
this.config = config; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public Configuration getConfiguration() { |
| 72 |
return config; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public void declareFunction( XQueryFunction function) throws XPathException { |
| 83 |
SymbolicName keyObj = function.getIdentificationKey(); |
| 84 |
XQueryFunction existing = functions.get(keyObj); |
| 85 |
if (existing == function) { |
| 86 |
return; |
| 87 |
} |
| 88 |
if (existing != null) { |
| 89 |
XPathException err = new XPathException("Duplicate definition of function " + |
| 90 |
function.getDisplayName() + |
| 91 |
" (see line " + existing.getLineNumber() + " in " + existing.getSystemId() + ')'); |
| 92 |
err.setErrorCode("XQST0034"); |
| 93 |
err.setIsStaticError(true); |
| 94 |
err.setLocator(function); |
| 95 |
throw err; |
| 96 |
} |
| 97 |
functions.put(keyObj, function); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public Function getFunctionItem(SymbolicName.F functionName, StaticContext staticContext) |
| 114 |
throws XPathException { |
| 115 |
XQueryFunction fd = functions.get(functionName); |
| 116 |
if (fd != null) { |
| 117 |
if (fd.isPrivate() && !fd.getSystemId().equals(staticContext.getStaticBaseURI())) { |
| 118 |
throw new XPathException("Cannot call the private function " + |
| 119 |
functionName.getComponentName().getDisplayName() + " from outside its module", "XPST0017"); |
| 120 |
} |
| 121 |
final UserFunction fn = fd.getUserFunction(); |
| 122 |
FunctionItemType type = new SpecificFunctionType( |
| 123 |
fd.getArgumentTypes(), fd.getResultType(), fd.getAnnotations()); |
| 124 |
if (fn == null) { |
| 125 |
|
| 126 |
UserFunction uf = new UserFunction(); |
| 127 |
uf.setFunctionName(functionName.getComponentName()); |
| 128 |
uf.setResultType(fd.getResultType()); |
| 129 |
uf.setParameterDefinitions(fd.getParameterDefinitions()); |
| 130 |
final UserFunctionReference ref = new UserFunctionReference(uf); |
| 131 |
fd.registerReference(ref); |
| 132 |
return new UnresolvedXQueryFunctionItem(fd, functionName, ref); |
| 133 |
|
| 134 |
} else { |
| 135 |
return fn; |
| 136 |
} |
| 137 |
} else { |
| 138 |
return null; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public boolean isAvailable(SymbolicName.F functionName) { |
| 152 |
return functions.get(functionName) != null; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public static class UnresolvedCallable implements UserFunctionResolvable, Callable { |
| 162 |
SymbolicName.F symbolicName; |
| 163 |
UserFunction function; |
| 164 |
|
| 165 |
public UnresolvedCallable(SymbolicName.F symbolicName) { |
| 166 |
this.symbolicName = symbolicName; |
| 167 |
} |
| 168 |
|
| 169 |
public StructuredQName getFunctionName() { |
| 170 |
return symbolicName.getComponentName(); |
| 171 |
} |
| 172 |
|
| 173 |
public int getArity() { |
| 174 |
return symbolicName.getArity(); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public Sequence<?> call(XPathContext context, Sequence<?>[] arguments) throws XPathException { |
| 191 |
if (function == null) { |
| 192 |
throw new XPathException("Forwards reference to XQuery function has not been resolved"); |
| 193 |
} |
| 194 |
Sequence[] args = new Sequence[arguments.length]; |
| 195 |
for (int i = 0; i < arguments.length; i++) { |
| 196 |
args[i] = ((SequenceIterator<?>) arguments[i].iterate()).materialize(); |
| 197 |
} |
| 198 |
return function.call(context.newCleanContext(), args); |
| 199 |
} |
| 200 |
|
| 201 |
public void setFunction(UserFunction function) { |
| 202 |
this.function = function; |
| 203 |
} |
| 204 |
|
| 205 |
public UserFunction getFunction() { |
| 206 |
return function; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public Expression bind( SymbolicName.F functionName, Expression[] arguments, StaticContext env, List<String> reasons) { |
| 229 |
XQueryFunction fd = functions.get(functionName); |
| 230 |
if (fd != null) { |
| 231 |
if (fd.isPrivate() && fd.getStaticContext() != env) { |
| 232 |
reasons.add("Cannot call the private XQuery function " + |
| 233 |
functionName.getComponentName().getDisplayName() + " from outside its module"); |
| 234 |
return null; |
| 235 |
} |
| 236 |
UserFunctionCall ufc = new UserFunctionCall(); |
| 237 |
ufc.setFunctionName(fd.getFunctionName()); |
| 238 |
ufc.setArguments(arguments); |
| 239 |
ufc.setStaticType(fd.getResultType()); |
| 240 |
UserFunction fn = fd.getUserFunction(); |
| 241 |
if (fn == null) { |
| 242 |
|
| 243 |
fd.registerReference(ufc); |
| 244 |
} else { |
| 245 |
ufc.setFunction(fn); |
| 246 |
} |
| 247 |
return ufc; |
| 248 |
} else { |
| 249 |
return null; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public XQueryFunction getDeclaration(StructuredQName functionName, int staticArgs) { |
| 260 |
SymbolicName functionKey = XQueryFunction.getIdentificationKey( |
| 261 |
functionName, staticArgs); |
| 262 |
return functions.get(functionKey); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public XQueryFunction getDeclarationByKey(SymbolicName functionKey) { |
| 274 |
return functions.get(functionKey); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public Iterator<XQueryFunction> getFunctionDefinitions() { |
| 286 |
return functions.values().iterator(); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
protected void fixupGlobalFunctions( QueryModule env) throws XPathException { |
| 302 |
ExpressionVisitor visitor = ExpressionVisitor.make(env); |
| 303 |
for (XQueryFunction fn : functions.values()) { |
| 304 |
fn.compile(); |
| 305 |
} |
| 306 |
for (XQueryFunction fn : functions.values()) { |
| 307 |
fn.checkReferences(visitor); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
protected void optimizeGlobalFunctions(QueryModule topModule) throws XPathException { |
| 322 |
for (XQueryFunction fn : functions.values()) { |
| 323 |
if (((QueryModule)fn.getStaticContext()).getTopLevelModule() == topModule) { |
| 324 |
fn.optimize(); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public void explainGlobalFunctions( ExpressionPresenter out) throws XPathException { |
| 337 |
for (XQueryFunction fn : functions.values()) { |
| 338 |
fn.explain(out); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public UserFunction getUserDefinedFunction( String uri, String localName, int arity) { |
| 356 |
SymbolicName functionKey = new SymbolicName.F(new StructuredQName("", uri, localName), arity); |
| 357 |
XQueryFunction fd = functions.get(functionKey); |
| 358 |
if (fd == null) { |
| 359 |
return null; |
| 360 |
} |
| 361 |
return fd.getUserFunction(); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public FunctionLibrary copy() { |
| 374 |
XQueryFunctionLibrary qfl = new XQueryFunctionLibrary(config); |
| 375 |
qfl.functions = new HashMap<>(functions); |
| 376 |
return qfl; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
} |