1 package org.apache.maven.plugins.checkstyle.rss;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.plugin.logging.Log;
24 import org.apache.maven.plugin.logging.SystemStreamLog;
25 import org.apache.velocity.context.Context;
26 import org.apache.velocity.exception.ResourceNotFoundException;
27 import org.apache.velocity.exception.VelocityException;
28 import org.codehaus.plexus.velocity.VelocityComponent;
29
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.io.IOException;
33 import java.io.Writer;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class VelocityTemplate
53 {
54 private String templateDirectory;
55
56 private Log log;
57
58 private VelocityComponent velocity;
59
60 public VelocityTemplate( VelocityComponent velocityComponent, String templateBaseDirectory )
61 {
62 this.velocity = velocityComponent;
63 this.templateDirectory = templateBaseDirectory;
64 }
65
66 public String getTemplateDirectory()
67 {
68 return templateDirectory;
69 }
70
71 public VelocityComponent getVelocity()
72 {
73 return velocity;
74 }
75
76
77
78
79
80
81
82
83
84
85
86 public void generate( String outputFilename, String template, Context context )
87 throws VelocityException, MojoExecutionException, IOException
88 {
89 Writer writer = null;
90
91 try
92 {
93 File f = new File( outputFilename );
94
95 if ( !f.getParentFile().exists() )
96 {
97 f.getParentFile().mkdirs();
98 }
99
100 writer = new FileWriter( f );
101
102 getVelocity().getEngine().mergeTemplate( templateDirectory + "/" + template, context, writer );
103 }
104 catch ( ResourceNotFoundException e )
105 {
106 throw new ResourceNotFoundException( "Template not found: " + templateDirectory + "/" + template, e );
107 }
108 catch ( VelocityException | IOException e )
109 {
110 throw e;
111 }
112 catch ( Exception e )
113 {
114 throw new MojoExecutionException( e.getMessage(), e );
115 }
116 finally
117 {
118 if ( writer != null )
119 {
120 writer.flush();
121 writer.close();
122
123 getLog().debug( "File " + outputFilename + " created..." );
124 }
125 }
126 }
127
128 public void setTemplateDirectory( String templateDirectory )
129 {
130 this.templateDirectory = templateDirectory;
131 }
132
133 public void setVelocity( VelocityComponent velocity )
134 {
135 this.velocity = velocity;
136 }
137
138 public Log getLog()
139 {
140 if ( this.log == null )
141 {
142 this.log = new SystemStreamLog();
143 }
144 return log;
145 }
146
147 public void setLog( Log log )
148 {
149 this.log = log;
150 }
151
152 }