Author: tchemit Date: 2011-01-31 13:49:25 +0100 (Mon, 31 Jan 2011) New Revision: 1871 Url: http://nuiton.org/repositories/revision/i18n/1871 Log: Evolution #1275: Improve parserJava performance Improve it ano-672 Added: tags/i18n-2.3/maven-i18n-plugin/src/test/ tags/i18n-2.3/maven-i18n-plugin/src/test/java/ tags/i18n-2.3/maven-i18n-plugin/src/test/java/org/ tags/i18n-2.3/maven-i18n-plugin/src/test/java/org/nuiton/ tags/i18n-2.3/maven-i18n-plugin/src/test/java/org/nuiton/i18n/ tags/i18n-2.3/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/ tags/i18n-2.3/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/ tags/i18n-2.3/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/ tags/i18n-2.3/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/JavaFileParserTest.java Removed: tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/src/main/java/org/ Modified: tags/i18n-2.3/ant-i18n-task/pom.xml tags/i18n-2.3/maven-i18n-plugin/pom.xml tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/pom.xml tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_en_GB.properties tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_fr_FR.properties tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/src/test/java/org/nuiton/i18n/I18nTest.java tags/i18n-2.3/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java tags/i18n-2.3/nuiton-i18n/pom.xml tags/i18n-2.3/pom.xml Modified: tags/i18n-2.3/ant-i18n-task/pom.xml =================================================================== --- tags/i18n-2.3/ant-i18n-task/pom.xml 2011-01-31 12:29:28 UTC (rev 1870) +++ tags/i18n-2.3/ant-i18n-task/pom.xml 2011-01-31 12:49:25 UTC (rev 1871) @@ -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> Modified: tags/i18n-2.3/maven-i18n-plugin/pom.xml =================================================================== --- tags/i18n-2.3/maven-i18n-plugin/pom.xml 2011-01-31 12:29:28 UTC (rev 1870) +++ tags/i18n-2.3/maven-i18n-plugin/pom.xml 2011-01-31 12:49:25 UTC (rev 1871) @@ -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> <!-- ************************************************************* --> Modified: tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/pom.xml =================================================================== --- tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/pom.xml 2011-01-31 12:29:28 UTC (rev 1870) +++ tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/pom.xml 2011-01-31 12:49:25 UTC (rev 1871) @@ -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> Modified: tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_en_GB.properties =================================================================== --- tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_en_GB.properties 2011-01-31 12:29:28 UTC (rev 1870) +++ tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_en_GB.properties 2011-01-31 12:49:25 UTC (rev 1871) @@ -1 +1 @@ -javaGetter.key1=Hello \ No newline at end of file +say.hello=Hello \ No newline at end of file Modified: tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_fr_FR.properties =================================================================== --- tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_fr_FR.properties 2011-01-31 12:29:28 UTC (rev 1870) +++ tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_fr_FR.properties 2011-01-31 12:49:25 UTC (rev 1871) @@ -1 +1 @@ -javaGetter.key1=Salut \ No newline at end of file +say.hello=Salut \ No newline at end of file Modified: tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/src/test/java/org/nuiton/i18n/I18nTest.java =================================================================== --- tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/src/test/java/org/nuiton/i18n/I18nTest.java 2011-01-31 12:29:28 UTC (rev 1870) +++ tags/i18n-2.3/maven-i18n-plugin/src/it/ano-672/src/test/java/org/nuiton/i18n/I18nTest.java 2011-01-31 12:49:25 UTC (rev 1871) @@ -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,21 +34,52 @@ 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 _() { - I18n.setInitializer(new DefaultI18nInitializer("ano-672-i18n")); - I18n.init(Locale.FRANCE); + String expected, actual; - String expected = "Salut"; - String actual = I18n._("javaGetter.key1"); + I18n.setDefaultLocale(Locale.FRANCE); + + expected = FRENCH_HELLO; + actual = I18n._(I18n_KEY); Assert.assertEquals(expected, actual); - I18n.init(Locale.ENGLISH); + I18n.setDefaultLocale(Locale.ENGLISH); - expected = "Hello"; - actual = I18n._("javaGetter.key1"); + 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 = ENGLISH_HELLO; + actual = I18n.l_(Locale.UK, I18n_KEY); + Assert.assertEquals(expected, actual); + } } Modified: tags/i18n-2.3/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java =================================================================== --- tags/i18n-2.3/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java 2011-01-31 12:29:28 UTC (rev 1870) +++ tags/i18n-2.3/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java 2011-01-31 12:49:25 UTC (rev 1871) @@ -35,7 +35,6 @@ 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,16 +129,25 @@ 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 public void parseFile(File file) throws IOException { String line = null; @@ -164,155 +172,20 @@ @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); } + // one key found in file, so file is marked as touched + setTouched(true); + + // register 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()); - - 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); - } - } - - 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; - } - } } Added: tags/i18n-2.3/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/JavaFileParserTest.java =================================================================== --- tags/i18n-2.3/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/JavaFileParserTest.java (rev 0) +++ tags/i18n-2.3/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/JavaFileParserTest.java 2011-01-31 12:49:25 UTC (rev 1871) @@ -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)); + } + } +} Property changes on: tags/i18n-2.3/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/JavaFileParserTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: tags/i18n-2.3/nuiton-i18n/pom.xml =================================================================== --- tags/i18n-2.3/nuiton-i18n/pom.xml 2011-01-31 12:29:28 UTC (rev 1870) +++ tags/i18n-2.3/nuiton-i18n/pom.xml 2011-01-31 12:49:25 UTC (rev 1871) @@ -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> Modified: tags/i18n-2.3/pom.xml =================================================================== --- tags/i18n-2.3/pom.xml 2011-01-31 12:29:28 UTC (rev 1870) +++ tags/i18n-2.3/pom.xml 2011-01-31 12:49:25 UTC (rev 1871) @@ -37,7 +37,7 @@ </parent> <artifactId>i18n</artifactId> - <version>2.2</version> + <version>2.3-SNAPSHOT</version> <modules> <module>nuiton-i18n</module>