r1912 - in trunk/maven-i18n-plugin/src: main/java/org/nuiton/i18n/plugin/bundle test/java/org/nuiton/i18n/plugin/bundle
Author: tchemit Date: 2011-05-10 13:15:01 +0200 (Tue, 10 May 2011) New Revision: 1912 Url: http://nuiton.org/repositories/revision/i18n/1912 Log: improve converter from String.format to MessageFormat Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormat.java trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormatTest.java Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormat.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormat.java 2011-05-10 10:35:33 UTC (rev 1911) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormat.java 2011-05-10 11:15:01 UTC (rev 1912) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2007 - 2011 CodeLutin + * Copyright (C) 2007 - 2011 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 @@ -24,26 +24,28 @@ */ package org.nuiton.i18n.plugin.bundle; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import java.text.MessageFormat; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - /** - * Converter used to change printf args syntax by {@link MessageFormat} syntax + * Converter used to change printf args syntax by {@link MessageFormat} syntax. * <p/> * Created: 05/05/11 * * @author fdesbois <desbois@codelutin.com> - * $Id$ + * @author tchemit <chemit@codelutin.com> * @plexus.component role="org.nuiton.i18n.plugin.bundle.BundleFormatConverter" role-hint="toMessageFormat" * @since 2.4 */ public class StringFormatToMessageFormat implements BundleFormatConverter { - private static final Log log = LogFactory.getLog(StringFormatToMessageFormat.class); + private static final Log log = + LogFactory.getLog(StringFormatToMessageFormat.class); protected static Pattern PATTERN = Pattern.compile("%\\$?(\\d?)[^\\s']*"); @@ -55,7 +57,9 @@ Matcher matcher = PATTERN.matcher(value); boolean matches = matcher.find(); - log.debug("> value : " + value + " _ matches ? " + matches); + if (log.isDebugEnabled()) { + log.debug("> value : " + value + " _ matches ? " + matches); + } if (matches) { @@ -63,33 +67,58 @@ matcher.reset(); StringBuffer sb = new StringBuffer(); - for (int i = 0; matcher.find(); i++) { + int i = 0; - log.debug("> match group : " + matcher.group(0)); - log.debug("> match group for number : " + matcher.group(1)); + while (matcher.find()) { - int nb = i; + if (log.isDebugEnabled()) { + log.debug("> match group : " + matcher.group(0)); + log.debug("> match group for number : " + matcher.group(1)); + } - // Group 1 is the arg number, for MessageFormat it starts from 0 - // compare to printf that starts from 1 - if (!"".equals(matcher.group(1))) { - nb = Integer.parseInt(matcher.group(1)) - 1; + String argNumber = matcher.group(1); + + int nb; + + if (StringUtils.isNotEmpty(argNumber)) { + + // there is a arg position number, so must use the -1 value + nb = Integer.parseInt(argNumber) - 1; + } else { + + // use the current argument position value + nb = i; } +// int nb = i; +// // Group 1 is the arg number, for MessageFormat it starts from 0 +// // compare to printf that starts from 1 +// if (!"".equals(matcher.group(1))) { +// nb = Integer.parseInt(matcher.group(1)) - 1; +// } + // Append replacement for current occurence matcher.appendReplacement(sb, "\\{" + nb + "\\}"); + i++; } // Append last chars from input String matcher.appendTail(sb); result = sb.toString(); - log.debug("Result : " + result); + if (log.isDebugEnabled()) { + log.debug("Result : " + result); + } - result = escapeQuoteChar(result); +// result = escapeQuoteChar(result); } else { + + // there is no argument in incoming string value result = value; } + + result = escapeQuoteChar(result); + return result; } @@ -102,15 +131,18 @@ protected String escapeQuoteChar(String value) { // Replace ' by '' - String result = value.replaceAll("([^'])'([^'])", "$1''$2"); + String result = value.replaceAll("'", "''"); +// String result = value.replaceAll("([^'])'([^'])", "$1''$2"); // Manage ' as last char - result = result.replaceFirst("([^'])'$", "$1''"); +// result = result.replaceFirst("([^'])'$", "$1''"); // Manage ' as first char - result = result.replaceFirst("^'([^'])", "''$1"); +// result = result.replaceFirst("^'([^'])", "''$1"); - log.debug("Result with ' escape : " + result); + if (log.isDebugEnabled()) { + log.debug("Result with ' escape : " + result); + } return result; } Modified: trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormatTest.java =================================================================== --- trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormatTest.java 2011-05-10 10:35:33 UTC (rev 1911) +++ trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormatTest.java 2011-05-10 11:15:01 UTC (rev 1912) @@ -28,41 +28,70 @@ import org.junit.Test; /** + * Tests the {@link StringFormatToMessageFormat}. + * <p/> * Created: 05/05/11 * * @author fdesbois <desbois@codelutin.com> - * $Id$ */ public class StringFormatToMessageFormatTest { + BundleFormatConverter converter; + @Test public void testConvert() throws Exception { - BundleFormatConverter converter = new StringFormatToMessageFormat(); - String expected = "Salut à tous pour l'ouverture"; - String actual = converter.convert("Salut à tous pour l'ouverture"); - Assert.assertEquals(expected, actual); + converter = new StringFormatToMessageFormat(); - expected = "Salut à tous pour l''ouverture de {0}"; - actual = converter.convert("Salut à tous pour l'ouverture de %s"); - Assert.assertEquals(expected, actual); +// String expected; +// String actual; - expected = "Salut à tous pour l''ouverture de {0}"; - actual = converter.convert("Salut à tous pour l'ouverture de %$1s"); - Assert.assertEquals(expected, actual); + convertAndAssertEquals("Salut à tous pour l'ouverture", + "Salut à tous pour l''ouverture"); - expected = "Salut à tous pour l''ouverture de {1} le {0}"; - actual = converter.convert("Salut à tous pour l'ouverture de %$2s le %$1t"); - Assert.assertEquals(expected, actual); + convertAndAssertEquals("Salut à tous pour l''ouverture", + "Salut à tous pour l''''ouverture"); - expected = "Salut à tous pour l''ouverture de {1} le {0} à l''heure {0}"; - actual = converter.convert("Salut à tous pour l'ouverture de %$2s le %$1td à l'heure %1tH"); - Assert.assertEquals(expected, actual); +// actual = converter.convert("Salut à tous pour l'ouverture"); +// Assert.assertEquals(expected, actual); - expected = "Salut à tous pour l''ouverture de ''{0}''"; - actual = converter.convert("Salut à tous pour l''ouverture de '%$1s'"); + convertAndAssertEquals("Salut à tous pour l'ouverture de %s", + "Salut à tous pour l''ouverture de {0}"); +// expected = "Salut à tous pour l''ouverture de {0}"; +// actual = converter.convert("Salut à tous pour l'ouverture de %s"); +// Assert.assertEquals(expected, actual); + + convertAndAssertEquals("Salut à tous pour l'ouverture de %$1s", + "Salut à tous pour l''ouverture de {0}"); +// expected = "Salut à tous pour l''ouverture de {0}"; +// actual = converter.convert("Salut à tous pour l'ouverture de %$1s"); +// Assert.assertEquals(expected, actual); + + convertAndAssertEquals("Salut à tous pour l'ouverture de %$2s le %$1t", + "Salut à tous pour l''ouverture de {1} le {0}" + ); +// expected = "Salut à tous pour l''ouverture de {1} le {0}"; +// actual = converter.convert("Salut à tous pour l'ouverture de %$2s le %$1t"); +// Assert.assertEquals(expected, actual); + + convertAndAssertEquals( + "Salut à tous pour l'ouverture de %$2s le %$1td à l'heure %1tH", + "Salut à tous pour l''ouverture de {1} le {0} à l''heure {0}" + ); +// expected = "Salut à tous pour l''ouverture de {1} le {0} à l''heure {0}"; +// actual = converter.convert("Salut à tous pour l'ouverture de %$2s le %$1td à l'heure %1tH"); +// Assert.assertEquals(expected, actual); + + convertAndAssertEquals("Salut à tous pour l''ouverture de '%$1s'", + "Salut à tous pour l''''ouverture de ''{0}''"); +// expected = "Salut à tous pour l''ouverture de ''{0}''"; +// actual = converter.convert("Salut à tous pour l''ouverture de '%$1s'"); +// Assert.assertEquals(expected, actual); + } + + protected void convertAndAssertEquals(String incoming, String expected) { + String actual = converter.convert(incoming); Assert.assertEquals(expected, actual); } - }
participants (1)
-
tchemit@users.nuiton.org