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 =
646 new CheckstyleReportGenerator( getSink(), bundle, project.getBasedir(), siteTool );
647
648 generator.setLog( getLog() );
649 generator.setEnableRulesSummary( enableRulesSummary );
650 generator.setEnableSeveritySummary( enableSeveritySummary );
651 generator.setEnableFilesSummary( enableFilesSummary );
652 generator.setEnableRSS( enableRSS );
653 generator.setCheckstyleConfig( results.getConfiguration() );
654 if ( linkXRef )
655 {
656 String relativePath = PathTool.getRelativePath( getOutputDirectory(), xrefLocation.getAbsolutePath() );
657 if ( StringUtils.isEmpty( relativePath ) )
658 {
659 relativePath = ".";
660 }
661 relativePath = relativePath + "/" + xrefLocation.getName();
662 if ( xrefLocation.exists() )
663 {
664
665
666 generator.setXrefLocation( relativePath );
667 }
668 else
669 {
670
671 for ( Iterator<ReportPlugin> reports = getProject().getReportPlugins().iterator(); reports.hasNext(); )
672 {
673 ReportPlugin report = reports.next();
674
675 String artifactId = report.getArtifactId();
676 if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) )
677 {
678 generator.setXrefLocation( relativePath );
679 }
680 }
681 }
682
683 if ( generator.getXrefLocation() == null )
684 {
685 getLog().warn( "Unable to locate Source XRef to link to - DISABLED" );
686 }
687 }
688 generator.generateReport( results );
689 }
690
691
692
693
694
695
696
697 private void mergeDeprecatedInfo()
698 {
699 if ( "config/sun_checks.xml".equals( configLocation ) && !"sun".equals( format ) )
700 {
701 configLocation = (String) FORMAT_TO_CONFIG_LOCATION.get( format );
702 }
703
704 if ( StringUtils.isEmpty( propertiesLocation ) )
705 {
706 if ( propertiesFile != null )
707 {
708 propertiesLocation = propertiesFile.getPath();
709 }
710 else if ( propertiesURL != null )
711 {
712 propertiesLocation = propertiesURL.toExternalForm();
713 }
714 }
715
716 if ( "LICENSE.txt".equals( headerLocation ) )
717 {
718 File defaultHeaderFile = new File( project.getBasedir(), "LICENSE.txt" );
719 if ( !defaultHeaderFile.equals( headerFile ) )
720 {
721 headerLocation = headerFile.getPath();
722 }
723 }
724
725 if ( StringUtils.isEmpty( suppressionsLocation ) )
726 {
727 suppressionsLocation = suppressionsFile;
728 }
729
730 if ( StringUtils.isEmpty( packageNamesLocation ) )
731 {
732 packageNamesLocation = packageNamesFile;
733 }
734 }
735
736
737
738 public String getOutputName()
739 {
740 return "checkstyle";
741 }
742
743 private AuditListener getListener()
744 throws MavenReportException
745 {
746 AuditListener listener = null;
747
748 if ( StringUtils.isNotEmpty( outputFileFormat ) )
749 {
750 File resultFile = outputFile;
751
752 OutputStream out = getOutputStream( resultFile );
753
754 if ( "xml".equals( outputFileFormat ) )
755 {
756 listener = new XMLLogger( out, true );
757 }
758 else if ( "plain".equals( outputFileFormat ) )
759 {
760 listener = new DefaultLogger( out, true );
761 }
762 else
763 {
764
765 throw new MavenReportException( "Invalid output file format: (" + outputFileFormat
766 + "). Must be 'plain' or 'xml'." );
767 }
768 }
769
770 return listener;
771 }
772
773 private OutputStream getOutputStream( File file )
774 throws MavenReportException
775 {
776 File parentFile = file.getAbsoluteFile().getParentFile();
777
778 if ( !parentFile.exists() )
779 {
780 parentFile.mkdirs();
781 }
782
783 FileOutputStream fileOutputStream;
784 try
785 {
786 fileOutputStream = new FileOutputStream( file );
787 }
788 catch ( FileNotFoundException e )
789 {
790 throw new MavenReportException( "Unable to create output stream: " + file, e );
791 }
792 return fileOutputStream;
793 }
794
795 private DefaultLogger getConsoleListener()
796 throws MavenReportException
797 {
798 DefaultLogger consoleListener;
799
800 if ( useFile == null )
801 {
802 stringOutputStream = new ByteArrayOutputStream();
803 consoleListener = new DefaultLogger( stringOutputStream, false );
804 }
805 else
806 {
807 OutputStream out = getOutputStream( useFile );
808
809 consoleListener = new DefaultLogger( out, true );
810 }
811
812 return consoleListener;
813 }
814
815 private static ResourceBundle getBundle( Locale locale )
816 {
817 return ResourceBundle.getBundle( "checkstyle-report", locale, CheckstyleReport.class.getClassLoader() );
818 }
819
820
821 public boolean canGenerateReport()
822 {
823
824 return sourceDirectory.exists() || ( includeTestSourceDirectory && testSourceDirectory.exists() );
825 }
826
827
828 public void setReportOutputDirectory( File reportOutputDirectory )
829 {
830 super.setReportOutputDirectory( reportOutputDirectory );
831 this.outputDirectory = reportOutputDirectory;
832 }
833 }