1 package org.apache.velocity.tools.config;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.List;
23 import org.apache.velocity.tools.ToolboxFactory;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public class EasyFactoryConfiguration extends FactoryConfiguration
87 {
88 private boolean addedDefaults = false;
89 private EasyWrap<ToolboxConfiguration> toolbox;
90
91 public EasyFactoryConfiguration()
92 {
93 this(false);
94 }
95
96
97
98
99
100 public EasyFactoryConfiguration(boolean includeDefaults)
101 {
102
103 this(includeDefaults, String.valueOf(includeDefaults));
104 }
105
106
107
108
109
110
111
112 public EasyFactoryConfiguration(boolean includeDefaults, String source)
113 {
114 super(EasyFactoryConfiguration.class, source);
115
116 if (includeDefaults)
117 {
118 addDefaultTools();
119
120
121 List<String> sources = getSources();
122 String first = sources.remove(0);
123 sources.add(first);
124 }
125 }
126
127
128
129
130
131 public EasyFactoryConfiguration addDefaultTools()
132 {
133 if (!addedDefaults)
134 {
135 addConfiguration(ConfigurationUtils.getDefaultTools());
136 addedDefaults = true;
137 }
138 return this;
139 }
140
141 public EasyFactoryConfiguration autoLoad()
142 {
143 return autoLoad(true);
144 }
145
146 public EasyFactoryConfiguration autoLoad(boolean includeDefaults)
147 {
148 addConfiguration(ConfigurationUtils.getAutoLoaded(includeDefaults));
149 addedDefaults = true;
150 return this;
151 }
152
153 public EasyData data(String key, Object value)
154 {
155 Data datum = new Data();
156 datum.setKey(key);
157 datum.setValue(value);
158 addData(datum);
159 return new EasyData(datum, this);
160 }
161
162 public EasyFactoryConfiguration data(String key, String type, Object value)
163 {
164 EasyData datum = data(key, value);
165 datum.type(type);
166 return this;
167 }
168
169 protected EasyFactoryConfiguration data(String key, Data.Type type, Object value)
170 {
171 EasyData datum = data(key, value);
172 datum.type(type);
173 return this;
174 }
175
176 public EasyFactoryConfiguration string(String key, Object value)
177 {
178 return data(key, Data.Type.STRING, value);
179 }
180
181 public EasyFactoryConfiguration number(String key, Object value)
182 {
183 return data(key, Data.Type.NUMBER, value);
184 }
185
186 public EasyFactoryConfiguration bool(String key, Object value)
187 {
188 return data(key, Data.Type.BOOLEAN, value);
189 }
190
191 public EasyWrap<ToolboxConfiguration> toolbox(String scope)
192 {
193 ToolboxConfiguration toolbox = new ToolboxConfiguration();
194 toolbox.setScope(scope);
195 addToolbox(toolbox);
196 this.toolbox =
197 new EasyWrap<ToolboxConfiguration>(toolbox, this);
198 return this.toolbox;
199 }
200
201 public EasyWrap<ToolConfiguration> tool(String classname)
202 {
203 return tool(null, classname);
204 }
205
206 public EasyWrap<ToolConfiguration> tool(Class clazz)
207 {
208 return tool(null, clazz);
209 }
210
211 public EasyWrap<ToolConfiguration> tool(String key, String classname)
212 {
213 if (toolbox == null)
214 {
215 toolbox(ToolboxFactory.DEFAULT_SCOPE);
216 }
217 return toolbox.tool(key, classname);
218 }
219
220 public EasyWrap<ToolConfiguration> tool(String key, Class clazz)
221 {
222 return tool(key, clazz.getName());
223 }
224
225 public EasyFactoryConfiguration property(String name, Object value)
226 {
227 setProperty(name, value);
228 return this;
229 }
230
231
232 public static class EasyData
233 {
234 private final Data datum;
235 private final Configuration parent;
236
237 public EasyData(Data datum, Configuration parent)
238 {
239 this.datum = datum;
240 this.parent = parent;
241 }
242
243 public Data getData()
244 {
245 return this.datum;
246 }
247
248 public Configuration getParent()
249 {
250 return this.parent;
251 }
252
253 public EasyData type(String type)
254 {
255 this.datum.setType(type);
256 return this;
257 }
258
259 protected EasyData type(Data.Type type)
260 {
261 this.datum.setType(type);
262 return this;
263 }
264
265 public EasyData target(Class clazz)
266 {
267 this.datum.setTargetClass(clazz);
268 return this;
269 }
270
271 public EasyData classname(String classname)
272 {
273 this.datum.setClassname(classname);
274 return this;
275 }
276
277 public EasyData converter(String converter)
278 {
279 this.datum.setConverter(converter);
280 return this;
281 }
282
283 public EasyData converter(Class clazz)
284 {
285 this.datum.setConverter(clazz);
286 return this;
287 }
288 }
289
290
291 public class EasyWrap<C extends Configuration>
292 {
293 private final C config;
294 private final Configuration parent;
295
296 public EasyWrap(C config, Configuration parent)
297 {
298 this.config = config;
299 this.parent = parent;
300 }
301
302 public C getConfiguration()
303 {
304 return this.config;
305 }
306
307 public Configuration getParent()
308 {
309 return this.parent;
310 }
311
312 public EasyWrap<C> property(String name, Object value)
313 {
314 this.config.setProperty(name, value);
315 return this;
316 }
317
318 public EasyWrap<C> restrictTo(String path)
319 {
320 if (this.config instanceof ToolConfiguration)
321 {
322 ToolConfiguration tool = (ToolConfiguration)this.config;
323 tool.setRestrictTo(path);
324 return this;
325 }
326 else if (this.config instanceof ToolboxConfiguration)
327 {
328 ToolboxConfiguration toolbox = (ToolboxConfiguration)this.config;
329 for (ToolConfiguration tool : toolbox.getTools())
330 {
331 tool.setRestrictTo(path);
332 }
333 return this;
334 }
335 throw new IllegalStateException("Wrapping unknown "+Configuration.class.getName()+": "+getConfiguration());
336 }
337
338 public EasyWrap addDefaultTools()
339 {
340 EasyFactoryConfiguration.this.addDefaultTools();
341 return this;
342 }
343
344 public EasyWrap tool(Class clazz)
345 {
346 return tool(null, clazz);
347 }
348
349 public EasyWrap tool(String classname)
350 {
351 return tool(null, classname);
352 }
353
354 public EasyWrap tool(String key, Class clazz)
355 {
356 return tool(key, clazz.getName());
357 }
358
359 public EasyWrap tool(String key, String classname)
360 {
361 ToolConfiguration tool = new ToolConfiguration();
362 if (key != null)
363 {
364 tool.setKey(key);
365 }
366 tool.setClassname(classname);
367 if (this.config instanceof ToolConfiguration)
368 {
369 ToolboxConfiguration toolbox = (ToolboxConfiguration)getParent();
370 toolbox.addTool(tool);
371 return new EasyWrap(tool, toolbox);
372 }
373 else if (this.config instanceof ToolboxConfiguration)
374 {
375 ToolboxConfiguration toolbox = (ToolboxConfiguration)getConfiguration();
376 toolbox.addTool(tool);
377 return new EasyWrap(tool, toolbox);
378 }
379 throw new IllegalStateException("Wrapping unknown "+Configuration.class.getName()+": "+getConfiguration());
380 }
381 }
382
383 }