1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.exceptions.cli;
17
18 import java.io.BufferedWriter;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.OutputStreamWriter;
22 import java.io.Writer;
23 import java.util.List;
24 import java.util.Locale;
25 import java.util.ResourceBundle;
26
27 import javax.xml.stream.XMLStreamWriter;
28
29 import org.apache.commons.cli.CommandLine;
30 import org.apache.commons.cli.CommandLineParser;
31 import org.apache.commons.cli.HelpFormatter;
32 import org.apache.commons.cli.OptionBuilder;
33 import org.apache.commons.cli.Options;
34 import org.apache.commons.cli.ParseException;
35 import org.apache.commons.cli.PosixParser;
36 import org.apache.commons.io.IOUtils;
37 import org.apache.commons.lang.StringUtils;
38
39 import de.smartics.analysis.javadoc.Filter;
40 import de.smartics.analysis.javadoc.filter.ImplementsInterfaceFilter;
41 import de.smartics.analysis.javadoc.util.StringFunction;
42 import de.smartics.exceptions.core.Code;
43 import de.smartics.report.ReportException;
44 import de.smartics.report.conf.DefaultProjectConfiguration;
45 import de.smartics.report.conf.ProjectConfiguration;
46 import de.smartics.report.generator.AbstractXmlReportGenerator;
47 import de.smartics.report.launch.ReportLauncher;
48
49
50
51
52
53
54
55 public class Main
56 {
57
58
59
60
61
62
63
64
65
66 public static final String PROJECT_NAME = "project-name";
67
68
69
70
71
72
73 public static final String REPORT_FILE = "report-file";
74
75
76
77
78
79
80 public static final String REPORT_GENERATOR = "report-generator";
81
82
83
84
85
86
87 public static final String REPORT_ENCODING = "report-encoding";
88
89
90
91
92
93
94 public static final String REPORT_EXCLUDES = "report-excludes";
95
96
97
98
99
100
101 public static final String REPORT_INCLUDES = "report-includes";
102
103
104
105
106
107
108 public static final String JAVA_SOURCE_ENCODING = "java-source-encoding";
109
110
111
112
113
114
115 public static final String JAVA_SOURCE_PATH = "java-source-path";
116
117
118
119
120
121
122
123
124 public static final String JAVA_CLASS_PATH = "java-class-path";
125
126
127
128
129
130
131
132 public static final String CSS_FILE = "css-file";
133
134
135
136
137
138
139
140
141
142
143 public Main()
144 {
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public int run(final String... args) throws ReportException
166 {
167 final Options options = createOptions();
168 try
169 {
170 final CommandLine commandLine = createCommandLine(options, args);
171 final ProjectConfiguration<XMLStreamWriter> config =
172 createConfig(commandLine);
173 run(config);
174 System.out.println("Report '" + commandLine.getOptionValue(REPORT_FILE)
175 + "' written successfully.");
176 return 0;
177 }
178 catch (final ParseException e)
179 {
180 final HelpFormatter formatter = new HelpFormatter();
181 System.err.println(e.getLocalizedMessage());
182 formatter.printHelp("java " + getClass().getName(), options);
183 return -1;
184 }
185 }
186
187
188
189
190
191
192
193
194
195 private static ProjectConfiguration<XMLStreamWriter> createConfig(
196 final CommandLine commandLine)
197 {
198 final List<String> classRootDirectoryNames =
199 readClassRootDirectoryNames(commandLine);
200 final String sourcePath = commandLine.getOptionValue(JAVA_SOURCE_PATH);
201 final List<String> sourceRootDirectoryNames =
202 StringFunction.split(sourcePath, ";");
203
204 final String includesString =
205 commandLine.getOptionValue(REPORT_INCLUDES, "**");
206 final List<String> includes = StringFunction.split(includesString, ",");
207 final String excludesString = commandLine.getOptionValue(REPORT_EXCLUDES);
208 final List<String> excludes = StringFunction.split(excludesString, ",");
209
210 final DefaultProjectConfiguration.Builder<XMLStreamWriter> builder =
211 new DefaultProjectConfiguration.Builder<XMLStreamWriter>(
212 commandLine.getOptionValue(PROJECT_NAME));
213
214 builder.setIncludes(includes);
215 builder.setExcludes(excludes);
216 builder.setSourceEncoding(commandLine.getOptionValue(JAVA_SOURCE_ENCODING,
217 "UTF-8"));
218 builder.setClassRootDirectoryNames(classRootDirectoryNames);
219 builder.setSourceRootDirectoryNames(sourceRootDirectoryNames);
220
221 builder.setReportEncoding(commandLine.getOptionValue(REPORT_ENCODING,
222 "UTF-8"));
223 builder.setReporter(commandLine.getOptionValue(REPORT_GENERATOR,
224 "de.smartics.exceptions.report.generator.HtmlReportGenerator"));
225 builder.setReport(commandLine.getOptionValue(REPORT_FILE));
226 builder.setStyleSheet(commandLine.getOptionValue(CSS_FILE));
227
228 final Filter filter = new ImplementsInterfaceFilter(Code.class);
229 builder.setFilter(filter);
230
231 final ResourceBundle bundle =
232 ResourceBundle.getBundle(
233 "de/smartics/exceptions/report/ExceptionCodeReportBundle",
234 Locale.ENGLISH, Main.class.getClassLoader());
235 builder.setBundle(bundle);
236
237 final ProjectConfiguration<XMLStreamWriter> config = builder.build();
238 return config;
239 }
240
241
242
243
244
245
246 private static List<String> readClassRootDirectoryNames(
247 final CommandLine commandLine)
248 {
249 String classPath = commandLine.getOptionValue(JAVA_CLASS_PATH);
250 if (StringUtils.isBlank(classPath))
251 {
252 classPath = System.getProperty("java.class.path");
253 }
254 final List<String> classRootDirectoryNames =
255 StringFunction.split(classPath, ";");
256 return classRootDirectoryNames;
257 }
258
259
260
261
262
263
264
265
266 public void run(final ProjectConfiguration<XMLStreamWriter> config)
267 throws ReportException
268 {
269 final String reportEncoding = config.getReportEncoding();
270 final String report = config.getReport();
271
272 Writer writer = null;
273 try
274 {
275 writer =
276 new BufferedWriter(new OutputStreamWriter(
277 new FileOutputStream(report), reportEncoding));
278 final XMLStreamWriter output =
279 AbstractXmlReportGenerator.createOutput(writer);
280 run(config, output);
281 }
282 catch (final ReportException e)
283 {
284 throw e;
285 }
286 catch (final Exception e)
287 {
288 throw new ReportException(e);
289 }
290 finally
291 {
292 IOUtils.closeQuietly(writer);
293 }
294 }
295
296
297
298
299
300
301
302
303
304
305 public void run(final ProjectConfiguration<XMLStreamWriter> configuration,
306 final XMLStreamWriter output) throws ReportException
307 {
308 final ReportLauncher<XMLStreamWriter> launcher =
309 new ReportLauncher<XMLStreamWriter>("exceptioncodes-cli");
310 launcher.launch(configuration, output);
311 }
312
313
314
315
316
317
318
319
320
321
322 private static CommandLine createCommandLine(final Options options,
323 final String[] args) throws ParseException
324 {
325 final CommandLineParser parser = new PosixParser();
326 final CommandLine cmd = parser.parse(options, args);
327 return cmd;
328 }
329
330
331
332
333
334
335 private static Options createOptions()
336 {
337 final Options options = new Options();
338
339 options
340 .addOption(OptionBuilder.withLongOpt(JAVA_SOURCE_ENCODING)
341 .withArgName("encoding").hasArg()
342 .withDescription("the encoding of the Java source files.")
343 .create("se"));
344 options.addOption(OptionBuilder.withLongOpt(JAVA_SOURCE_PATH)
345 .withArgName("path1;path2").hasArg()
346 .withDescription("the semicolon separated source root directories.")
347 .isRequired().create("path"));
348 options.addOption(OptionBuilder.withLongOpt(JAVA_CLASS_PATH)
349 .withArgName("path1;path2").hasArg()
350 .withDescription("the semicolon separated classpath root directories.")
351 .create("cp"));
352 options.addOption(OptionBuilder
353 .withLongOpt(REPORT_INCLUDES)
354 .withArgName("path;path")
355 .hasArg()
356 .withDescription(
357 "the semicolon separated included directories. Use Ant syntax.")
358 .create("i"));
359 options.addOption(OptionBuilder
360 .withLongOpt(REPORT_EXCLUDES)
361 .withArgName("path;path")
362 .hasArg()
363 .withDescription(
364 "the semicolon separated excluded directories. Use Ant syntax.")
365 .create("e"));
366 options.addOption(OptionBuilder.withLongOpt(REPORT_ENCODING)
367 .withArgName("encoding").hasArg()
368 .withDescription("the encoding of the report documentation.")
369 .create("c"));
370 options.addOption(OptionBuilder
371 .withLongOpt(REPORT_GENERATOR)
372 .withArgName("class")
373 .hasArg()
374 .withDescription(
375 "the class name of the report generator instance to use.")
376 .create("g"));
377 options.addOption(OptionBuilder.withLongOpt(REPORT_FILE)
378 .withArgName("file").hasArg()
379 .withDescription("the name of the report file to write.").isRequired()
380 .create("f"));
381 options.addOption(OptionBuilder.withLongOpt(PROJECT_NAME)
382 .withArgName("project-name").hasArg()
383 .withDescription("the name of the project to collect information for.")
384 .isRequired().create("p"));
385 options
386 .addOption(OptionBuilder
387 .withLongOpt(CSS_FILE)
388 .withArgName("css-file")
389 .hasArg()
390 .withDescription(
391 "the link reference to the CSS file to add to the head element of the generated HTML document.")
392 .isRequired().create("s"));
393 return options;
394 }
395
396
397
398
399
400
401
402
403
404
405 public static void main(final String... args) throws Exception,
406 ReportException
407 {
408 final Main main = new Main();
409 final int returnCode = main.run(args);
410 System.exit(returnCode);
411 }
412 }