| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.query; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.Expression; |
| 12 |
import net.sf.saxon.expr.StaticContext; |
| 13 |
import net.sf.saxon.expr.UserFunctionCall; |
| 14 |
import net.sf.saxon.expr.UserFunctionResolvable; |
| 15 |
import net.sf.saxon.expr.parser.XPathParser; |
| 16 |
import net.sf.saxon.functions.CallableFunction; |
| 17 |
import net.sf.saxon.functions.FunctionLibrary; |
| 18 |
import net.sf.saxon.lib.Feature; |
| 19 |
import net.sf.saxon.om.Function; |
| 20 |
import net.sf.saxon.om.StructuredQName; |
| 21 |
import net.sf.saxon.trans.SymbolicName; |
| 22 |
import net.sf.saxon.trans.XPathException; |
| 23 |
import net.sf.saxon.type.AnyFunctionType; |
| 24 |
|
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.List; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class UnboundFunctionLibrary implements FunctionLibrary { |
| 35 |
|
| 36 |
private List<UserFunctionResolvable> unboundFunctionReferences = new ArrayList<>(20); |
| 37 |
private List<StaticContext> correspondingStaticContext = new ArrayList<>(20); |
| 38 |
private List<List<String>> correspondingReasons = new ArrayList<>(); |
| 39 |
private boolean resolving = false; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public UnboundFunctionLibrary() { |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public Expression bind(SymbolicName.F functionName, Expression[] arguments, StaticContext env, List<String> reasons) { |
| 67 |
if (resolving) { |
| 68 |
return null; |
| 69 |
} |
| 70 |
if (!reasons.isEmpty() && reasons.get(0).startsWith("Cannot call the private XQuery function")) { |
| 71 |
|
| 72 |
return null; |
| 73 |
} |
| 74 |
UserFunctionCall ufc = new UserFunctionCall(); |
| 75 |
ufc.setFunctionName(functionName.getComponentName()); |
| 76 |
ufc.setArguments(arguments); |
| 77 |
unboundFunctionReferences.add(ufc); |
| 78 |
correspondingStaticContext.add(env); |
| 79 |
correspondingReasons.add(reasons); |
| 80 |
return ufc; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public Function getFunctionItem(SymbolicName.F functionName, StaticContext staticContext) { |
| 97 |
if (resolving) { |
| 98 |
return null; |
| 99 |
} |
| 100 |
XQueryFunctionLibrary.UnresolvedCallable uc = new XQueryFunctionLibrary.UnresolvedCallable(functionName); |
| 101 |
unboundFunctionReferences.add(uc); |
| 102 |
correspondingStaticContext.add(null); |
| 103 |
correspondingReasons.add(new ArrayList<>()); |
| 104 |
CallableFunction fi = new CallableFunction(functionName, uc, AnyFunctionType.getInstance()); |
| 105 |
|
| 106 |
return fi; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public boolean isAvailable(SymbolicName.F functionName) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public void bindUnboundFunctionReferences( XQueryFunctionBinder lib, Configuration config) throws XPathException { |
| 135 |
resolving = true; |
| 136 |
for (int i = 0; i < unboundFunctionReferences.size(); i++) { |
| 137 |
UserFunctionResolvable ref = unboundFunctionReferences.get(i); |
| 138 |
if (ref instanceof UserFunctionCall) { |
| 139 |
UserFunctionCall ufc = (UserFunctionCall) ref; |
| 140 |
QueryModule importingModule = (QueryModule) correspondingStaticContext.get(i); |
| 141 |
if (importingModule == null) { |
| 142 |
|
| 143 |
continue; |
| 144 |
} |
| 145 |
correspondingStaticContext.set(i, null); |
| 146 |
|
| 147 |
|
| 148 |
final StructuredQName q = ufc.getFunctionName(); |
| 149 |
final int arity = ufc.getArity(); |
| 150 |
|
| 151 |
XQueryFunction fd = lib.getDeclaration(q, arity); |
| 152 |
if (fd != null) { |
| 153 |
fd.registerReference(ufc); |
| 154 |
ufc.setStaticType(fd.getResultType()); |
| 155 |
} else { |
| 156 |
StringBuilder sb = new StringBuilder("Cannot find a " + arity + |
| 157 |
"-argument function named " + q.getEQName() + "()"); |
| 158 |
List<String> reasons = correspondingReasons.get(i); |
| 159 |
for (String reason : reasons) { |
| 160 |
sb.append(". ").append(reason); |
| 161 |
} |
| 162 |
if (reasons.isEmpty()) { |
| 163 |
String supplementary = XPathParser.getMissingFunctionExplanation(q, config); |
| 164 |
if (supplementary != null) { |
| 165 |
sb.append(". ").append(supplementary); |
| 166 |
} |
| 167 |
} |
| 168 |
XPathException err = new XPathException(sb.toString(), "XPST0017", ufc.getLocation()); |
| 169 |
err.setIsStaticError(true); |
| 170 |
throw err; |
| 171 |
} |
| 172 |
} else if (ref instanceof XQueryFunctionLibrary.UnresolvedCallable) { |
| 173 |
XQueryFunctionLibrary.UnresolvedCallable uc = (XQueryFunctionLibrary.UnresolvedCallable) ref; |
| 174 |
final StructuredQName q = uc.getFunctionName(); |
| 175 |
final int arity = uc.getArity(); |
| 176 |
|
| 177 |
XQueryFunction fd = lib.getDeclaration(q, arity); |
| 178 |
if (fd != null) { |
| 179 |
fd.registerReference(uc); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
} else { |
| 185 |
String msg = "Cannot find a " + arity + |
| 186 |
"-argument function named " + q.getEQName() + "()"; |
| 187 |
if (!config.getBooleanProperty(Feature.ALLOW_EXTERNAL_FUNCTIONS)) { |
| 188 |
msg += ". Note: external function calls have been disabled"; |
| 189 |
} |
| 190 |
XPathException err = new XPathException(msg); |
| 191 |
err.setErrorCode("XPST0017"); |
| 192 |
err.setIsStaticError(true); |
| 193 |
throw err; |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public FunctionLibrary copy() { |
| 209 |
UnboundFunctionLibrary qfl = new UnboundFunctionLibrary(); |
| 210 |
qfl.unboundFunctionReferences = new ArrayList<>(unboundFunctionReferences); |
| 211 |
qfl.correspondingStaticContext = new ArrayList<>(correspondingStaticContext); |
| 212 |
qfl.resolving = resolving; |
| 213 |
return qfl; |
| 214 |
} |
| 215 |
|
| 216 |
} |
| 217 |
|