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

1   /*
2    * $Id: BlockDirective.java 901355 2010-01-20 19:58:12Z 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.velocity.context.VelocityUtil;
33  import org.apache.velocity.context.InternalContextAdapter;
34  import org.apache.velocity.runtime.directive.Directive;
35  import org.apache.velocity.runtime.parser.node.Node;
36  import org.apache.velocity.tools.view.ViewContext;
37  
38  /***
39   * Base abstract directive for those models who need to evaluate, but not use, a
40   * body.
41   *
42   * @version $Rev: 901355 $ $Date: 2010-01-20 20:58:12 +0100 (mer, 20 gen 2010) $
43   * @since 2.2.2
44   */
45  public abstract class BlockDirective extends Directive {
46  
47      /*** {@inheritDoc} */
48  
49      @Override
50      public int getType() {
51          return BLOCK;
52      }
53  
54      /*** {@inheritDoc} */
55  
56      @Override
57      public boolean render(InternalContextAdapter context, Writer writer,
58              Node node) throws IOException {
59          ViewContext viewContext = (ViewContext) context
60                  .getInternalUserContext();
61          Map<String, Object> params = VelocityUtil.getParameters(context, node);
62          HttpServletRequest request = viewContext.getRequest();
63          HttpServletResponse response = viewContext.getResponse();
64          ServletContext servletContext = viewContext.getServletContext();
65          start(context, writer, params, request, response, servletContext);
66          VelocityUtil.evaluateBody(context, writer, node);
67          end(context, writer, params, request, response, servletContext);
68          return true;
69      }
70  
71      /***
72       * Starts the directive, before evaluating the body.
73       *
74       * @param context The Velocity context.
75       * @param writer The writer user to write the result.
76       * @param params The parameters got from the first node of the directive.
77       * @param request The HTTP request.
78       * @param response The HTTP response.
79       * @param servletContext The servlet context.
80       * @since 2.2.2
81       */
82      protected abstract void start(InternalContextAdapter context, Writer writer,
83              Map<String, Object> params, HttpServletRequest request,
84              HttpServletResponse response, ServletContext servletContext);
85  
86      /***
87       * Ends the directive, after evaluating the body.
88       *
89       * @param context The Velocity context.
90       * @param writer The writer user to write the result.
91       * @param params The parameters got from the first node of the directive.
92       * @param request The HTTP request.
93       * @param response The HTTP response.
94       * @param servletContext The servlet context.
95       * @throws IOException If something goes wrong when finishing this directive.
96       * @since 2.2.2
97       */
98      protected abstract void end(InternalContextAdapter context, Writer writer,
99              Map<String, Object> params, HttpServletRequest request,
100             HttpServletResponse response, ServletContext servletContext)
101             throws IOException;
102 }