1 /*
2 * $Id: VelocityRendererBuilder.java 1066512 2011-02-02 16:13:31Z 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 package org.apache.tiles.request.velocity.render;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26
27 import org.apache.tiles.request.ApplicationContext;
28 import org.apache.velocity.tools.view.VelocityView;
29
30 /**
31 * Builds a {@link VelocityRenderer}.
32 *
33 * @version $Rev: 1066512 $ $Date: 2011-02-03 03:13:31 +1100 (Thu, 03 Feb 2011) $
34 */
35 public final class VelocityRendererBuilder {
36
37 /**
38 * The initialization parameters for VelocityView.
39 */
40 private Map<String, String> params = new HashMap<String, String>();
41
42 /**
43 * The application context.
44 */
45 private ApplicationContext applicationContext;
46
47 /**
48 * Constructor.
49 */
50 private VelocityRendererBuilder() {
51 }
52
53 /**
54 * Returns a new instance of the builder.
55 *
56 * @return A new builder.
57 */
58 public static VelocityRendererBuilder createInstance() {
59 return new VelocityRendererBuilder();
60 }
61
62 /**
63 * Sets a parameter for the internal servlet.
64 *
65 * @param key The name of the parameter.
66 * @param value The value of the parameter.
67 * @return This builder.
68 */
69 public VelocityRendererBuilder setParameter(String key, String value) {
70 params.put(key, value);
71 return this;
72 }
73
74 /**
75 * Sets the application context.
76 *
77 * @param applicationContext The application context.
78 * @return This builder.
79 */
80 public VelocityRendererBuilder setApplicationContext(ApplicationContext applicationContext) {
81 this.applicationContext = applicationContext;
82 return this;
83 }
84
85 /**
86 * Creates the Velocity renderer.
87 *
88 * @return The Velocity renderer.
89 */
90 public VelocityRenderer build() {
91 VelocityView velocityView = new VelocityView(
92 new ApplicationContextJeeConfig(applicationContext, params));
93 return new VelocityRenderer(velocityView);
94 }
95 }