This class provides access to built-in and user-defined functions. @author BaseX Team, BSD License @author Christian Gruen
| 32 | * @author Christian Gruen |
| 33 | */ |
| 34 | public final class Functions { |
| 35 | /** Definitions of built-in functions. */ |
| 36 | public static final QNmMap<FuncDefinition> BUILT_IN = new QNmMap<>(); |
| 37 | |
| 38 | /** URIs of built-in functions. */ |
| 39 | private static final TokenSet URIS = new TokenSet(); |
| 40 | /** Cast parameter. */ |
| 41 | private static final QNm[] CAST_PARAM = { new QNm("value") }; |
| 42 | |
| 43 | /** Private constructor. */ |
| 44 | private Functions() { } |
| 45 | |
| 46 | // initializes built-in XQuery functions |
| 47 | static { |
| 48 | // add built-in functions |
| 49 | final ArrayList<FuncDefinition> list = new ArrayList<>(); |
| 50 | Function.init(list); |
| 51 | // add built-in API functions if available |
| 52 | final Class<?> clz = Reflect.find("org.basex.query.func.ApiFunction"); |
| 53 | final Method mth = Reflect.method(clz, "init", ArrayList.class); |
| 54 | if(mth != null) Reflect.invoke(mth, null, list); |
| 55 | |
| 56 | for(final FuncDefinition fd : list) { |
| 57 | final QNm name = fd.name; |
| 58 | if(BUILT_IN.put(name, fd) != null) throw Util.notExpected("Function % defined twice.", name); |
| 59 | URIS.add(name.uri()); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Checks if the specified URI is statically available. |
| 65 | * @param uri URI to check |
| 66 | * @return result of check |
| 67 | */ |
| 68 | public static boolean staticURI(final byte[] uri) { |
| 69 | for(final byte[] u : URIS) { |
| 70 | if(eq(uri, u)) return true; |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Creates a function call or a function item expression. |
| 77 | * @param qnm function name |
| 78 | * @param fb function arguments |
| 79 | * @param qc query context |
| 80 | * @return function call |
| 81 | * @throws QueryException query exception |
| 82 | */ |
| 83 | public static Expr get(final QNm qnm, final FuncBuilder fb, final QueryContext qc) |
| 84 | throws QueryException { |
| 85 | |
| 86 | // partial function call? |
| 87 | if(fb.placeholders > 0) return dynamic(item(qnm, fb.arity, false, fb.info, qc), fb); |
| 88 | |
| 89 | final QNm name = funcName(qnm, fb.arity, fb.info, qc); |
| 90 | |
| 91 | // constructor function |