You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2010/04/20 23:49:35 UTC

svn commit: r936073 - in /maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd: AbstractPmdReport.java PmdReport.java

Author: hboutemy
Date: Tue Apr 20 21:49:34 2010
New Revision: 936073

URL: http://svn.apache.org/viewvc?rev=936073&view=rev
Log:
code refactoring

Modified:
    maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java
    maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java

Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java?rev=936073&r1=936072&r2=936073&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java (original)
+++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java Tue Apr 20 21:49:34 2010
@@ -261,7 +261,7 @@ public abstract class AbstractPmdReport
      * @return a List of the files where the PMD tool will be executed
      * @throws java.io.IOException
      */
-    protected Map getFilesToProcess( )
+    protected Map getFilesToProcess()
         throws IOException
     {
         String sourceXref = constructXRefLocation( false );
@@ -413,7 +413,7 @@ public abstract class AbstractPmdReport
         }
         try
         {
-            Map filesToProcess = getFilesToProcess( );
+            Map filesToProcess = getFilesToProcess();
             if ( filesToProcess.isEmpty() )
             {
                 return false;

Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java?rev=936073&r1=936072&r2=936073&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java (original)
+++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java Tue Apr 20 21:49:34 2010
@@ -165,178 +165,197 @@ public class PmdReport
             {
                 Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() );
 
-                Sink sink = getSink();
+                Report report = generateReport( locale );
 
-                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
-                        {
-                            sets[idx] = ruleSetFactory.createRuleSet( rulesInput );
-                        }
-                        finally
-                        {
-                            rulesInput.close();
-                        }
-                    }
-                }
-                catch ( IOException e )
-                {
-                    throw new MavenReportException( e.getMessage(), e );
-                }
-                catch ( ResourceNotFoundException e )
+                if ( !isHtml() )
                 {
-                    throw new MavenReportException( e.getMessage(), e );
+                    renderPmdFormat( report );
                 }
-                catch ( FileResourceCreationException e )
+            }
+            finally
+            {
+                Thread.currentThread().setContextClassLoader( origLoader );
+            }
+        }
+    }
+
+    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( e.getMessage(), e );
+                    throw new MavenReportException( "Could not resolve " + set );
                 }
 
-                Map files;
+                InputStream rulesInput = new FileInputStream( ruleset );
                 try
                 {
-                    files = getFilesToProcess( );
+                    sets[idx] = ruleSetFactory.createRuleSet( rulesInput );
                 }
-                catch ( IOException e )
+                finally
                 {
-                    throw new MavenReportException( "Can't get file list", e );
+                    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 );
+        }
 
-                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!" );
-                }
+        Map files;
+        try
+        {
+            files = getFilesToProcess( );
+        }
+        catch ( IOException e )
+        {
+            throw new MavenReportException( "Can't get file list", e );
+        }
 
-                for ( Iterator i = files.entrySet().iterator(); i.hasNext(); )
-                {
-                    Map.Entry entry = (Map.Entry) i.next();
-                    File file = (File) entry.getKey();
-                    PmdFileInfo fileInfo = (PmdFileInfo) 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();
-                            }
-                        }
-                        catch ( UnsupportedEncodingException e1 )
-                        {
-                            throw new MavenReportException( "Encoding '" + getSourceEncoding() + "' is not supported.", e1 );
-                        }
-                        catch ( PMDException pe )
-                        {
-                            String msg = pe.getLocalizedMessage();
-                            Throwable r = pe.getCause();
-                            if ( r != null )
-                            {
-                                msg = msg + ": " + r.getLocalizedMessage();
-                            }
-                            getLog().warn( msg );
-                            reportSink.ruleViolationAdded(
-                                new ProcessingErrorRuleViolation( file, msg ) );
-                        }
-                        catch ( FileNotFoundException e2 )
-                        {
-                            getLog().warn( "Error opening source file: " + file );
-                            reportSink.ruleViolationAdded(
-                                new ProcessingErrorRuleViolation( file, e2.getLocalizedMessage() ) );
-                        }
-                        catch ( Exception e3 )
-                        {
-                            getLog().warn( "Failure executing PMD for: " + file, e3 );
-                            reportSink.ruleViolationAdded(
-                                new ProcessingErrorRuleViolation( file, e3.getLocalizedMessage() ) );
-                        }
-                    }
-                    reportSink.endFile( file );
-                }
+        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!" );
+        }
 
