This is an automated email from the git hooks/post-receive script. New commit to annotated tag v2.3 in repository i18n. See https://gitlab.nuiton.org/nuiton/i18n.git commit 8e70f2b587a9883776304631e6c8aae468c84201 Author: Tony Chemit <chemit@codelutin.com> Date: Mon Jan 31 12:49:25 2011 +0000 Evolution #1275: Improve parserJava performance Improve it ano-672 --- ant-i18n-task/pom.xml | 2 +- maven-i18n-plugin/pom.xml | 7 +- maven-i18n-plugin/src/it/ano-672/pom.xml | 6 +- .../src/main/java/org/nuiton/i18n/test/MyBean.java | 35 ----- .../main/resources/i18n/ano-672_en_GB.properties | 2 +- .../main/resources/i18n/ano-672_fr_FR.properties | 2 +- .../src/test/java/org/nuiton/i18n/I18nTest.java | 51 +++++-- .../i18n/plugin/parser/impl/ParserJavaMojo.java | 163 +++------------------ .../plugin/parser/impl/JavaFileParserTest.java | 151 +++++++++++++++++++ nuiton-i18n/pom.xml | 2 +- pom.xml | 2 +- 11 files changed, 224 insertions(+), 199 deletions(-) diff --git a/ant-i18n-task/pom.xml b/ant-i18n-task/pom.xml index 0b212e1..9bfefd9 100644 --- a/ant-i18n-task/pom.xml +++ b/ant-i18n-task/pom.xml @@ -34,7 +34,7 @@ <parent> <groupId>org.nuiton</groupId> <artifactId>i18n</artifactId> - <version>2.2</version> + <version>2.3-SNAPSHOT</version> </parent> <groupId>org.nuiton.i18n</groupId> diff --git a/maven-i18n-plugin/pom.xml b/maven-i18n-plugin/pom.xml index 4daa919..b6767c5 100644 --- a/maven-i18n-plugin/pom.xml +++ b/maven-i18n-plugin/pom.xml @@ -33,7 +33,7 @@ <parent> <groupId>org.nuiton</groupId> <artifactId>i18n</artifactId> - <version>2.2</version> + <version>2.3-SNAPSHOT</version> </parent> <groupId>org.nuiton.i18n</groupId> @@ -106,6 +106,11 @@ <scope>runtime</scope> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + </dependencies> <!-- ************************************************************* --> diff --git a/maven-i18n-plugin/src/it/ano-672/pom.xml b/maven-i18n-plugin/src/it/ano-672/pom.xml index e7b4d81..ffde2ad 100644 --- a/maven-i18n-plugin/src/it/ano-672/pom.xml +++ b/maven-i18n-plugin/src/it/ano-672/pom.xml @@ -58,7 +58,6 @@ <i18n.bundles>fr_FR,en_GB</i18n.bundles> </properties> - <dependencies> <dependency> @@ -66,7 +65,7 @@ <artifactId>nuiton-i18n</artifactId> <version>@pom.version@</version> </dependency> - + <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> @@ -95,11 +94,10 @@ <executions> <execution> <goals> - <goal>parserJava</goal> <goal>gen</goal> <goal>bundle</goal> </goals> - </execution> + </execution> </executions> </plugin> </plugins> diff --git a/maven-i18n-plugin/src/it/ano-672/src/main/java/org/nuiton/i18n/test/MyBean.java b/maven-i18n-plugin/src/it/ano-672/src/main/java/org/nuiton/i18n/test/MyBean.java deleted file mode 100644 index e87cbdb..0000000 --- a/maven-i18n-plugin/src/it/ano-672/src/main/java/org/nuiton/i18n/test/MyBean.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * #%L - * I18n :: Maven Plugin - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2007 - 2010 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.test; - -import static org.nuiton.i18n.I18n._; -import static org.nuiton.i18n.I18n.n_; - -public class MyBean { - - protected String field1 = n_("javaGetter.key1"); - - protected String field2 = _("javaGetter.key2"); -} diff --git a/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_en_GB.properties b/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_en_GB.properties index 7659f42..276e656 100644 --- a/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_en_GB.properties +++ b/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_en_GB.properties @@ -1 +1 @@ -javaGetter.key1=Hello \ No newline at end of file +say.hello=Hello \ No newline at end of file diff --git a/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_fr_FR.properties b/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_fr_FR.properties index 26280a4..a452a57 100644 --- a/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_fr_FR.properties +++ b/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_fr_FR.properties @@ -1 +1 @@ -javaGetter.key1=Salut \ No newline at end of file +say.hello=Salut \ No newline at end of file diff --git a/maven-i18n-plugin/src/it/ano-672/src/test/java/org/nuiton/i18n/I18nTest.java b/maven-i18n-plugin/src/it/ano-672/src/test/java/org/nuiton/i18n/I18nTest.java index 45ab870..9b0c1b0 100644 --- a/maven-i18n-plugin/src/it/ano-672/src/test/java/org/nuiton/i18n/I18nTest.java +++ b/maven-i18n-plugin/src/it/ano-672/src/test/java/org/nuiton/i18n/I18nTest.java @@ -1,7 +1,7 @@ /* * #%L * I18n :: Maven Plugin - * + * * * $Id$ * $HeadURL$ * %% @@ -24,7 +24,9 @@ */ package org.nuiton.i18n; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.nuiton.i18n.init.DefaultI18nInitializer; @@ -32,20 +34,51 @@ import java.util.Locale; public class I18nTest { + public static final String I18n_KEY = "say.hello"; + + public static final String FRENCH_HELLO = "Salut"; + + public static final String ENGLISH_HELLO = "Hello"; + + @Before + public void setup() { + I18n.init(new DefaultI18nInitializer("ano-672-i18n"), null); + } + + @After + public void after() { + I18n.close(); + } + @Test - public void testI18n() { + public void _() { + + String expected, actual; - I18n.setInitializer(new DefaultI18nInitializer("ano-672-i18n")); - I18n.init(Locale.FRANCE); + I18n.setDefaultLocale(Locale.FRANCE); - String expected = "Salut"; - String actual = I18n._("javaGetter.key1"); + expected = FRENCH_HELLO; + actual = I18n._(I18n_KEY); Assert.assertEquals(expected, actual); - I18n.init(Locale.ENGLISH); + I18n.setDefaultLocale(Locale.ENGLISH); + + expected = ENGLISH_HELLO; + actual = I18n._(I18n_KEY); + Assert.assertEquals(expected, actual); + } + + @Test + public void l_() { + + String expected, actual; + + expected = FRENCH_HELLO; + actual = I18n.l_(Locale.FRANCE, I18n_KEY); + Assert.assertEquals(expected, actual); - expected = "Hello"; - actual = I18n._("javaGetter.key1"); + expected = ENGLISH_HELLO; + actual = I18n.l_(Locale.UK, I18n_KEY); Assert.assertEquals(expected, actual); } diff --git a/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java b/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java index c062cb4..618dfef 100755 --- a/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java +++ b/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java @@ -35,7 +35,6 @@ import org.nuiton.i18n.plugin.parser.SourceEntry; import org.nuiton.io.FileUpdater; import org.nuiton.io.FileUpdaterHelper; import org.nuiton.io.SortedProperties; -import org.nuiton.processor.filters.DefaultFilter; import java.io.File; import java.io.FileInputStream; @@ -130,14 +129,23 @@ public class ParserJavaMojo extends AbstractI18nParserMojo { protected static class JavaFileParser extends AbstractFileParser { - protected final I18nFilter filter; + /** + * Pattern used to detect i18n keys. + * + * @since 2.3 + */ + protected final Pattern i18nPattern = + Pattern.compile("(?:^_\\(\\s*\\\"|[^l]_\\(\\s*\\\"|l_\\([^,]+\\s*,\\s*\\\")(.*?)\""); public JavaFileParser(Log log, String encoding, SortedProperties oldParser, boolean showTouchedFiles) { super(log, encoding, oldParser, showTouchedFiles); - filter = new I18nFilter(log); + } + + public Pattern getI18nPattern() { + return i18nPattern; } @Override @@ -164,155 +172,20 @@ public class ParserJavaMojo extends AbstractI18nParserMojo { @Override public void parseLine(File file, String line) throws IOException { - String keysSet = filter.parse(line); + Matcher matcher = i18nPattern.matcher(line); - if (keysSet.equals(I18nFilter.EMPTY_STRING)) { - // no key detected on this line - return; - } + while (matcher.find()) { - // one key found in file, so file is marked as touched - setTouched(true); - // Found a set of i18n Strings, split it. - String[] keys = keysSet.split("="); - for (String key : keys) { + String key = matcher.group(1); if (getLog().isDebugEnabled()) { getLog().debug(file.getName() + " detected key = " + key); } - registerKey(key); - } - } - } - - /** - * Pour filtrer les clefs i18n dans un fichier java. - * - * @since 2.1 - */ - public static class I18nFilter extends DefaultFilter { - - /** Instance logger */ - private final Log log; - - // private String header = "_\\(\\s*\""; - private String header = "^_\\(\\s*\\\"|[^l]_\\(\\s*\\\"|l_\\([^,]+\\s*,\\s*\\\""; - - private String footer = "\"\\s*(\\)|,|\\+|$)"; - - private Pattern headerPattern = Pattern.compile(getHeader()); + // one key found in file, so file is marked as touched + setTouched(true); - private Pattern footerPattern = Pattern.compile(getFooter()); - - private Matcher matcher; - - public I18nFilter(Log log) { - this.log = log; - } - - protected void setFooter(String footer) { - this.footer = footer; - } - - protected void setHeader(String header) { - this.header = header; - } - - @Override - protected String getHeader() { - return header; - } - - @Override - protected String getFooter() { - return footer; - } - - @Override - public int getMatchIndexFor(String input, String sequence) { - int index = NOT_FOUND; - - setMatcher(null); - if (sequence.equals(getHeader())) { - setMatcher(getHeaderPattern().matcher(input)); - } else if (sequence.equals(getFooter())) { - setMatcher(getFooterPattern().matcher(input)); - } - if (getMatcher() != null) { - try { - getMatcher().find(); - index = getMatcher().start(); - } catch (RuntimeException e) { - if (log.isDebugEnabled()) { - log.debug("Could not match with " + getMatcher() + " input " + input); - } - } - } - - return index; - } - - @Override - public int getMatchLengthFor(String sequence) { - int length = NOT_FOUND; - - try { - length = getMatcher().end() - getMatcher().start(); - } catch (RuntimeException e) { - if (log.isDebugEnabled()) { - log.debug("Could not match with " + getMatcher() + " input " + sequence); - } + // register key + registerKey(key); } - - return length; - } - - /** - * methode appele lorsqu'on a la chaine entiere entre le header et le - * footer. - * - * @param ch la chaine trouve - * @return ce qu'il faut ecrire dans le fichier de sortie - */ - @Override - protected String performInFilter(String ch) { - return ch.replaceAll("\"\\s*\\+\\s*\"", "") + "="; - } - - @Override - public String performHeaderFooterFilter(String ch) { - return ch.substring(ch.indexOf('"') + 1, ch.lastIndexOf('"')); - } - - /** - * methode appele lorsqu'on a la chaine entiere a l'exterieur du - * header/footer - * - * @param ch la chaine trouve - * @return ce qu'il faut ecrire dans le fichier de sortie - */ - @Override - protected String performOutFilter(String ch) { - return EMPTY_STRING; - } - - /** @return Returns the footerPattern. */ - protected Pattern getFooterPattern() { - return footerPattern; - } - - /** @return Returns the headerPattern. */ - protected Pattern getHeaderPattern() { - return headerPattern; - } - - /** @return Returns the matcher. */ - protected Matcher getMatcher() { - return matcher; - } - - /** @param matcher The matcher to set. */ - protected void setMatcher(Matcher matcher) { - this.matcher = matcher; } } } diff --git a/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/JavaFileParserTest.java b/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/JavaFileParserTest.java new file mode 100644 index 0000000..9f4d77a --- /dev/null +++ b/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/JavaFileParserTest.java @@ -0,0 +1,151 @@ +/* + * #%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.parser.impl; + +import org.apache.maven.plugin.logging.SystemStreamLog; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.io.SortedProperties; + +import java.io.File; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + +/** + * Tests the class {@link ParserJavaMojo.JavaFileParser} + * + * @author tchemit <chemit@codelutin.com> + * @since 2.3 + */ +public class JavaFileParserTest { + + public static final String ENCODING = "utf-8"; + + protected static final SystemStreamLog log = new SystemStreamLog(); + + protected ParserJavaMojo.JavaFileParser parser; + + protected SortedProperties oldParser; + + protected Set<String> detectedKeys; + + @Before + public void setUp() throws Exception { + oldParser = new SortedProperties(ENCODING); + + + parser = new ParserJavaMojo.JavaFileParser( + log, + ENCODING, + oldParser, + false) { + @Override + protected void registerKey(String key) { + detectedKeys.add(key); + } + }; + detectedKeys = new HashSet<String>(); + } + + @After + public void tearDown() throws Exception { + detectedKeys.clear(); + detectedKeys = null; + parser = null; + } + + @Test + public void getKeys() throws IOException { + + File file = new File("getKeys"); + String line; + + line = "_(\"myKey\");"; + parseLine(file, line, "myKey"); + + line = "_(\"MyKey\" "; + parseLine(file, line, "MyKey"); + + line = " blabla _(\"myKey2\");"; + parseLine(file, line, "myKey2"); + + line = " blabla \n somewhere else..." + + "_(\"myKey3\");"; + parseLine(file, line, "myKey3"); + } + + @Test + public void getKeys2() throws IOException { + + String line; + File file = new File("getKeys2"); + + line = "n_(\"myKey\");"; + parseLine(file, line, "myKey"); + + line = "n_(\"MyKey\" "; + parseLine(file, line, "MyKey"); + + line = " blabla n_(\"myKey2\");"; + parseLine(file, line, "myKey2"); + + line = " blabla \n somewhere else..." + + "n_(\"myKey3\");"; + parseLine(file, line, "myKey3"); + } + + @Test + public void getKeys3() throws IOException { + + String line; + File file = new File("getKeys3"); + + line = "l_(Locale.UK, \"myKey\");"; + parseLine(file, line, "myKey"); + + line = "l_( Locale.UK , \"MyKey\""; + parseLine(file, line, "MyKey"); + + line = " blabla l_(Locale.UK, \"myKey2\");"; + parseLine(file, line, "myKey2"); + + line = " blabla \n somewhere else..." + + "l_(Locale.UK, \"myKey3\");"; + parseLine(file, line, "myKey3"); + } + + protected void parseLine(File f, String line, String... expectedKeys) throws IOException { + parser.parseLine(f, line); + for (String expectedKey : expectedKeys) { + + Assert.assertTrue("Key " + expectedKey + + " was expected from line " + line, + detectedKeys.contains(expectedKey)); + } + } +} diff --git a/nuiton-i18n/pom.xml b/nuiton-i18n/pom.xml index e6bc8d1..8c13c17 100644 --- a/nuiton-i18n/pom.xml +++ b/nuiton-i18n/pom.xml @@ -34,7 +34,7 @@ <parent> <groupId>org.nuiton</groupId> <artifactId>i18n</artifactId> - <version>2.2</version> + <version>2.3-SNAPSHOT</version> </parent> <groupId>org.nuiton.i18n</groupId> diff --git a/pom.xml b/pom.xml index e5a5c7e..eedbd5f 100644 --- a/pom.xml +++ b/pom.xml @@ -37,7 +37,7 @@ </parent> <artifactId>i18n</artifactId> - <version>2.2</version> + <version>2.3-SNAPSHOT</version> <modules> <module>nuiton-i18n</module> -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.