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

1   /*
2    * $Id: InsertDefinitionVModel.java 901361 2010-01-20 20:10:27Z apetrelli $
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  
22  package org.apache.tiles.velocity.template;
23  
24  import java.io.IOException;
25  import java.io.Writer;
26  import java.util.Map;
27  
28  import javax.servlet.ServletContext;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.tiles.servlet.context.ServletUtil;
33  import org.apache.tiles.template.InsertDefinitionModel;
34  import org.apache.tiles.velocity.context.VelocityUtil;
35  import org.apache.velocity.context.Context;
36  import org.apache.velocity.context.InternalContextAdapter;
37  import org.apache.velocity.runtime.Renderable;
38  
39  /***
40   * Wraps {@link InsertDefinitionModel} to be used in Velocity. For the list of
41   * parameters, see
42   * {@link InsertDefinitionModel#start(org.apache.tiles.TilesContainer, Object...)}
43   * , {@link InsertDefinitionModel#end(org.apache.tiles.TilesContainer,
44   * String, String, String, String, String, String, Object...)} and
45   * {@link InsertDefinitionModel#execute(org.apache.tiles.TilesContainer,
46   * String, String, String, String, String, String, Object...)}.
47   *
48   * @version $Rev: 901361 $ $Date: 2010-01-20 21:10:27 +0100 (mer, 20 gen 2010) $
49   * @since 2.2.0
50   * @deprecated Use Velocity directives.
51   */
52  @Deprecated
53  public class InsertDefinitionVModel implements Executable, BodyExecutable {
54  
55      /***
56       * The template model.
57       */
58      private InsertDefinitionModel model;
59  
60      /***
61       * The Servlet context.
62       */
63      private ServletContext servletContext;
64  
65      /***
66       * Constructor.
67       *
68       * @param model The template model.
69       * @param servletContext The servlet context.
70       * @since 2.2.0
71       */
72      public InsertDefinitionVModel(InsertDefinitionModel model,
73              ServletContext servletContext) {
74          this.model = model;
75          this.servletContext = servletContext;
76      }
77  
78      /*** {@inheritDoc} */
79      public Renderable execute(HttpServletRequest request,
80              HttpServletResponse response, Context velocityContext,
81              Map<String, Object> params) {
82          return new AbstractDefaultToStringRenderable(velocityContext, params, response, request) {
83  
84              public boolean render(InternalContextAdapter context, Writer writer)
85                      throws IOException {
86                  model.execute(ServletUtil.getCurrentContainer(request,
87                          servletContext), (String) params.get("name"),
88                          (String) params.get("template"), (String) params
89                                  .get("templateType"), (String) params
90                                  .get("templateExpression"), (String) params
91                                  .get("role"), (String) params.get("preparer"),
92                          velocityContext, request, response, writer);
93                  return true;
94              }
95          };
96      }
97  
98      /*** {@inheritDoc} */
99      public Renderable end(HttpServletRequest request, HttpServletResponse response,
100             Context velocityContext) {
101         Map<String, Object> params = VelocityUtil.getParameterStack(velocityContext).pop();
102         return new AbstractDefaultToStringRenderable(velocityContext, params,
103                 response, request) {
104 
105             public boolean render(InternalContextAdapter context, Writer writer)
106                     throws IOException {
107                 model.end(ServletUtil.getCurrentContainer(request,
108                         servletContext), (String) params.get("name"),
109                         (String) params.get("template"), (String) params
110                                 .get("templateType"), (String) params
111                                 .get("templateExpression"), (String) params
112                                 .get("role"), (String) params.get("preparer"),
113                         velocityContext, request, response, writer);
114                 return true;
115             }
116         };
117     }
118 
119     /*** {@inheritDoc} */
120     public void start(HttpServletRequest request, HttpServletResponse response,
121             Context velocityContext, Map<String, Object> params) {
122         VelocityUtil.getParameterStack(velocityContext).push(params);
123         model.start(ServletUtil.getCurrentContainer(request,
124                 servletContext), velocityContext, request, response);
125     }
126 }