/**
* Mad-Advertisement
* Copyright (C) 2011 Thorsten Marx <thmarx@gmx.net>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.mad.ads.base.api.render.impl.freemarker;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import net.mad.ads.base.api.render.BannerRenderer;
import net.mad.ads.base.api.render.RenderContext;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreemarkerBannerRenderer implements BannerRenderer {
private Configuration cfg = null;
private Map<String, Template> templates = null;
@Override
public void init(String templatePath) throws IOException {
cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File(templatePath));
cfg.setObjectWrapper(new DefaultObjectWrapper());
this.templates = new HashMap<String, Template>();
}
@Override
public void registerTemplate(String name, String file) throws IOException {
Template temp = cfg.getTemplate(file);
templates.put(name, temp);
}
@Override
public String render(String name, RenderContext context) throws IOException {
try {
Template temp = templates.get(name);
context.put("standardCompress", new freemarker.template.utility.StandardCompress());
StringWriter sw = new StringWriter();
BufferedWriter bw = new BufferedWriter(sw);
temp.process(context, bw);
bw.flush();
return sw.toString();
} catch (Exception e) {
throw new IOException(e);
}
}
}