Coverage Report - org.apache.maven.plugin.checkstyle.rss.DefaultCheckstyleRssGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultCheckstyleRssGenerator
65%
19/29
50%
1/2
6.5
 
 1  
 package org.apache.maven.plugin.checkstyle.rss;
 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 java.io.IOException;
 23  
 
 24  
 import org.apache.maven.plugin.MojoExecutionException;
 25  
 import org.apache.maven.plugin.checkstyle.CheckstyleReport;
 26  
 import org.apache.maven.plugin.checkstyle.CheckstyleResults;
 27  
 import org.apache.maven.plugin.checkstyle.VelocityTemplate;
 28  
 import org.apache.maven.reporting.MavenReportException;
 29  
 import org.apache.velocity.VelocityContext;
 30  
 import org.apache.velocity.context.Context;
 31  
 import org.apache.velocity.exception.ResourceNotFoundException;
 32  
 import org.apache.velocity.exception.VelocityException;
 33  
 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 34  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.ServiceLocator;
 35  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Serviceable;
 36  
 import org.codehaus.plexus.util.StringUtils;
 37  
 import org.codehaus.plexus.velocity.VelocityComponent;
 38  
 
 39  
 import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
 40  
 
 41  
 /**
 42  
  * @author Olivier Lamy
 43  
  * @since 2.4
 44  
  * @plexus.component role="org.apache.maven.plugin.checkstyle.rss.CheckstyleRssGenerator"
 45  
  *                   role-hint="default"
 46  
  */
 47  10
 public class DefaultCheckstyleRssGenerator
 48  
     implements CheckstyleRssGenerator, Serviceable
 49  
 {
 50  
    
 51  
     /**
 52  
      * Velocity Component.
 53  
      */
 54  
     private VelocityComponent velocityComponent;
 55  
 
 56  
     /**
 57  
      * ServiceLocator used to lookup VelocityComponent
 58  
      * Fix for MCHECKSTYLE-101 to avoid VelocityComponent beeing initialized when skip=true
 59  
      */
 60  
     private ServiceLocator serviceLocator;
 61  
     
 62  
     /**
 63  
      * @see org.apache.maven.plugin.checkstyle.rss.CheckstyleRssGenerator#generateRSS(org.apache.maven.plugin.checkstyle.CheckstyleResults)
 64  
      */
 65  
     public void generateRSS( CheckstyleResults results, CheckstyleRssGeneratorRequest checkstyleRssGeneratorRequest )
 66  
         throws MavenReportException
 67  
     {
 68  7
         if ( velocityComponent == null )
 69  
         {
 70  
             try
 71  
             {
 72  7
                 velocityComponent = (VelocityComponent) serviceLocator.lookup( VelocityComponent.ROLE );
 73  
             }
 74  0
             catch ( ComponentLookupException e )
 75  
             {
 76  0
                 throw new MavenReportException( "Failed to setup Velocity", e );
 77  7
             }
 78  
         }
 79  
 
 80  7
         VelocityTemplate vtemplate = new VelocityTemplate( velocityComponent, CheckstyleReport.PLUGIN_RESOURCES );
 81  7
         vtemplate.setLog( checkstyleRssGeneratorRequest.getLog() );
 82  
 
 83  7
         Context context = new VelocityContext();
 84  7
         context.put( "results", results );
 85  7
         context.put( "project", checkstyleRssGeneratorRequest.getMavenProject() );
 86  7
         context.put( "copyright", checkstyleRssGeneratorRequest.getCopyright() );
 87  7
         context.put( "levelInfo", SeverityLevel.INFO );
 88  7
         context.put( "levelWarning", SeverityLevel.WARNING );
 89  7
         context.put( "levelError", SeverityLevel.ERROR );
 90  7
         context.put( "stringutils", new StringUtils() );
 91  
 
 92  
         try
 93  
         {
 94  7
             vtemplate.generate( checkstyleRssGeneratorRequest.getOutputDirectory().getPath() + "/checkstyle.rss", "checkstyle-rss.vm", context );
 95  
         }
 96  0
         catch ( ResourceNotFoundException e )
 97  
         {
 98  0
             throw new MavenReportException( "Unable to find checkstyle-rss.vm resource.", e );
 99  
         }
 100  0
         catch ( MojoExecutionException e )
 101  
         {
 102  0
             throw new MavenReportException( "Unable to generate checkstyle.rss.", e );
 103  
         }
 104  0
         catch ( VelocityException e )
 105  
         {
 106  0
             throw new MavenReportException( "Unable to generate checkstyle.rss.", e );
 107  
         }
 108  0
         catch ( IOException e )
 109  
         {
 110  0
             throw new MavenReportException( "Unable to generate checkstyle.rss.", e );
 111  7
         }
 112  7
     }
 113  
     
 114  
     /** {@inheritDoc} */
 115  
     public void service( ServiceLocator locator )
 116  
     {
 117  10
         this.serviceLocator = locator;
 118  10
     }
 119  
 }