1 52 53 package freemarker.template.utility; 54 55 import java.io.IOException ; 56 import java.io.Writer ; 57 import java.util.Map ; 58 import org.python.core.PyObject; 59 import org.python.util.PythonInterpreter; 60 import freemarker.template.TemplateTransformModel; 61 import freemarker.core.Environment; 62 63 67 68 public class JythonRuntime extends PythonInterpreter 69 implements TemplateTransformModel 70 { 71 public Writer getWriter(final Writer out, 72 final Map args) 73 { 74 final StringBuffer buf = new StringBuffer (); 75 final Environment env = Environment.getCurrentEnvironment(); 76 return new Writer () { 77 public void write(char cbuf[], int off, int len) { 78 buf.append(cbuf, off, len); 79 } 80 81 public void flush() throws IOException { 82 interpretBuffer(); 83 out.flush(); 84 } 85 86 public void close() { 87 interpretBuffer(); 88 } 89 90 private void interpretBuffer() { 91 synchronized(JythonRuntime.this) { 92 PyObject prevOut = systemState.stdout; 93 try { 94 setOut(out); 95 set("env", env); 96 exec(buf.toString()); 97 buf.setLength(0); 98 } finally { 99 setOut(prevOut); 100 } 101 } 102 } 103 }; 104 } 105 } 106 | Popular Tags |