1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.exceptions;
17
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Locale;
22 import java.util.ResourceBundle;
23
24 import org.apache.maven.artifact.DependencyResolutionRequiredException;
25 import org.apache.maven.doxia.sink.Sink;
26 import org.apache.maven.plugin.logging.Log;
27 import org.apache.maven.reporting.MavenReportException;
28
29 import de.smartics.exceptions.report.ReportBuilder;
30 import de.smartics.exceptions.report.ReportConfiguration;
31 import de.smartics.exceptions.report.data.InMemoryExceptionCodesReport;
32 import de.smartics.exceptions.report.data.ProjectConfiguration;
33 import de.smartics.exceptions.report.data.ReportProblem;
34 import de.smartics.exceptions.report.generator.ReportGenerator;
35 import de.smartics.exceptions.report.utils.StringFunction;
36 import de.smartics.maven.exceptions.conf.ConfigUtils;
37 import de.smartics.maven.exceptions.conf.DefaultProjectConfiguration;
38 import de.smartics.maven.exceptions.runtime.ProjectClassLoader;
39 import de.smartics.maven.util.PathUtils;
40
41
42
43
44
45
46
47
48
49
50
51 public class ExceptionCodeReport extends AbstractElementReport
52 {
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 private String outputName;
75
76
77
78
79
80
81
82
83
84
85
86
87 private String additionalSources;
88
89
90
91
92
93
94
95 private String sourceEncoding;
96
97
98
99
100
101
102
103 private String sourceVersion;
104
105
106
107
108
109
110
111
112
113 @SuppressWarnings("rawtypes")
114 private ArrayList includes;
115
116
117
118
119
120
121
122
123
124 @SuppressWarnings("rawtypes")
125 private ArrayList excludes;
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 private String reportGenerator;
141
142
143
144
145
146
147
148 private String reportEncoding;
149
150
151
152
153
154
155
156
157 private boolean skip;
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 public String getOutputName()
181 {
182 return outputName;
183 }
184
185
186
187 @Override
188 public void executeReport(final Locale locale) throws MavenReportException
189 {
190 if (skip)
191 {
192 final Log log = getLog();
193 if (log.isInfoEnabled())
194 {
195 log.info("Report '" + getName(locale) + "' skipped.");
196 }
197 return;
198 }
199
200 try
201 {
202 final ProjectConfiguration<Sink> projectConfig =
203 createProjectConfiguration(locale);
204 final ReportConfiguration reportConfig =
205 createReportConfiguration(projectConfig);
206
207 final ReportBuilder builder = ReportBuilder.create(reportConfig);
208 final InMemoryExceptionCodesReport report =
209 new InMemoryExceptionCodesReport();
210 builder.reportTo(report);
211
212 if (report.hasProblems())
213 {
214 final String message = toString(report.getProblems());
215 getLog().error(message);
216 throw new MavenReportException(
217 "Problems encountered while generating report.\n"
218 + " Please refer to error log for details.");
219 }
220
221 final ReportGenerator<Sink> generator =
222 projectConfig.getReporterInstance();
223 generator.writeReport(projectConfig, report);
224 }
225 catch (final Exception e)
226 {
227 e.printStackTrace();
228 throw new MavenReportException(
229 "Cannot generate " + getName(locale) + ".", e);
230 }
231 }
232
233 private static String toString(final List<ReportProblem> problems)
234 {
235 final StringBuilder buffer = new StringBuilder(512);
236
237 for (final ReportProblem problem : problems)
238 {
239 buffer.append(problem).append('\n');
240 }
241
242 return buffer.toString();
243 }
244
245 @SuppressWarnings("unchecked")
246 private ProjectConfiguration<Sink> createProjectConfiguration(
247 final Locale locale) throws DependencyResolutionRequiredException
248 {
249 final List<String> classRootDirectoryNames =
250 project.getCompileClasspathElements();
251
252 final List<String> sourceRootDirectoryNames = new ArrayList<String>();
253 sourceRootDirectoryNames.addAll(StringFunction.split(
254 this.additionalSources, ","));
255 sourceRootDirectoryNames.addAll(project.getCompileSourceRoots());
256 sourceRootDirectoryNames.addAll(ConfigUtils
257 .discoverSourceJars(classRootDirectoryNames));
258 final List<String> toolClassPath = PathUtils.toClassPath(pluginArtifacts);
259
260 final Log log = getLog();
261 if (log.isDebugEnabled())
262 {
263 log.debug("Tool class path: " + toolClassPath);
264 log.debug("Class roots : " + classRootDirectoryNames);
265 log.debug("Source roots : " + sourceRootDirectoryNames);
266 }
267
268 final DefaultProjectConfiguration.Builder<Sink> builder =
269 new DefaultProjectConfiguration.Builder<Sink>(project.getName());
270
271 builder.setIncludes(includes);
272 builder.setExcludes(excludes);
273 builder.setSourceEncoding(this.sourceEncoding);
274 builder.setSourceVersion(this.sourceVersion);
275 builder.setToolClassPath(toolClassPath);
276 builder.setClassRootDirectoryNames(classRootDirectoryNames);
277 builder.setSourceRootDirectoryNames(sourceRootDirectoryNames);
278 builder.setJavadocDir(javadocDir);
279
280 builder.setReportEncoding(this.reportEncoding);
281 builder.setReporter(this.reportGenerator);
282 builder.setReport(new File(this.outputDirectory.getAbsolutePath(),
283 outputName).getPath());
284
285 final ResourceBundle bundle = getBundle(locale);
286 builder.setBundle(bundle);
287
288 final ProjectConfiguration<Sink> config = builder.build();
289
290 final Sink sink = getSink();
291 final ReportGenerator<Sink> generator = config.getReporterInstance();
292 generator.setOutput(sink);
293
294 return config;
295 }
296
297 private ReportConfiguration createReportConfiguration(
298 final ProjectConfiguration<Sink> projectConfig)
299 {
300 final ReportConfiguration reportConfig = new ReportConfiguration();
301 reportConfig.setEncoding(this.sourceEncoding);
302 final ProjectClassLoader classLoader =
303 new ProjectClassLoader(Thread.currentThread().getContextClassLoader(),
304 projectConfig.getClassRootDirectoryNames());
305 reportConfig.setProjectClassLoader(classLoader);
306 for (final String name : projectConfig.getSourceRootDirectoryNames())
307 {
308 final File file = new File(name);
309 reportConfig.addSourceTree(file);
310 }
311
312 reportConfig.addIncludes(projectConfig.getIncludes());
313 reportConfig.addExcludes(projectConfig.getExcludes());
314
315 return reportConfig;
316 }
317
318 @Override
319 protected String getBundleName()
320 {
321 return "de/smartics/exceptions/report/ExceptionCodeReportBundle";
322 }
323
324
325
326 }