KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constructor JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.TimeZone JavaDoc;
29
30 import org.xquark.xquery.JavaModule;
31 import org.xquark.xquery.parser.*;
32
33 public class DynamicContext {
34
35     private Date JavaDoc currentDateTime = null;
36     private TimeZone JavaDoc currentTimeZone = null;
37
38     private JavaInvoker javaInvoker = new JavaInvoker();
39     
40     public DynamicContext() {}
41     
42     public Object JavaDoc invoke(LibraryFunctionCall lfc, Object JavaDoc[] params) throws XQueryException {
43         QName funcName = lfc.getFuncName();
44         int index = funcName.getNameSpace().indexOf(':');
45         String JavaDoc type = funcName.getNameSpace().substring(0,index);
46         if (type.equals("java"))
47             return javaInvoker.invoke(lfc,params);
48         return null;
49     }
50
51     public void addImportModule(XQueryModule module) throws XQueryException {
52         if (module instanceof JavaModule) {
53             try {
54                 JavaModule mod = (JavaModule) module;
55                 Class JavaDoc javaClass = mod.getJavaClass();
56                 Class JavaDoc[] classes = null;
57                 Constructor JavaDoc constructor = javaClass.getConstructor(classes);
58                 Object JavaDoc obj = constructor.newInstance(null);
59                 javaInvoker.addClass(javaClass.getName(), obj);
60             } catch (InvocationTargetException JavaDoc e) {
61                 throw new XQueryException("Could not import module " + module.getNamespace());
62             } catch (IllegalAccessException JavaDoc e) {
63                 throw new XQueryException("Could not import module " + module.getNamespace());
64             } catch (InstantiationException JavaDoc e) {
65                 throw new XQueryException("Could not import module " + module.getNamespace());
66             } catch (NoSuchMethodException JavaDoc e) {
67                 throw new XQueryException("Could not import module " + module.getNamespace());
68             }
69         }
70     }
71 }
72
Popular Tags