package com.bea.canvas;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.codehaus.groovy.syntax.SyntaxException;
/**
* This class represents a Template.
*
* @author Cedric Beust, Mar 6, 2004
*
*/
public class Template {
private boolean m_verbose = false;
private String m_templatePath = null;
private BufferedReader m_templateFile = null;
private static final String MARKER_START = "<%";
private static final String MARKER_END = "%>";
public Template(String templatePath) throws FileNotFoundException {
m_templatePath = templatePath;
m_templateFile = new BufferedReader(new FileReader(new File(m_templatePath)));
}
private String readTemplate() {
StringBuffer sb = new StringBuffer();
try {
String line = null;
line = m_templateFile.readLine();
while (null != line) {
sb.append(line).append("\n");
line = m_templateFile.readLine();
}
}
catch (IOException e) {
// XXX Auto-generated catch block
e.printStackTrace();
}
String result = sb.toString();
return result;
}
public void merge(Context context, StringBuffer result) {
StringBuffer sb = new StringBuffer();
String fullFile = readTemplate();
int fullFileIndex = 0;
try {
GroovyShell shell = bindContextVariables(context);
Binding b = shell.getContext();
if (m_verbose) dumpBinding(b);
while (fullFileIndex < fullFile.length()) {
int startIndex = fullFile.indexOf(MARKER_START, fullFileIndex);
if (-1 != startIndex) {
// Append all the simple text until the marker
result.append(fullFile.substring(fullFileIndex, startIndex));
int endIndex = fullFile.indexOf(MARKER_END, fullFileIndex);
assert -1 != endIndex : "MARKER NOT CLOSED";
String fragment =
fullFile.substring(startIndex + MARKER_START.length(), endIndex);
// Append all the substituted text
String interpretedText = interpret(shell, context, fragment);
result.append(interpretedText);
// Position index after end marker
fullFileIndex = endIndex + MARKER_END.length();
}
else {
result.append(fullFile.substring(fullFileIndex));
// Position index after the end of the file
fullFileIndex = fullFile.length() + 1;
}
}
}
catch (SyntaxException e1) {
e1.printStackTrace();
result.append(fullFile.substring(fullFileIndex));
// Position index after the end of the file
fullFileIndex = fullFile.length() + 1;
}
catch (ClassNotFoundException e1) {
e1.printStackTrace();
result.append(fullFile.substring(fullFileIndex));
// Position index after the end of the file
fullFileIndex = fullFile.length() + 1;
}
catch (IOException e1) {
e1.printStackTrace();
result.append(fullFile.substring(fullFileIndex));
// Position index after the end of the file
fullFileIndex = fullFile.length() + 1;
}
}
// private void bindJavaLangObject(GroovyShell shell, String name, Object oValue) {
// Binding binding = shell.getContext();
// String value = oValue.toString();
// binding.setVariable(name, value);
// if (m_verbose) ppp("BOUND java.lang VARIABLE " + name + "=" + value);
// }
private void dumpBinding(Binding b) {
ppp("@@@ BINDING:");
for (Iterator it = b.getVariables().keySet().iterator(); it.hasNext(); ) {
Object k = it.next();
Object v = b.getVariables().get(k);
ppp(" " + k + " = " + v);
}
ppp("@@@");
}
private void bindObject(GroovyShell shell, String name, Object value)
throws SyntaxException, ClassNotFoundException, IOException
{
Binding b = shell.getContext();
b.setVariable(name, value);
}
private int m_scriptCount = 0;
private String interpretFragment(GroovyShell shell, String source)
throws SyntaxException, ClassNotFoundException, IOException
{
if (m_verbose) ppp("INTERPRETING ==" + source + "==");
Object result = shell.evaluate(source, "TemporarySwellScript" + m_scriptCount);
m_scriptCount++;
return result.toString();
}
private String interpret(GroovyShell shell, Context context, String source)
throws SyntaxException, ClassNotFoundException, IOException
{
return interpretFragment(shell, source);
}
private GroovyShell bindContextVariables(Context context)
throws SyntaxException, ClassNotFoundException, IOException
{
Binding binding = new Binding();
GroovyShell result = new GroovyShell(binding);
for (Iterator it = context.keySet().iterator(); it.hasNext(); ) {
Object oVariable = it.next();
Object oValue = context.get(oVariable);
if (null != oValue) {
String name = oVariable.toString();
bindObject(result, name, oValue);
}
else {
warn("No variable: " + oVariable.toString());
}
}
return result;
}
private static void ppp(String s) {
System.out.println("[Template] " + s);
}
private static void warn(String s) {
System.out.println("Warning:" + s);
}
static public void main(String[] argv) {
//call groovy expressions from Java code
Binding binding = new Binding();
binding.setVariable("foo", new Integer(2));
GroovyShell shell = new GroovyShell(binding);
try {
Object value =
shell.evaluate("println 'Hello World!'; x = 123; return foo * 10", "TestScript");
assert value.equals(new Integer(20));
assert binding.getVariable("x").equals(new Integer(123));
System.out.println("value:" + value);
}
catch (SyntaxException e) {
// XXX Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e) {
// XXX Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// XXX Auto-generated catch block
e.printStackTrace();
}
}
}