1/*2 * $Id: VelocityUtil.java 902965 2010-01-25 20:12:46Z apetrelli $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122package org.apache.tiles.velocity.context;
2324import java.io.IOException;
25import java.io.StringWriter;
26import java.io.Writer;
27import java.util.Map;
2829import javax.servlet.ServletContext;
30import javax.servlet.http.HttpServletRequest;
3132import org.apache.tiles.ArrayStack;
33import org.apache.velocity.context.Context;
34import org.apache.velocity.context.InternalContextAdapter;
35import org.apache.velocity.runtime.Renderable;
36import org.apache.velocity.runtime.parser.node.ASTBlock;
37import org.apache.velocity.runtime.parser.node.ASTMap;
38import org.apache.velocity.runtime.parser.node.Node;
3940/***41 * Utilities for Velocity usage in Tiles.42 *43 * @version $Rev: 902965 $ $Date: 2010-01-25 21:12:46 +0100 (lun, 25 gen 2010) $44 * @since 2.2.045 */46publicfinalclassVelocityUtil {
4748/***49 * A renderable object that does not render anything.50 *51 * @since 2.2.052 */53publicstaticfinal Renderable EMPTY_RENDERABLE;
5455static {
56 EMPTY_RENDERABLE = new Renderable() {
5758 @Override
59public String toString() {
60return"";
61 }
6263publicboolean render(InternalContextAdapter context, Writer writer) {
64// Does nothing, really!65returntrue;
66 }
67 };
68 }
6970/***71 * The attribute key that will be used to store the parameter map, to use across Velocity tool calls.72 *73 * @since 2.2.074 */75privatestaticfinal String PARAMETER_MAP_STACK_KEY = "org.apache.tiles.velocity.PARAMETER_MAP_STACK";
7677/***78 * Private constructor to avoid instantiation.79 */80privateVelocityUtil() {
81 }
8283/***84 * Null-safe conversion from Boolean to boolean.85 *86 * @param obj The Boolean object.87 * @param defaultValue This value will be returned if <code>obj</code> is null.88 * @return The boolean value of <code>obj</code> or, if null, <code>defaultValue</code>.89 * @since 2.2.090 */91publicstaticboolean toSimpleBoolean(Boolean obj, boolean defaultValue) {
92return obj != null ? obj : defaultValue;
93 }
9495/***96 * Returns or creates the parameter stack to use. It is useful to store parameters across tool calls.97 *98 * @param context The Velocity context.99 * @return The parameter stack.100 * @since 2.2.0101 * @deprecated Use Velocity directives.102 */103 @Deprecated
104 @SuppressWarnings("unchecked")
105publicstatic ArrayStack<Map<String, Object>> getParameterStack(Context context) {
106 ArrayStack<Map<String, Object>> stack = (ArrayStack<Map<String, Object>>) context
107 .get(PARAMETER_MAP_STACK_KEY);
108if (stack == null) {
109 stack = new ArrayStack<Map<String, Object>>();
110 context.put(PARAMETER_MAP_STACK_KEY, stack);
111 }
112return stack;
113 }
114115/***116 * Sets an attribute in the desired scope.117 *118 * @param velocityContext The Velocity context.119 * @param request The HTTP request.120 * @param servletContext The servlet context.121 * @param name The name of the attribute.122 * @param obj The value of the attribute.123 * @param scope The scope. It can be <code>page</code>, <code>request</code>124 * , <code>session</code>, <code>application</code>.125 * @since 2.2.0126 */127publicstaticvoid setAttribute(Context velocityContext,
128 HttpServletRequest request, ServletContext servletContext,
129 String name, Object obj, String scope) {
130if (scope == null) {
131 scope = "page";
132 }
133if ("page".equals(scope)) {
134 velocityContext.put(name, obj);
135 } elseif ("request".equals(scope)) {
136 request.setAttribute(name, obj);
137 } elseif ("session".equals(scope)) {
138 request.getSession().setAttribute(name, obj);
139 } elseif ("application".equals(scope)) {
140 servletContext.setAttribute(name, obj);
141 }
142 }
143144/***145 * Evaluates the body (child node at position 1) and returns it as a string.146 *147 * @param context The Velocity context.148 * @param node The node to use.149 * @return The evaluated body.150 * @throws IOException If something goes wrong.151 * @since 2.2.2152 */153publicstatic String getBodyAsString(InternalContextAdapter context, Node node)
154 throws IOException {
155 ASTBlock block = (ASTBlock) node.jjtGetChild(1);
156 StringWriter stringWriter = new StringWriter();
157 block.render(context, stringWriter);
158 stringWriter.close();
159 String body = stringWriter.toString();
160if (body != null) {
161 body = body.replaceAll("^//s*|//s*$", "");
162if (body.length() <= 0) {
163 body = null;
164 }
165 }
166return body;
167 }
168169/***170 * Evaluates the body writing in the passed writer.171 *172 * @param context The Velocity context.173 * @param writer The writer to write into.174 * @param node The node to use.175 * @throws IOException If something goes wrong.176 * @since 2.2.2177 */178publicstaticvoid evaluateBody(InternalContextAdapter context, Writer writer,
179 Node node) throws IOException {
180 ASTBlock block = (ASTBlock) node.jjtGetChild(1);
181 block.render(context, writer);
182 }
183184/***185 * Extracts the parameters from the directives, by getting the child at186 * position 0 supposing it is a map.187 *188 * @param context The Velocity context.189 * @param node The node to use.190 * @return The extracted parameters.191 * @since 2.2.2192 */193 @SuppressWarnings("unchecked")
194publicstatic Map<String, Object> getParameters(InternalContextAdapter context,
195 Node node) {
196 ASTMap astMap = (ASTMap) node.jjtGetChild(0);
197 Map<String, Object> params = (Map<String, Object>) astMap
198 .value(context);
199return params;
200 }
201 }