1 22 23 package org.xquark.xquery.metadata; 24 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 30 import org.xquark.xquery.parser.LibraryFunctionCall; 31 import org.xquark.xquery.parser.XQueryException; 32 33 public class JavaInvoker implements InvokerInterface { 34 35 private DynamicContext dc = null; 36 private HashMap javaClassMap = new HashMap (); 37 38 public Object invoke(String funcName, Object [] params) { 39 return null; 40 } 41 42 public Object invoke(LibraryFunctionCall lfc, Object [] params) throws XQueryException { 43 try { 44 Object javaObj = (Object ) javaClassMap.get(lfc.getFuncName().getNameSpace().substring(5)); 45 Method method = lfc.getFunctionDefinition().getMethod(); 46 Class [] paramTypes = method.getParameterTypes(); 47 Object [] javaObjs = new Object [params.length]; 50 for (int i=0;i<params.length;i++) { 51 Object obj = getJavaObjectFromDataModelObject(params[i],paramTypes[i]); 52 javaObjs[i] = obj; 53 } 54 Object obj = method.invoke(javaObj, javaObjs); 55 return obj; 56 } catch (IllegalAccessException e) { 57 throw new XQueryException(e.getMessage()); 58 } catch (InvocationTargetException e) { 59 throw new XQueryException(e.getMessage()); 60 } 61 } 62 63 public void setDC(DynamicContext dc) { 64 this.dc = dc; 65 } 66 67 public void addClass(String name, Object obj) { 68 javaClassMap.put(name,obj); 69 } 70 71 95 private Object getJavaObjectFromDataModelObject(Object obj, Class cl) { 96 if (obj instanceof ArrayList ) 97 return null; 98 if (cl == String .class) { 99 return obj.toString(); 100 } else if (cl == int.class) { 101 return new Integer (((Number )obj).intValue()); 102 } 103 return null; 104 } 105 106 107 } 108 | Popular Tags |