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