View Javadoc

1   /*
2    *  jDTAUS Core Resource Mojo
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.mojo.resource.util;
22  
23  import java.io.Writer;
24  import java.text.SimpleDateFormat;
25  import java.util.Date;
26  import java.util.Properties;
27  import org.apache.velocity.VelocityContext;
28  import org.apache.velocity.app.VelocityEngine;
29  import org.jdtaus.mojo.resource.model.Implementation;
30  import org.jdtaus.mojo.resource.model.ModelManager;
31  import org.jdtaus.mojo.resource.model.Module;
32  
33  /**
34   * Generates source code of an accessor class to a {@code ResourceBundle}.
35   *
36   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
37   * @version $JDTAUS: BundleGenerator.java 8641 2012-09-27 06:45:17Z schulte $
38   * @plexus.component role="org.jdtaus.mojo.resource.util.BundleGenerator"
39   *                   role-hint="default"
40   */
41  public class BundleGenerator
42  {
43  
44      /** Name of the generator. */
45      private static final String GENERATOR_NAME =
46          BundleGenerator.class.getName();
47  
48      /** Constant for the version of the generator. */
49      private static final String GENERATOR_VERSION = "3.3";
50  
51      /** Name of the velocity classpath resource loader implementation. */
52      private static final String VELOCITY_RESOURCE_LOADER =
53          "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader";
54  
55      /** Location of the {@code Bundle.java.vm} template. */
56      private static final String TEMPLATE_LOCATION =
57          "META-INF/templates/Bundle.java.vm";
58  
59      /** {@code VelocityEngine} of the generator. */
60      private VelocityEngine velocityEngine;
61  
62      /**
63       * {@code ModelManager} of the genrator.
64       * @plexus.requirement
65       */
66      private ModelManager modelManager;
67  
68      /**
69       * Generates a java source file for a bundle.
70       *
71       * @param module the module defining the {@code bundle}.
72       * @param implementation the implementation to generate a java source file
73       * for.
74       * @param writer the writer to write the java source file to.
75       *
76       * @throws Exception if generating the bundle fails.
77       */
78      public void generateJava( final Module module,
79                                final Implementation implementation,
80                                final Writer writer )
81          throws Exception
82      {
83          final VelocityContext ctx = new VelocityContext();
84          ctx.put( "module", module );
85          ctx.put( "implementation", implementation );
86          ctx.put( "modelManager", this.modelManager );
87          ctx.put( "generatorName", GENERATOR_NAME );
88          ctx.put( "generatorVersion", GENERATOR_VERSION );
89          ctx.put( "templateLocation", TEMPLATE_LOCATION );
90          ctx.put( "comment", Boolean.TRUE );
91          ctx.put( "now", new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ).
92                   format( new Date() ) );
93  
94          this.getVelocity().mergeTemplate(
95              TEMPLATE_LOCATION, "UTF-8", ctx, writer );
96  
97      }
98  
99      /**
100      * Gets the {@code VelocityEngine} used for generating source code.
101      *
102      * @return the {@code VelocityEngine} used for generating source code.
103      *
104      * @throws Exception if initializing a new velocity engine fails.
105      */
106     private VelocityEngine getVelocity() throws Exception
107     {
108         if ( this.velocityEngine == null )
109         {
110             final VelocityEngine engine = new VelocityEngine();
111             final Properties props = new Properties();
112             props.put( "resource.loader", "class" );
113             props.put( "class.resource.loader.class",
114                        VELOCITY_RESOURCE_LOADER );
115 
116             engine.init( props );
117             this.velocityEngine = engine;
118         }
119 
120         return this.velocityEngine;
121     }
122 }