View Javadoc
1   package org.apache.maven.plugins.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 org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.logging.Log;
24  import org.apache.maven.plugin.logging.SystemStreamLog;
25  import org.apache.velocity.context.Context;
26  import org.apache.velocity.exception.ResourceNotFoundException;
27  import org.apache.velocity.exception.VelocityException;
28  import org.codehaus.plexus.velocity.VelocityComponent;
29  
30  import java.io.File;
31  import java.io.FileOutputStream;
32  import java.io.IOException;
33  import java.io.OutputStreamWriter;
34  import java.io.Writer;
35  import java.nio.charset.StandardCharsets;
36  
37  /**
38   * <p>
39   * A component to work with Velocity templates from within plugins.
40   * <p>
41   * You will need to reference the velocity component as a parameter
42   * in your plugin.  Like this:
43   * <pre>
44   * /&#042;&#042;
45   *  &#042; Velocity Component
46   *  &#042; &#064;component
47   *  &#042;/
48   *  private VelocityComponent velocity;
49   * </pre>
50   *
51   * @author <a href="mailto:joakim@erdfelt.net">Joakim Erdfelt</a>
52   *
53   */
54  @Deprecated
55  public class VelocityTemplate
56  {
57      private String templateDirectory;
58  
59      private Log log;
60  
61      private VelocityComponent velocity;
62  
63      public VelocityTemplate( VelocityComponent velocityComponent, String templateBaseDirectory )
64      {
65          this.velocity = velocityComponent;
66          this.templateDirectory = templateBaseDirectory;
67      }
68  
69      public String getTemplateDirectory()
70      {
71          return templateDirectory;
72      }
73  
74      public VelocityComponent getVelocity()
75      {
76          return velocity;
77      }
78  
79      /**
80       * Using a specified Velocity Template and provided context, create the outputFilename.
81       *
82       * @param outputFilename the file to be generated.
83       * @param template       the velocity template to use
84       * @param context        the velocity context map
85       * @throws VelocityException if the template was not found or any other Velocity exception
86       * @throws MojoExecutionException if merging the velocity template failed
87       * @throws IOException if there was an error writing to the output file
88       */
89      public void generate( String outputFilename, String template, Context context )
90          throws VelocityException, MojoExecutionException, IOException
91      {
92  
93          File outputFile = new File( outputFilename );
94          if ( !outputFile.getParentFile().exists() )
95          {
96              outputFile.getParentFile().mkdirs();
97          }
98  
99          try ( Writer writer = new OutputStreamWriter( new FileOutputStream( outputFile ), StandardCharsets.UTF_8 ) )
100         {
101             getVelocity().getEngine().mergeTemplate( templateDirectory + "/" + template, context, writer );
102         }
103         catch ( ResourceNotFoundException e )
104         {
105             throw new ResourceNotFoundException( "Template not found: " + templateDirectory + "/" + template, e );
106         }
107         catch ( RuntimeException e )
108         {
109             throw new MojoExecutionException( e.getMessage(), e );
110         }
111     }
112 
113     public void setTemplateDirectory( String templateDirectory )
114     {
115         this.templateDirectory = templateDirectory;
116     }
117 
118     public void setVelocity( VelocityComponent velocity )
119     {
120         this.velocity = velocity;
121     }
122 
123     public Log getLog()
124     {
125         if ( this.log == null )
126         {
127             this.log = new SystemStreamLog();
128         }
129         return log;
130     }
131 
132     public void setLog( Log log )
133     {
134         this.log = log;
135     }
136 
137 }