1/*2 * $Id: FreemarkerAutotagRuntime.java 1360343 2012-07-11 18:35:52Z nlebas $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */21package org.apache.tiles.request.freemarker.autotag;
2223import java.util.Map;
2425import org.apache.tiles.autotag.core.runtime.ModelBody;
26import org.apache.tiles.autotag.core.runtime.AutotagRuntime;
27import org.apache.tiles.request.Request;
28import org.apache.tiles.request.freemarker.FreemarkerRequest;
29import org.apache.tiles.request.freemarker.FreemarkerRequestUtil;
3031import freemarker.core.Environment;
32import freemarker.template.TemplateDirectiveBody;
33import freemarker.template.TemplateDirectiveModel;
34import freemarker.template.TemplateModel;
3536/**37 * A Runtime for implementing a Freemarker Template Directive. 38 */39publicclassFreemarkerAutotagRuntimeimplements AutotagRuntime<Request>, TemplateDirectiveModel {
4041private Environment env;
42private TemplateDirectiveBody body;
43private Map<String, TemplateModel> params;
4445/** {@inheritDoc} */46 @SuppressWarnings("unchecked")
47 @Override
48publicvoid execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) {
49this.env = env;
50this.body = body;
51this.params = params;
52 }
5354/** {@inheritDoc} */55 @Override
56publicRequest createRequest() {
57return FreemarkerRequest.createServletFreemarkerRequest(FreemarkerRequestUtil.getApplicationContext(env), env);
58 }
5960/** {@inheritDoc} */61 @Override
62public ModelBody createModelBody() {
63returnnewFreemarkerModelBody(env.getOut(), body);
64 }
6566/** {@inheritDoc} */67 @Override
68public <T> T getParameter(String name, Class<T> type, T defaultValue) {
69return FreemarkerUtil.getAsObject((TemplateModel)params.get(name), type, defaultValue);
70 }
71 }