Java 类org.apache.velocity.io.VelocityWriter 实例源码

项目:jersey-mvc-velocity    文件:VelocityViewProcessor.java   
@Override
@SuppressWarnings("unchecked")
public void writeTo(final Template template, final Viewable viewable, final MediaType mediaType,
                    final MultivaluedMap<String, Object> httpHeaders, final OutputStream out) throws IOException {
    try {
        Object model = viewable.getModel();
        if (!(model instanceof Map)) {
            model = new HashMap<String, Object>() {{
                put("model", viewable.getModel());
            }};
        }

        VelocityContext velocityContext = new VelocityContext();
        for (String key : ((Map<String, Object>) model).keySet()) {
            velocityContext.put(key, ((Map<String, Object>) model).get(key));
        }

        Charset encoding = setContentType(mediaType, httpHeaders);

        VelocityWriter writer = new VelocityWriter(new OutputStreamWriter(out, encoding));

        template.merge(velocityContext, writer);
        writer.flush();

    } catch (VelocityException te) {
        throw new ContainerException(te);
    }
}