1 34 package groovy.text; 35 36 import groovy.lang.Binding; 37 import groovy.lang.GroovyShell; 38 import groovy.lang.Script; 39 import groovy.lang.Writable; 40 41 import java.io.BufferedReader ; 42 import java.io.IOException ; 43 import java.io.PrintWriter ; 44 import java.io.Reader ; 45 import java.io.StringWriter ; 46 import java.io.Writer ; 47 import java.util.Map ; 48 49 import org.codehaus.groovy.control.CompilationFailedException; 50 import org.codehaus.groovy.runtime.InvokerHelper; 51 52 53 59 public class SimpleTemplateEngine extends TemplateEngine { 60 61 64 public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException , IOException { 65 SimpleTemplate template = new SimpleTemplate(); 66 GroovyShell shell = new GroovyShell(); 67 String script = template.parse(reader); 68 template.script = shell.parse(script); 69 return template; 70 } 71 72 private static class SimpleTemplate implements Template { 73 74 private Script script; 75 76 public Writable make() { 77 return make(null); 78 } 79 80 public Writable make(final Map map) { 81 return new Writable() { 82 87 public Writer writeTo(Writer writer) throws IOException { 88 Binding binding; 89 if (map == null) binding = new Binding(); else binding = new Binding(map); 90 Script scriptObject = InvokerHelper.createScript(script.getClass(), binding); 91 PrintWriter pw = new PrintWriter (writer); 92 scriptObject.setProperty("out", pw); 93 scriptObject.run(); 94 pw.flush(); 95 return writer; 96 } 97 98 103 public String toString() { 104 try { 105 StringWriter sw = new StringWriter (); 106 writeTo(sw); 107 return sw.toString(); 108 } catch (Exception e) { 109 return e.toString(); 110 } 111 } 112 }; 113 } 114 115 123 private String parse(Reader reader) throws IOException { 124 if (!reader.markSupported()) { 125 reader = new BufferedReader (reader); 126 } 127 StringWriter sw = new StringWriter (); 128 startScript(sw); 129 boolean start = false; 130 int c; 131 while((c = reader.read()) != -1) { 132 if (c == '<') { 133 reader.mark(1); 134 c = reader.read(); 135 if (c != '%') { 136 sw.write('<'); 137 reader.reset(); 138 continue; 139 } else { 140 reader.mark(1); 141 c = reader.read(); 142 if (c == '=') { 143 groovyExpression(reader, sw); 144 } else { 145 reader.reset(); 146 groovySection(reader, sw); 147 } 148 continue; 149 } 150 } 151 if (c == '\"') { 152 sw.write('\\'); 153 } 154 sw.write(c); 155 } 156 endScript(sw); 157 String result = sw.toString(); 158 return result; 160 } 161 162 private void startScript(StringWriter sw) { 163 sw.write("/* Generated by SimpleTemplateEngine */ "); 164 sw.write("out.print(\""); 165 } 166 167 private void endScript(StringWriter sw) { 168 sw.write("\");\n"); 169 } 170 171 178 private void groovyExpression(Reader reader, StringWriter sw) throws IOException { 179 sw.write("\");out.print(\"${"); 180 int c; 181 while((c = reader.read()) != -1) { 182 if (c == '%') { 183 c = reader.read(); 184 if (c != '>') { 185 sw.write('%'); 186 } else { 187 break; 188 } 189 } 190 sw.write(c); 191 } 192 sw.write("}\");out.print(\""); 193 } 194 195 202 private void groovySection(Reader reader, StringWriter sw) throws IOException { 203 sw.write("\");"); 204 int c; 205 while((c = reader.read()) != -1) { 206 if (c == '%') { 207 c = reader.read(); 208 if (c != '>') { 209 sw.write('%'); 210 } else { 211 break; 212 } 213 } 214 sw.write(c); 215 } 216 sw.write(";out.print(\""); 217 } 218 219 } 220 } 221 | Popular Tags |