View Javadoc

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