KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > JavaModule


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2004 XQuark Group.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.xquery;
24
25 import java.lang.reflect.Method JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
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 JavaDoc className = null;
37     private Class JavaDoc javaClass = null;
38     private ArrayList JavaDoc javaClassMethods = new ArrayList JavaDoc();
39     
40     public JavaModule(String JavaDoc className, TypeVisitor typeVisitor) throws XQueryException {
41         this.className = className;
42         // create all stuff necessary
43
// create function declaration
44
try {
45             javaClass = Class.forName(className);
46             Method JavaDoc methods[] = javaClass.getMethods();
47             ArrayList JavaDoc funcDeclList = new ArrayList JavaDoc();
48             HashMap JavaDoc funcDeclMap = new HashMap JavaDoc();
49             for (int i = 0; i < methods.length; i++) {
50                 Method JavaDoc method = (Method JavaDoc) methods[i];
51                 if (method.getDeclaringClass() == Object JavaDoc.class)
52                     continue;
53                 javaClassMethods.add(method);
54                 String JavaDoc name = method.getName();
55                 Class JavaDoc parameterTypes[] = method.getParameterTypes();
56                 ArrayList JavaDoc parameters = new ArrayList JavaDoc(parameterTypes.length);
57                 int index = 1;
58                 for (int j = 0; j < parameterTypes.length; j++) {
59                     Class JavaDoc parameterType = (Class JavaDoc) 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 JavaDoc 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 JavaDoc map = new HashMap JavaDoc();
80             map.put("java:" + className, funcDeclMap);
81             this.setFunctions(map);
82             this.setDeclList(funcDeclList);
83         } catch (ClassNotFoundException JavaDoc e) {
84             throw new XQueryException(e.getMessage());
85         }
86     }
87     
88     public Class JavaDoc getJavaClass() {
89         return javaClass;
90     }
91     
92     private final String JavaDoc XSPREFIX = "xs";
93     private final String JavaDoc XSURI = "http://www.w3.org/2001/XMLSchema";
94     
95     private QName getQNameFromJavaType(Class JavaDoc 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