Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/LoggerStreamConsumer.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/LoggerStreamConsumer.java:1.1 --- /dev/null Tue Oct 23 13:36:17 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/LoggerStreamConsumer.java Tue Oct 23 13:36:11 2007 @@ -0,0 +1,33 @@ +package org.codelutin.i18n.plugin; + +import org.apache.maven.plugin.logging.Log; +import org.codehaus.plexus.util.cli.StreamConsumer; + +public class LoggerStreamConsumer implements StreamConsumer { + + public static final int DEBUG = 0; + public static final int INFO = 1; + public static final int WARN = 2; + public static final int ERROR = 3; + + private Log logger; + private int loglevel; + + public LoggerStreamConsumer(Log logger, int loglevel) { + this.logger = logger; + this.loglevel = loglevel; + } + + public void consumeLine(String line) { + if (loglevel == DEBUG) { + logger.debug(line); + } else if (loglevel == INFO) { + logger.info(line); + } else if (loglevel == WARN) { + logger.warn(line); + } else if (loglevel == ERROR) { + logger.error(line); + } + } + +} Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Gettext.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Gettext.java:1.1 --- /dev/null Tue Oct 23 13:36:17 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Gettext.java Tue Oct 23 13:36:12 2007 @@ -0,0 +1,110 @@ +package org.codelutin.i18n.plugin; + +import java.io.File; +import java.io.IOException; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.codehaus.plexus.util.DirectoryScanner; +import org.codehaus.plexus.util.cli.CommandLineException; +import org.codehaus.plexus.util.cli.CommandLineUtils; +import org.codehaus.plexus.util.cli.Commandline; +import org.codehaus.plexus.util.cli.StreamConsumer; +import org.codelutin.util.FileUtil; +import org.codelutin.util.StringUtil; + +/** + * + * @author julien + * @goal gettext + */ +public class Gettext extends AbstractI18nPlugin { + + /** + * @description Source Encoding. + * @parameter expression="${i18n.encoding}" default-value="utf-8" + */ + protected String encoding; + + /** + * @description Gettext keywords (see -k in help for details). + * @parameter expression="${i18n.keywords}" default-value="-k_ -k_n" + */ + protected String keywords; + + /** + * @description xgettext command. + * @parameter expression="${i18n.xgettextCmd}" default-value="xgettext" + */ + protected String xgettextCmd; + + /** + * @description Source java. + * @parameter expression="${i18n.srcJava}" default-value="${project.build.sourceDirectory}" + * @readonly + */ + protected File srcJava; + + /** + * @description Nom du projet. + * @parameter expression="${i18n.artifactId}" default-value="${project.artifactId}" + * @readonly + */ + protected String artifactId; + + public void execute() throws MojoExecutionException, MojoFailureException { + log.info("Exec Xgettext commande"); + + // Initialisation du répertoire de travail + out.mkdirs(); + File xgettextOut = new File(out.getAbsolutePath() + File.separatorChar + artifactId + ".po"); + + // Récupération des clés + Commandline xgettext = new Commandline(); + xgettext.setExecutable(xgettextCmd); + xgettext.createArgument().setValue("--from-code=" + encoding); + xgettext.createArgument().setValue("--output=" + xgettextOut.getAbsolutePath()); + xgettext.createArgument().setValue("--language=Java"); + xgettext.createArgument().setLine(keywords); + + DirectoryScanner ds = new DirectoryScanner(); + ds.setBasedir(srcJava); + ds.setIncludes(new String[] {"**/*.java"}); + ds.scan(); + String[] files = ds.getIncludedFiles(); + + for (int i = 0; i < files.length; i++) { + xgettext.createArgument().setValue(getAbsolutePath(files[i])); + } + + log.info("Xgettext commande : " + xgettext); + + StreamConsumer outLog = new LoggerStreamConsumer(log, LoggerStreamConsumer.INFO); + StreamConsumer errLog = new LoggerStreamConsumer(log, LoggerStreamConsumer.WARN); + + try { + CommandLineUtils.executeCommandLine(xgettext, outLog, errLog); + } catch (CommandLineException e) { + log.error("Could not execute " + xgettextCmd + ".", e); + throw new MojoFailureException("Could not execute " + xgettextCmd + "."); + } + + // Création des bundles + String[] bundlesSplit = StringUtil.split(bundles, ","); + for (String bundle : bundlesSplit) { + bundle = bundle.trim(); + File bundleOut = new File(out.getAbsolutePath() + File.separatorChar + artifactId + "-" + bundle + ".po"); + try { + FileUtil.copy(xgettextOut, bundleOut); + log.info("Generate bundle " + bundleOut.getAbsolutePath()); + } catch (IOException e) { + log.error("File Error I/O ", e); + throw new MojoFailureException("File Error I/O "); + } + } + } + + private String getAbsolutePath(String path) { + return srcJava.getAbsolutePath() + File.separator + path; + } +} Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/AbstractI18nPlugin.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/AbstractI18nPlugin.java:1.1 --- /dev/null Tue Oct 23 13:36:17 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/AbstractI18nPlugin.java Tue Oct 23 13:36:12 2007 @@ -0,0 +1,53 @@ +package org.codelutin.i18n.plugin; + +import java.io.File; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.logging.Log; + +/** + * Classe permettant d'obenir les paramètres pendant les différentes phases + * du plugin. + * + * @author julien + */ +public abstract class AbstractI18nPlugin extends AbstractMojo { + + /** + * @description Langues des bundles générés. + * @parameter expression="${i18n.bundles}" default-value="fr, en" + * @required + */ + protected String bundles; + + /** + * @description Répertoire sources des fichiers i18n. + * @parameter expression="${i18n.src}" default-value="${project.build}/resources/i18n" + * @required + */ + protected File src; + + /** + * @description Répertoire des fichiers générés i18n. + * @parameter expression="${i18n.out}" default-value="${project.build}/target/i18n" + * @required + */ + protected File out; + + /** + * @description Force la génération des fichiers properties de langues i18n. + * @parameter expression="${i18n.genProperties}" default-value="false" + */ + protected boolean genProperties; + + /** + * @description Met les fichiers générés dans le répertoire des sources i18n. + * @parameter expression="${i18n.genSrc}" default-value="true" + */ + protected boolean genSrc; + + /** + * Log + */ + protected Log log = getLog(); +}