Coverage Report - org.apache.maven.plugin.checkstyle.CheckstyleReportListener
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckstyleReportListener
88%
28/32
90%
9/10
1,417
 
 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.SeverityLevel;
 26  
 import org.codehaus.plexus.util.StringUtils;
 27  
 
 28  
 import java.io.File;
 29  
 import java.util.ArrayList;
 30  
 import java.util.Iterator;
 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: org.apache.maven.plugin.checkstyle.CheckstyleReportListener.html 816652 2012-05-08 13:52:31Z hboutemy $
 39  
  */
 40  
 public class CheckstyleReportListener
 41  
     extends AutomaticBean
 42  
     implements AuditListener
 43  
 {
 44  
     private List sourceDirectories;
 45  
 
 46  
     private CheckstyleResults results;
 47  
 
 48  
     private String currentFile;
 49  
 
 50  
     private List events;
 51  
 
 52  
     private SeverityLevel severityLevel;
 53  
 
 54  
     /**
 55  
      * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
 56  
      */
 57  
     public CheckstyleReportListener( File sourceDirectory )
 58  12
     {
 59  12
         this.sourceDirectories = new ArrayList();
 60  12
         this.sourceDirectories.add( sourceDirectory );
 61  12
     }
 62  
 
 63  
     /**
 64  
      * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
 65  
      */
 66  
     public void addSourceDirectory( File sourceDirectory )
 67  
     {
 68  0
         this.sourceDirectories.add( sourceDirectory );
 69  0
     }
 70  
 
 71  
     /**
 72  
      * @param severityLevel
 73  
      */
 74  
     public void setSeverityLevelFilter( SeverityLevel severityLevel )
 75  
     {
 76  4
         this.severityLevel = severityLevel;
 77  4
     }
 78  
 
 79  
     /**
 80  
      * @return
 81  
      */
 82  
     public SeverityLevel getSeverityLevelFilter()
 83  
     {
 84  4
         return severityLevel;
 85  
     }
 86  
 
 87  
     /**
 88  
      * @see com.puppycrawl.tools.checkstyle.api.AuditListener#auditStarted(com.puppycrawl.tools.checkstyle.api.AuditEvent)
 89  
      */
 90  
     public void auditStarted( AuditEvent event )
 91  
     {
 92  12
         setResults( new CheckstyleResults() );
 93  12
     }
 94  
 
 95  
     /**
 96  
      * @see com.puppycrawl.tools.checkstyle.api.AuditListener#auditFinished(com.puppycrawl.tools.checkstyle.api.AuditEvent)
 97  
      */
 98  
     public void auditFinished( AuditEvent event )
 99  
     {
 100  
         //do nothing
 101  12
     }
 102  
 
 103  
     /**
 104  
      * @see com.puppycrawl.tools.checkstyle.api.AuditListener#fileStarted(com.puppycrawl.tools.checkstyle.api.AuditEvent)
 105  
      */
 106  
     public void fileStarted( AuditEvent event )
 107  
     {
 108  38
         for ( Iterator it = sourceDirectories.iterator(); it.hasNext(); )
 109  
         {
 110  38
             File sourceDirectory = (File) it.next();
 111  
 
 112  38
             currentFile = StringUtils.substring( event.getFileName(), sourceDirectory.getPath().length() + 1 );
 113  38
             currentFile = StringUtils.replace( currentFile, "\\", "/" );
 114  
 
 115  38
             events = getResults().getFileViolations( currentFile );
 116  
         }
 117  
 
 118  38
         if ( events == null )
 119  
         {
 120  0
             events = new ArrayList();
 121  
         }
 122  38
     }
 123  
 
 124  
     /**
 125  
      * @see com.puppycrawl.tools.checkstyle.api.AuditListener#fileFinished(com.puppycrawl.tools.checkstyle.api.AuditEvent)
 126  
      */
 127  
     public void fileFinished( AuditEvent event )
 128  
     {
 129  38
         getResults().setFileViolations( currentFile, events );
 130  38
         currentFile = null;
 131  38
     }
 132  
 
 133  
     /**
 134  
      * @see com.puppycrawl.tools.checkstyle.api.AuditListener#addError(com.puppycrawl.tools.checkstyle.api.AuditEvent)
 135  
      */
 136  
     public void addError( AuditEvent event )
 137  
     {
 138  82
         if ( SeverityLevel.IGNORE.equals( event.getSeverityLevel() ) )
 139  
         {
 140  16
             return;
 141  
         }
 142  
 
 143  66
         if ( severityLevel == null || severityLevel.equals( event.getSeverityLevel() ) )
 144  
         {
 145  48
             events.add( event );
 146  
         }
 147  66
     }
 148  
 
 149  
     /**
 150  
      * @see com.puppycrawl.tools.checkstyle.api.AuditListener#addException(com.puppycrawl.tools.checkstyle.api.AuditEvent, java.lang.Throwable)
 151  
      */
 152  
     public void addException( AuditEvent event, Throwable throwable )
 153  
     {
 154  
         //Do Nothing
 155  0
     }
 156  
 
 157  
     /**
 158  
      * @return
 159  
      */
 160  
     public CheckstyleResults getResults()
 161  
     {
 162  87
         return results;
 163  
     }
 164  
 
 165  
     /**
 166  
      * @param results
 167  
      */
 168  
     public void setResults( CheckstyleResults results )
 169  
     {
 170  12
         this.results = results;
 171  12
     }
 172  
 }
 173