All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.i18n.plugin.bundle.AbstractMakeI18nBundleMojo Maven / Gradle / Ivy

/*
 * #%L
 * I18n :: Maven Plugin
 * 
 * $Id: AbstractMakeI18nBundleMojo.java 1993 2012-11-11 10:36:04Z tchemit $
 * $HeadURL: http://svn.nuiton.org/svn/i18n/tags/i18n-2.5.2/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java $
 * %%
 * Copyright (C) 2007 - 2010 CodeLutin
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */
package org.nuiton.i18n.plugin.bundle;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.SortedSet;

/**
 * Common mojo to all final bundle maker.
 *
 * @author tchemit 
 * @since 2.0
 */
public abstract class AbstractMakeI18nBundleMojo extends AbstractI18nBundleMojo {

    /**
     * Encoding used to load any i18n property files.
     * 

* If not defined, will use the {@link #encoding} parameter. * * @since 2.4 */ @Parameter(property = "i18n.bundleInputEncoding") protected String bundleInputEncoding; /** * Encoding used to write any i18n property files. *

* If not defined, will use the {@link #encoding} parameter. * * @since 2.4 */ @Parameter(property = "i18n.bundleOutputEncoding") protected String bundleOutputEncoding; /** * Root directory where to generate aggregated bundles (this directory will * be added as resources of the project). * * @since 1.0.0 */ @Parameter(property = "i18n.bundleOutputDir", defaultValue = "${basedir}/target/generated-sources/resources", required = true) protected File bundleOutputDir; /** * Package name of the generate aggregated bundles. *

* Note: By default we use the META-INF package * since it is the favorite package of I18n runtime initializer. *

* The package name is dotted as it will be stored as folder like in Java * language. *

* Example : *

     *     package name : foo.bar
     *     directory    : foo/bar
     * 
* * @since 2.3.2 */ @Parameter(property = "i18n.bundleOutputPackage", defaultValue = "META-INF", required = true) protected String bundleOutputPackage; /** * Name of the bundle to generate. * * @since 1.0.2 */ @Parameter(property = "i18n.bundleOutputName", defaultValue = "${project.artifactId}-i18n", required = true) protected String bundleOutputName; /** * A flag to generate a bundle with the first locale defined as a default * bundle (with no locale specialization). * * @since 2.1 */ @Parameter(property = "i18n.generateDefaultLocale", defaultValue = "false") protected boolean generateDefaultLocale; /** * A flag to check that bundles are complete (no missing i18n translations). *

* Note : This behaviour will be activated is {@link #failsIfWarning} is on. * * @since 1.0.0 */ @Parameter(property = "i18n.checkBundle", defaultValue = "true") protected boolean checkBundle; /** * A flag to show missing i18n translation. *

* Note : Need the {@link #checkBundle} to be activated). * * @since 1.0.0 */ @Parameter(property = "i18n.showEmpty", defaultValue = "false") protected boolean showEmpty; /** * A flag to make the build fails if there is some warnings while generating * bundle, says when it misses some translations. *

* Note : This parameter should be used in a release profile to * ensure bundles are complete. * * @since 2.0 */ @Parameter(property = "i18n.failsIfWarning", defaultValue = "false") protected boolean failsIfWarning; /** to keep all none translated i18n keys by locale. */ protected Map> unsafeMapping; /** * The definitive directory where to generate the bundles (includes the * package of bundle). * * @since 2.3.2 */ protected File outputFolder; @Override public void init() throws Exception { super.init(); if (failsIfWarning) { // check bundle if wants to fail on unsafe bundles checkBundle = true; unsafeMapping = new HashMap>(); } else { unsafeMapping = null; } // get the definitive folder where to generate bundles (including // bundle package) outputFolder = getBundleOutputFolder(); if (isVerbose()) { getLog().info("Will generates bundles in " + outputFolder); } createDirectoryIfNecessary(outputFolder); if (StringUtils.isEmpty(bundleInputEncoding)) { // use the default encoding bundleInputEncoding = getEncoding(); if (getLog().isDebugEnabled()) { getLog().debug("Use as input encoding the default one : " + bundleInputEncoding); } } if (StringUtils.isEmpty(bundleOutputEncoding)) { // use the default encoding bundleOutputEncoding = getEncoding(); if (getLog().isDebugEnabled()) { getLog().debug("Use as output encoding the default one : " + bundleOutputEncoding); } } } protected void failsIfWarning() throws MojoFailureException { if (!failsIfWarning) { // no check return; } if (unsafeMapping != null && !unsafeMapping.isEmpty()) { // there is at least one not complete bundle, faisl the build throw new MojoFailureException( "Bundles for locale(s) " + unsafeMapping.keySet() + " are not complete. Use the -Di18n.showEmpty to see " + "missing translations."); } } /** * Gets the bundle file for the given parameters. * * @param root the root directory where bundles are stored * @param artifactId the artifactId (says the prefix of bundle) * @param locale the locale used in bundle ({@code null} means no locale specialized) * @param create a flag to create the file if none existing * @return the bundle file * @throws IOException if any IO problem while creating it (if needed). * @since 2.1 */ protected abstract File getBundleFile(File root, String artifactId, Locale locale, boolean create) throws IOException; /** * Generates the default bundle, says the bundle with no locale specialized. *

* This bundle is a copy of the bundle of the first locale (which in fact * is considered as the main locale). * * @throws IOException if any IO problem while the copy. * @since 2.1 */ protected void generateDefaultBundle() throws IOException { File bundleFirstLocale = getBundleFile(outputFolder, bundleOutputName, locales[0], false ); File bundleWithoutLocale = getBundleFile(outputFolder, bundleOutputName, null, false ); if (!isSilent()) { getLog().info("Generate default bundle at " + bundleWithoutLocale); } FileUtils.copyFile(bundleFirstLocale, bundleWithoutLocale); } protected File getBundleOutputFolder() { File result = bundleOutputDir; if (StringUtils.isNotEmpty(bundleOutputPackage)) { String[] paths = bundleOutputPackage.split("\\."); for (String path : paths) { result = new File(result, path); } } return result; } public String getBundleOutputEncoding() { return bundleOutputEncoding; } public String getBundleInputEncoding() { return bundleInputEncoding; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy