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 private String name;
81
82
83
84
85
86
87
88 private String description;
89
90
91
92
93
94
95 public String getName( Locale locale )
96 {
97 if ( StringUtils.isEmpty( name ) )
98 {
99 return getBundle( locale ).getString( "report.javadoc.name" );
100 }
101
102 return name;
103 }
104
105
106 public String getDescription( Locale locale )
107 {
108 if ( StringUtils.isEmpty( description ) )
109 {
110 return getBundle( locale ).getString( "report.javadoc.description" );
111 }
112
113 return description;
114 }
115
116
117 public void generate( Sink sink, Locale locale )
118 throws MavenReportException
119 {
120 outputDirectory = getReportOutputDirectory();
121
122 executeReport( locale );
123 }
124
125
126 public String getOutputName()
127 {
128 return destDir + "/index";
129 }
130
131
132 public boolean isExternalReport()
133 {
134 return true;
135 }
136
137
138 public boolean canGenerateReport()
139 {
140 boolean canGenerate;
141 if ( aggregate != isAggregator() )
142 {
143 canGenerate = false;
144 }
145 else if ( isAggregator() && !project.isExecutionRoot() )
146 {
147 canGenerate = false;
148 }
149 else
150 {
151 List sourcePaths = getSourcePaths();
152
153 List files = getFiles( sourcePaths );
154
155 canGenerate = canGenerateReport( files );
156 }
157 return canGenerate;
158 }
159
160
161 public String getCategoryName()
162 {
163 return CATEGORY_PROJECT_REPORTS;
164 }
165
166
167 public File getReportOutputDirectory()
168 {
169 if ( reportOutputDirectory == null )
170 {
171 return outputDirectory;
172 }
173
174 return reportOutputDirectory;
175 }
176
177
178
179
180
181
182 public void setReportOutputDirectory( File reportOutputDirectory )
183 {
184 if ( ( reportOutputDirectory != null ) && ( !reportOutputDirectory.getAbsolutePath().endsWith( destDir ) ) )
185 {
186 this.reportOutputDirectory = new File( reportOutputDirectory, destDir );
187 }
188 else
189 {
190 this.reportOutputDirectory = reportOutputDirectory;
191 }
192 }
193
194
195 public void execute()
196 throws MojoExecutionException, MojoFailureException
197 {
198 if ( skip )
199 {
200 getLog().info( "Skipping javadoc generation" );
201 return;
202 }
203
204 try
205 {
206 RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
207 SiteRendererSink sink = new SiteRendererSink( context );
208 Locale locale = Locale.getDefault();
209 generate( sink, locale );
210 }
211 catch ( MavenReportException e )
212 {
213 if ( failOnError )
214 {
215 throw new MojoExecutionException( "An error has occurred in " + getName( Locale.ENGLISH )
216 + " report generation:" + e.getMessage(), e );
217 }
218
219 getLog().error( "An error has occurred in " + getName( Locale.ENGLISH )
220 + " report generation:" + e.getMessage(), e );
221 }
222 catch ( RuntimeException e )
223 {
224 if ( failOnError )
225 {
226 throw e;
227 }
228
229 getLog().error( e.getMessage(), e );
230 }
231 }
232
233
234 protected boolean isAggregator()
235 {
236
237 return aggregate;
238 }
239
240
241
242
243
244
245
246 private ResourceBundle getBundle( Locale locale )
247 {
248 return ResourceBundle.getBundle( "javadoc-report", locale, getClass().getClassLoader() );
249 }
250 }