KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > script > jpython > JPythonInterpreter


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.jpython;
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 import org.python.util.PythonInterpreter;
27
28 /**
29  * A simple implementation of <code>Interpreter</code> interface to use
30  * JPython python parser.
31  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
32  * @version $Id: JPythonInterpreter.java,v 1.10 2005/03/27 08:58:35 cam Exp $
33  */

34 public class JPythonInterpreter implements org.apache.batik.script.Interpreter {
35     private PythonInterpreter interpreter = null;
36
37     public JPythonInterpreter() {
38         interpreter = new PythonInterpreter();
39     }
40
41     // org.apache.batik.script.Intepreter implementation
42

43     public Object JavaDoc evaluate(Reader JavaDoc scriptreader)
44         throws InterpreterException, IOException JavaDoc {
45         return evaluate(scriptreader, "");
46     }
47
48     public Object JavaDoc evaluate(Reader JavaDoc scriptreader, String JavaDoc description)
49         throws InterpreterException, IOException JavaDoc {
50
51         // oups jpython doesn't accept reader in its eval method :-(
52
StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
53         char[] buffer = new char[1024];
54         int val = 0;
55         while ((val = scriptreader.read(buffer)) != -1) {
56             sbuffer.append(buffer,0, val);
57         }
58         String JavaDoc str = sbuffer.toString();
59         return evaluate(str);
60     }
61
62     public Object JavaDoc evaluate(String JavaDoc script)
63         throws InterpreterException {
64         try {
65             interpreter.exec(script);
66         } catch (org.python.core.PyException e) {
67             throw new InterpreterException(e, e.getMessage(), -1, -1);
68         } catch (RuntimeException JavaDoc re) {
69             throw new InterpreterException(re, re.getMessage(), -1, -1);
70         }
71         return null;
72     }
73
74     public void dispose() {
75     }
76
77     public void bindObject(String JavaDoc name, Object JavaDoc object) {
78         interpreter.set(name, object);
79     }
80
81     public void setOut(Writer JavaDoc out) {
82         interpreter.setOut(out);
83     }
84
85     // org.apache.batik.i18n.Localizable implementation
86

87     public Locale JavaDoc getLocale() {
88         return null;
89     }
90
91     public void setLocale(Locale JavaDoc locale) {
92     }
93
94     public String JavaDoc formatMessage(String JavaDoc key, Object JavaDoc[] args) {
95         return null;
96     }
97 }
98
Popular Tags