1 package org.apache.velocity.tools;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import org.apache.velocity.app.VelocityEngine;
29 import org.apache.velocity.context.Context;
30
31
32
33
34
35
36
37
38
39
40 public class ToolContext implements Context
41 {
42 public static final String PATH_KEY = "requestPath";
43 public static final String CONTEXT_KEY = "velocityContext";
44 public static final String ENGINE_KEY = "velocityEngine";
45 public static final String LOCALE_KEY = "locale";
46 public static final String LOG_KEY = "log";
47 public static final String CATCH_EXCEPTIONS_KEY = "catchExceptions";
48
49 private List<Toolbox> toolboxes = new ArrayList<Toolbox>();
50
51
52 private Map<String,Object> toolProps = new HashMap<String,Object>(12);
53
54 private Map<String,Object> localContext = new HashMap<String,Object>();
55 private boolean userOverwrite = true;
56
57 public ToolContext()
58 {
59
60 putToolProperty(CONTEXT_KEY, this);
61 }
62
63
64
65
66
67 public ToolContext(VelocityEngine engine)
68 {
69 this();
70
71 putVelocityEngine(engine);
72 }
73
74
75
76
77 public ToolContext(Map<String,Object> toolProps)
78 {
79 this();
80
81 if (toolProps != null)
82 {
83 this.toolProps.putAll(toolProps);
84 }
85 }
86
87
88
89
90
91
92
93 public void setUserCanOverwriteTools(boolean overwrite)
94 {
95 this.userOverwrite = overwrite;
96 }
97
98
99
100
101
102 public boolean getUserCanOverwriteTools()
103 {
104 return this.userOverwrite;
105 }
106
107 public void addToolbox(Toolbox toolbox)
108 {
109 toolboxes.add(toolbox);
110 }
111
112
113
114
115
116
117 public Map<String,Object> getToolbox()
118 {
119 Map<String,Object> aggregate = new HashMap<String,Object>();
120 Map<String,Object> toolProps = getToolProperties();
121 for (Toolbox toolbox : getToolboxes())
122 {
123 aggregate.putAll(toolbox.getAll(toolProps));
124 }
125 return aggregate;
126 }
127
128
129
130
131
132 public Map<String,Class> getToolClassMap()
133 {
134 Map<String,Class> toolClasses = new HashMap<String,Class>();
135
136
137 int n = getToolboxes().size();
138 for (int i = n - 1; i >= 0; i--)
139 {
140 Toolbox toolbox = getToolboxes().get(i);
141 toolClasses.putAll(toolbox.getToolClassMap());
142 }
143 return toolClasses;
144 }
145
146 protected List<Toolbox> getToolboxes()
147 {
148 return this.toolboxes;
149 }
150
151 protected Map<String,Object> getToolProperties()
152 {
153 return this.toolProps;
154 }
155
156
157
158
159
160
161
162
163 public void putVelocityEngine(VelocityEngine engine)
164 {
165
166 putToolProperty(ENGINE_KEY, engine);
167 putToolProperty(LOG_KEY, engine.getLog());
168
169
170
171 Object ehme =
172 engine.getProperty(VelocityEngine.EVENTHANDLER_METHODEXCEPTION);
173 if (ehme != null)
174 {
175 putToolProperty(CATCH_EXCEPTIONS_KEY, Boolean.FALSE);
176 }
177 }
178
179 public Object putToolProperty(String key, Object value)
180 {
181 return toolProps.put(key, value);
182 }
183
184 public void putToolProperties(Map<String,Object> props)
185 {
186 if (props != null)
187 {
188 for (Map.Entry<String,Object> prop : props.entrySet())
189 {
190 putToolProperty(prop.getKey(), prop.getValue());
191 }
192 }
193 }
194
195 public Object put(String key, Object value)
196 {
197 return localContext.put(key, value);
198 }
199
200 public Object get(String key)
201 {
202
203 Object value = userOverwrite ? internalGet(key) : findTool(key);
204 if (value == null)
205 {
206 value = userOverwrite ? findTool(key) : internalGet(key);
207 }
208 return value;
209 }
210
211 protected Object internalGet(String key)
212 {
213 return localContext.get(key);
214 }
215
216 protected Object findTool(String key)
217 {
218 String path = (String)toolProps.get(PATH_KEY);
219 for (Toolbox toolbox : getToolboxes())
220 {
221 Object tool = toolbox.get(key, path, toolProps);
222 if (tool != null)
223 {
224 return tool;
225 }
226 }
227 return null;
228 }
229
230
231 public Set<String> keySet()
232 {
233 Set<String> keys = new HashSet<String>();
234 for (Toolbox toolbox : getToolboxes())
235 {
236 keys.addAll(toolbox.getKeys());
237 }
238 keys.addAll(localContext.keySet());
239 return keys;
240 }
241
242 public boolean containsKey(Object key)
243 {
244 return keySet().contains(key);
245 }
246
247 public Object[] getKeys()
248 {
249 return keySet().toArray();
250 }
251
252 public Object remove(Object key)
253 {
254
255 return localContext.remove(key);
256 }
257
258 public void putAll(Map context)
259 {
260 localContext.putAll(context);
261 }
262 }