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

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