1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dishevelled.codegen;
25
26 import java.io.File;
27 import java.io.FileWriter;
28 import java.io.IOException;
29
30 import java.util.Properties;
31
32 import org.apache.commons.lang.StringUtils;
33
34 import org.apache.commons.io.FileUtils;
35
36 import org.apache.velocity.Template;
37 import org.apache.velocity.VelocityContext;
38
39 import org.apache.velocity.app.Velocity;
40
41
42
43
44
45
46
47 public final class Codegen
48 {
49
50
51
52
53 private Codegen()
54 {
55
56 }
57
58
59
60
61
62
63
64
65
66 public static String readLicense(final String license)
67 {
68 String text = null;
69 try
70 {
71 text = FileUtils.readFileToString(new File(license));
72 }
73 catch (IOException e)
74 {
75
76 }
77 return text;
78 }
79
80
81
82
83
84
85
86 public static void generateSource(final InterfaceDescription id)
87 {
88 if (id == null)
89 {
90 throw new IllegalArgumentException("id must not be null");
91 }
92
93 FileWriter fw = null;
94 try
95 {
96 Properties p = new Properties();
97 p.setProperty("resource.loader", "class");
98 p.setProperty("class.resource.loader.class",
99 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
100
101 Velocity.init(p);
102
103 fw = new FileWriter(id.getUpper() + ".java");
104
105 VelocityContext context = new VelocityContext();
106 context.put("id", id);
107 context.put("CodegenUtils", new CodegenUtils());
108 context.put("StringUtils", new StringUtils());
109
110 Template template = Velocity.getTemplate("org/dishevelled/codegen/Interface.wm");
111 template.merge(context, fw);
112 }
113 catch (Exception e)
114 {
115 e.printStackTrace();
116 System.exit(1);
117 }
118 finally
119 {
120 try
121 {
122 fw.close();
123 }
124 catch (Exception e)
125 {
126
127 }
128 }
129 }
130
131
132
133
134
135
136
137 public static void generateAbstractUnitTest(final InterfaceDescription id)
138 {
139 if (id == null)
140 {
141 throw new IllegalArgumentException("id must not be null");
142 }
143
144 FileWriter fw = null;
145 try
146 {
147 Properties p = new Properties();
148 p.setProperty("resource.loader", "class");
149 p.setProperty("class.resource.loader.class",
150 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
151
152 Velocity.init(p);
153
154 fw = new FileWriter("Abstract" + id.getUpper() + "Test.java");
155
156 VelocityContext context = new VelocityContext();
157 context.put("id", id);
158 context.put("CodegenUtils", new CodegenUtils());
159 context.put("StringUtils", new StringUtils());
160
161 Template template = Velocity.getTemplate("org/dishevelled/codegen/AbstractInterfaceTest.wm");
162 template.merge(context, fw);
163 }
164 catch (Exception e)
165 {
166 e.printStackTrace();
167 System.exit(1);
168 }
169 finally
170 {
171 try
172 {
173 fw.close();
174 }
175 catch (Exception e)
176 {
177
178 }
179 }
180 }
181
182
183
184
185
186
187
188
189 public static void generateSource(final ClassDescription cd, final Style style)
190 {
191 if (cd == null)
192 {
193 throw new IllegalArgumentException("cd must not be null");
194 }
195 if (style == null)
196 {
197 throw new IllegalArgumentException("style must not be null");
198 }
199
200 FileWriter fw = null;
201 try
202 {
203 Properties p = new Properties();
204 p.setProperty("resource.loader", "class");
205 p.setProperty("class.resource.loader.class",
206 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
207
208 Velocity.init(p);
209
210 fw = new FileWriter(cd.getUpper() + ".java");
211
212 VelocityContext context = new VelocityContext();
213 context.put("cd", cd);
214 context.put("CodegenUtils", new CodegenUtils());
215 context.put("StringUtils", new StringUtils());
216
217 Template template = Velocity.getTemplate("org/dishevelled/codegen/" + style + ".wm");
218 template.merge(context, fw);
219 }
220 catch (Exception e)
221 {
222 e.printStackTrace();
223 System.exit(1);
224 }
225 finally
226 {
227 try
228 {
229 fw.close();
230 }
231 catch (Exception e)
232 {
233
234 }
235 }
236 }
237
238
239
240
241
242
243
244
245 public static void generateBuilderSource(final ClassDescription cd, final Style style)
246 {
247
248 if (cd == null)
249 {
250 throw new IllegalArgumentException("cd must not be null");
251 }
252 if (style == null)
253 {
254 throw new IllegalArgumentException("style must not be null");
255 }
256
257 FileWriter fw = null;
258 try
259 {
260 Properties p = new Properties();
261 p.setProperty("resource.loader", "class");
262 p.setProperty("class.resource.loader.class",
263 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
264
265 Velocity.init(p);
266
267 fw = new FileWriter(cd.getUpper() + "Builder.java");
268
269 VelocityContext context = new VelocityContext();
270 context.put("cd", cd);
271 context.put("CodegenUtils", new CodegenUtils());
272 context.put("StringUtils", new StringUtils());
273
274 Template template = Velocity.getTemplate("org/dishevelled/codegen/" + style + "Builder.wm");
275 template.merge(context, fw);
276 }
277 catch (Exception e)
278 {
279 e.printStackTrace();
280 System.exit(1);
281 }
282 finally
283 {
284 try
285 {
286 fw.close();
287 }
288 catch (Exception e)
289 {
290
291 }
292 }
293 }
294
295
296
297
298
299
300
301
302 public static void generateUnitTest(final ClassDescription cd, final Style style)
303 {
304 if (cd == null)
305 {
306 throw new IllegalArgumentException("cd must not be null");
307 }
308 if (style == null)
309 {
310 throw new IllegalArgumentException("style must not be null");
311 }
312
313 FileWriter fw = null;
314 try
315 {
316 Properties p = new Properties();
317 p.setProperty("resource.loader", "class");
318 p.setProperty("class.resource.loader.class",
319 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
320
321 Velocity.init(p);
322
323 fw = new FileWriter(cd.getUpper() + "Test.java");
324
325 VelocityContext context = new VelocityContext();
326 context.put("cd", cd);
327 context.put("CodegenUtils", new CodegenUtils());
328 context.put("StringUtils", new StringUtils());
329
330 Template template = Velocity.getTemplate("org/dishevelled/codegen/" + style + "Test.wm");
331 template.merge(context, fw);
332 }
333 catch (Exception e)
334 {
335 e.printStackTrace();
336 System.exit(1);
337 }
338 finally
339 {
340 try
341 {
342 fw.close();
343 }
344 catch (Exception e)
345 {
346
347 }
348 }
349 }
350
351
352
353
354
355
356 public static void generateEnum(final ClassDescription cd)
357 {
358 if (cd == null)
359 {
360 throw new IllegalArgumentException("cd must not be null");
361 }
362
363 FileWriter fw = null;
364 try
365 {
366 Properties p = new Properties();
367 p.setProperty("resource.loader", "class");
368 p.setProperty("class.resource.loader.class",
369 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
370
371 Velocity.init(p);
372
373 fw = new FileWriter(cd.getUpper() + ".java");
374
375 VelocityContext context = new VelocityContext();
376 context.put("cd", cd);
377 context.put("CodegenUtils", new CodegenUtils());
378 context.put("StringUtils", new StringUtils());
379
380 Template template = Velocity.getTemplate("org/dishevelled/codegen/Enum.wm");
381 template.merge(context, fw);
382 }
383 catch (Exception e)
384 {
385 e.printStackTrace();
386 System.exit(1);
387 }
388 finally
389 {
390 try
391 {
392 fw.close();
393 }
394 catch (Exception e)
395 {
396
397 }
398 }
399 }
400
401
402
403
404
405
406 public static void generateEnumWithLookup(final ClassDescription cd)
407 {
408 if (cd == null)
409 {
410 throw new IllegalArgumentException("cd must not be null");
411 }
412
413 FileWriter fw = null;
414 try
415 {
416 Properties p = new Properties();
417 p.setProperty("resource.loader", "class");
418 p.setProperty("class.resource.loader.class",
419 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
420
421 Velocity.init(p);
422
423 fw = new FileWriter(cd.getUpper() + ".java");
424
425 VelocityContext context = new VelocityContext();
426 context.put("cd", cd);
427 context.put("CodegenUtils", new CodegenUtils());
428 context.put("StringUtils", new StringUtils());
429
430 Template template = Velocity.getTemplate("org/dishevelled/codegen/EnumWithLookup.wm");
431 template.merge(context, fw);
432 }
433 catch (Exception e)
434 {
435 e.printStackTrace();
436 System.exit(1);
437 }
438 finally
439 {
440 try
441 {
442 fw.close();
443 }
444 catch (Exception e)
445 {
446
447 }
448 }
449 }
450 }