This project has retired. For details please refer to its Attic page.
FreemarkerAutotagRuntime xref
View Javadoc

1   /*
2    * $Id: FreemarkerAutotagRuntime.java 1360343 2012-07-11 18:35:52Z nlebas $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.request.freemarker.autotag;
22  
23  import java.util.Map;
24  
25  import org.apache.tiles.autotag.core.runtime.ModelBody;
26  import org.apache.tiles.autotag.core.runtime.AutotagRuntime;
27  import org.apache.tiles.request.Request;
28  import org.apache.tiles.request.freemarker.FreemarkerRequest;
29  import org.apache.tiles.request.freemarker.FreemarkerRequestUtil;
30  
31  import freemarker.core.Environment;
32  import freemarker.template.TemplateDirectiveBody;
33  import freemarker.template.TemplateDirectiveModel;
34  import freemarker.template.TemplateModel;
35  
36  /**
37   * A Runtime for implementing a Freemarker Template Directive.   
38   */
39  public class FreemarkerAutotagRuntime implements AutotagRuntime<Request>, TemplateDirectiveModel {
40  
41      private Environment env;
42      private TemplateDirectiveBody body;
43      private Map<String, TemplateModel> params;
44      
45      /** {@inheritDoc} */
46      @SuppressWarnings("unchecked")
47      @Override
48      public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) {
49          this.env = env;
50          this.body = body;
51          this.params = params;
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public Request createRequest() {
57          return FreemarkerRequest.createServletFreemarkerRequest(FreemarkerRequestUtil.getApplicationContext(env), env);
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public ModelBody createModelBody() {
63          return new FreemarkerModelBody(env.getOut(), body);
64      }
65      
66      /** {@inheritDoc} */
67      @Override
68      public <T> T getParameter(String name, Class<T> type, T defaultValue) {
69          return FreemarkerUtil.getAsObject((TemplateModel)params.get(name), type, defaultValue);
70      }
71  }