01: /*
02:
03: Licensed to the Apache Software Foundation (ASF) under one or more
04: contributor license agreements. See the NOTICE file distributed with
05: this work for additional information regarding copyright ownership.
06: The ASF licenses this file to You under the Apache License, Version 2.0
07: (the "License"); you may not use this file except in compliance with
08: the License. You may obtain a copy of the License at
09:
10: http://www.apache.org/licenses/LICENSE-2.0
11:
12: Unless required by applicable law or agreed to in writing, software
13: distributed under the License is distributed on an "AS IS" BASIS,
14: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: See the License for the specific language governing permissions and
16: limitations under the License.
17:
18: */
19: package org.apache.batik.script.jpython;
20:
21: import java.io.IOException;
22: import java.io.Reader;
23: import java.io.Writer;
24: import java.util.Locale;
25:
26: import org.apache.batik.script.InterpreterException;
27: import org.python.util.PythonInterpreter;
28:
29: /**
30: * A simple implementation of <code>Interpreter</code> interface to use
31: * JPython python parser.
32: * @author <a href="mailto:cjolif@ilog.fr">Christophe Jolif</a>
33: * @version $Id: JPythonInterpreter.java 475477 2006-11-15 22:44:28Z cam $
34: */
35: public class JPythonInterpreter implements
36: org.apache.batik.script.Interpreter {
37: private PythonInterpreter interpreter = null;
38:
39: public JPythonInterpreter() {
40: interpreter = new PythonInterpreter();
41: }
42:
43: // org.apache.batik.script.Intepreter implementation
44:
45: public Object evaluate(Reader scriptreader) throws IOException {
46: return evaluate(scriptreader, "");
47: }
48:
49: public Object evaluate(Reader scriptreader, String description)
50: throws IOException {
51:
52: // oups jpython doesn't accept reader in its eval method :-(
53: StringBuffer sbuffer = new StringBuffer();
54: char[] buffer = new char[1024];
55: int val = 0;
56: while ((val = scriptreader.read(buffer)) != -1) {
57: sbuffer.append(buffer, 0, val);
58: }
59: String str = sbuffer.toString();
60: return evaluate(str);
61: }
62:
63: public Object evaluate(String script) {
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 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 name, Object object) {
78: interpreter.set(name, object);
79: }
80:
81: public void setOut(Writer out) {
82: interpreter.setOut(out);
83: }
84:
85: // org.apache.batik.i18n.Localizable implementation
86:
87: public Locale getLocale() {
88: return null;
89: }
90:
91: public void setLocale(Locale locale) {
92: }
93:
94: public String formatMessage(String key, Object[] args) {
95: return null;
96: }
97: }
|