1 | |
package org.apache.maven.plugin.checkstyle; |
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 | |
|
53 | |
|
54 | |
|
55 | |
public class VelocityTemplate |
56 | |
{ |
57 | |
private String templateDirectory; |
58 | |
|
59 | |
private Log log; |
60 | |
|
61 | |
private VelocityComponent velocity; |
62 | |
|
63 | |
public VelocityTemplate( VelocityComponent velocityComponent, String templateBaseDirectory ) |
64 | 7 | { |
65 | 7 | this.velocity = velocityComponent; |
66 | 7 | this.templateDirectory = templateBaseDirectory; |
67 | 7 | } |
68 | |
|
69 | |
public String getTemplateDirectory() |
70 | |
{ |
71 | 0 | return templateDirectory; |
72 | |
} |
73 | |
|
74 | |
public VelocityComponent getVelocity() |
75 | |
{ |
76 | 7 | return velocity; |
77 | |
} |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public void generate( String outputFilename, String template, Context context ) |
90 | |
throws VelocityException, MojoExecutionException, IOException |
91 | |
{ |
92 | |
File f; |
93 | |
|
94 | |
try |
95 | |
{ |
96 | 7 | f = new File( outputFilename ); |
97 | |
|
98 | 7 | if ( !f.getParentFile().exists() ) |
99 | |
{ |
100 | 0 | f.getParentFile().mkdirs(); |
101 | |
} |
102 | |
|
103 | 7 | Writer writer = new FileWriter( f ); |
104 | |
|
105 | 7 | getVelocity().getEngine().mergeTemplate( templateDirectory + "/" + template, context, writer ); |
106 | |
|
107 | 7 | writer.flush(); |
108 | 7 | writer.close(); |
109 | |
|
110 | 7 | getLog().debug( "File " + outputFilename + " created..." ); |
111 | |
} |
112 | |
|
113 | 0 | catch ( ResourceNotFoundException e ) |
114 | |
{ |
115 | 0 | throw new ResourceNotFoundException( "Template not found: " + templateDirectory + "/" + template ); |
116 | |
} |
117 | 0 | catch ( VelocityException e ) |
118 | |
{ |
119 | 0 | throw e; |
120 | |
} |
121 | 0 | catch ( IOException e ) |
122 | |
{ |
123 | 0 | throw e; |
124 | |
} |
125 | 0 | catch ( Exception e ) |
126 | |
{ |
127 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
128 | 7 | } |
129 | 7 | } |
130 | |
|
131 | |
public void setTemplateDirectory( String templateDirectory ) |
132 | |
{ |
133 | 0 | this.templateDirectory = templateDirectory; |
134 | 0 | } |
135 | |
|
136 | |
public void setVelocity( VelocityComponent velocity ) |
137 | |
{ |
138 | 0 | this.velocity = velocity; |
139 | 0 | } |
140 | |
|
141 | |
public Log getLog() |
142 | |
{ |
143 | 7 | if ( this.log == null ) |
144 | |
{ |
145 | 0 | this.log = new SystemStreamLog(); |
146 | |
} |
147 | 7 | return log; |
148 | |
} |
149 | |
|
150 | |
public void setLog( Log log ) |
151 | |
{ |
152 | 7 | this.log = log; |
153 | 7 | } |
154 | |
|
155 | |
} |