1 package net.sf.saxon.functions; 2 3 import net.sf.saxon.Configuration; 4 import net.sf.saxon.expr.Expression; 5 import net.sf.saxon.expr.UserFunctionCall; 6 import net.sf.saxon.instruct.UserFunction; 7 import net.sf.saxon.trans.IndependentContext; 8 import net.sf.saxon.trans.XPathException; 9 10 import java.util.HashMap ; 11 12 24 25 public class ExecutableFunctionLibrary implements FunctionLibrary { 26 27 private Configuration config; 28 private HashMap functions = new HashMap (20); 29 32 public ExecutableFunctionLibrary(Configuration config) { 33 this.config = config; 34 } 35 36 39 40 public void addFunction(UserFunction fn) { 41 long key = ((long)fn.getNumberOfArguments())<<32 | (fn.getFunctionNameCode() & 0xfffff); 42 functions.put(new Long (key), fn); 43 } 44 45 54 55 public boolean isAvailable(int fingerprint, String uri, String local, int arity) { 56 if (arity == -1) { 57 for (int i=0; i<=20; i++) { 58 if (isAvailable(fingerprint, uri, local, i)) { 59 return true; 60 } 61 } 62 return false; 63 } 64 long key = ((long)arity)<<32 | fingerprint; 65 return functions.get(new Long (key)) != null; 66 } 67 68 86 87 public Expression bind(int nameCode, String uri, String local, Expression[] staticArgs) 88 throws XPathException { 89 long key = ((long)staticArgs.length)<<32 | (nameCode & 0xfffff); 90 UserFunction fn = (UserFunction)functions.get(new Long (key)); 91 if (fn == null) { 92 return null; 93 } 94 IndependentContext env = new IndependentContext(config); UserFunctionCall fc = new UserFunctionCall(); 96 fc.setFunctionNameCode(nameCode); 97 fc.setArguments(staticArgs); 98 fc.setFunction(fn, env); 99 fc.checkFunctionCall(fn, env); 100 fc.setStaticType(fn.getResultType()); 101 return fc; 102 } 103 104 111 112 public FunctionLibrary copy() { 113 ExecutableFunctionLibrary efl = new ExecutableFunctionLibrary(config); 114 efl.functions = new HashMap (functions); 115 return efl; 116 } 117 118 } 119 120 | Popular Tags |