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