Home » tiles-2.2.1-src » org.apache.tiles.velocity.context » [javadoc | source]

    1   /*
    2    * $Id: VelocityUtil.java 797765 2009-07-25 13:20:26Z 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.context;
   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   
   31   import org.apache.tiles.ArrayStack;
   32   import org.apache.velocity.context.Context;
   33   import org.apache.velocity.context.InternalContextAdapter;
   34   import org.apache.velocity.runtime.Renderable;
   35   
   36   /**
   37    * Utilities for Velocity usage in Tiles.
   38    *
   39    * @version $Rev: 797765 $ $Date: 2009-07-25 15:20:26 +0200 (sab, 25 lug 2009) $
   40    * @since 2.2.0
   41    */
   42   public final class VelocityUtil {
   43   
   44       /**
   45        * A renderable object that does not render anything.
   46        *
   47        * @since 2.2.0
   48        */
   49       public static final Renderable EMPTY_RENDERABLE;
   50   
   51       static {
   52           EMPTY_RENDERABLE = new Renderable() {
   53   
   54               @Override
   55               public String toString() {
   56                   return "";
   57               }
   58   
   59               public boolean render(InternalContextAdapter context, Writer writer)
   60                       throws IOException {
   61                   // Does nothing, really!
   62                   return true;
   63               }
   64           };
   65       }
   66   
   67       /**
   68        * The attribute key that will be used to store the parameter map, to use across Velocity tool calls.
   69        *
   70        * @since 2.2.0
   71        */
   72       private static final String PARAMETER_MAP_STACK_KEY = "org.apache.tiles.velocity.PARAMETER_MAP_STACK";
   73   
   74       /**
   75        * Private constructor to avoid instantiation.
   76        */
   77       private VelocityUtil() {
   78       }
   79   
   80       /**
   81        * Null-safe conversion from Boolean to boolean.
   82        *
   83        * @param obj The Boolean object.
   84        * @param defaultValue This value will be returned if <code>obj</code> is null.
   85        * @return The boolean value of <code>obj</code> or, if null, <code>defaultValue</code>.
   86        * @since 2.2.0
   87        */
   88       public static boolean toSimpleBoolean(Boolean obj, boolean defaultValue) {
   89           return obj != null ? obj : defaultValue;
   90       }
   91   
   92       /**
   93        * Returns or creates the parameter stack to use. It is useful to store parameters across tool calls.
   94        *
   95        * @param context The Velocity context.
   96        * @return The parameter stack.
   97        * @since 2.2.0
   98        */
   99       @SuppressWarnings("unchecked")
  100       public static ArrayStack<Map<String, Object>> getParameterStack(Context context) {
  101           ArrayStack<Map<String, Object>> stack = (ArrayStack<Map<String, Object>>) context
  102                   .get(PARAMETER_MAP_STACK_KEY);
  103           if (stack == null) {
  104               stack = new ArrayStack<Map<String, Object>>();
  105               context.put(PARAMETER_MAP_STACK_KEY, stack);
  106           }
  107           return stack;
  108       }
  109   
  110       /**
  111        * Sets an attribute in the desired scope.
  112        *
  113        * @param velocityContext The Velocity context.
  114        * @param request The HTTP request.
  115        * @param servletContext The servlet context.
  116        * @param name The name of the attribute.
  117        * @param obj The value of the attribute.
  118        * @param scope The scope. It can be <code>page</code>, <code>request</code>
  119        * , <code>session</code>, <code>application</code>.
  120        * @since 2.2.0
  121        */
  122       public static void setAttribute(Context velocityContext,
  123               HttpServletRequest request, ServletContext servletContext,
  124               String name, Object obj, String scope) {
  125           if (scope == null) {
  126               scope = "page";
  127           }
  128           if ("page".equals(scope)) {
  129               velocityContext.put(name, obj);
  130           } else if ("request".equals(scope)) {
  131               request.setAttribute(name, obj);
  132           } else if ("session".equals(scope)) {
  133               request.getSession().setAttribute(name, obj);
  134           } else if ("application".equals(scope)) {
  135               servletContext.setAttribute(name, obj);
  136           }
  137       }
  138   }

Home » tiles-2.2.1-src » org.apache.tiles.velocity.context » [javadoc | source]