Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/Generate.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/Generate.java:1.1 --- /dev/null Fri Oct 26 14:56:44 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/Generate.java Fri Oct 26 14:56:39 2007 @@ -0,0 +1,105 @@ +package org.codelutin.i18n.plugin.core; + + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Set; +import java.util.jar.JarFile; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; +import org.codelutin.util.FileUtil; + +/** + * + * @author julien + * + * @goal gen + * @phase compile + * @execute goal=merge + */ +public class Generate extends AbstractI18nPlugin { + + /** + * @description msgcat command. + * @parameter expression="${i18n.msgmergeCmd}" default-value="msgmerge" + */ + protected String msgmergeCmd; + + /** + * @description Dépendance du projet. + * @parameter default-value="${project}" + * @readonly + */ + protected MavenProject project; + + /** + * @description Target java. + * @parameter expression="${i18n.outJava}" default-value="${project.build.outputDirectory}" + * @readonly + */ + protected File outJava; + + public void execute() throws MojoExecutionException, MojoFailureException { + if(gen) { + Set artifacts = project.getArtifacts(); + + try { + for (String bundle : bundles) { + File bundleJava = new File(outJava.getAbsolutePath() + File.separatorChar + "language-" + bundle + ".properties"); + + File bundleOut = new File(out.getAbsolutePath() + File.separatorChar + "language-" + bundle + ".properties"); + concactProperties(bundleOut, bundleJava); + + for (Artifact artifact : artifacts) { + File libBundle = extract(artifact.getFile().getAbsolutePath(), "language-" + bundle + ".properties"); + + if(libBundle != null) { + concactProperties(libBundle, bundleJava); + libBundle.delete(); + } + } + FileUtil.copy(bundleJava, bundleOut); + + log.info("Generate bundle " + bundleJava.getAbsolutePath()); + } + } catch (Exception e) { + log.error("Generate Error I/O ", e); + throw new MojoFailureException("Generate Error I/O "); + } + } + } + + private File extract(String zipName, String fileName) { + try { + ZipFile file = new JarFile(zipName); + ZipEntry entry = file.getEntry("i18n/" + fileName); + if(entry != null) { + InputStream in = file.getInputStream(entry); + File target = new File(out, fileName); + + OutputStream out = new BufferedOutputStream(new FileOutputStream(target)); + byte[] buffer = new byte[BUFFER_SIZE]; + int len = 0; + while ((len = in.read(buffer, 0, BUFFER_SIZE)) != -1) { + out.write(buffer, 0, len); + } + out.close(); + in.close(); + + return target; + } + } catch (Exception e) { + log.error(e); + } + return null; + } + +} Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/Merge.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/Merge.java:1.1 --- /dev/null Fri Oct 26 14:56:44 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/Merge.java Fri Oct 26 14:56:39 2007 @@ -0,0 +1,93 @@ +package org.codelutin.i18n.plugin.core; + +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.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.i18n.plugin.util.LoggerStreamConsumer; +import org.codelutin.util.FileUtil; + +/** + * + * @author julien + * + * @goal merge + * @phase compile + * @execute goal=get + */ +public class Merge extends AbstractI18nPlugin { + + /** + * @description msgcat command. + * @parameter expression="${i18n.msgmergeCmd}" default-value="msgmerge" + */ + protected String msgmergeCmd; + + /** + * @description Target java. + * @parameter expression="${i18n.outJava}" default-value="${project.build.outputDirectory}" + * @readonly + */ + protected File outJava; + + public void execute() throws MojoExecutionException, MojoFailureException { + for (String bundle : bundles) { + // Merge + File bundleSrc = new File(src.getAbsolutePath() + File.separatorChar + "language-" + bundle + ".properties"); + File bundleOut = new File(out.getAbsolutePath() + File.separatorChar + "language-" + bundle + ".properties"); + + if(bundleSrc.exists()) { + Commandline msgmerge = new Commandline(); + msgmerge.setExecutable(msgmergeCmd); + msgmerge.createArgument().setValue("-q"); + msgmerge.createArgument().setValue("--sort-output"); + msgmerge.createArgument().setValue("--no-location"); + msgmerge.createArgument().setValue("--output-file=" + bundleOut.getAbsolutePath()); + msgmerge.createArgument().setValue("--properties-input"); + msgmerge.createArgument().setValue("--properties-output"); + msgmerge.createArgument().setValue(bundleSrc.getAbsolutePath()); + msgmerge.createArgument().setValue(bundleOut.getAbsolutePath()); + + log.info("Msgmerge command : " + msgmerge); + + StreamConsumer outLog = new LoggerStreamConsumer(log, LoggerStreamConsumer.INFO); + StreamConsumer errLog = new LoggerStreamConsumer(log, LoggerStreamConsumer.WARN); + + try { + CommandLineUtils.executeCommandLine(msgmerge, outLog, errLog); + } catch (CommandLineException e) { + log.error("Could not execute " + msgmergeCmd + ".", e); + throw new MojoFailureException("Could not execute " + msgmergeCmd + "."); + } + + + log.info("Merge bundle " + bundleSrc.getAbsolutePath()); + } + + try { + // Copie dans les sources + if(genSrc) { + if(bundleSrc.exists()) { + // Sauvegarde avant copie + FileUtil.copy(bundleSrc, new File(src.getAbsolutePath() + File.separatorChar + "language-" + bundle + ".properties" + "~")); + } + FileUtil.copy(bundleOut, bundleSrc); + log.info("Copy bundle " + bundleSrc.getAbsolutePath()); + } + + // Copie dans le classes + File bundleJava = new File(outJava.getAbsolutePath() + File.separatorChar + "i18n" + File.separatorChar + "language-" + bundle + ".properties"); + FileUtil.copy(bundleOut, bundleJava); + log.info("Copy bundle " + bundleJava.getAbsolutePath()); + } catch (IOException e) { + log.error("File Error I/O ", e); + throw new MojoFailureException("File Error I/O "); + } + } + } +} Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/Getter.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/Getter.java:1.1 --- /dev/null Fri Oct 26 14:56:44 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/Getter.java Fri Oct 26 14:56:39 2007 @@ -0,0 +1,50 @@ +package org.codelutin.i18n.plugin.core; + +import java.io.File; +import java.io.IOException; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.DirectoryScanner; +import org.codelutin.util.FileUtil; + +/** + * + * @author julien + * + * @goal get + * @phase compile + */ +public class Getter extends AbstractI18nPlugin { + + protected MavenProject project; + + public void execute() throws MojoExecutionException, MojoFailureException { + try { + File bundleGetters = new File(out.getAbsolutePath() + File.separatorChar + "language.properties"); + + DirectoryScanner ds = new DirectoryScanner(); + ds.setBasedir(out); + ds.setIncludes(new String[] {"*.getter"}); + ds.scan(); + String[] files = ds.getIncludedFiles(); + + for (int i = 0; i < files.length; i++) { + File bundleGetter = new File(out.getAbsolutePath() + File.separatorChar + files[i]); + concactProperties(bundleGetter, bundleGetters); +// bundleGetter.delete(); + } + + // Création des bundles + for (String bundle : bundles) { + File bundleOut = new File(out.getAbsolutePath() + File.separatorChar + "language-" + bundle + ".properties"); + FileUtil.copy(bundleGetters, bundleOut); + log.info("Generate bundle " + bundleOut.getAbsolutePath()); + } + } catch (IOException e) { + log.error("File Error I/O ", e); + throw new MojoFailureException("File Error I/O"); + } + } +} Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/AbstractI18nPlugin.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/AbstractI18nPlugin.java:1.1 --- /dev/null Fri Oct 26 14:56:44 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/AbstractI18nPlugin.java Fri Oct 26 14:56:39 2007 @@ -0,0 +1,84 @@ +package org.codelutin.i18n.plugin.core; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.util.Properties; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.logging.Log; +import org.codehaus.plexus.util.StringInputStream; +import org.codelutin.i18n.plugin.util.SortedProperties; + +/** + * 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="" + * @required + */ + protected String[] bundles; + + /** + * @description Répertoire sources des fichiers i18n. + * @parameter expression="${i18n.src}" default-value="${basedir}/src/i18n" + * @required + */ + protected File src; + + /** + * @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; + + /** + * @description Répertoire des fichiers générés i18n. + * @parameter expression="${i18n.out}" default-value="${basedir}/target/gen/i18n" + * @required + */ + protected File out; + + /** + * @description Récupère les fichiers de propriètés dans les dépendances du projet et génére le fichiers de propriété. + * @parameter expression="${i18n.gen}" default-value="true" + */ + protected boolean gen; + + /** + * Log + */ + protected Log log = getLog(); + + /** + * Concatène deux fichiers de propriétés + * @param in + * @param out + * @throws FileNotFoundException + * @throws IOException + */ + protected static void concactProperties(File in, File out) throws FileNotFoundException, IOException { + Properties propertiesIn = new SortedProperties(); + propertiesIn.load(new FileInputStream(in)); + + Properties propertiesOut = new SortedProperties(propertiesIn); + propertiesOut.load(new FileInputStream(out)); + propertiesOut.store(new FileOutputStream(out), null); + } + + /** Taille du buffer pour les lectures/écritures */ + protected static final int BUFFER_SIZE = 8 * 1024; + +}