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 com.puppycrawl.tools.checkstyle.Checker;
23 import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
24 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
25 import com.puppycrawl.tools.checkstyle.DefaultLogger;
26 import com.puppycrawl.tools.checkstyle.ModuleFactory;
27 import com.puppycrawl.tools.checkstyle.PackageNamesLoader;
28 import com.puppycrawl.tools.checkstyle.PropertiesExpander;
29 import com.puppycrawl.tools.checkstyle.XMLLogger;
30 import com.puppycrawl.tools.checkstyle.api.AuditListener;
31 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
32 import com.puppycrawl.tools.checkstyle.api.Configuration;
33 import com.puppycrawl.tools.checkstyle.api.FilterSet;
34 import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
35 import com.puppycrawl.tools.checkstyle.filters.SuppressionsLoader;
36 import org.apache.maven.artifact.DependencyResolutionRequiredException;
37 import org.apache.maven.doxia.tools.SiteTool;
38 import org.apache.maven.model.ReportPlugin;
39 import org.apache.maven.plugin.MojoExecutionException;
40 import org.apache.maven.project.MavenProject;
41 import org.apache.maven.reporting.AbstractMavenReport;
42 import org.apache.maven.reporting.MavenReportException;
43 import org.apache.velocity.VelocityContext;
44 import org.apache.velocity.context.Context;
45 import org.apache.velocity.exception.ResourceNotFoundException;
46 import org.apache.velocity.exception.VelocityException;
47 import org.codehaus.doxia.site.renderer.SiteRenderer;
48 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
49 import org.codehaus.plexus.personality.plexus.lifecycle.phase.ServiceLocator;
50 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Serviceable;
51 import org.codehaus.plexus.resource.ResourceManager;
52 import org.codehaus.plexus.resource.loader.FileResourceCreationException;
53 import org.codehaus.plexus.resource.loader.FileResourceLoader;
54 import org.codehaus.plexus.util.FileUtils;
55 import org.codehaus.plexus.util.PathTool;
56 import org.codehaus.plexus.util.StringInputStream;
57 import org.codehaus.plexus.util.StringOutputStream;
58 import org.codehaus.plexus.util.StringUtils;
59 import org.codehaus.plexus.velocity.VelocityComponent;
60
61 import java.io.File;
62 import java.io.FileInputStream;
63 import java.io.FileNotFoundException;
64 import java.io.FileOutputStream;
65 import java.io.IOException;
66 import java.io.OutputStream;
67 import java.net.MalformedURLException;
68 import java.net.URL;
69 import java.net.URLClassLoader;
70 import java.util.ArrayList;
71 import java.util.Calendar;
72 import java.util.Collections;
73 import java.util.HashMap;
74 import java.util.Iterator;
75 import java.util.List;
76 import java.util.Locale;
77 import java.util.Map;
78 import java.util.Properties;
79 import java.util.ResourceBundle;
80
81
82
83
84
85
86
87
88
89
90 public class CheckstyleReport
91 extends AbstractMavenReport
92 implements Serviceable
93 {
94 private static final String PLUGIN_RESOURCES = "org/apache/maven/plugin/checkstyle";
95
96
97
98
99 private static final Map FORMAT_TO_CONFIG_LOCATION;
100
101 static
102 {
103 Map fmt2Cfg = new HashMap();
104
105 fmt2Cfg.put( "sun", "config/sun_checks.xml" );
106 fmt2Cfg.put( "turbine", "config/turbine_checks.xml" );
107 fmt2Cfg.put( "avalon", "config/avalon_checks.xml" );
108 fmt2Cfg.put( "maven", "config/maven_checks.xml" );
109
110 FORMAT_TO_CONFIG_LOCATION = Collections.unmodifiableMap( fmt2Cfg );
111 }
112
113
114
115
116
117
118
119 private boolean skip;
120
121
122
123
124
125
126
127
128
129
130 private File outputDirectory;
131
132
133
134
135
136
137
138 private boolean enableRulesSummary;
139
140
141
142
143
144
145
146 private boolean enableSeveritySummary;
147
148
149
150
151
152
153
154 private boolean enableFilesSummary;
155
156
157
158
159
160
161 private boolean enableRSS;
162
163
164
165
166
167
168
169 private String includes;
170
171
172
173
174
175
176
177 private String excludes;
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 private String configLocation;
214
215
216
217
218
219
220
221
222 private String format;
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 private String propertiesLocation;
247
248
249
250
251
252
253
254
255 private File propertiesFile;
256
257
258
259
260
261
262
263
264 private URL propertiesURL;
265
266
267
268
269
270
271 private String propertyExpansion;
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298 private String headerLocation;
299
300
301
302
303
304
305
306
307
308 private File headerFile;
309
310
311
312
313
314
315 private String cacheFile;
316
317
318
319
320
321
322
323 private File useFile;
324
325
326
327
328
329
330
331
332
333 protected SiteTool siteTool;
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355 private String suppressionsLocation;
356
357
358
359
360
361
362
363
364 private String suppressionsFileExpression;
365
366
367
368
369
370
371
372
373
374
375
376 private String suppressionsFile;
377
378
379
380
381
382
383
384
385
386 private File outputFile;
387
388
389
390
391
392
393
394 private String outputFileFormat;
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413 private String packageNamesLocation;
414
415
416
417
418
419
420
421
422 private String packageNamesFile;
423
424
425
426
427
428
429 private boolean failsOnError;
430
431
432
433
434
435
436
437 private File sourceDirectory;
438
439
440
441
442
443
444
445
446 private File testSourceDirectory;
447
448
449
450
451
452
453
454 private boolean includeTestSourceDirectory;
455
456
457
458
459
460
461
462
463 private MavenProject project;
464
465
466
467
468
469
470 private boolean consoleOutput;
471
472
473
474
475
476
477
478
479 private boolean linkXRef;
480
481
482
483
484
485
486 private File xrefLocation;
487
488
489
490
491
492
493
494
495
496 private String encoding;
497
498
499
500
501
502
503 private SiteRenderer siteRenderer;
504
505
506
507
508 private VelocityComponent velocityComponent;
509
510
511
512
513
514 private ServiceLocator serviceLocator;
515
516
517
518
519
520
521 public void service( ServiceLocator locator )
522 {
523 this.serviceLocator = locator;
524 }
525
526 private static final File[] EMPTY_FILE_ARRAY = new File[0];
527
528 private StringOutputStream stringOutputStream;
529
530
531
532
533
534
535 private ResourceManager locator;
536
537
538
539
540 public String getName( Locale locale )
541 {
542 return getBundle( locale ).getString( "report.checkstyle.name" );
543 }
544
545
546
547
548 public String getDescription( Locale locale )
549 {
550 return getBundle( locale ).getString( "report.checkstyle.description" );
551 }
552
553
554
555
556 protected String getOutputDirectory()
557 {
558 return outputDirectory.getAbsolutePath();
559 }
560
561
562
563
564 protected MavenProject getProject()
565 {
566 return project;
567 }
568
569
570
571
572 protected SiteRenderer getSiteRenderer()
573 {
574 return siteRenderer;
575 }
576
577
578
579
580 public void executeReport( Locale locale )
581 throws MavenReportException
582 {
583 if ( !skip )
584 {
585 mergeDeprecatedInfo();
586
587 locator.addSearchPath( FileResourceLoader.ID, project.getFile().getParentFile().getAbsolutePath() );
588 locator.addSearchPath( "url", "" );
589
590 locator.setOutputDirectory( new File( project.getBuild().getDirectory() ) );
591
592 if ( !canGenerateReport() )
593 {
594 getLog().info( "Source directory does not exist - skipping report." );
595 return;
596 }
597
598
599
600
601
602
603
604
605 ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
606
607 try
608 {
609
610
611
612 ClassLoader checkstyleClassLoader = PackageNamesLoader.class.getClassLoader();
613 Thread.currentThread().setContextClassLoader( checkstyleClassLoader );
614
615
616 String configFile = getConfigFile();
617 Properties overridingProperties = getOverridingProperties();
618 ModuleFactory moduleFactory;
619 Configuration config;
620 CheckstyleResults results;
621
622 moduleFactory = getModuleFactory();
623
624 config = ConfigurationLoader.loadConfiguration( configFile,
625 new PropertiesExpander( overridingProperties ) );
626 String effectiveEncoding =
627 StringUtils.isNotEmpty( encoding ) ? encoding : System.getProperty( "file.encoding", "UTF-8" );
628 if ( StringUtils.isEmpty( encoding ) )
629 {
630 getLog().warn(
631 "File encoding has not been set, using platform encoding " + effectiveEncoding
632 + ", i.e. build is platform dependent!" );
633 }
634 Configuration[] modules = config.getChildren();
635 for ( int i = 0; i < modules.length; i++ )
636 {
637 Configuration module = modules[i];
638 if ( "TreeWalker".equals( module.getName() )
639 || "com.puppycrawl.tools.checkstyle.TreeWalker".equals( module.getName() ) )
640 {
641 if ( module instanceof DefaultConfiguration )
642 {
643 ( (DefaultConfiguration) module ).addAttribute( "charset", effectiveEncoding );
644 }
645 else
646 {
647 getLog().warn( "Failed to configure file encoding on module " + module );
648 }
649 }
650 }
651
652 results = executeCheckstyle( config, moduleFactory );
653
654 ResourceBundle bundle = getBundle( locale );
655 generateReportStatics();
656 generateMainReport( results, config, moduleFactory, bundle );
657 if ( enableRSS )
658 {
659 generateRSS( results );
660 }
661
662 }
663 catch ( CheckstyleException e )
664 {
665 throw new MavenReportException( "Failed during checkstyle configuration", e );
666 }
667 finally
668 {
669
670 Thread.currentThread().setContextClassLoader( currentClassLoader );
671 }
672 }
673 }
674
675 private void generateReportStatics()
676 throws MavenReportException
677 {
678 ReportResource rresource = new ReportResource( PLUGIN_RESOURCES, outputDirectory );
679 try
680 {
681 rresource.copy( "images/rss.png" );
682 }
683 catch ( IOException e )
684 {
685 throw new MavenReportException( "Unable to copy static resources.", e );
686 }
687 }
688
689 private void generateRSS( CheckstyleResults results )
690 throws MavenReportException
691 {
692 if ( velocityComponent == null )
693 {
694 try
695 {
696 velocityComponent = (VelocityComponent) serviceLocator.lookup( VelocityComponent.ROLE );
697 }
698 catch ( ComponentLookupException e )
699 {
700 throw new MavenReportException( "Failed to setup Velocity", e );
701 }
702 }
703
704 VelocityTemplate vtemplate = new VelocityTemplate( velocityComponent, PLUGIN_RESOURCES );
705 vtemplate.setLog( getLog() );
706
707 Context context = new VelocityContext();
708 context.put( "results", results );
709 context.put( "project", project );
710 context.put( "copyright", getCopyright() );
711 context.put( "levelInfo", SeverityLevel.INFO );
712 context.put( "levelWarning", SeverityLevel.WARNING );
713 context.put( "levelError", SeverityLevel.ERROR );
714 context.put( "stringutils", new StringUtils() );
715
716 try
717 {
718 vtemplate.generate( outputDirectory.getPath() + "/checkstyle.rss", "checkstyle-rss.vm", context );
719 }
720 catch ( ResourceNotFoundException e )
721 {
722 throw new MavenReportException( "Unable to find checkstyle-rss.vm resource.", e );
723 }
724 catch ( MojoExecutionException e )
725 {
726 throw new MavenReportException( "Unable to generate checkstyle.rss.", e );
727 }
728 catch ( VelocityException e )
729 {
730 throw new MavenReportException( "Unable to generate checkstyle.rss.", e );
731 }
732 catch ( IOException e )
733 {
734 throw new MavenReportException( "Unable to generate checkstyle.rss.", e );
735 }
736 }
737
738 private String getCopyright()
739 {
740 String copyright;
741 int currentYear = Calendar.getInstance().get( Calendar.YEAR );
742 if ( StringUtils.isNotEmpty( project.getInceptionYear() )
743 && !String.valueOf( currentYear ).equals( project.getInceptionYear() ) )
744 {
745 copyright = project.getInceptionYear() + " - " + currentYear;
746 }
747 else
748 {
749 copyright = String.valueOf( currentYear );
750 }
751
752 if ( ( project.getOrganization() != null ) && StringUtils.isNotEmpty( project.getOrganization().getName() ) )
753 {
754 copyright = copyright + " " + project.getOrganization().getName();
755 }
756 return copyright;
757 }
758
759 private void generateMainReport( CheckstyleResults results, Configuration config, ModuleFactory moduleFactory,
760 ResourceBundle bundle )
761 {
762 CheckstyleReportGenerator generator = new CheckstyleReportGenerator( getSink(), bundle, project.getBasedir(), siteTool );
763
764 generator.setLog( getLog() );
765 generator.setEnableRulesSummary( enableRulesSummary );
766 generator.setEnableSeveritySummary( enableSeveritySummary );
767 generator.setEnableFilesSummary( enableFilesSummary );
768 generator.setEnableRSS( enableRSS );
769 generator.setCheckstyleConfig( config );
770 generator.setCheckstyleModuleFactory( moduleFactory );
771 if ( linkXRef )
772 {
773 String relativePath = PathTool.getRelativePath( getOutputDirectory(), xrefLocation.getAbsolutePath() );
774 if ( StringUtils.isEmpty( relativePath ) )
775 {
776 relativePath = ".";
777 }
778 relativePath = relativePath + "/" + xrefLocation.getName();
779 if ( xrefLocation.exists() )
780 {
781
782
783 generator.setXrefLocation( relativePath );
784 }
785 else
786 {
787
788 for ( Iterator reports = getProject().getReportPlugins().iterator(); reports.hasNext(); )
789 {
790 ReportPlugin report = (ReportPlugin) reports.next();
791
792 String artifactId = report.getArtifactId();
793 if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) )
794 {
795 generator.setXrefLocation( relativePath );
796 }
797 }
798 }
799
800 if ( generator.getXrefLocation() == null )
801 {
802 getLog().warn( "Unable to locate Source XRef to link to - DISABLED" );
803 }
804 }
805 generator.generateReport( results );
806 }
807
808
809
810
811
812
813
814 private void mergeDeprecatedInfo()
815 {
816 if ( "config/sun_checks.xml".equals( configLocation ) && !"sun".equals( format ) )
817 {
818 configLocation = (String) FORMAT_TO_CONFIG_LOCATION.get( format );
819 }
820
821 if ( StringUtils.isEmpty( propertiesLocation ) )
822 {
823 if ( propertiesFile != null )
824 {
825 propertiesLocation = propertiesFile.getPath();
826 }
827 else if ( propertiesURL != null )
828 {
829 propertiesLocation = propertiesURL.toExternalForm();
830 }
831 }
832
833 if ( "LICENSE.txt".equals( headerLocation ) )
834 {
835 File defaultHeaderFile = new File( project.getBasedir(), "LICENSE.txt" );
836 if ( !defaultHeaderFile.equals( headerFile ) )
837 {
838 headerLocation = headerFile.getPath();
839 }
840 }
841
842 if ( StringUtils.isEmpty( suppressionsLocation ) )
843 {
844 suppressionsLocation = suppressionsFile;
845 }
846
847 if ( StringUtils.isEmpty( packageNamesLocation ) )
848 {
849 packageNamesLocation = packageNamesFile;
850 }
851 }
852
853 private CheckstyleResults executeCheckstyle( Configuration config, ModuleFactory moduleFactory )
854 throws MavenReportException, CheckstyleException
855 {
856 File[] files;
857 try
858 {
859 files = getFilesToProcess( includes, excludes );
860 }
861 catch ( IOException e )
862 {
863 throw new MavenReportException( "Error getting files to process", e );
864 }
865
866 FilterSet filterSet = getSuppressions();
867
868 Checker checker = new Checker();
869
870
871
872 List classPathStrings;
873 List outputDirectories = new ArrayList();
874 try
875 {
876 classPathStrings = this.project.getCompileClasspathElements();
877 outputDirectories.add( this.project.getBuild().getOutputDirectory() );
878
879 if ( includeTestSourceDirectory && ( testSourceDirectory != null ) && ( testSourceDirectory.exists() )
880 && ( testSourceDirectory.isDirectory() ) )
881 {
882 classPathStrings = this.project.getTestClasspathElements();
883 outputDirectories.add( this.project.getBuild().getTestOutputDirectory() );
884 }
885 }
886 catch ( DependencyResolutionRequiredException e )
887 {
888 throw new MavenReportException( e.getMessage(), e );
889 }
890
891 List urls = new ArrayList( classPathStrings.size() );
892
893 Iterator iter = classPathStrings.iterator();
894 while ( iter.hasNext() )
895 {
896 try
897 {
898 urls.add( new File( ( (String) iter.next() ) ).toURL() );
899 }
900 catch ( MalformedURLException e )
901 {
902 throw new MavenReportException( e.getMessage(), e );
903 }
904 }
905
906 Iterator iterator = outputDirectories.iterator();
907 while ( iterator.hasNext() )
908 {
909 try
910 {
911 String outputDirectoryString = (String) iterator.next();
912 if ( outputDirectoryString != null )
913 {
914 File outputDirectoryFile = new File( outputDirectoryString );
915 if ( outputDirectoryFile.exists() )
916 {
917 URL outputDirectoryUrl = outputDirectoryFile.toURL();
918 getLog().debug( "Adding the outputDirectory " + outputDirectoryUrl.toString()
919 + " to the Checkstyle class path" );
920 urls.add( outputDirectoryUrl );
921 }
922 }
923 }
924 catch ( MalformedURLException e )
925 {
926 throw new MavenReportException( e.getMessage(), e );
927 }
928 catch ( IOException e )
929 {
930 throw new MavenReportException( e.getMessage(), e );
931 }
932 }
933
934 URLClassLoader projectClassLoader = new URLClassLoader( (URL[]) urls.toArray( new URL[urls.size()] ), null );
935 checker.setClassloader( projectClassLoader );
936
937 if ( moduleFactory != null )
938 {
939 checker.setModuleFactory( moduleFactory );
940 }
941
942 if ( filterSet != null )
943 {
944 checker.addFilter( filterSet );
945 }
946
947 checker.configure( config );
948
949 AuditListener listener = getListener();
950
951 if ( listener != null )
952 {
953 checker.addListener( listener );
954 }
955
956 if ( consoleOutput )
957 {
958 checker.addListener( getConsoleListener() );
959 }
960
961 CheckstyleReportListener sinkListener = new CheckstyleReportListener( sourceDirectory );
962 if ( includeTestSourceDirectory && ( testSourceDirectory != null ) && ( testSourceDirectory.exists() )
963 && ( testSourceDirectory.isDirectory() ) )
964 {
965 sinkListener.addSourceDirectory( testSourceDirectory );
966 }
967
968 checker.addListener( sinkListener );
969
970 int nbErrors = checker.process( files );
971
972 checker.destroy();
973
974 if ( stringOutputStream != null )
975 {
976 getLog().info( stringOutputStream.toString() );
977 }
978
979 if ( failsOnError && nbErrors > 0 )
980 {
981
982
983
984 throw new MavenReportException( "There are " + nbErrors + " checkstyle errors." );
985 }
986 else if ( nbErrors > 0 )
987 {
988 getLog().info( "There are " + nbErrors + " checkstyle errors." );
989 }
990
991 return sinkListener.getResults();
992 }
993
994
995
996
997 public String getOutputName()
998 {
999 return "checkstyle";
1000 }
1001
1002 private AuditListener getListener()
1003 throws MavenReportException
1004 {
1005 AuditListener listener = null;
1006
1007 if ( StringUtils.isNotEmpty( outputFileFormat ) )
1008 {
1009 File resultFile = outputFile;
1010
1011 OutputStream out = getOutputStream( resultFile );
1012
1013 if ( "xml".equals( outputFileFormat ) )
1014 {
1015 listener = new XMLLogger( out, true );
1016 }
1017 else if ( "plain".equals( outputFileFormat ) )
1018 {
1019 listener = new DefaultLogger( out, true );
1020 }
1021 else
1022 {
1023
1024 throw new MavenReportException( "Invalid output file format: (" + outputFileFormat
1025 + "). Must be 'plain' or 'xml'." );
1026 }
1027 }
1028
1029 return listener;
1030 }
1031
1032 private OutputStream getOutputStream( File file )
1033 throws MavenReportException
1034 {
1035 File parentFile = file.getAbsoluteFile().getParentFile();
1036
1037 if ( !parentFile.exists() )
1038 {
1039 parentFile.mkdirs();
1040 }
1041
1042 FileOutputStream fileOutputStream;
1043 try
1044 {
1045 fileOutputStream = new FileOutputStream( file );
1046 }
1047 catch ( FileNotFoundException e )
1048 {
1049 throw new MavenReportException( "Unable to create output stream: " + file, e );
1050 }
1051 return fileOutputStream;
1052 }
1053
1054 private File[] getFilesToProcess( String includes, String excludes )
1055 throws IOException
1056 {
1057 StringBuffer excludesStr = new StringBuffer();
1058
1059 if ( StringUtils.isNotEmpty( excludes ) )
1060 {
1061 excludesStr.append( excludes );
1062 }
1063
1064 String[] defaultExcludes = FileUtils.getDefaultExcludes();
1065 for ( int i = 0; i < defaultExcludes.length; i++ )
1066 {
1067 if ( excludesStr.length() > 0 )
1068 {
1069 excludesStr.append( "," );
1070 }
1071
1072 excludesStr.append( defaultExcludes[i] );
1073 }
1074
1075 List files = FileUtils.getFiles( sourceDirectory, includes, excludesStr.toString() );
1076 if ( includeTestSourceDirectory && ( testSourceDirectory != null ) && ( testSourceDirectory.exists() )
1077 && ( testSourceDirectory.isDirectory() ) )
1078 {
1079 files.addAll( FileUtils.getFiles( testSourceDirectory, includes, excludesStr.toString() ) );
1080 }
1081
1082 return (File[]) files.toArray( EMPTY_FILE_ARRAY );
1083 }
1084
1085 private Properties getOverridingProperties()
1086 throws MavenReportException
1087 {
1088 Properties p = new Properties();
1089
1090 try
1091 {
1092 File propertiesFile = locator.resolveLocation( propertiesLocation, "checkstyle-checker.properties" );
1093
1094 if ( propertiesFile != null )
1095 {
1096 p.load( new FileInputStream( propertiesFile ) );
1097 }
1098
1099 if ( StringUtils.isNotEmpty( propertyExpansion ) )
1100 {
1101
1102 propertyExpansion = StringUtils.replace( propertyExpansion, "\\", "\\\\" );
1103 p.load( new StringInputStream( propertyExpansion ) );
1104 }
1105
1106
1107
1108
1109 if ( "config/maven_checks.xml".equals( configLocation ) )
1110 {
1111 if ( "LICENSE.txt".equals( headerLocation ) )
1112 {
1113 headerLocation = "config/maven-header.txt";
1114 }
1115 }
1116 if ( StringUtils.isNotEmpty( headerLocation ) )
1117 {
1118 try
1119 {
1120 File headerFile = locator.resolveLocation( headerLocation, "checkstyle-header.txt" );
1121
1122 if ( headerFile != null )
1123 {
1124 p.setProperty( "checkstyle.header.file", headerFile.getAbsolutePath() );
1125 }
1126 }
1127 catch ( IOException e )
1128 {
1129 throw new MavenReportException( "Unable to process header location: " + headerLocation, e );
1130 }
1131 }
1132
1133 if ( cacheFile != null )
1134 {
1135 p.setProperty( "checkstyle.cache.file", cacheFile );
1136 }
1137 }
1138 catch ( IOException e )
1139 {
1140 throw new MavenReportException( "Failed to get overriding properties", e );
1141 }
1142
1143 if ( suppressionsFileExpression != null )
1144 {
1145 String suppresionFile = getSuppressionLocation();
1146
1147 if ( suppresionFile != null )
1148 {
1149 p.setProperty( suppressionsFileExpression, suppresionFile );
1150 }
1151 }
1152
1153 return p;
1154 }
1155
1156 private String getConfigFile()
1157 throws MavenReportException
1158 {
1159 try
1160 {
1161 File configFile = locator.getResourceAsFile( configLocation, "checkstyle-checker.xml" );
1162
1163 if ( configFile == null )
1164 {
1165 throw new MavenReportException( "Unable to process config location: " + configLocation );
1166 }
1167 return configFile.getAbsolutePath();
1168 }
1169 catch ( org.codehaus.plexus.resource.loader.ResourceNotFoundException e )
1170 {
1171 throw new MavenReportException( "Unable to find configuration file at location "
1172 + configLocation, e );
1173 }
1174 catch ( FileResourceCreationException e )
1175 {
1176 throw new MavenReportException( "Unable to process configuration file location "
1177 + configLocation, e );
1178 }
1179
1180 }
1181
1182 private ModuleFactory getModuleFactory()
1183 throws CheckstyleException
1184 {
1185
1186 ModuleFactory moduleFactory = PackageNamesLoader.loadModuleFactory( Thread.currentThread()
1187 .getContextClassLoader() );
1188
1189 try
1190 {
1191
1192 File packageNamesFile = locator.resolveLocation( packageNamesLocation, "checkstyle-packages.xml" );
1193
1194 if ( packageNamesFile != null )
1195 {
1196
1197 moduleFactory = PackageNamesLoader.loadModuleFactory( packageNamesFile.getAbsolutePath() );
1198 }
1199 }
1200 catch ( IOException e )
1201 {
1202 getLog().error( "Unable to process package names location: " + packageNamesLocation, e );
1203 }
1204 return moduleFactory;
1205 }
1206
1207 private String getSuppressionLocation()
1208 throws MavenReportException
1209 {
1210 try
1211 {
1212 File suppressionsFile = locator.resolveLocation( suppressionsLocation, "checkstyle-suppressions.xml" );
1213
1214 if ( suppressionsFile == null )
1215 {
1216 return null;
1217 }
1218
1219 return suppressionsFile.getAbsolutePath();
1220 }
1221 catch ( IOException e )
1222 {
1223 throw new MavenReportException( "Failed to process supressions location: " + suppressionsLocation, e );
1224 }
1225 }
1226
1227 private FilterSet getSuppressions()
1228 throws MavenReportException
1229 {
1230 try
1231 {
1232 File suppressionsFile = locator.resolveLocation( suppressionsLocation, "checkstyle-suppressions.xml" );
1233
1234 if ( suppressionsFile == null )
1235 {
1236 return null;
1237 }
1238
1239 return SuppressionsLoader.loadSuppressions( suppressionsFile.getAbsolutePath() );
1240 }
1241 catch ( CheckstyleException ce )
1242 {
1243 throw new MavenReportException( "failed to load suppressions location: " + suppressionsLocation, ce );
1244 }
1245 catch ( IOException e )
1246 {
1247 throw new MavenReportException( "Failed to process supressions location: " + suppressionsLocation, e );
1248 }
1249 }
1250
1251 private DefaultLogger getConsoleListener()
1252 throws MavenReportException
1253 {
1254 DefaultLogger consoleListener;
1255
1256 if ( useFile == null )
1257 {
1258 stringOutputStream = new StringOutputStream();
1259 consoleListener = new DefaultLogger( stringOutputStream, false );
1260 }
1261 else
1262 {
1263 OutputStream out = getOutputStream( useFile );
1264
1265 consoleListener = new DefaultLogger( out, true );
1266 }
1267
1268 return consoleListener;
1269 }
1270
1271 private static ResourceBundle getBundle( Locale locale )
1272 {
1273 return ResourceBundle.getBundle( "checkstyle-report", locale, CheckstyleReport.class.getClassLoader() );
1274 }
1275
1276
1277
1278
1279 public boolean canGenerateReport()
1280 {
1281
1282 return sourceDirectory.exists();
1283 }
1284
1285
1286
1287
1288 public void setReportOutputDirectory( File reportOutputDirectory )
1289 {
1290 super.setReportOutputDirectory( reportOutputDirectory );
1291 this.outputDirectory = reportOutputDirectory;
1292 }
1293 }