
org.nuiton.i18n.plugin.AbstractI18nMojo Maven / Gradle / Ivy
/*
* #%L
* I18n :: Maven Plugin
*
* $Id: AbstractI18nMojo.java 1961 2012-07-09 13:12:35Z tchemit $
* $HeadURL: http://svn.nuiton.org/svn/i18n/tags/i18n-2.5.2/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/AbstractI18nMojo.java $
* %%
* Copyright (C) 2007 - 2010 CodeLutin, Tony Chemit
* %%
* 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;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.nuiton.i18n.I18nUtil;
import org.nuiton.plugin.AbstractPlugin;
import org.nuiton.plugin.PluginHelper;
import org.nuiton.plugin.PluginWithEncoding;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* Lower level i18n mojo abstraction.
*
* We defines here all commons parameters and shared behaviour.
*
* @author tchemit
* @author jruchaud
*/
public abstract class AbstractI18nMojo extends AbstractPlugin implements PluginWithEncoding {
/** Le nombre de getters détectés pendant le cycle de vie du build. */
private static int NB_GETTER_FILES;
/**
* Dependance du projet.
*
* @since 1.0.2
*/
@Component
protected MavenProject project;
/**
* Name to use as prefix of generated files.
*
* Note : By default, use the artifact id.
*/
@Parameter(property = "i18n.artifactId", defaultValue = "${project.artifactId}", readonly = true)
protected String artifactId;
/**
* Locales to treate, separated by comma.
*
* Example :
* fr_FR,en_GB
*/
@Parameter(property = "i18n.bundles", defaultValue = "fr_FR,en_GB", required = true)
protected String bundles;
/** Directory where to find project i18n files. */
@Parameter(property = "i18n.src", defaultValue = "${basedir}/src/main/resources/i18n", required = true)
protected File src;
/** Directory where to generate i18n files. */
@Parameter(property = "i18n.out", defaultValue = "${basedir}/target/generated-sources/i18n", required = true)
protected File out;
/** Encoding used to load and store properties. */
@Parameter(property = "i18n.encoding", defaultValue = "${project.build.sourceEncoding}", required = true)
protected String encoding;
/**
* To update generated files to user i18n files.
*
* Note : By default, this is active, in order to have a project uptodate
* with last i18n bundles detected.
*/
@Parameter(property = "i18n.genSrc", defaultValue = "true")
protected boolean genSrc;
/**
* Verbose flag.
*
* Note : if not setted, we used the {@code maven.verbose} property.
*/
@Parameter(property = "i18n.verbose", defaultValue = "${maven.verbose}")
protected boolean verbose;
/**
* Silent flag to see only errors in console.
*
* @since 1.0.0-rc-5
*/
@Parameter(property = "i18n.silent", defaultValue = "false")
protected boolean silent;
/**
* Strict mode to only keep in user i18n detected i18n keys and remove obsolete keys.
*
* Note : By default not active. Use this with care since it can
* delete keys. Moreover if this flag is activated, then all files will be parsed.
*/
@Parameter(property = "i18n.strictMode", defaultValue = "false")
protected boolean strictMode;
/** locales to process */
protected Locale[] locales;
@Override
protected boolean checkPackaging() {
// nothing to do on a pom module
return !acceptPackaging(Packaging.pom);
}
@Override
public void init() throws Exception {
if (verbose) {
// in verbose mode, no silent
silent = false;
getLog().info("config - verbose mode is on");
}
locales = I18nUtil.parseLocales(bundles);
if (locales == null || locales.length == 0) {
throw new IllegalStateException(
"Il faut au moins une locale declaree (utiliser " +
"la propriete 'bundles')");
}
}
public String getArtifactId() {
return artifactId;
}
/**
* @return {@code true} si des getters ont etes enregistres pendant le
* cycle de vie, {@code false} sinon.
*/
protected boolean needGeneration() {
boolean needGeneration = NB_GETTER_FILES > 0;
return needGeneration;
}
/**
* Prend en compte qu'un getter a été détecté.
*
* Cela veut dire qu'un goal de parser a détecté des clefs. Il faudra donc
* activer les goal get et gen.
*/
protected void addGetter() {
NB_GETTER_FILES++;
}
/**
* @param root le repertoire ou sont stockes les fichiers i18n
* @param artifactId le nom de l'artifact
* @param locale le nom de la locale (peut-être nulle)
* @param create {@code true} pour creer le fichier si non present
* @return le fichier i18n
* @throws IOException si probleme lors de la creation du fichier
*/
public File getI18nFile(File root,
String artifactId,
Locale locale,
boolean create) throws IOException {
String path = root.getAbsolutePath() + File.separatorChar + artifactId;
if (locale != null) {
path += "_" + locale.toString();
}
path += ".properties";
File file = new File(path);
if (create && !file.exists()) {
createNewFile(file);
}
return file;
}
/**
* @param root le repertoire ou sont stockes les fichiers getter
* @param getter le nom du getter
* @param create {@code true} pour creer le fichier si non present
* @return le fichier i18n
* @throws IOException si probleme lors de la creation du fichier
*/
public File getGetterFile(File root, String getter, boolean create)
throws IOException {
File file = new File(
root.getAbsolutePath() + File.separatorChar + getter);
if (create && !file.exists()) {
createNewFile(file);
}
return file;
}
protected void checkBundle(Locale locale,
Properties propertiesOut,
boolean showEmpty,
Map> unsafeHolder) {
// on verifie qu'il n'y a pas de traduction vide
SortedSet emptyEntries = PluginHelper.getEmptyKeys(propertiesOut);
if (!emptyEntries.isEmpty()) {
if (unsafeHolder != null) {
// push empties i18n keys in the holder
SortedSet empties = unsafeHolder.get(locale);
if (empties == null) {
empties = new TreeSet();
unsafeHolder.put(locale, empties);
}
empties.addAll(emptyEntries);
}
StringBuilder buffer = new StringBuilder();
int size = emptyEntries.size();
buffer.append("bundle ");
buffer.append(locale);
buffer.append(" contains ");
buffer.append(size);
buffer.append("/");
buffer.append(propertiesOut.size());
buffer.append(" empty entries!");
if (showEmpty) {
int index = 0;
for (String key : emptyEntries) {
buffer.append("\n - ");
buffer.append(index++);
buffer.append("/");
buffer.append(size);
buffer.append(" : ");
buffer.append(key);
}
} else {
buffer.append(" (use -Di18n.showEmpty to see these" +
" entries)");
}
getLog().warn(buffer.toString());
} else {
if (!silent && verbose) {
getLog().info("bundle " + locale + " is valid (no empty" +
" entries).");
}
}
}
@Override
public File getBackupFile(File file) {
return new File(file.getAbsolutePath() + "~");
}
@Override
protected void backupFile(File f) throws IOException {
File dst = getBackupFile(f);
copyFile(f, dst);
}
@Override
public MavenProject getProject() {
return project;
}
@Override
public void setProject(MavenProject project) {
this.project = project;
}
@Override
public boolean isVerbose() {
return verbose;
}
@Override
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
public boolean isSilent() {
return silent;
}
public boolean isStrictMode() {
return strictMode;
}
@Override
public String getEncoding() {
return encoding;
}
@Override
public void setEncoding(String encoding) {
this.encoding = encoding;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy