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.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.SortedSet;
26 import java.util.TreeSet;
27 import org.apache.velocity.tools.ToolboxFactory;
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 public class FactoryConfiguration
66 extends CompoundConfiguration<ToolboxConfiguration>
67 {
68 private final SortedSet<Data> data = new TreeSet<Data>();
69 private final List<String> sources = new ArrayList<String>();
70
71 public FactoryConfiguration()
72 {
73 this("");
74 }
75
76
77
78
79
80 public FactoryConfiguration(String source)
81 {
82 this(FactoryConfiguration.class, source);
83 }
84
85
86
87
88 protected FactoryConfiguration(Class clazz, String source)
89 {
90 addSource(clazz.getName()+"("+source+")");
91 }
92
93
94
95
96 public String getSource()
97 {
98 return this.sources.get(0);
99 }
100
101
102
103
104
105 public void setSource(String source)
106 {
107 this.sources.set(0, source);
108 }
109
110
111
112
113
114
115 public List<String> getSources()
116 {
117 return this.sources;
118 }
119
120 public void addSource(String source)
121 {
122 this.sources.add(source);
123 }
124
125 public void addData(Data newDatum)
126 {
127
128 Data datum = getData(newDatum);
129 if (datum != null)
130 {
131
132
133 removeData(datum);
134 }
135
136 data.add(newDatum);
137 }
138
139 public boolean removeData(Data datum)
140 {
141 return data.remove(datum);
142 }
143
144 public Data getData(String key)
145 {
146
147 Data findme = new Data();
148 findme.setKey(key);
149 return getData(findme);
150 }
151
152 public Data getData(Data findme)
153 {
154 for (Data datum : data)
155 {
156 if (datum.equals(findme))
157 {
158 return datum;
159 }
160 }
161 return null;
162 }
163
164 public boolean hasData()
165 {
166 return !data.isEmpty();
167 }
168
169 public SortedSet<Data> getData()
170 {
171 return data;
172 }
173
174 public void setData(Collection<Data> data)
175 {
176 for (Data datum : data)
177 {
178 addData(datum);
179 }
180 }
181
182 public void addToolbox(ToolboxConfiguration toolbox)
183 {
184 addChild(toolbox);
185 }
186
187 public void removeToolbox(ToolboxConfiguration toolbox)
188 {
189 removeChild(toolbox);
190 }
191
192 public ToolboxConfiguration getToolbox(String scope)
193 {
194 for (ToolboxConfiguration toolbox : getToolboxes())
195 {
196 if (scope.equals(toolbox.getScope()))
197 {
198 return toolbox;
199 }
200 }
201 return null;
202 }
203
204 public Collection<ToolboxConfiguration> getToolboxes()
205 {
206 return getChildren();
207 }
208
209 public void setToolboxes(Collection<ToolboxConfiguration> toolboxes)
210 {
211 setChildren(toolboxes);
212 }
213
214 public void addConfiguration(FactoryConfiguration config)
215 {
216
217 setData(config.getData());
218
219
220 for (String source : config.getSources())
221 {
222 addSource(source);
223 }
224
225
226 super.addConfiguration(config);
227 }
228
229 @Override
230 public void validate()
231 {
232 super.validate();
233
234 for (Data datum : data)
235 {
236 datum.validate();
237 }
238 }
239
240
241
242
243
244
245
246
247
248
249 @Override
250 public boolean equals(Object o)
251 {
252 if (o instanceof FactoryConfiguration)
253 {
254 FactoryConfiguration that = (FactoryConfiguration)o;
255 return that.toString(false).equals(this.toString(false));
256 }
257 return false;
258 }
259
260 @Override
261 public int hashCode()
262 {
263 return toString(false).hashCode();
264 }
265
266 @Override
267 public String toString()
268 {
269 return toString(true);
270 }
271
272 public String toString(boolean includeSources)
273 {
274 StringBuilder out = new StringBuilder();
275 out.append("\nFactoryConfiguration from ");
276 if (includeSources)
277 {
278 out.append(getSources().size());
279 out.append(" sources ");
280 }
281 appendProperties(out);
282 if (hasData())
283 {
284 out.append("including ");
285 out.append(data.size());
286 out.append(" data");
287 }
288 if (getToolboxes().isEmpty())
289 {
290 out.append("\n ");
291 }
292 else
293 {
294 appendChildren(out, "toolboxes: \n ", "\n ");
295 }
296 if (hasData())
297 {
298 for (Data datum : data)
299 {
300 out.append(datum);
301 out.append("\n ");
302 }
303 }
304 if (includeSources)
305 {
306 int count = 0;
307 for (String source : getSources())
308 {
309 out.append("\n Source ");
310 out.append(count++);
311 out.append(": ");
312 out.append(source);
313 }
314 out.append("\n");
315 }
316 return out.toString();
317 }
318
319
320 public ToolboxFactory createFactory()
321 {
322 ToolboxFactory factory = new ToolboxFactory();
323 factory.configure(this);
324 return factory;
325 }
326
327 }