KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > metadata > JavaInvoker


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.metadata;
24
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
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 JavaDoc javaClassMap = new HashMap JavaDoc();
37     
38     public Object JavaDoc invoke(String JavaDoc funcName, Object JavaDoc[] params) {
39         return null;
40     }
41     
42     public Object JavaDoc invoke(LibraryFunctionCall lfc, Object JavaDoc[] params) throws XQueryException {
43         try {
44             Object JavaDoc javaObj = (Object JavaDoc) javaClassMap.get(lfc.getFuncName().getNameSpace().substring(5));
45             Method JavaDoc method = lfc.getFunctionDefinition().getMethod();
46             Class JavaDoc[] paramTypes = method.getParameterTypes();
47             // translate dataModel objects to java objects
48
// cast to match parmeter types of method
49
Object JavaDoc[] javaObjs = new Object JavaDoc[params.length];
50             for (int i=0;i<params.length;i++) {
51                 Object JavaDoc obj = getJavaObjectFromDataModelObject(params[i],paramTypes[i]);
52                 javaObjs[i] = obj;
53             }
54             Object JavaDoc obj = method.invoke(javaObj, javaObjs);
55             return obj;
56         } catch (IllegalAccessException JavaDoc e) {
57             throw new XQueryException(e.getMessage());
58         } catch (InvocationTargetException JavaDoc 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 JavaDoc name, Object JavaDoc obj) {
68         javaClassMap.put(name,obj);
69     }
70
71 /*
72     Simple Type -> Java Type
73     xsd:string -> java.lang.String
74     xsd:integer -> java.math.BigInteger
75     xsd:int -> int
76     xsd:long -> long
77     xsd:short -> short
78     xsd:decimal -> java.math.BigDecimal
79     xsd:float -> float
80     xsd:double -> double
81     xsd:boolean -> boolean
82     xsd:byte -> byte
83     xsd:unsignedInt -> long
84     xsd:unsignedShort -> int
85     xsd:unsignedByte -> short
86     xsd:QName -> javax.xml.namespace.QName
87     xsd:dateTime -> java.util.Calendar
88     xsd:date -> java.util.Calendar
89     xsd:time -> java.util.Calendar
90     xsd:anyURI -> java.net.URI (J2SE 1.4 only) java.lang.String
91     xsd:base64Binary -> byte[]
92     xsd:hexBinary -> byte[]
93     xsd:anySimpleType -> java.lang.String
94 */

95     private Object JavaDoc getJavaObjectFromDataModelObject(Object JavaDoc obj, Class JavaDoc cl) {
96         if (obj instanceof ArrayList JavaDoc)
97             return null;
98         if (cl == String JavaDoc.class) {
99             return obj.toString();
100         } else if (cl == int.class) {
101             return new Integer JavaDoc(((Number JavaDoc)obj).intValue());
102         }
103         return null;
104     }
105
106
107 }
108
Popular Tags