1 52 53 package freemarker.template.utility; 54 55 import freemarker.template.*; 56 import freemarker.core.Environment; 57 import java.io.*; 58 import java.util.Map ; 59 60 90 public class CaptureOutput implements TemplateTransformModel { 91 92 public Writer getWriter(final Writer out, final Map args) throws TemplateModelException { 93 String errmsg = "Must specify the name of the variable in " 94 + "which to capture the output with the 'var' or 'local' or 'global' parameter."; 95 if (args == null) throw new TemplateModelException(errmsg); 96 97 boolean local = false, global=false; 98 final TemplateModel nsModel = (TemplateModel) args.get("namespace"); 99 Object varNameModel = args.get("var"); 100 if (varNameModel == null) { 101 varNameModel = args.get("local"); 102 if (varNameModel == null) { 103 varNameModel = args.get("global"); 104 global = true; 105 } else { 106 local = true; 107 } 108 if (varNameModel == null) { 109 throw new TemplateModelException(errmsg); 110 } 111 } 112 if (args.size()==2) { 113 if (nsModel == null) { 114 throw new TemplateModelException("Second parameter can only be namespace"); 115 } 116 if (local) { 117 throw new TemplateModelException("Cannot specify namespace for a local assignment"); 118 } 119 if (global) { 120 throw new TemplateModelException("Cannot specify namespace for a global assignment"); 121 } 122 if (!(nsModel instanceof Environment.Namespace)) { 123 throw new TemplateModelException("namespace parameter does not specify a namespace. It is a " + nsModel.getClass().getName()); 124 } 125 } 126 else if (args.size() != 1) throw new TemplateModelException( 127 "Bad parameters. Use only one of 'var' or 'local' or 'global' parameters."); 128 129 if(!(varNameModel instanceof TemplateScalarModel)) { 130 throw new TemplateModelException("'var' or 'local' or 'global' parameter doesn't evaluate to a string"); 131 } 132 final String varName = ((TemplateScalarModel) varNameModel).getAsString(); 133 if(varName == null) { 134 throw new TemplateModelException("'var' or 'local' or 'global' parameter evaluates to null string"); 135 } 136 137 final StringBuffer buf = new StringBuffer (); 138 final Environment env = Environment.getCurrentEnvironment(); 139 final boolean localVar = local; 140 final boolean globalVar = global; 141 142 return new Writer() { 143 144 public void write(char cbuf[], int off, int len) { 145 buf.append(cbuf, off, len); 146 } 147 148 public void flush() throws IOException { 149 out.flush(); 150 } 151 152 public void close() throws IOException { 153 SimpleScalar result = new SimpleScalar(buf.toString()); 154 try { 155 if (localVar) { 156 env.setLocalVariable(varName, result); 157 } else if (globalVar) { 158 env.setGlobalVariable(varName, result); 159 } 160 else { 161 if (nsModel == null) { 162 env.setVariable(varName, result); 163 } else { 164 ((Environment.Namespace) nsModel).put(varName, result); 165 } 166 } 167 } catch (java.lang.IllegalStateException ise) { throw new IOException("Could not set variable " + varName + ": " + ise.getMessage()); 169 } 170 } 171 }; 172 } 173 } 174 | Popular Tags |