001/*
002 *   Copyright (C) 2005 Christian Schulte <cs@schulte.it>
003 *   All rights reserved.
004 *
005 *   Redistribution and use in source and binary forms, with or without
006 *   modification, are permitted provided that the following conditions
007 *   are met:
008 *
009 *     o Redistributions of source code must retain the above copyright
010 *       notice, this list of conditions and the following disclaimer.
011 *
012 *     o Redistributions in binary form must reproduce the above copyright
013 *       notice, this list of conditions and the following disclaimer in
014 *       the documentation and/or other materials provided with the
015 *       distribution.
016 *
017 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 *
028 *   $JOMC: AbstractResourcesWriteMojo.java 5266 2016-05-08 11:53:06Z schulte $
029 *
030 */
031package org.jomc.mojo;
032
033import java.io.File;
034import java.util.Locale;
035import java.util.logging.Level;
036import javax.xml.bind.JAXBContext;
037import javax.xml.bind.util.JAXBSource;
038import javax.xml.transform.Source;
039import org.apache.maven.model.Resource;
040import org.apache.maven.plugin.MojoExecutionException;
041import org.apache.maven.plugins.annotations.Parameter;
042import org.apache.maven.project.MavenProject;
043import org.jomc.model.Module;
044import org.jomc.modlet.ModelContext;
045import org.jomc.modlet.ModelValidationReport;
046import org.jomc.modlet.ObjectFactory;
047import org.jomc.tools.ResourceFileProcessor;
048
049/**
050 * Base class for writing resource files.
051 *
052 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
053 * @version $JOMC: AbstractResourcesWriteMojo.java 5266 2016-05-08 11:53:06Z schulte $
054 */
055public abstract class AbstractResourcesWriteMojo extends AbstractJomcMojo
056{
057
058    /**
059     * Constant for the name of the tool backing the class.
060     */
061    private static final String TOOLNAME = "ResourceFileProcessor";
062
063    /**
064     * The language of the default language properties file of generated resource bundle properties resources.
065     */
066    @Parameter( name = "resourceBundleDefaultLanguage",
067                property = "jomc.resourceBundleDefaultLanguage" )
068    private String resourceBundleDefaultLanguage;
069
070    /**
071     * Creates a new {@code AbstractResourcesWriteMojo} instance.
072     */
073    public AbstractResourcesWriteMojo()
074    {
075        super();
076    }
077
078    @Override
079    protected final void executeTool() throws Exception
080    {
081        this.logSeparator();
082
083        if ( this.isResourceProcessingEnabled() )
084        {
085            this.logProcessingModule( TOOLNAME, this.getResourcesModuleName() );
086
087            final ModelContext context = this.createModelContext( this.getResourcesClassLoader() );
088            final ResourceFileProcessor tool = this.createResourceFileProcessor( context );
089            final JAXBContext jaxbContext = context.createContext( this.getModel() );
090            final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( tool.getModel() ) );
091            final ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
092
093            if ( this.resourceBundleDefaultLanguage != null )
094            {
095                tool.setResourceBundleDefaultLocale(
096                    new Locale( this.resourceBundleDefaultLanguage.toLowerCase( Locale.ENGLISH ) ) );
097
098            }
099
100            this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
101
102            if ( validationReport.isModelValid() )
103            {
104                final Module module =
105                    tool.getModules() != null ? tool.getModules().getModule( this.getResourcesModuleName() ) : null;
106
107                if ( module != null )
108                {
109                    if ( !this.getResourcesDirectory().exists() && !this.getResourcesDirectory().mkdirs() )
110                    {
111                        throw new MojoExecutionException( Messages.getMessage(
112                            "failedCreatingDirectory", this.getResourcesDirectory().getAbsolutePath() ) );
113
114                    }
115
116                    tool.writeResourceBundleResourceFiles( module, this.getResourcesDirectory() );
117
118                    if ( !this.getResourcesDirectory().equals( this.getResourcesOutputDirectory() ) )
119                    {
120                        this.copyDirectory( this.getResourcesDirectory(), this.getResourcesOutputDirectory() );
121                    }
122
123                    final Resource resource = new Resource();
124                    resource.setDirectory( this.getResourcesDirectory().getAbsolutePath() );
125                    resource.setFiltering( false );
126
127                    this.addMavenResource( this.getMavenProject(), resource );
128
129                    this.logToolSuccess( TOOLNAME );
130                }
131                else
132                {
133                    this.logMissingModule( this.getResourcesModuleName() );
134                }
135            }
136            else
137            {
138                throw new MojoExecutionException( Messages.getMessage( "resourceProcessingFailure" ) );
139            }
140        }
141        else if ( this.isLoggable( Level.INFO ) )
142        {
143            this.log( Level.INFO, Messages.getMessage( "resourceProcessingDisabled" ), null );
144        }
145    }
146
147    /**
148     * Gets the name of the module to write resource files of.
149     *
150     * @return The name of the module to write resource files of.
151     *
152     * @throws MojoExecutionException if getting the name fails.
153     */
154    protected abstract String getResourcesModuleName() throws MojoExecutionException;
155
156    /**
157     * Gets the class loader to use for writing resource files.
158     *
159     * @return The class loader to use for writing resource files.
160     *
161     * @throws MojoExecutionException if getting the class loader fails.
162     */
163    protected abstract ClassLoader getResourcesClassLoader() throws MojoExecutionException;
164
165    /**
166     * Gets the directory to write the resource files to.
167     *
168     * @return The directory to write the resource files to.
169     *
170     * @throws MojoExecutionException if getting the directory fails.
171     */
172    protected abstract File getResourcesDirectory() throws MojoExecutionException;
173
174    /**
175     * Gets the directory to copy resource files to.
176     *
177     * @return The directory to copy resource files to.
178     *
179     * @throws MojoExecutionException if getting the directory fails.
180     *
181     * @since 1.2
182     */
183    protected abstract File getResourcesOutputDirectory() throws MojoExecutionException;
184
185    /**
186     * Adds a resource to a {@code MavenProjecŧ}.
187     *
188     * @param mavenProject The {@code MavenProject} to add a resource to.
189     * @param resource The {@code Resource} to add.
190     *
191     * @throws MojoExecutionException if adding the resource fails.
192     *
193     * @since 1.2
194     */
195    protected abstract void addMavenResource( MavenProject mavenProject, Resource resource )
196        throws MojoExecutionException;
197
198}