Author: tchemit Date: 2011-01-31 13:51:13 +0100 (Mon, 31 Jan 2011) New Revision: 1872 Url: http://nuiton.org/repositories/revision/i18n/1872 Log: Evolution #1275: Improve parserJava performance (from the tag 2.3) Improve it ano-672 (from the tag 2.3) Added: trunk/maven-i18n-plugin/src/test/ trunk/maven-i18n-plugin/src/test/java/ trunk/maven-i18n-plugin/src/test/java/org/ trunk/maven-i18n-plugin/src/test/java/org/nuiton/ trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/ trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/ trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/ trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/ trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/JavaFileParserTest.java Removed: trunk/maven-i18n-plugin/src/it/ano-672/src/main/java/org/ Modified: trunk/maven-i18n-plugin/pom.xml trunk/maven-i18n-plugin/src/it/ano-672/pom.xml trunk/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_en_GB.properties trunk/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_fr_FR.properties trunk/maven-i18n-plugin/src/it/ano-672/src/test/java/org/nuiton/i18n/I18nTest.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java Modified: trunk/maven-i18n-plugin/pom.xml =================================================================== --- trunk/maven-i18n-plugin/pom.xml 2011-01-31 12:49:25 UTC (rev 1871) +++ trunk/maven-i18n-plugin/pom.xml 2011-01-31 12:51:13 UTC (rev 1872) @@ -105,6 +105,11 @@ <artifactId>log4j</artifactId> <scope>runtime</scope> </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> </dependencies> Modified: trunk/maven-i18n-plugin/src/it/ano-672/pom.xml =================================================================== --- trunk/maven-i18n-plugin/src/it/ano-672/pom.xml 2011-01-31 12:49:25 UTC (rev 1871) +++ trunk/maven-i18n-plugin/src/it/ano-672/pom.xml 2011-01-31 12:51:13 UTC (rev 1872) @@ -95,7 +95,6 @@ <executions> <execution> <goals> - <goal>parserJava</goal> <goal>gen</goal> <goal>bundle</goal> </goals> Modified: trunk/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_en_GB.properties =================================================================== --- trunk/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) +++ trunk/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_en_GB.properties 2011-01-31 12:51:13 UTC (rev 1872) @@ -1 +1 @@ -javaGetter.key1=Hello \ No newline at end of file +say.hello=Hello \ No newline at end of file Modified: trunk/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_fr_FR.properties =================================================================== --- trunk/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) +++ trunk/maven-i18n-plugin/src/it/ano-672/src/main/resources/i18n/ano-672_fr_FR.properties 2011-01-31 12:51:13 UTC (rev 1872) @@ -1 +1 @@ -javaGetter.key1=Salut \ No newline at end of file +say.hello=Salut \ No newline at end of file Modified: trunk/maven-i18n-plugin/src/it/ano-672/src/test/java/org/nuiton/i18n/I18nTest.java =================================================================== --- trunk/maven-i18n-plugin/src/it/ano-672/src/test/java/org/nuiton/i18n/I18nTest.java 2011-01-31 12:49:25 UTC (rev 1871) +++ trunk/maven-i18n-plugin/src/it/ano-672/src/test/java/org/nuiton/i18n/I18nTest.java 2011-01-31 12:51:13 UTC (rev 1872) @@ -1,30 +1,32 @@ /* * #%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 + * 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 + * + * 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; +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,53 @@ 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.init(new DefaultI18nInitializer("ano-672-i18n"), 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.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: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java 2011-01-31 12:49:25 UTC (rev 1871) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java 2011-01-31 12:51:13 UTC (rev 1872) @@ -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; @@ -118,16 +117,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; @@ -152,155 +160,21 @@ @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: trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/JavaFileParserTest.java =================================================================== --- trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/JavaFileParserTest.java (rev 0) +++ trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/JavaFileParserTest.java 2011-01-31 12:51:13 UTC (rev 1872) @@ -0,0 +1,128 @@ +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: trunk/maven-i18n-plugin/src/test/java/org/nuiton/i18n/plugin/parser/impl/JavaFileParserTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native