Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
VelocityRenderer |
|
| 2.3333333333333335;2.333 |
1 | /* | |
2 | * $Id: VelocityRenderer.java 1306435 2012-03-28 15:39:11Z nlebas $ | |
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.request.velocity.render; | |
23 | ||
24 | import java.io.IOException; | |
25 | ||
26 | import org.apache.tiles.request.Request; | |
27 | import org.apache.tiles.request.render.CannotRenderException; | |
28 | import org.apache.tiles.request.render.Renderer; | |
29 | import org.apache.tiles.request.servlet.ServletRequest; | |
30 | import org.apache.tiles.request.servlet.ServletUtil; | |
31 | import org.apache.velocity.Template; | |
32 | import org.apache.velocity.context.Context; | |
33 | import org.apache.velocity.tools.view.VelocityView; | |
34 | ||
35 | /** | |
36 | * Attribute renderer for rendering Velocity templates as attributes. <br> | |
37 | * It is available only to Servlet-based environment.<br> | |
38 | * It uses {@link VelocityView} to render the response.<br> | |
39 | * To initialize it correctly, call {@link #setParameter(String, String)} for | |
40 | * all the parameters that you want to set, and then call {@link #commit()}. | |
41 | * | |
42 | * @version $Rev: 1306435 $ $Date: 2012-03-29 02:39:11 +1100 (Thu, 29 Mar 2012) $ | |
43 | */ | |
44 | public class VelocityRenderer implements Renderer { | |
45 | ||
46 | /** | |
47 | * The VelocityView object to use. | |
48 | */ | |
49 | private VelocityView velocityView; | |
50 | ||
51 | /** | |
52 | * Constructor. | |
53 | * | |
54 | * @param velocityView The Velocity view manager. | |
55 | */ | |
56 | 3 | public VelocityRenderer(VelocityView velocityView) { |
57 | 3 | this.velocityView = velocityView; |
58 | 3 | } |
59 | ||
60 | /** {@inheritDoc} */ | |
61 | @Override | |
62 | public void render(String path, Request request) throws IOException { | |
63 | 2 | if (path == null) { |
64 | 1 | throw new CannotRenderException("Cannot dispatch a null path"); |
65 | } | |
66 | ||
67 | 1 | ServletRequest servletRequest = ServletUtil.getServletRequest(request); |
68 | // then get a context | |
69 | 1 | Context context = velocityView.createContext(servletRequest |
70 | .getRequest(), servletRequest.getResponse()); | |
71 | ||
72 | // get the template | |
73 | 1 | Template template = velocityView.getTemplate((String) path); |
74 | ||
75 | // merge the template and context into the writer | |
76 | 1 | velocityView.merge(template, context, request.getWriter()); |
77 | 1 | } |
78 | ||
79 | /** {@inheritDoc} */ | |
80 | public boolean isRenderable(String path, Request request) { | |
81 | 3 | return path != null && path.startsWith("/") && path.endsWith(".vm"); |
82 | } | |
83 | } |