mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-24 21:07:26 +08:00
60 lines
1.4 KiB
Java
60 lines
1.4 KiB
Java
/*
|
|
* Copyright (c) 2008 Mysema Ltd.
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
package com.mysema.query.apt;
|
|
|
|
import java.io.IOException;
|
|
import java.io.Writer;
|
|
import java.util.Map;
|
|
|
|
import freemarker.template.Configuration;
|
|
import freemarker.template.DefaultObjectWrapper;
|
|
import freemarker.template.TemplateException;
|
|
|
|
/**
|
|
* Serializer provides
|
|
*
|
|
* @author tiwe
|
|
* @version $Id$
|
|
*/
|
|
public interface Serializer {
|
|
|
|
/**
|
|
*
|
|
* @param model
|
|
* @param writer
|
|
* @throws Exception
|
|
*/
|
|
public void serialize(Map<String, Object> model, Writer writer)
|
|
throws Exception;
|
|
|
|
public final class FreeMarker implements Serializer {
|
|
|
|
private static final Configuration cfg;
|
|
|
|
static {
|
|
cfg = new Configuration();
|
|
cfg.setClassForTemplateLoading(Serializer.class, "/");
|
|
cfg.setObjectWrapper(new DefaultObjectWrapper());
|
|
}
|
|
|
|
private final String templateLocation;
|
|
|
|
public FreeMarker(String template) {
|
|
if (template == null)
|
|
throw new IllegalArgumentException("template was null");
|
|
templateLocation = template;
|
|
}
|
|
|
|
public void serialize(Map<String, Object> model, Writer writer)
|
|
throws IOException, TemplateException {
|
|
cfg.getTemplate(templateLocation).process(model, writer);
|
|
writer.flush();
|
|
}
|
|
|
|
}
|
|
|
|
}
|