1 22 23 package org.xquark.xquery; 24 25 import java.lang.reflect.Method ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 29 import org.xquark.xquery.parser.*; 30 import org.xquark.xquery.parser.util.Constants; 31 import org.xquark.xquery.typing.TypeException; 32 import org.xquark.xquery.typing.TypeVisitor; 33 34 public class JavaModule extends XQueryModule { 35 36 private String className = null; 37 private Class javaClass = null; 38 private ArrayList javaClassMethods = new ArrayList (); 39 40 public JavaModule(String className, TypeVisitor typeVisitor) throws XQueryException { 41 this.className = className; 42 try { 45 javaClass = Class.forName(className); 46 Method methods[] = javaClass.getMethods(); 47 ArrayList funcDeclList = new ArrayList (); 48 HashMap funcDeclMap = new HashMap (); 49 for (int i = 0; i < methods.length; i++) { 50 Method method = (Method ) methods[i]; 51 if (method.getDeclaringClass() == Object .class) 52 continue; 53 javaClassMethods.add(method); 54 String name = method.getName(); 55 Class parameterTypes[] = method.getParameterTypes(); 56 ArrayList parameters = new ArrayList (parameterTypes.length); 57 int index = 1; 58 for (int j = 0; j < parameterTypes.length; j++) { 59 Class parameterType = (Class ) parameterTypes[j]; 60 Variable varj = new Variable(new QName("param"+i++, null), null); 61 XQueryExpression retExpr = getQNameFromJavaType(parameterType); 62 ItemType itemType = new ItemType(retExpr, this); 63 SequenceType seqType = new SequenceType(itemType, Constants.NOTHING, this); 64 varj.setTypeDeclaration(seqType); 65 varj.setBindingType(Constants.PARAMETER_BINDINGTYPE); 66 varj.accept(typeVisitor); 67 parameters.add(varj); 68 } 69 Class returnType = method.getReturnType(); 70 XQueryExpression retExpr = getQNameFromJavaType(returnType); 71 ItemType itemType = new ItemType(retExpr, this); 72 SequenceType seqType = new SequenceType(itemType, Constants.NOTHING, this); 73 74 FunctionDeclaration funcDecl = new FunctionDeclaration(new QName(name, null), parameters, seqType, null, this); 75 funcDecl.setMethod(method); 76 funcDeclList.add(funcDecl); 77 funcDeclMap.put(funcDecl.getFuncName().getLocalName(), funcDecl); 78 } 79 HashMap map = new HashMap (); 80 map.put("java:" + className, funcDeclMap); 81 this.setFunctions(map); 82 this.setDeclList(funcDeclList); 83 } catch (ClassNotFoundException e) { 84 throw new XQueryException(e.getMessage()); 85 } 86 } 87 88 public Class getJavaClass() { 89 return javaClass; 90 } 91 92 private final String XSPREFIX = "xs"; 93 private final String XSURI = "http://www.w3.org/2001/XMLSchema"; 94 95 private QName getQNameFromJavaType(Class javaType) throws TypeException, XQueryException { 96 if (javaType.getName() == "String") 97 return new QName(XSURI,XSPREFIX,"string",null,null); 98 else if (javaType.getName() == "int") 99 return new QName(XSURI,XSPREFIX,"integer",null,null); 100 return null; 101 } 102 103 } 104 | Popular Tags |