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