1 package org.apache.maven.plugin.checkstyle;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.net.URL;
29 import java.util.Calendar;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.Locale;
34 import java.util.Map;
35 import java.util.ResourceBundle;
36
37 import org.apache.maven.doxia.siterenderer.Renderer;
38 import org.apache.maven.doxia.tools.SiteTool;
39 import org.apache.maven.model.ReportPlugin;
40 import org.apache.maven.plugin.checkstyle.rss.CheckstyleRssGenerator;
41 import org.apache.maven.plugin.checkstyle.rss.CheckstyleRssGeneratorRequest;
42 import org.apache.maven.project.MavenProject;
43 import org.apache.maven.reporting.AbstractMavenReport;
44 import org.apache.maven.reporting.MavenReportException;
45 import org.codehaus.plexus.resource.ResourceManager;
46 import org.codehaus.plexus.resource.loader.FileResourceLoader;
47 import org.codehaus.plexus.util.PathTool;
48 import org.codehaus.plexus.util.StringUtils;
49
50 import com.puppycrawl.tools.checkstyle.DefaultLogger;
51 import com.puppycrawl.tools.checkstyle.XMLLogger;
52 import com.puppycrawl.tools.checkstyle.api.AuditListener;
53 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
54
55
56
57
58
59
60
61
62
63
64
65
66 public class CheckstyleReport
67 extends AbstractMavenReport
68 {
69 public static final String PLUGIN_RESOURCES = "org/apache/maven/plugin/checkstyle";
70
71
72
73
74 private static final Map<String, String> FORMAT_TO_CONFIG_LOCATION;
75
76 static
77 {
78 Map<String, String> fmt2Cfg = new HashMap<String, String>();
79
80 fmt2Cfg.put( "sun", "config/sun_checks.xml" );
81 fmt2Cfg.put( "turbine", "config/turbine_checks.xml" );
82 fmt2Cfg.put( "avalon", "config/avalon_checks.xml" );
83 fmt2Cfg.put( "maven", "config/maven_checks.xml" );
84
85 FORMAT_TO_CONFIG_LOCATION = Collections.unmodifiableMap( fmt2Cfg );
86 }
87
88
89
90
91
92
93
94 private boolean skip;
95
96
97
98
99
100
101
102
103
104
105 private File outputDirectory;
106
107
108
109
110
111
112
113 private boolean enableRulesSummary;
114
115
116
117
118
119
120
121 private boolean enableSeveritySummary;
122
123
124
125
126
127
128
129 private boolean enableFilesSummary;
130
131
132
133
134
135
136 private boolean enableRSS;
137
138
139
140
141
142
143
144 private String includes;
145
146
147
148
149
150
151
152 private String excludes;
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 private String configLocation;
189
190
191
192
193
194
195
196
197 private String format;
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 private String propertiesLocation;
222
223
224
225
226
227
228
229
230 private File propertiesFile;
231
232
233
234
235
236
237
238
239 private URL propertiesURL;
240
241
242
243
244
245
246 private String propertyExpansion;
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272 private String headerLocation;
273
274
275
276
277
278
279
280
281
282 private File headerFile;
283
284
285
286
287
288
289 private String cacheFile;
290
291
292
293
294
295
296
297 private File useFile;
298
299
300
301
302
303
304
305
306
307 protected SiteTool siteTool;
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329 private String suppressionsLocation;
330
331
332
333
334
335
336
337
338 private String suppressionsFileExpression;
339
340
341
342
343
344
345
346
347
348
349
350 private String suppressionsFile;
351
352
353
354
355
356
357
358
359
360 private File outputFile;
361
362
363
364
365
366
367
368 private String outputFileFormat;
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387 private String packageNamesLocation;
388
389
390
391
392
393
394
395
396 private String packageNamesFile;
397
398
399
400
401
402
403 private boolean failsOnError;
404
405
406
407
408
409
410
411 private File sourceDirectory;
412
413
414
415
416
417
418
419
420 private File testSourceDirectory;
421
422
423
424
425
426
427
428 private boolean includeTestSourceDirectory;
429
430
431
432
433
434
435
436
437 private MavenProject project;
438
439
440
441
442
443
444 private boolean consoleOutput;
445
446
447
448
449
450
451
452
453 private boolean linkXRef;
454
455
456
457
458
459
460 private File xrefLocation;
461
462
463
464
465
466
467
468
469
470 private String encoding;
471
472
473
474
475
476
477 private Renderer siteRenderer;
478
479 private ByteArrayOutputStream stringOutputStream;
480
481
482
483
484
485
486 private ResourceManager locator;
487
488
489
490
491
492
493
494
495
496 protected CheckstyleRssGenerator checkstyleRssGenerator;
497
498
499
500
501
502
503
504 protected CheckstyleExecutor checkstyleExecutor;
505
506
507
508 public String getName( Locale locale )
509 {
510 return getBundle( locale ).getString( "report.checkstyle.name" );
511 }
512
513
514 public String getDescription( Locale locale )
515 {
516 return getBundle( locale ).getString( "report.checkstyle.description" );
517 }
518
519
520 protected String getOutputDirectory()
521 {
522 return outputDirectory.getAbsolutePath();
523 }
524
525
526 protected MavenProject getProject()
527 {
528 return project;
529 }
530
531
532 protected Renderer getSiteRenderer()
533 {
534 return siteRenderer;
535 }
536
537
538 public void executeReport( Locale locale )
539 throws MavenReportException
540 {
541 if ( !skip )
542 {
543 mergeDeprecatedInfo();
544
545 locator.addSearchPath( FileResourceLoader.ID, project.getFile().getParentFile().getAbsolutePath() );
546 locator.addSearchPath( "url", "" );
547
548 locator.setOutputDirectory( new File( project.getBuild().getDirectory() ) );
549
550 if ( !canGenerateReport() )
551 {
552 getLog().info( "Source directory does not exist - skipping report." );
553 return;
554 }
555
556
557
558
559
560
561
562
563 ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
564
565 try
566 {
567 CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
568 request.setConsoleListener( getConsoleListener() ).setConsoleOutput( consoleOutput )
569 .setExcludes( excludes ).setFailsOnError( failsOnError ).setIncludes( includes )
570 .setIncludeTestSourceDirectory( includeTestSourceDirectory ).setListener( getListener() )
571 .setLog( getLog() ).setProject( project ).setSourceDirectory( sourceDirectory )
572 .setStringOutputStream( stringOutputStream ).setSuppressionsLocation( suppressionsLocation )
573 .setTestSourceDirectory( testSourceDirectory ).setConfigLocation( configLocation )
574 .setPropertyExpansion( propertyExpansion ).setHeaderLocation( headerLocation )
575 .setCacheFile( cacheFile ).setSuppressionsFileExpression( suppressionsFileExpression )
576 .setEncoding( encoding ).setPropertiesLocation( propertiesLocation );
577
578 CheckstyleResults results = checkstyleExecutor.executeCheckstyle( request );
579
580 ResourceBundle bundle = getBundle( locale );
581 generateReportStatics();
582 generateMainReport( results, bundle );
583 if ( enableRSS )
584 {
585 CheckstyleRssGeneratorRequest checkstyleRssGeneratorRequest =
586 new CheckstyleRssGeneratorRequest( this.project, this.getCopyright(), outputDirectory, getLog() );
587 checkstyleRssGenerator.generateRSS( results, checkstyleRssGeneratorRequest );
588 }
589
590 }
591 catch ( CheckstyleException e )
592 {
593 throw new MavenReportException( "Failed during checkstyle configuration", e );
594 }
595 catch (CheckstyleExecutorException e)
596 {
597 throw new MavenReportException( "Failed during checkstyle execution", e );
598 }
599 finally
600 {
601
602 Thread.currentThread().setContextClassLoader( currentClassLoader );
603 }
604 }
605 }
606
607 private void generateReportStatics()
608 throws MavenReportException
609 {
610 ReportResource rresource = new ReportResource( PLUGIN_RESOURCES, outputDirectory );
611 try
612 {
613 rresource.copy( "images/rss.png" );
614 }
615 catch ( IOException e )
616 {
617 throw new MavenReportException( "Unable to copy static resources.", e );
618 }
619 }
620
621
622 private String getCopyright()
623 {
624 String copyright;
625 int currentYear = Calendar.getInstance().get( Calendar.YEAR );
626 if ( StringUtils.isNotEmpty( project.getInceptionYear() )
627 && !String.valueOf( currentYear ).equals( project.getInceptionYear() ) )
628 {
629 copyright = project.getInceptionYear() + " - " + currentYear;
630 }
631 else
632 {
633 copyright = String.valueOf( currentYear );
634 }
635
636 if ( ( project.getOrganization() != null ) && StringUtils.isNotEmpty( project.getOrganization().getName() ) )
637 {
638 copyright = copyright + " " + project.getOrganization().getName();
639 }
640 return copyright;
641 }
642
643 private void generateMainReport( CheckstyleResults results, ResourceBundle bundle )
644 {
645 CheckstyleReportGenerator generator = new CheckstyleReportGenerator( getSink(), bundle, project.getBasedir(), siteTool );
646
647 generator.setLog( getLog() );
648 generator.setEnableRulesSummary( enableRulesSummary );
649 generator.setEnableSeveritySummary( enableSeveritySummary );
650 generator.setEnableFilesSummary( enableFilesSummary );
651 generator.setEnableRSS( enableRSS );
652 generator.setCheckstyleConfig( results.getConfiguration() );
653 if ( linkXRef )
654 {
655 String relativePath = PathTool.getRelativePath( getOutputDirectory(), xrefLocation.getAbsolutePath() );
656 if ( StringUtils.isEmpty( relativePath ) )
657 {
658 relativePath = ".";
659 }
660 relativePath = relativePath + "/" + xrefLocation.getName();
661 if ( xrefLocation.exists() )
662 {
663
664
665 generator.setXrefLocation( relativePath );
666 }
667 else
668 {
669
670 for ( Iterator<ReportPlugin> reports = getProject().getReportPlugins().iterator(); reports.hasNext(); )
671 {
672 ReportPlugin report = reports.next();
673
674 String artifactId = report.getArtifactId();
675 if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) )
676 {
677 generator.setXrefLocation( relativePath );
678 }
679 }
680 }
681
682 if ( generator.getXrefLocation() == null )
683 {
684 getLog().warn( "Unable to locate Source XRef to link to - DISABLED" );
685 }
686 }
687 generator.generateReport( results );
688 }
689
690
691
692
693
694
695
696 private void mergeDeprecatedInfo()
697 {
698 if ( "config/sun_checks.xml".equals( configLocation ) && !"sun".equals( format ) )
699 {
700 configLocation = (String) FORMAT_TO_CONFIG_LOCATION.get( format );
701 }
702
703 if ( StringUtils.isEmpty( propertiesLocation ) )
704 {
705 if ( propertiesFile != null )
706 {
707 propertiesLocation = propertiesFile.getPath();
708 }
709 else if ( propertiesURL != null )
710 {
711 propertiesLocation = propertiesURL.toExternalForm();
712 }
713 }
714
715 if ( "LICENSE.txt".equals( headerLocation ) )
716 {
717 File defaultHeaderFile = new File( project.getBasedir(), "LICENSE.txt" );
718 if ( !defaultHeaderFile.equals( headerFile ) )
719 {
720 headerLocation = headerFile.getPath();
721 }
722 }
723
724 if ( StringUtils.isEmpty( suppressionsLocation ) )
725 {
726 suppressionsLocation = suppressionsFile;
727 }
728
729 if ( StringUtils.isEmpty( packageNamesLocation ) )
730 {
731 packageNamesLocation = packageNamesFile;
732 }
733 }
734
735
736
737 public String getOutputName()
738 {
739 return "checkstyle";
740 }
741
742 private AuditListener getListener()
743 throws MavenReportException
744 {
745 AuditListener listener = null;
746
747 if ( StringUtils.isNotEmpty( outputFileFormat ) )
748 {
749 File resultFile = outputFile;
750
751 OutputStream out = getOutputStream( resultFile );
752
753 if ( "xml".equals( outputFileFormat ) )
754 {
755 listener = new XMLLogger( out, true );
756 }
757 else if ( "plain".equals( outputFileFormat ) )
758 {
759 listener = new DefaultLogger( out, true );
760 }
761 else
762 {
763
764 throw new MavenReportException( "Invalid output file format: (" + outputFileFormat
765 + "). Must be 'plain' or 'xml'." );
766 }
767 }
768
769 return listener;
770 }
771
772 private OutputStream getOutputStream( File file )
773 throws MavenReportException
774 {
775 File parentFile = file.getAbsoluteFile().getParentFile();
776
777 if ( !parentFile.exists() )
778 {
779 parentFile.mkdirs();
780 }
781
782 FileOutputStream fileOutputStream;
783 try
784 {
785 fileOutputStream = new FileOutputStream( file );
786 }
787 catch ( FileNotFoundException e )
788 {
789 throw new MavenReportException( "Unable to create output stream: " + file, e );
790 }
791 return fileOutputStream;
792 }
793
794 private DefaultLogger getConsoleListener()
795 throws MavenReportException
796 {
797 DefaultLogger consoleListener;
798
799 if ( useFile == null )
800 {
801 stringOutputStream = new ByteArrayOutputStream();
802 consoleListener = new DefaultLogger( stringOutputStream, false );
803 }
804 else
805 {
806 OutputStream out = getOutputStream( useFile );
807
808 consoleListener = new DefaultLogger( out, true );
809 }
810
811 return consoleListener;
812 }
813
814 private static ResourceBundle getBundle( Locale locale )
815 {
816 return ResourceBundle.getBundle( "checkstyle-report", locale, CheckstyleReport.class.getClassLoader() );
817 }
818
819
820 public boolean canGenerateReport()
821 {
822
823 return sourceDirectory.exists();
824 }
825
826
827 public void setReportOutputDirectory( File reportOutputDirectory )
828 {
829 super.setReportOutputDirectory( reportOutputDirectory );
830 this.outputDirectory = reportOutputDirectory;
831 }
832 }