if(varName == null) {
throw new TemplateModelException("'var' or 'local' or 'global' parameter evaluates to null string");
}
final StringBuffer buf = new StringBuffer();
final Environment env = Environment.getCurrentEnvironment();
final boolean localVar = local;
final boolean globalVar = global;
return new Writer() {
public void write(char cbuf[], int off, int len) {
buf.append(cbuf, off, len);
}
public void flush() throws IOException {
out.flush();
}
public void close() throws IOException {
SimpleScalar result = new SimpleScalar(buf.toString());
try {
if (localVar) {
env.setLocalVariable(varName, result);
} else if (globalVar) {
env.setGlobalVariable(varName, result);
}
else {
if (nsModel == null) {
env.setVariable(varName, result);
} else {
((Environment.Namespace) nsModel).put(varName, result);
}
}
} catch (java.lang.IllegalStateException ise) { // if somebody uses 'local' outside a macro