Coverage Report - org.apache.maven.plugin.checkstyle.CheckstyleReportListener
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckstyleReportListener
82%
33/40
90%
9/10
0
 
 1  
 package org.apache.maven.plugin.checkstyle;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
 23  
 import com.puppycrawl.tools.checkstyle.api.AuditListener;
 24  
 import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
 25  
 import com.puppycrawl.tools.checkstyle.api.Configuration;
 26  
 import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
 27  
 import org.codehaus.plexus.util.StringUtils;
 28  
 
 29  
 import java.io.File;
 30  
 import java.util.ArrayList;
 31  
 import java.util.List;
 32  
 
 33  
 /**
 34  
  * Listener in charge of receiving events from the Checker.
 35  
  *
 36  
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 37  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 38  
  * @version $Id$
 39  
  */
 40  
 public class CheckstyleReportListener
 41  
     extends AutomaticBean
 42  
     implements AuditListener
 43  
 {
 44  
     private List<File> sourceDirectories;
 45  
 
 46  
     private CheckstyleResults results;
 47  
 
 48  
     private String currentFile;
 49  
 
 50  
     private List<AuditEvent> events;
 51  
 
 52  
     private SeverityLevel severityLevel;
 53  
 
 54  
     private Configuration checkstyleConfiguration;
 55  
 
 56  
     /**
 57  
      * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
 58  
      */
 59  
     public CheckstyleReportListener( File sourceDirectory )
 60  8
     {
 61  8
         this.sourceDirectories = new ArrayList<File>();
 62  8
         this.sourceDirectories.add( sourceDirectory );
 63  8
     }
 64  
     /**
 65  
      * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
 66  
      * @param configuration checkstyle configuration
 67  
      * @since 2.5
 68  
      */
 69  
     public CheckstyleReportListener( File sourceDirectory, Configuration configuration )
 70  30
     {
 71  30
         this.sourceDirectories = new ArrayList<File>();
 72  30
         this.sourceDirectories.add( sourceDirectory );
 73  30
         this.checkstyleConfiguration = configuration;
 74  30
     }
 75  
 
 76  
     /**
 77  
      * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
 78  
      */
 79  
     public void addSourceDirectory( File sourceDirectory )
 80  
     {
 81  0
         this.sourceDirectories.add( sourceDirectory );
 82  0
     }
 83  
 
 84  
     /**
 85  
      * @param severityLevel
 86  
      */
 87  
     public void setSeverityLevelFilter( SeverityLevel severityLevel )
 88  
     {
 89  8
         this.severityLevel = severityLevel;
 90  8
     }
 91  
 
 92  
     /**
 93  
      * @return
 94  
      */
 95  
     public SeverityLevel getSeverityLevelFilter()
 96  
     {
 97  8
         return severityLevel;
 98  
     }
 99  
 
 100  
     /** {@inheritDoc} */
 101  
     public void auditStarted( AuditEvent event )
 102  
     {
 103  38
         setResults( new CheckstyleResults() );
 104  38
     }
 105  
 
 106  
     /** {@inheritDoc} */
 107  
     public void auditFinished( AuditEvent event )
 108  
     {
 109  
         //do nothing
 110  38
     }
 111  
 
 112  
     /** {@inheritDoc} */
 113  
     public void fileStarted( AuditEvent event )
 114  
     {
 115  62
         for ( File sourceDirectory : sourceDirectories )
 116  
         {
 117  62
             currentFile = StringUtils.substring( event.getFileName(), sourceDirectory.getPath().length() + 1 );
 118  62
             currentFile = StringUtils.replace( currentFile, "\\", "/" );
 119  
 
 120  62
             events = getResults().getFileViolations( currentFile );
 121  
         }
 122  
 
 123  62
         if ( events == null )
 124  
         {
 125  0
             events = new ArrayList<AuditEvent>();
 126  
         }
 127  62
     }
 128  
 
 129  
     /** {@inheritDoc} */
 130  
     public void fileFinished( AuditEvent event )
 131  
     {
 132  62
         getResults().setFileViolations( currentFile, events );
 133  62
         currentFile = null;
 134  62
     }
 135  
 
 136  
     /** {@inheritDoc} */
 137  
     public void addError( AuditEvent event )
 138  
     {
 139  238
         if ( SeverityLevel.IGNORE.equals( event.getSeverityLevel() ) )
 140  
         {
 141  32
             return;
 142  
         }
 143  
 
 144  206
         if ( severityLevel == null || severityLevel.equals( event.getSeverityLevel() ) )
 145  
         {
 146  170
             events.add( event );
 147  
         }
 148  206
     }
 149  
 
 150  
     /** {@inheritDoc} */
 151  
     public void addException( AuditEvent event, Throwable throwable )
 152  
     {
 153  
         //Do Nothing
 154  0
     }
 155  
 
 156  
     /**
 157  
      * @return
 158  
      */
 159  
     public CheckstyleResults getResults()
 160  
     {
 161  160
         results.setConfiguration( checkstyleConfiguration );
 162  160
         return results;
 163  
     }
 164  
 
 165  
     /**
 166  
      * @param results
 167  
      */
 168  
     public void setResults( CheckstyleResults results )
 169  
     {
 170  38
         this.results = results;
 171  38
     }
 172  
 
 173  
     /**
 174  
      * @since 2.5
 175  
      */
 176  
     public Configuration getCheckstyleConfiguration()
 177  
     {
 178  0
         return checkstyleConfiguration;
 179  
     }
 180  
 
 181  
     /**
 182  
      * @since 2.5
 183  
      */
 184  
     public void setCheckstyleConfiguration( Configuration checkstyleConfiguration )
 185  
     {
 186  0
         this.checkstyleConfiguration = checkstyleConfiguration;
 187  0
     }
 188  
 
 189  
 }
 190