View Javadoc
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.exec.CheckstyleResults;
27  import org.apache.maven.reporting.MavenReportException;
28  import org.apache.velocity.VelocityContext;
29  import org.apache.velocity.context.Context;
30  import org.apache.velocity.exception.ResourceNotFoundException;
31  import org.apache.velocity.exception.VelocityException;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.component.annotations.Requirement;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.codehaus.plexus.velocity.VelocityComponent;
36  
37  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
38  
39  /**
40   * @author Olivier Lamy
41   * @since 2.4
42   */
43  @Component( role = CheckstyleRssGenerator.class, hint = "default" )
44  public class DefaultCheckstyleRssGenerator
45      implements CheckstyleRssGenerator
46  {
47      @Requirement
48      private VelocityComponent velocityComponent;
49  
50      /**
51       * @see org.apache.maven.plugin.checkstyle.rss.CheckstyleRssGenerator#generateRSS(org.apache.maven.plugin.checkstyle.exec.CheckstyleResults)
52       */
53      public void generateRSS( CheckstyleResults results, CheckstyleRssGeneratorRequest checkstyleRssGeneratorRequest )
54          throws MavenReportException
55      {
56  
57          VelocityTemplate vtemplate = new VelocityTemplate( velocityComponent, CheckstyleReport.PLUGIN_RESOURCES );
58          vtemplate.setLog( checkstyleRssGeneratorRequest.getLog() );
59  
60          Context context = new VelocityContext();
61          context.put( "results", results );
62          context.put( "project", checkstyleRssGeneratorRequest.getMavenProject() );
63          context.put( "copyright", checkstyleRssGeneratorRequest.getCopyright() );
64          context.put( "levelInfo", SeverityLevel.INFO );
65          context.put( "levelWarning", SeverityLevel.WARNING );
66          context.put( "levelError", SeverityLevel.ERROR );
67          context.put( "stringutils", new StringUtils() );
68  
69          try
70          {
71              vtemplate.generate( checkstyleRssGeneratorRequest.getOutputDirectory().getPath() + "/checkstyle.rss",
72                                  "checkstyle-rss.vm", context );
73          }
74          catch ( ResourceNotFoundException e )
75          {
76              throw new MavenReportException( "Unable to find checkstyle-rss.vm resource.", e );
77          }
78          catch ( MojoExecutionException e )
79          {
80              throw new MavenReportException( "Unable to generate checkstyle.rss.", e );
81          }
82          catch ( VelocityException e )
83          {
84              throw new MavenReportException( "Unable to generate checkstyle.rss.", e );
85          }
86          catch ( IOException e )
87          {
88              throw new MavenReportException( "Unable to generate checkstyle.rss.", e );
89          }
90      }
91      
92  }