Coverage Report - org.apache.maven.plugin.checkstyle.CheckstyleReportListener
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckstyleReportListener
0%
0/42
0%
0/10
1.333
 
 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.Iterator;
 32  
 import java.util.List;
 33  
 
 34  
 /**
 35  
  * Listener in charge of receiving events from the Checker.
 36  
  *
 37  
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 38  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 39  
  * @version $Id$
 40  
  */
 41  
 public class CheckstyleReportListener
 42  
     extends AutomaticBean
 43  
     implements AuditListener
 44  
 {
 45  
     private List sourceDirectories;
 46  
 
 47  
     private CheckstyleResults results;
 48  
 
 49  
     private String currentFile;
 50  
 
 51  
     private List events;
 52  
 
 53  
     private SeverityLevel severityLevel;
 54  
     
 55  
     private Configuration checkstyleConfiguration;
 56  
 
 57  
     /**
 58  
      * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
 59  
      */
 60  
     public CheckstyleReportListener( File sourceDirectory )
 61  0
     {
 62  0
         this.sourceDirectories = new ArrayList();
 63  0
         this.sourceDirectories.add( sourceDirectory );
 64  0
     }
 65  
     /**
 66  
      * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
 67  
      * @param configuration checkstyle configuration
 68  
      * @since 2.5
 69  
      */
 70  
     public CheckstyleReportListener( File sourceDirectory, Configuration configuration )
 71  0
     {
 72  0
         this.sourceDirectories = new ArrayList();
 73  0
         this.sourceDirectories.add( sourceDirectory );
 74  0
         this.checkstyleConfiguration = configuration;
 75  0
     }    
 76  
 
 77  
     /**
 78  
      * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
 79  
      */
 80  
     public void addSourceDirectory( File sourceDirectory )
 81  
     {
 82  0
         this.sourceDirectories.add( sourceDirectory );
 83  0
     }
 84  
 
 85  
     /**
 86  
      * @param severityLevel
 87  
      */
 88  
     public void setSeverityLevelFilter( SeverityLevel severityLevel )
 89  
     {
 90  0
         this.severityLevel = severityLevel;
 91  0
     }
 92  
 
 93  
     /**
 94  
      * @return
 95  
      */
 96  
     public SeverityLevel getSeverityLevelFilter()
 97  
     {
 98  0
         return severityLevel;
 99  
     }
 100  
 
 101  
     /** {@inheritDoc} */
 102  
     public void auditStarted( AuditEvent event )
 103  
     {
 104  0
         setResults( new CheckstyleResults() );
 105  0
     }
 106  
 
 107  
     /** {@inheritDoc} */
 108  
     public void auditFinished( AuditEvent event )
 109  
     {
 110  
         //do nothing
 111  0
     }
 112  
 
 113  
     /** {@inheritDoc} */
 114  
     public void fileStarted( AuditEvent event )
 115  
     {
 116  0
         for ( Iterator it = sourceDirectories.iterator(); it.hasNext(); )
 117  
         {
 118  0
             File sourceDirectory = (File) it.next();
 119  
 
 120  0
             currentFile = StringUtils.substring( event.getFileName(), sourceDirectory.getPath().length() + 1 );
 121  0
             currentFile = StringUtils.replace( currentFile, "\\", "/" );
 122  
 
 123  0
             events = getResults().getFileViolations( currentFile );
 124  0
         }
 125  
 
 126  0
         if ( events == null )
 127  
         {
 128  0
             events = new ArrayList();
 129  
         }
 130  0
     }
 131  
 
 132  
     /** {@inheritDoc} */
 133  
     public void fileFinished( AuditEvent event )
 134  
     {
 135  0
         getResults().setFileViolations( currentFile, events );
 136  0
         currentFile = null;
 137  0
     }
 138  
 
 139  
     /** {@inheritDoc} */
 140  
     public void addError( AuditEvent event )
 141  
     {
 142  0
         if ( SeverityLevel.IGNORE.equals( event.getSeverityLevel() ) )
 143  
         {
 144  0
             return;
 145  
         }
 146  
 
 147  0
         if ( severityLevel == null || severityLevel.equals( event.getSeverityLevel() ) )
 148  
         {
 149  0
             events.add( event );
 150  
         }
 151  0
     }
 152  
 
 153  
     /** {@inheritDoc} */
 154  
     public void addException( AuditEvent event, Throwable throwable )
 155  
     {
 156  
         //Do Nothing
 157  0
     }
 158  
 
 159  
     /**
 160  
      * @return
 161  
      */
 162  
     public CheckstyleResults getResults()
 163  
     {
 164  0
         results.setConfiguration( checkstyleConfiguration );
 165  0
         return results;
 166  
     }
 167  
 
 168  
     /**
 169  
      * @param results
 170  
      */
 171  
     public void setResults( CheckstyleResults results )
 172  
     {
 173  0
         this.results = results;
 174  0
     }
 175  
 
 176  
     /**
 177  
      * @since 2.5
 178  
      */
 179  
     public Configuration getCheckstyleConfiguration()
 180  
     {
 181  0
         return checkstyleConfiguration;
 182  
     }
 183  
 
 184  
     /**
 185  
      * @since 2.5
 186  
      */
 187  
     public void setCheckstyleConfiguration( Configuration checkstyleConfiguration )
 188  
     {
 189  0
         this.checkstyleConfiguration = checkstyleConfiguration;
 190  0
     }
 191  
 
 192  
 }
 193