r1917 - trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle
Author: tchemit Date: 2011-05-10 22:16:51 +0200 (Tue, 10 May 2011) New Revision: 1917 Url: http://nuiton.org/repositories/revision/i18n/1917 Log: Evolution #1513: Bundle mojo can have distinct encoding to read and write properties files Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractI18nBundleMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/BundleMojo.java Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractI18nBundleMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractI18nBundleMojo.java 2011-05-10 20:11:19 UTC (rev 1916) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractI18nBundleMojo.java 2011-05-10 20:16:51 UTC (rev 1917) @@ -26,7 +26,6 @@ package org.nuiton.i18n.plugin.bundle; import org.nuiton.i18n.plugin.AbstractI18nMojo; -import org.nuiton.plugin.PluginHelper; import java.io.File; import java.io.IOException; @@ -91,16 +90,4 @@ return bundleOut; } - /** - * @param src the source file to read - * @return the url instanciated from lines of the source file. - * @throws IOException if any pb while reading file - * @deprecated since 1.2.2, use the {@link PluginHelper#getLinesAsURL(File)} - * instead. - */ - @Deprecated - public static URL[] getLinesAsURL(File src) throws IOException { - return PluginHelper.getLinesAsURL(src); - } - } Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java 2011-05-10 20:11:19 UTC (rev 1916) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java 2011-05-10 20:16:51 UTC (rev 1917) @@ -44,6 +44,26 @@ public abstract class AbstractMakeI18nBundleMojo extends AbstractI18nBundleMojo { /** + * Encoding used to load any i18n property files. + * <p/> + * If not defined, will use the {@link #encoding} parameter. + * + * @parameter expression="${i18n.bundleInputEncoding}" + * @since 2.4 + */ + protected String bundleInputEncoding; + + /** + * Encoding used to write any i18n property files. + * <p/> + * If not defined, will use the {@link #encoding} parameter. + * + * @parameter expression="${i18n.bundleOutputEncoding}" + * @since 2.4 + */ + protected String bundleOutputEncoding; + + /** * Root directory where to generate aggregated bundles (this directory will * be added as resources of the project). * @@ -53,7 +73,6 @@ */ protected File bundleOutputDir; - /** * Package name of the generate aggregated bundles. * <p/> @@ -152,13 +171,34 @@ // 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 { @@ -235,4 +275,12 @@ return result; } + public String getBundleOutputEncoding() { + return bundleOutputEncoding; + } + + public String getBundleInputEncoding() { + return bundleInputEncoding; + } + } Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/BundleMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/BundleMojo.java 2011-05-10 20:11:19 UTC (rev 1916) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/BundleMojo.java 2011-05-10 20:16:51 UTC (rev 1917) @@ -25,6 +25,8 @@ package org.nuiton.i18n.plugin.bundle; +import org.apache.commons.lang.StringUtils; +import org.apache.maven.plugin.MojoExecutionException; import org.nuiton.i18n.bundle.I18nBundleEntry; import org.nuiton.i18n.bundle.I18nBundleUtil; import org.nuiton.i18n.init.DefaultI18nInitializer; @@ -95,13 +97,31 @@ */ protected Map<String, BundleFormatConverter> bundleFormatConverters; + /** Format converter to apply if */ + protected BundleFormatConverter converter; + @Override public void init() throws Exception { super.init(); // add root bundle directory as resources of the project - + addResourceDir(bundleOutputDir, "**/*.properties"); + + if (StringUtils.isNotEmpty(bundleFormatConverter)) { + + // get converter from universe + converter = bundleFormatConverters.get(bundleFormatConverter); + + if (converter == null) { + + // unknown converter + throw new MojoExecutionException( + "There is no bundleFormatConverter named \"" + + bundleFormatConverter + "\", known ones are " + + bundleFormatConverters.keySet()); + } + } } @Override @@ -111,11 +131,18 @@ String version = getProject().getVersion(); version = PluginHelper.removeSnapshotSuffix(version); + String inputEncoding = getBundleInputEncoding(); + String outputEncoding = getBundleOutputEncoding(); + if (!silent) { getLog().info("config - resources dir : " + bundleOutputDir); getLog().info("config - package name : " + bundleOutputPackage); getLog().info("config - bundle name : " + bundleOutputName); - getLog().info("config - format converter : " + bundleFormatConverter); + getLog().info("config - input encoding : " + inputEncoding); + getLog().info("config - output encoding : " + outputEncoding); + if (bundleFormatConverter != null) { + getLog().info("config - format converter : " + bundleFormatConverter); + } getLog().info("config - locales : " + Arrays.toString(locales)); getLog().info("config - version : " + version); } @@ -134,7 +161,7 @@ ); SortedProperties propertiesOut = - new SortedProperties(encoding, false); + new SortedProperties(outputEncoding, false); StringBuilder buffer = new StringBuilder(); URL[] urls = getCollectI18nResources(locale); @@ -150,7 +177,7 @@ List<String> bundlesUrls = new ArrayList<String>(); - Charset loadEncoding = Charset.forName(encoding); + Charset loadEncoding = Charset.forName(inputEncoding); for (URL url : urls) { long t000 = System.nanoTime(); I18nBundleEntry bundleEntry = @@ -183,8 +210,8 @@ } // Apply conversion if necessary, depends on input bundleFormatConverter - if (bundleFormatConverter != null) { - applyConversion(propertiesOut, bundleFormatConverter); + if (converter != null) { + applyConversion(propertiesOut); } propertiesOut.store(bundleOut); @@ -258,20 +285,13 @@ } /** - * Apply conversion over {@code properties} with converter named - * {@code converterName}. + * Apply conversion over {@code properties} with internal converter. * * @param properties Properties to walk through - * @param converterName Name of the converter * @since 2.4 */ - protected void applyConversion(Properties properties, String converterName) { + protected void applyConversion(Properties properties) { - // Instantiate the converter - BundleFormatConverter converter = - bundleFormatConverters.get(converterName); - - // Apply conversion for (Entry<Object, Object> entry : properties.entrySet()) { String convertedValue = converter.convert((String) entry.getValue()); properties.setProperty((String) entry.getKey(), convertedValue);
participants (1)
-
tchemit@users.nuiton.org