-                reportSink.endDocument();
+        for ( Iterator i = files.entrySet().iterator(); i.hasNext(); )
+        {
+            Map.Entry entry = (Map.Entry) i.next();
+            File file = (File) entry.getKey();
+            PmdFileInfo fileInfo = (PmdFileInfo) entry.getValue();
 
-                if ( !isHtml() )
+            // 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
                 {
-                    // Use the PMD renderers to render in any format aside from HTML.
-                    Renderer r = createRenderer();
-                    Writer writer = null;
+                    // 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
                     {
-                        File targetFile = new File( targetDirectory, "pmd." + format );
-                        FileOutputStream tStream = new FileOutputStream( targetFile );
-                        writer = new OutputStreamWriter( tStream, getOutputEncoding() );
-
-                        r.setWriter( writer );
-                        r.start();
-                        r.renderFileReport( report );
-                        r.end();
-                        writer.close();
-
-                        File siteDir = getReportOutputDirectory();
-                        siteDir.mkdirs();
-                        FileUtils.copyFile( targetFile, new File( siteDir, "pmd." + format ) );
+                        pmd.processFile( reader, sets[idx], ruleContext );
                     }
-                    catch ( IOException ioe )
+                    finally
                     {
-                        throw new MavenReportException( ioe.getMessage(), ioe );
+                        reader.close();
                     }
-                    finally
+                }
+                catch ( UnsupportedEncodingException e1 )
+                {
+                    throw new MavenReportException( "Encoding '" + getSourceEncoding() + "' is not supported.", e1 );
+                }
+                catch ( PMDException pe )
+                {
+                    String msg = pe.getLocalizedMessage();
+                    Throwable r = pe.getCause();
+                    if ( r != null )
                     {
-                        IOUtil.close( writer );
+                        msg = msg + ": " + r.getLocalizedMessage();
                     }
+                    getLog().warn( msg );
+                    reportSink.ruleViolationAdded( new ProcessingErrorRuleViolation( file, msg ) );
+                }
+                catch ( FileNotFoundException e2 )
+                {
+                    getLog().warn( "Error opening source file: " + file );
+                    reportSink.ruleViolationAdded(
+                        new ProcessingErrorRuleViolation( file, e2.getLocalizedMessage() ) );
+                }
+                catch ( Exception e3 )
+                {
+                    getLog().warn( "Failure executing PMD for: " + file, e3 );
+                    reportSink.ruleViolationAdded(
+                        new ProcessingErrorRuleViolation( file, e3.getLocalizedMessage() ) );
                 }
             }
-            finally
-            {
-                Thread.currentThread().setContextClassLoader( origLoader );
-            }
+            reportSink.endFile( file );
+        }
+
+        reportSink.endDocument();
+
+        return report;
+    }
+
+    /**
+     * Use the PMD renderers to render in any format aside from HTML.
+     *
+     * @param report
+     * @throws MavenReportException
+     */
+    private void renderPmdFormat( Report report )
+        throws MavenReportException
+    {
+        Renderer r = createRenderer();
+
+        Writer writer = null;
+
+        try
+        {
+            File targetFile = new File( targetDirectory, "pmd." + format );
+            FileOutputStream tStream = new FileOutputStream( targetFile );
+            writer = new OutputStreamWriter( tStream, getOutputEncoding() );
+
+            r.setWriter( writer );
+            r.start();
+            r.renderFileReport( report );
+            r.end();
+            writer.close();
+
+            File siteDir = getReportOutputDirectory();
+            siteDir.mkdirs();
+            FileUtils.copyFile( targetFile, new File( siteDir, "pmd." + format ) );
+        }
+        catch ( IOException ioe )
+        {
+            throw new MavenReportException( ioe.getMessage(), ioe );
+        }
+        finally
+        {
+            IOUtil.close( writer );
         }
     }
 
@@ -434,7 +453,7 @@ public class PmdReport
             catch ( Exception e )
             {
                 throw new MavenReportException(
-                    "Can't find the custom format " + format + ": " + e.getClass().getName() );
+                    "Can't find the custom format " + format + ": " + e.getClass().getName(), e );
             }
         }