| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
package org.melati.template.freemarker; |
| 44 | |
|
| 45 | |
import java.io.FileNotFoundException; |
| 46 | |
import java.util.HashMap; |
| 47 | |
import java.util.Map; |
| 48 | |
|
| 49 | |
import org.apache.velocity.exception.ParseErrorException; |
| 50 | |
import org.apache.velocity.exception.ResourceNotFoundException; |
| 51 | |
import org.melati.poem.AccessPoemException; |
| 52 | |
import org.melati.template.AbstractTemplateEngine; |
| 53 | |
|
| 54 | |
import org.melati.MelatiConfig; |
| 55 | |
import org.melati.template.NotFoundException; |
| 56 | |
import org.melati.template.Template; |
| 57 | |
import org.melati.template.TemplateContext; |
| 58 | |
import org.melati.template.TemplateEngine; |
| 59 | |
import org.melati.template.TemplateEngineException; |
| 60 | |
import org.melati.template.velocity.VelocityTemplate; |
| 61 | |
import org.melati.util.MelatiBugMelatiException; |
| 62 | |
import org.melati.util.MelatiStringWriter; |
| 63 | |
import org.melati.util.MelatiWriter; |
| 64 | |
|
| 65 | |
import freemarker.ext.beans.BeansWrapper; |
| 66 | |
import freemarker.template.Configuration; |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 51 | public class FreemarkerTemplateEngine extends AbstractTemplateEngine implements TemplateEngine { |
| 76 | |
|
| 77 | |
|
| 78 | |
public static final String NAME = "freemarker"; |
| 79 | |
|
| 80 | |
private Configuration config; |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
@Override |
| 86 | |
public void init(MelatiConfig melatiConfig) throws TemplateEngineException { |
| 87 | |
try { |
| 88 | 3 | config = new Configuration(); |
| 89 | 3 | config.setClassForTemplateLoading(this.getClass(), "/"); |
| 90 | 3 | BeansWrapper bw = new BeansWrapper(); |
| 91 | 3 | bw.setExposureLevel(BeansWrapper.EXPOSE_ALL); |
| 92 | 3 | config.setObjectWrapper(bw); |
| 93 | |
|
| 94 | 0 | } catch (Exception e) { |
| 95 | 0 | throw new TemplateEngineException(e); |
| 96 | 3 | } |
| 97 | |
|
| 98 | 3 | } |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
@Override |
| 104 | |
public TemplateContext getTemplateContext() throws TemplateEngineException { |
| 105 | 2 | Map<String, Object> context = new HashMap<String, Object>(); |
| 106 | 2 | return new FreemarkerServletTemplateContext(context); |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
@Override |
| 113 | |
public String getName() { |
| 114 | 1 | return NAME; |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
@Override |
| 121 | |
public String templateExtension() { |
| 122 | 2 | return ".fml"; |
| 123 | |
} |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
@Override |
| 129 | |
public Template template(String templateName) throws NotFoundException { |
| 130 | |
freemarker.template.Template t; |
| 131 | |
try { |
| 132 | 1 | t = config.getTemplate(templateName); |
| 133 | 1 | return new FreemarkerTemplate(t); |
| 134 | 0 | } catch (FileNotFoundException e) { |
| 135 | 0 | if (templateName.endsWith(templateExtension())) { |
| 136 | |
|
| 137 | |
|
| 138 | 0 | String velocityTemplateName = templateName.substring(0, templateName |
| 139 | |
.lastIndexOf(templateExtension())) |
| 140 | |
+ ".vm"; |
| 141 | |
try { |
| 142 | 0 | return new VelocityTemplate(velocityTemplateName); |
| 143 | 0 | } catch (ParseErrorException p) { |
| 144 | 0 | throw new MelatiBugMelatiException( |
| 145 | |
"Problem converting a Velocity template to a Freemarker template: " + velocityTemplateName, |
| 146 | |
p); |
| 147 | 0 | } catch (ResourceNotFoundException e2) { |
| 148 | 0 | throw new NotFoundException("Could not find template " + templateName + " or " + velocityTemplateName); |
| 149 | |
} |
| 150 | 0 | } else throw new NotFoundException("Could not find template " + templateName, e); |
| 151 | 0 | } catch (Exception e) { |
| 152 | 0 | throw new TemplateEngineException(e); |
| 153 | |
} |
| 154 | |
} |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
@Override |
| 162 | |
public void expandTemplate(MelatiWriter out, String templateName, TemplateContext templateContext) throws NotFoundException { |
| 163 | 0 | expandTemplate(out, template(templateName), templateContext); |
| 164 | 0 | } |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
@Override |
| 178 | |
public void expandTemplate(MelatiWriter out, Template template, TemplateContext templateContext) { |
| 179 | |
try { |
| 180 | 1 | template.write(out, templateContext); |
| 181 | 0 | } catch (TemplateEngineException problem) { |
| 182 | 0 | Exception underlying = problem.subException; |
| 183 | 0 | if (underlying instanceof AccessPoemException) { |
| 184 | 0 | throw (AccessPoemException)underlying; |
| 185 | |
} |
| 186 | 0 | throw problem; |
| 187 | 1 | } |
| 188 | |
|
| 189 | 1 | } |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
@Override |
| 195 | |
public String expandedTemplate(Template template, TemplateContext templateContext) { |
| 196 | 1 | MelatiStringWriter s = new MelatiStringWriter(); |
| 197 | 1 | expandTemplate(s, template, templateContext); |
| 198 | 1 | return s.toString(); |
| 199 | |
} |
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
@Override |
| 205 | |
public MelatiStringWriter getStringWriter() { |
| 206 | 0 | return new MelatiStringWriter(); |
| 207 | |
} |
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
@Override |
| 217 | |
public Object getEngine() { |
| 218 | 0 | return null; |
| 219 | |
} |
| 220 | |
|
| 221 | |
} |