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 try
125 {
126 executeReport( locale );
127 }
128 catch ( MavenReportException e )
129 {
130 if ( failOnError )
131 {
132 throw e;
133 }
134 getLog().error( "Error while creating javadoc report: " + e.getMessage(), e );
135 }
136 catch ( RuntimeException e )
137 {
138 if ( failOnError )
139 {
140 throw e;
141 }
142 getLog().error( "Error while creating javadoc report: " + e.getMessage(), e );
143 }
144 }
145
146
147 public String getOutputName()
148 {
149 return destDir + "/index";
150 }
151
152
153 public boolean isExternalReport()
154 {
155 return true;
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 public boolean canGenerateReport()
223 {
224 boolean canGenerate = false;
225
226 if ( !this.isAggregator() || ( this.isAggregator() && this.project.isExecutionRoot() ) )
227 {
228 List<String> sourcePaths;
229 List<String> files;
230 try
231 {
232 sourcePaths = getSourcePaths();
233 files = getFiles( sourcePaths );
234 }
235 catch ( MavenReportException e )
236 {
237 getLog().error( e.getMessage(), e );
238 return false;
239 }
240
241 canGenerate = canGenerateReport( files );
242 }
243 if ( getLog().isDebugEnabled() )
244 {
245 getLog().debug( " canGenerateReport " + canGenerate + " project " + this.project );
246 }
247 return canGenerate;
248 }
249
250
251 public String getCategoryName()
252 {
253 return CATEGORY_PROJECT_REPORTS;
254 }
255
256
257 public File getReportOutputDirectory()
258 {
259 if ( reportOutputDirectory == null )
260 {
261 return outputDirectory;
262 }
263
264 return reportOutputDirectory;
265 }
266
267
268
269
270
271
272 public void setReportOutputDirectory( File reportOutputDirectory )
273 {
274 updateReportOutputDirectory( reportOutputDirectory, destDir );
275 }
276
277 public void setDestDir( String destDir )
278 {
279 this.destDir = destDir;
280 updateReportOutputDirectory( reportOutputDirectory, destDir );
281 }
282
283 private void updateReportOutputDirectory( File reportOutputDirectory, String destDir )
284 {
285 if ( reportOutputDirectory != null && destDir != null
286 && !reportOutputDirectory.getAbsolutePath().endsWith( destDir ) )
287 {
288 this.reportOutputDirectory = new File( reportOutputDirectory, destDir );
289 }
290 else
291 {
292 this.reportOutputDirectory = reportOutputDirectory;
293 }
294 }
295
296
297 public void execute()
298 throws MojoExecutionException, MojoFailureException
299 {
300 if ( skip )
301 {
302 getLog().info( "Skipping javadoc generation" );
303 return;
304 }
305
306 try
307 {
308 RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
309 SiteRendererSink sink = new SiteRendererSink( context );
310 Locale locale = Locale.getDefault();
311 generate( sink, locale );
312 }
313 catch ( MavenReportException e )
314 {
315 failOnError( "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation", e );
316 }
317 catch ( RuntimeException e )
318 {
319 failOnError( "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation", e );
320 }
321 }
322
323 @Override
324 protected boolean isAggregator()
325 {
326
327 return aggregate;
328 }
329
330
331
332
333
334
335
336 private ResourceBundle getBundle( Locale locale )
337 {
338 return ResourceBundle.getBundle( "javadoc-report", locale, getClass().getClassLoader() );
339 }
340 }