private Report generateReport( Locale locale )
throws MavenReportException
{
Sink sink = getSink();
PMD pmd = getPMD();
RuleContext ruleContext = new RuleContext();
Report report = new Report();
PmdReportListener reportSink = new PmdReportListener( sink, getBundle( locale ), aggregate );
report.addListener( reportSink );
ruleContext.setReport( report );
reportSink.beginDocument();
RuleSetFactory ruleSetFactory = new RuleSetFactory();
ruleSetFactory.setMinimumPriority( this.minimumPriority );
RuleSet[] sets = new RuleSet[rulesets.length];
try
{
for ( int idx = 0; idx < rulesets.length; idx++ )
{
String set = rulesets[idx];
getLog().debug( "Preparing ruleset: " + set );
File ruleset = locator.getResourceAsFile( set, getLocationTemp( set ) );
if ( null == ruleset )
{
throw new MavenReportException( "Could not resolve " + set );
}
InputStream rulesInput = new FileInputStream( ruleset );
try
{
RuleSet ruleSet = ruleSetFactory.createRuleSet( rulesInput );
sets[idx] = ruleSet;
ruleSet.start( ruleContext );
}
finally
{
rulesInput.close();
}
}
}
catch ( IOException e )
{
throw new MavenReportException( e.getMessage(), e );
}
catch ( ResourceNotFoundException e )
{
throw new MavenReportException( e.getMessage(), e );
}
catch ( FileResourceCreationException e )
{
throw new MavenReportException( e.getMessage(), e );
}
Map<File, PmdFileInfo> files;
try
{
files = getFilesToProcess();
}
catch ( IOException e )
{
throw new MavenReportException( "Can't get file list", e );
}
if ( StringUtils.isEmpty( getSourceEncoding() ) && !files.isEmpty() )
{
getLog().warn( "File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING
+ ", i.e. build is platform dependent!" );
}
for ( Map.Entry<File, PmdFileInfo> entry : files.entrySet() )
{
File file = entry.getKey();
PmdFileInfo fileInfo = entry.getValue();
// TODO: lazily call beginFile in case there are no rules
reportSink.beginFile( file, fileInfo );
ruleContext.setSourceCodeFilename( file.getAbsolutePath() );
for ( int idx = 0; idx < rulesets.length; idx++ )
{
try
{
// PMD closes this Reader even though it did not open it so we have
// to open a new one with every call to processFile().
Reader reader;
if ( StringUtils.isNotEmpty( getSourceEncoding() ) )
{
reader = ReaderFactory.newReader( file, getSourceEncoding() );
}
else
{
reader = ReaderFactory.newPlatformReader( file );
}
try
{
pmd.processFile( reader, sets[idx], ruleContext );
}
finally
{
reader.close();
}