001/*
002 *  jDTAUS Core Resource Mojo
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.mojo.resource.util;
022
023import java.io.Writer;
024import java.text.SimpleDateFormat;
025import java.util.Date;
026import java.util.Properties;
027import org.apache.velocity.VelocityContext;
028import org.apache.velocity.app.VelocityEngine;
029import org.jdtaus.mojo.resource.model.Implementation;
030import org.jdtaus.mojo.resource.model.ModelManager;
031import org.jdtaus.mojo.resource.model.Module;
032
033/**
034 * Generates source code of an accessor class to a {@code ResourceBundle}.
035 *
036 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
037 * @version $JDTAUS: BundleGenerator.java 8641 2012-09-27 06:45:17Z schulte $
038 * @plexus.component role="org.jdtaus.mojo.resource.util.BundleGenerator"
039 *                   role-hint="default"
040 */
041public class BundleGenerator
042{
043
044    /** Name of the generator. */
045    private static final String GENERATOR_NAME =
046        BundleGenerator.class.getName();
047
048    /** Constant for the version of the generator. */
049    private static final String GENERATOR_VERSION = "3.3";
050
051    /** Name of the velocity classpath resource loader implementation. */
052    private static final String VELOCITY_RESOURCE_LOADER =
053        "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader";
054
055    /** Location of the {@code Bundle.java.vm} template. */
056    private static final String TEMPLATE_LOCATION =
057        "META-INF/templates/Bundle.java.vm";
058
059    /** {@code VelocityEngine} of the generator. */
060    private VelocityEngine velocityEngine;
061
062    /**
063     * {@code ModelManager} of the genrator.
064     * @plexus.requirement
065     */
066    private ModelManager modelManager;
067
068    /**
069     * Generates a java source file for a bundle.
070     *
071     * @param module the module defining the {@code bundle}.
072     * @param implementation the implementation to generate a java source file
073     * for.
074     * @param writer the writer to write the java source file to.
075     *
076     * @throws Exception if generating the bundle fails.
077     */
078    public void generateJava( final Module module,
079                              final Implementation implementation,
080                              final Writer writer )
081        throws Exception
082    {
083        final VelocityContext ctx = new VelocityContext();
084        ctx.put( "module", module );
085        ctx.put( "implementation", implementation );
086        ctx.put( "modelManager", this.modelManager );
087        ctx.put( "generatorName", GENERATOR_NAME );
088        ctx.put( "generatorVersion", GENERATOR_VERSION );
089        ctx.put( "templateLocation", TEMPLATE_LOCATION );
090        ctx.put( "comment", Boolean.TRUE );
091        ctx.put( "now", new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ).
092                 format( new Date() ) );
093
094        this.getVelocity().mergeTemplate(
095            TEMPLATE_LOCATION, "UTF-8", ctx, writer );
096
097    }
098
099    /**
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}