1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.jdtaus.mojo.resource.util;
22
23 import java.io.Writer;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.Properties;
27 import org.apache.velocity.VelocityContext;
28 import org.apache.velocity.app.VelocityEngine;
29 import org.jdtaus.mojo.resource.model.Implementation;
30 import org.jdtaus.mojo.resource.model.ModelManager;
31 import org.jdtaus.mojo.resource.model.Module;
32
33
34
35
36
37
38
39
40
41 public class BundleGenerator
42 {
43
44
45 private static final String GENERATOR_NAME =
46 BundleGenerator.class.getName();
47
48
49 private static final String GENERATOR_VERSION = "3.3";
50
51
52 private static final String VELOCITY_RESOURCE_LOADER =
53 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader";
54
55
56 private static final String TEMPLATE_LOCATION =
57 "META-INF/templates/Bundle.java.vm";
58
59
60 private VelocityEngine velocityEngine;
61
62
63
64
65
66 private ModelManager modelManager;
67
68
69
70
71
72
73
74
75
76
77
78 public void generateJava( final Module module,
79 final Implementation implementation,
80 final Writer writer )
81 throws Exception
82 {
83 final VelocityContext ctx = new VelocityContext();
84 ctx.put( "module", module );
85 ctx.put( "implementation", implementation );
86 ctx.put( "modelManager", this.modelManager );
87 ctx.put( "generatorName", GENERATOR_NAME );
88 ctx.put( "generatorVersion", GENERATOR_VERSION );
89 ctx.put( "templateLocation", TEMPLATE_LOCATION );
90 ctx.put( "comment", Boolean.TRUE );
91 ctx.put( "now", new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ).
92 format( new Date() ) );
93
94 this.getVelocity().mergeTemplate(
95 TEMPLATE_LOCATION, "UTF-8", ctx, writer );
96
97 }
98
99
100
101
102
103
104
105
106 private VelocityEngine getVelocity() throws Exception
107 {
108 if ( this.velocityEngine == null )
109 {
110 final VelocityEngine engine = new VelocityEngine();
111 final Properties props = new Properties();
112 props.put( "resource.loader", "class" );
113 props.put( "class.resource.loader.class",
114 VELOCITY_RESOURCE_LOADER );
115
116 engine.init( props );
117 this.velocityEngine = engine;
118 }
119
120 return this.velocityEngine;
121 }
122 }