Author: fdesbois Date: 2011-05-10 22:36:43 +0200 (Tue, 10 May 2011) New Revision: 1918 Url: http://nuiton.org/repositories/revision/i18n/1918 Log: Rename converter and remove previous commented implementation Added: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormatConverter.java trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormatConverterTest.java Removed: 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 Deleted: 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 20:16:51 UTC (rev 1917) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormat.java 2011-05-10 20:36:43 UTC (rev 1918) @@ -1,150 +0,0 @@ -/* - * #%L - * I18n :: Maven Plugin - * - * $Id$ - * $HeadURL$ - * %% - * 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 - * 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 - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -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; - -/** - * Converter used to change printf args syntax by {@link MessageFormat} syntax. - * <p/> - * Created: 05/05/11 - * - * @author fdesbois <desbois@codelutin.com> - * @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); - - protected static Pattern PATTERN = Pattern.compile("%\\$?(\\d?)[^\\s']*"); - - @Override - public String convert(String value) { - - String result; - - Matcher matcher = PATTERN.matcher(value); - boolean matches = matcher.find(); - - if (log.isDebugEnabled()) { - log.debug("> value : " + value + " _ matches ? " + matches); - } - - if (matches) { - - // Reset done, because of first find() - matcher.reset(); - - StringBuffer sb = new StringBuffer(); - int i = 0; - - while (matcher.find()) { - - if (log.isDebugEnabled()) { - log.debug("> match group : " + matcher.group(0)); - log.debug("> match group for number : " + matcher.group(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(); - - if (log.isDebugEnabled()) { - log.debug("Result : " + result); - } - -// result = escapeQuoteChar(result); - - } else { - - // there is no argument in incoming string value - result = value; - } - - result = escapeQuoteChar(result); - - return result; - } - - /** - * Escape ' char with '', needed by {@link MessageFormat}. - * - * @param value Message that contains ' - * @return the message with ' escaped - */ - protected String escapeQuoteChar(String value) { - - // Replace ' by '' - String result = value.replaceAll("'", "''"); -// String result = value.replaceAll("([^'])'([^'])", "$1''$2"); - - // Manage ' as last char -// result = result.replaceFirst("([^'])'$", "$1''"); - - // Manage ' as first char -// result = result.replaceFirst("^'([^'])", "''$1"); - - if (log.isDebugEnabled()) { - log.debug("Result with ' escape : " + result); - } - - return result; - } - -} Copied: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormatConverter.java (from rev 1915, 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/StringFormatToMessageFormatConverter.java (rev 0) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormatConverter.java 2011-05-10 20:36:43 UTC (rev 1918) @@ -0,0 +1,135 @@ +/* + * #%L + * I18n :: Maven Plugin + * + * $Id$ + * $HeadURL$ + * %% + * 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 + * 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 + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +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; + +/** + * Converter used to change printf args syntax by {@link MessageFormat} syntax. + * <p/> + * Created: 05/05/11 + * + * @author fdesbois <desbois@codelutin.com> + * @author tchemit <chemit@codelutin.com> + * @plexus.component role="org.nuiton.i18n.plugin.bundle.BundleFormatConverter" role-hint="toMessageFormat" + * @since 2.4 + */ +public class StringFormatToMessageFormatConverter implements BundleFormatConverter { + + private static final Log log = + LogFactory.getLog(StringFormatToMessageFormatConverter.class); + + protected static Pattern PATTERN = Pattern.compile("%\\$?(\\d?)[^\\s']*"); + + @Override + public String convert(String value) { + + String result; + + Matcher matcher = PATTERN.matcher(value); + boolean matches = matcher.find(); + + if (log.isDebugEnabled()) { + log.debug("> value : " + value + " _ matches ? " + matches); + } + + if (matches) { + + // Reset done, because of first find() + matcher.reset(); + + StringBuffer sb = new StringBuffer(); + int i = 0; + + while (matcher.find()) { + + if (log.isDebugEnabled()) { + log.debug("> match group : " + matcher.group(0)); + log.debug("> match group for number : " + matcher.group(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; + } + + // Append replacement for current occurence + matcher.appendReplacement(sb, "\\{" + nb + "\\}"); + i++; + } + // Append last chars from input String + matcher.appendTail(sb); + result = sb.toString(); + + if (log.isDebugEnabled()) { + log.debug("Result : " + result); + } + + } else { + + // there is no argument in incoming string value + result = value; + } + + // Always escape quote ' to '' + result = escapeQuoteChar(result); + + return result; + } + + /** + * Escape ' char with '', needed by {@link MessageFormat}. + * + * @param value Message that contains ' + * @return the message with ' escaped + */ + protected String escapeQuoteChar(String value) { + + // Replace ' by '' + String result = value.replaceAll("'", "''"); + + if (log.isDebugEnabled()) { + log.debug("Result with ' escape : " + result); + } + + return result; + } + +} Property changes on: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormatConverter.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormatConverterTest.java (from rev 1915, 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/StringFormatToMessageFormatConverterTest.java (rev 0) +++ trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormatConverterTest.java 2011-05-10 20:36:43 UTC (rev 1918) @@ -0,0 +1,76 @@ +/* + * #%L + * I18n :: Maven Plugin + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2007 - 2011 CodeLutin + * %% + * 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 + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.i18n.plugin.bundle; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests the {@link StringFormatToMessageFormatConverter}. + * <p/> + * Created: 05/05/11 + * + * @author fdesbois <desbois@codelutin.com> + */ +public class StringFormatToMessageFormatConverterTest { + + BundleFormatConverter converter; + + @Test + public void testConvert() throws Exception { + + + converter = new StringFormatToMessageFormatConverter(); + + convertAndAssertEquals("Salut à tous pour l'ouverture", + "Salut à tous pour l''ouverture"); + + convertAndAssertEquals("Salut à tous pour l''ouverture", + "Salut à tous pour l''''ouverture"); + + convertAndAssertEquals("Salut à tous pour l'ouverture de %s", + "Salut à tous pour l''ouverture de {0}"); + + convertAndAssertEquals("Salut à tous pour l'ouverture de %$1s", + "Salut à tous pour l''ouverture de {0}"); + + convertAndAssertEquals("Salut à tous pour l'ouverture de %$2s le %$1t", + "Salut à tous pour l''ouverture de {1} le {0}" + ); + + 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}" + ); + + convertAndAssertEquals("Salut à tous pour l''ouverture de '%$1s'", + "Salut à tous pour l''''ouverture de ''{0}''"); + } + + protected void convertAndAssertEquals(String incoming, String expected) { + String actual = converter.convert(incoming); + Assert.assertEquals(expected, actual); + } +} Property changes on: trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormatConverterTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: 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 20:16:51 UTC (rev 1917) +++ trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/bundle/StringFormatToMessageFormatTest.java 2011-05-10 20:36:43 UTC (rev 1918) @@ -1,97 +0,0 @@ -/* - * #%L - * I18n :: Maven Plugin - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2007 - 2011 CodeLutin - * %% - * 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 - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package org.nuiton.i18n.plugin.bundle; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Tests the {@link StringFormatToMessageFormat}. - * <p/> - * Created: 05/05/11 - * - * @author fdesbois <desbois@codelutin.com> - */ -public class StringFormatToMessageFormatTest { - - BundleFormatConverter converter; - - @Test - public void testConvert() throws Exception { - - - converter = new StringFormatToMessageFormat(); - -// String expected; -// String actual; - - convertAndAssertEquals("Salut à tous pour l'ouverture", - "Salut à tous pour l''ouverture"); - - convertAndAssertEquals("Salut à tous pour l''ouverture", - "Salut à tous pour l''''ouverture"); - -// actual = converter.convert("Salut à tous pour l'ouverture"); -// Assert.assertEquals(expected, actual); - - 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); - } -}