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