KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > script > jacl > JaclInterpreter


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.script.jacl;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import org.apache.batik.script.InterpreterException;
26
27 import tcl.lang.Interp;
28 import tcl.lang.ReflectObject;
29 import tcl.lang.TclException;
30
31 /**
32  * A simple implementation of <code>Interpreter</code> interface to use
33  * JACL Tcl parser.
34  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
35  * @version $Id: JaclInterpreter.java,v 1.9 2004/08/18 07:14:56 vhardy Exp $
36  */

37 public class JaclInterpreter implements org.apache.batik.script.Interpreter {
38     private Interp interpreter = null;
39
40     public JaclInterpreter() {
41         interpreter = new Interp();
42         try {
43             interpreter.eval("package require java", 0);
44         } catch (TclException e) {
45         }
46     }
47
48     // org.apache.batik.script.Intepreter implementation
49

50     public Object JavaDoc evaluate(Reader JavaDoc scriptreader)
51         throws InterpreterException, IOException JavaDoc {
52         return evaluate(scriptreader, "");
53     }
54
55     public Object JavaDoc evaluate(Reader JavaDoc scriptreader, String JavaDoc description) throws IOException JavaDoc, InterpreterException {
56         // oups jacl doesn't accept reader in its eval method :-(
57
StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
58         char[] buffer = new char[1024];
59         int val = 0;
60         while ((val = scriptreader.read(buffer)) != -1) {
61             sbuffer.append(buffer, 0, val);
62         }
63         String JavaDoc str = sbuffer.toString();
64         return evaluate(str);
65     }
66
67     public Object JavaDoc evaluate(String JavaDoc script)
68         throws InterpreterException {
69         try {
70             interpreter.eval(script, 0);
71         } catch (TclException e) {
72             throw new InterpreterException(e, e.getMessage(), -1, -1);
73         } catch (RuntimeException JavaDoc re) {
74             throw new InterpreterException(re, re.getMessage(), -1, -1);
75         }
76         return interpreter.getResult();
77     }
78
79     public void dispose() {
80         interpreter.dispose();
81     }
82
83     public void bindObject(String JavaDoc name, Object JavaDoc object) {
84         try {
85             interpreter.
86                 setVar(name,
87                        ReflectObject.
88                        newInstance(interpreter, object.getClass(), object),
89                        0);
90         } catch (TclException e) {
91             // should not happened we just register an object
92
}
93     }
94
95     public void setOut(Writer JavaDoc out) {
96         // no implementation of a default output function in Jacl
97
}
98
99     // org.apache.batik.i18n.Localizable implementation
100

101     public Locale JavaDoc getLocale() {
102         return Locale.getDefault();
103     }
104
105     public void setLocale(Locale JavaDoc locale) {
106     }
107
108     public String JavaDoc formatMessage(String JavaDoc key, Object JavaDoc[] args) {
109         return null;
110     }
111 }
112
Popular Tags