1 package org.apache.maven.plugin.javadoc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.List;
24 import java.util.Locale;
25 import java.util.ResourceBundle;
26
27 import org.apache.maven.doxia.module.xhtml.decoration.render.RenderingContext;
28 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.apache.maven.reporting.MavenReport;
32 import org.apache.maven.reporting.MavenReportException;
33 import org.codehaus.doxia.sink.Sink;
34 import org.codehaus.plexus.util.StringUtils;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class JavadocReport
50 extends AbstractJavadocMojo
51 implements MavenReport
52 {
53
54
55
56
57
58
59
60
61
62
63 private File reportOutputDirectory;
64
65
66
67
68
69
70
71
72 private String destDir;
73
74
75
76
77
78
79
80
81 private String name;
82
83
84
85
86
87
88
89
90 private String description;
91
92
93
94
95
96
97 public String getName( Locale locale )
98 {
99 if ( StringUtils.isEmpty( name ) )
100 {
101 return getBundle( locale ).getString( "report.javadoc.name" );
102 }
103
104 return name;
105 }
106
107
108 public String getDescription( Locale locale )
109 {
110 if ( StringUtils.isEmpty( description ) )
111 {
112 return getBundle( locale ).getString( "report.javadoc.description" );
113 }
114
115 return description;
116 }
117
118
119 public void generate( Sink sink, Locale locale )
120 throws MavenReportException
121 {
122 outputDirectory = getReportOutputDirectory();
123
124 executeReport( locale );
125 }
126
127
128 public String getOutputName()
129 {
130 return destDir + "/index";
131 }
132
133
134 public boolean isExternalReport()
135 {
136 return true;
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 public boolean canGenerateReport()
204 {
205 boolean canGenerate = false;
206
207 if ( !this.isAggregator() || ( this.isAggregator() && this.project.isExecutionRoot() ) )
208 {
209 List sourcePaths = getSourcePaths();
210
211 List files = getFiles( sourcePaths );
212
213 canGenerate = canGenerateReport( files );
214 }
215 if ( getLog().isDebugEnabled() )
216 {
217 getLog().debug( " canGenerateReport " + canGenerate + " project " + this.project );
218 }
219 return canGenerate;
220 }
221
222
223 public String getCategoryName()
224 {
225 return CATEGORY_PROJECT_REPORTS;
226 }
227
228
229 public File getReportOutputDirectory()
230 {
231 if ( reportOutputDirectory == null )
232 {
233 return outputDirectory;
234 }
235
236 return reportOutputDirectory;
237 }
238
239
240
241
242
243
244 public void setReportOutputDirectory( File reportOutputDirectory )
245 {
246 updateReportOutputDirectory( reportOutputDirectory, destDir );
247 }
248
249 public void setDestDir( String destDir )
250 {
251 this.destDir = destDir;
252 updateReportOutputDirectory( reportOutputDirectory, destDir );
253 }
254
255 private void updateReportOutputDirectory( File reportOutputDirectory, String destDir )
256 {
257 if ( reportOutputDirectory != null && destDir != null
258 && !reportOutputDirectory.getAbsolutePath().endsWith( destDir ) )
259 {
260 this.reportOutputDirectory = new File( reportOutputDirectory, destDir );
261 }
262 else
263 {
264 this.reportOutputDirectory = reportOutputDirectory;
265 }
266 }
267
268
269 public void execute()
270 throws MojoExecutionException, MojoFailureException
271 {
272 if ( skip )
273 {
274 getLog().info( "Skipping javadoc generation" );
275 return;
276 }
277
278 try
279 {
280 RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
281 SiteRendererSink sink = new SiteRendererSink( context );
282 Locale locale = Locale.getDefault();
283 generate( sink, locale );
284 }
285 catch ( MavenReportException e )
286 {
287 if ( failOnError )
288 {
289 throw new MojoExecutionException( "An error has occurred in " + getName( Locale.ENGLISH )
290 + " report generation:" + e.getMessage(), e );
291 }
292
293 getLog().error( "An error has occurred in " + getName( Locale.ENGLISH )
294 + " report generation:" + e.getMessage(), e );
295 }
296 catch ( RuntimeException e )
297 {
298 if ( failOnError )
299 {
300 throw e;
301 }
302
303 getLog().error( e.getMessage(), e );
304 }
305 }
306
307
308 protected boolean isAggregator()
309 {
310
311 return aggregate;
312 }
313
314
315
316
317
318
319
320 private ResourceBundle getBundle( Locale locale )
321 {
322 return ResourceBundle.getBundle( "javadoc-report", locale, getClass().getClassLoader() );
323 }
324 }