| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions.registry; |
| 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.functions.FunctionLibrary; |
| 14 |
import net.sf.saxon.om.Function; |
| 15 |
import net.sf.saxon.trans.SymbolicName; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
|
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class OnDemandFunctionSet implements FunctionLibrary { |
| 34 |
|
| 35 |
private Configuration config; |
| 36 |
private String namespace; |
| 37 |
private String libraryClass; |
| 38 |
private FunctionLibrary library; |
| 39 |
|
| 40 |
public OnDemandFunctionSet(Configuration config, String namespace, String libraryClass) { |
| 41 |
this.config = config; |
| 42 |
this.namespace = namespace; |
| 43 |
this.libraryClass = libraryClass; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private boolean load(SymbolicName.F functionName, List<String> reasons) { |
| 56 |
if (functionName.getComponentName().hasURI(namespace)) { |
| 57 |
if (library == null) { |
| 58 |
try { |
| 59 |
Object lib = config.getDynamicLoader().getInstance(libraryClass, null); |
| 60 |
if (lib instanceof FunctionLibrary) { |
| 61 |
library = (FunctionLibrary)lib; |
| 62 |
} else { |
| 63 |
if (reasons != null) { |
| 64 |
reasons.add("Class " + libraryClass + " was loaded but it is not a FunctionLibrary"); |
| 65 |
} |
| 66 |
return false; |
| 67 |
} |
| 68 |
} catch (XPathException e) { |
| 69 |
if (reasons != null) { |
| 70 |
reasons.add("Failed to load class " + libraryClass + ": " + e.getMessage()); |
| 71 |
} |
| 72 |
return false; |
| 73 |
} |
| 74 |
} |
| 75 |
library.setConfiguration(config); |
| 76 |
return true; |
| 77 |
} else { |
| 78 |
return false; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public boolean isAvailable(SymbolicName.F functionName) { |
| 84 |
boolean match = load(functionName, null); |
| 85 |
return match && library.isAvailable(functionName); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public Expression bind(SymbolicName.F functionName, Expression[] staticArgs, StaticContext env, List<String> reasons) { |
| 90 |
boolean match = load(functionName, reasons); |
| 91 |
if (match) { |
| 92 |
return library.bind(functionName, staticArgs, env, reasons); |
| 93 |
} else { |
| 94 |
return null; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public FunctionLibrary copy() { |
| 100 |
return this; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
@Override |
| 105 |
public Function getFunctionItem(SymbolicName.F functionName, StaticContext staticContext) throws XPathException { |
| 106 |
boolean match = load(functionName, null); |
| 107 |
if (match) { |
| 108 |
return library.getFunctionItem(functionName, staticContext); |
| 109 |
} else { |
| 110 |
return null; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
} |
| 116 |
|