Author: tchemit Date: 2011-01-04 14:43:41 +0100 (Tue, 04 Jan 2011) New Revision: 1837 Url: http://nuiton.org/repositories/revision/i18n/1837 Log: pass to version 2.1-SNAPSHOT Evolution #1196: Introduce GWT Parser to grab i18n keys from java files refactor processor code to use for any Parser processor Added: trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/com/ trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/com/google/ trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/com/google/gwt/ trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/com/google/gwt/i18n/ trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/com/google/gwt/i18n/client/ trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/com/google/gwt/i18n/client/LocalizableResource.java trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/org/nuiton/i18n/test/MyGWTBean.java trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/com/ trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/com/google/ trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/com/google/gwt/ trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/com/google/gwt/i18n/ trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/com/google/gwt/i18n/client/ trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/com/google/gwt/i18n/client/LocalizableResource.java trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/org/nuiton/i18n/test/MyGWTBean.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserGWTJavaMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ProcessorHelper.java Removed: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/Parser.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/JspFileProcessor.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserXmlMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/TapestryFileProcessor.java Modified: trunk/ant-i18n-task/pom.xml trunk/maven-i18n-plugin/pom.xml trunk/maven-i18n-plugin/src/it/parsers/newProject/pom.xml trunk/maven-i18n-plugin/src/it/parsers/newProject/verify.groovy trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/pom.xml trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/verify.groovy trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJspMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserTapestryMojo.java trunk/nuiton-i18n/pom.xml trunk/pom.xml Modified: trunk/ant-i18n-task/pom.xml =================================================================== --- trunk/ant-i18n-task/pom.xml 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/ant-i18n-task/pom.xml 2011-01-04 13:43:41 UTC (rev 1837) @@ -34,7 +34,7 @@ <parent> <groupId>org.nuiton</groupId> <artifactId>i18n</artifactId> - <version>2.0.1-SNAPSHOT</version> + <version>2.1-SNAPSHOT</version> </parent> <groupId>org.nuiton.i18n</groupId> Modified: trunk/maven-i18n-plugin/pom.xml =================================================================== --- trunk/maven-i18n-plugin/pom.xml 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/maven-i18n-plugin/pom.xml 2011-01-04 13:43:41 UTC (rev 1837) @@ -33,7 +33,7 @@ <parent> <groupId>org.nuiton</groupId> <artifactId>i18n</artifactId> - <version>2.0.2-SNAPSHOT</version> + <version>2.1-SNAPSHOT</version> </parent> <groupId>org.nuiton.i18n</groupId> Modified: trunk/maven-i18n-plugin/src/it/parsers/newProject/pom.xml =================================================================== --- trunk/maven-i18n-plugin/src/it/parsers/newProject/pom.xml 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/maven-i18n-plugin/src/it/parsers/newProject/pom.xml 2011-01-04 13:43:41 UTC (rev 1837) @@ -73,6 +73,7 @@ <goal>parserJava</goal> <goal>parserValidation</goal> <goal>parserTapestry</goal> + <goal>parserGWTJava</goal> </goals> </execution> <execution> @@ -97,6 +98,7 @@ <goal>parserJava</goal> <goal>parserValidation</goal> <goal>parserTapestry</goal> + <goal>parserGWTJava</goal> </goals> <phase>compile</phase> </execution> Added: trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/com/google/gwt/i18n/client/LocalizableResource.java =================================================================== --- trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/com/google/gwt/i18n/client/LocalizableResource.java (rev 0) +++ trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/com/google/gwt/i18n/client/LocalizableResource.java 2011-01-04 13:43:41 UTC (rev 1837) @@ -0,0 +1,13 @@ +package com.google.gwt.i18n.client; + +import java.lang.annotation.*; +import static java.lang.annotation.ElementType.*; + +public class LocalizableResource { + + @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) + @Retention(RetentionPolicy.SOURCE) + public @interface Key { + String[] value(); + } +} \ No newline at end of file Property changes on: trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/com/google/gwt/i18n/client/LocalizableResource.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/org/nuiton/i18n/test/MyGWTBean.java (from rev 1834, trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/org/nuiton/i18n/test/MyBean.java) =================================================================== --- trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/org/nuiton/i18n/test/MyGWTBean.java (rev 0) +++ trunk/maven-i18n-plugin/src/it/parsers/newProject/src/main/java/org/nuiton/i18n/test/MyGWTBean.java 2011-01-04 13:43:41 UTC (rev 1837) @@ -0,0 +1,41 @@ +/* + * #%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 com.google.gwt.i18n.client.LocalizableResource; +import static com.google.gwt.i18n.client.LocalizableResource.Key; + +public class MyGWTBean { + + @Key("gwtJavaGetter.key1") + protected String field1; + + @LocalizableResource.Key("gwtJavaGetter.key2") + protected String field2; + + @com.google.gwt.i18n.client.LocalizableResource.Key("gwtJavaGetter.key3") + protected String field3; + +} Modified: trunk/maven-i18n-plugin/src/it/parsers/newProject/verify.groovy =================================================================== --- trunk/maven-i18n-plugin/src/it/parsers/newProject/verify.groovy 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/maven-i18n-plugin/src/it/parsers/newProject/verify.groovy 2011-01-04 13:43:41 UTC (rev 1837) @@ -51,4 +51,12 @@ assert content.contains( 'tmlGetter.key1=' ); assert content.contains( 'tmlGetter.key2=' ); +file = new File(basedir, 'target/generated-sources/i18n/gwt-java.getter'); +assert file.exists(); + +content = file.text; +assert content.contains( 'gwtJavaGetter.key1=' ); +assert content.contains( 'gwtJavaGetter.key2=' ); +assert content.contains( 'gwtJavaGetter.key3=' ); + return true; Modified: trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/pom.xml =================================================================== --- trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/pom.xml 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/pom.xml 2011-01-04 13:43:41 UTC (rev 1837) @@ -82,6 +82,7 @@ <id>first</id> <goals> <goal>parserJava</goal> + <goal>parserGWTJava</goal> <goal>parserValidation</goal> <goal>parserTapestry</goal> </goals> @@ -112,6 +113,7 @@ <id>second</id> <goals> <goal>parserJava</goal> + <goal>parserGWTJava</goal> <goal>parserValidation</goal> <goal>parserTapestry</goal> <goal>gen</goal> Added: trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/com/google/gwt/i18n/client/LocalizableResource.java =================================================================== --- trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/com/google/gwt/i18n/client/LocalizableResource.java (rev 0) +++ trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/com/google/gwt/i18n/client/LocalizableResource.java 2011-01-04 13:43:41 UTC (rev 1837) @@ -0,0 +1,13 @@ +package com.google.gwt.i18n.client; + +import java.lang.annotation.*; +import static java.lang.annotation.ElementType.*; + +public class LocalizableResource { + + @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) + @Retention(RetentionPolicy.SOURCE) + public @interface Key { + String[] value(); + } +} \ No newline at end of file Property changes on: trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/com/google/gwt/i18n/client/LocalizableResource.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/org/nuiton/i18n/test/MyGWTBean.java =================================================================== --- trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/org/nuiton/i18n/test/MyGWTBean.java (rev 0) +++ trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/org/nuiton/i18n/test/MyGWTBean.java 2011-01-04 13:43:41 UTC (rev 1837) @@ -0,0 +1,41 @@ +/* + * #%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 com.google.gwt.i18n.client.LocalizableResource; +import static com.google.gwt.i18n.client.LocalizableResource.Key; + +public class MyGWTBean { + + @Key("gwtJavaGetter.key1") + protected String field1; + + @LocalizableResource.Key("gwtJavaGetter.key2") + protected String field2; + + @com.google.gwt.i18n.client.LocalizableResource.Key("gwtJavaGetter.key3") + protected String field3; + +} Property changes on: trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/src/main/java/org/nuiton/i18n/test/MyGWTBean.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/verify.groovy =================================================================== --- trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/verify.groovy 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/maven-i18n-plugin/src/it/parsers/withNewKeys/verify.groovy 2011-01-04 13:43:41 UTC (rev 1837) @@ -55,6 +55,15 @@ assert content.contains( 'tmlGetter.key1=' ); assert content.contains( 'tmlGetter.key2=' ); + +file = new File(basedir, 'target/generated-sources/i18n/gwt-java.getter'); +assert file.exists(); + +content = file.text; +assert content.contains( 'gwtJavaGetter.key1=' ); +assert content.contains( 'gwtJavaGetter.key2=' ); +assert content.contains( 'gwtJavaGetter.key3=' ); + file = new File(basedir, 'src/main/resources/i18n/parsers-withNewKeys_fr_FR.properties'); assert file.exists(); @@ -72,4 +81,8 @@ assert content.contains( 'tmlGetter.key1=' ); assert content.contains( 'tmlGetter.key2=' ); +assert content.contains( 'gwtJavaGetter.key1=' ); +assert content.contains( 'gwtJavaGetter.key2=' ); +assert content.contains( 'gwtJavaGetter.key3=' ); + return true; Deleted: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/Parser.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/Parser.java 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/Parser.java 2011-01-04 13:43:41 UTC (rev 1837) @@ -1,67 +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.plugin.parser; - -import java.io.File; -import java.io.IOException; - -/** - * Interface type pour la définition d'un nouveau parser. - * <p/> - * Une implantation abstraite est proposée : {@link AbstractI18nParserMojo}. - * - * @author julien - * @deprecated since 1.2, use now {@link FileParser} api, which is - * multi-thread - */ -@Deprecated -public interface Parser { - - /** - * Lancement du parser - * - * @throws IOException if any io pb - */ - void parse() throws IOException; - - /** - * Parse sur un fichier - * - * @param file le fichier à parser - * @throws IOException if any pb - */ - void parseFile(File file) throws IOException; - - /** - * Parse une partie du fichier - * - * @param file le fichier à parser - * @param line la ligne à parser - * @throws IOException if any pb - */ - void parseLine(File file, String line) throws IOException; - -} Deleted: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/JspFileProcessor.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/JspFileProcessor.java 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/JspFileProcessor.java 2011-01-04 13:43:41 UTC (rev 1837) @@ -1,167 +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.plugin.parser.impl; - -import org.nuiton.processor.Processor; -import org.nuiton.processor.ProcessorUtil; -import org.nuiton.processor.filters.DefaultFilter; -import org.nuiton.processor.filters.Filter; - -import java.io.File; -import java.io.IOException; - -/** - * To transform jsp files to valid xml files. - * <p/> - * Says : - * <ul> - * <li>Remove jsp directive</li> - * <li>Remove jsp comment</li> - * <li>Remove any tags in attributes (used for example in struts)</li> - * <li>? Other thing</li> - * </ul> - * - * @author tchemit <chemit@codelutin.com> - * @since 2.0 - */ -public class JspFileProcessor extends Processor { - - public JspFileProcessor() { - setInputFilter( - new Filter[]{ - new JspDirectiveFilter(), - new JspCommentFilter(), - new JspAttributeWithTagFilter() - } - ); - } - - /** - * @param filein the source file to process - * @param fileout the output file to generate - * @param encoding encoding used to read and write files - * @throws IOException if any io problems while processing - * @since 1.0.4 - */ - public void process(File filein, - File fileout, - String encoding) throws IOException { - ProcessorUtil.doProcess(this, filein, fileout, encoding); - } - - /** - * To remove in attributes any sub tags inside it (used for example in - * struts) from jsp files. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.0 - */ - public static class JspAttributeWithTagFilter extends DefaultFilter { - - private String header = "=" + "\"" + "<"; - - private String footer = "\"" + "/" + ">" + "\""; - - @Override - protected String performInFilter(String ch) { - return "=\"\""; - } - - @Override - protected String performOutFilter(String ch) { - return ch; - } - - protected String getHeader() { - return header; - } - - protected String getFooter() { - return footer; - } - } - - /** - * To remove jsp comment from files. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.0 - */ - public static class JspCommentFilter extends DefaultFilter { - - private String header = "<" + "%" + "-" + "-"; - - private String footer = "-" + "-" + "%" + ">"; - - @Override - protected String performInFilter(String ch) { - return EMPTY_STRING; - } - - @Override - protected String performOutFilter(String ch) { - return ch; - } - - protected String getHeader() { - return header; - } - - protected String getFooter() { - return footer; - } - } - - /** - * To remove jsp directive from files. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.0 - */ - public static class JspDirectiveFilter extends DefaultFilter { - - private String header = "<" + "%"; - - private String footer = "%" + ">"; - - @Override - protected String performInFilter(String ch) { - return EMPTY_STRING; - } - - @Override - protected String performOutFilter(String ch) { - return ch; - } - - protected String getHeader() { - return header; - } - - protected String getFooter() { - return footer; - } - } -} Added: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserGWTJavaMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserGWTJavaMojo.java (rev 0) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserGWTJavaMojo.java 2011-01-04 13:43:41 UTC (rev 1837) @@ -0,0 +1,263 @@ +package org.nuiton.i18n.plugin.parser.impl; + +import org.apache.maven.plugin.logging.Log; +import org.nuiton.i18n.plugin.parser.AbstractFileParser; +import org.nuiton.i18n.plugin.parser.AbstractI18nParserMojo; +import org.nuiton.i18n.plugin.parser.FileParser; +import org.nuiton.i18n.plugin.parser.I18nSourceEntry; +import org.nuiton.i18n.plugin.parser.ParserException; +import org.nuiton.i18n.plugin.parser.SourceEntry; +import org.nuiton.io.FileUpdater; +import org.nuiton.io.MirroredFileUpdater; +import org.nuiton.io.SortedProperties; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.LineNumberReader; +import java.util.HashSet; +import java.util.Set; + +/** + * To detect from GWT java files Tapestry templates new I18n keys, says content of patterns : + * <ul> + * <li>{@code @Key("XXX")}</li> + * <li>{@code @LocalizableResource.Key("XXX")}</li> + * <li>{@code @com.google.gwt.i18n.client.LocalizableResource.Key("XXX")}</li> + * </ul> + * <b>Note: </b> this goal must always be invoked before the {@code process-resources} + * phase, otherwise all files will be considered as uptodate. + * + * @author tchemit <chemit@codelutin.com> + * @goal parserGWTJava + * @phase generate-resources + * @since 2.1 + */ +public class ParserGWTJavaMojo extends AbstractI18nParserMojo { + + /** + * Root directory of the default entry. + * + * @parameter expression="${i18n.defaultBasedir}" default-value="${basedir}/src/main/java" + */ + protected File defaultBasedir; + + /** + * Default included files to process (ant-like expression). + * + * @parameter expression="${i18n.defaultIncludes}" default-value="**\/*.java" + */ + protected String defaultIncludes; + + /** + * Defines the file name of the getter where to put detected i18n keys + * while getter phase. + * + * @parameter expression="${i18n.outputGetter}" default-value="gwt-java.getter" + * @since 2.0 + */ + protected String outputGetter; + + /** + * Where to generated temporary processed files. + * + * @parameter expression="${i18n.workdir}" default-value="${basedir}/target/i18n-workdir" + * @since 2.0 + */ + protected File workdir; + + protected MirroredFileUpdater entryUpdater; + + @Override + public String[] getDefaultIncludes() { + return new String[]{defaultIncludes}; + } + + @Override + public String[] getDefaultExcludes() { + return I18nSourceEntry.EMPTY_STRING_ARRAY; + } + + @Override + public File getDefaultBasedir() { + return defaultBasedir; + } + + @Override + protected boolean onEnterEntry(I18nSourceEntry entry) { + boolean b = super.onEnterEntry(entry); + if (!b) { + + // no skipped entry + // keep the file updater + entryUpdater = (MirroredFileUpdater) entry.getUpdater(); + } + return b; + } + + @Override + public FileUpdater newFileUpdater(SourceEntry entry) { + return new MirroredFileUpdater("", "", entry.getBasedir(), workdir) { + + @Override + public File getMirrorFile(File f) { + String file = + f.getAbsolutePath().substring(prefixSourceDirecotory); + return new File(destinationDirectory + File.separator + file); + } + }; + } + + @Override + @Deprecated + protected String getKeyModifierStart() { + return "_\\(\\s*\""; + } + + @Override + @Deprecated + protected String getKeyModifierEnd() { + return "\"\\s*(\\)|,|\\+|$)"; + } + + @Override + protected String getOutGetter() { + return outputGetter; + } + + @Override + public FileParser newFileParser() { + + return new GWTJavaFileParser(getLog(), + getEncoding(), + oldParser, + isShowTouchedFiles() + ); + } + + protected class GWTJavaFileParser extends AbstractFileParser { + + + public GWTJavaFileParser(Log log, + String encoding, + SortedProperties oldParser, + boolean showTouchedFiles) { + super(log, encoding, oldParser, showTouchedFiles); + } + + + protected File prepareFile(File file) throws IOException { + + // clean the jsp to make it xml + + File result = entryUpdater.getMirrorFile(file); + if (isVerbose()) { + getLog().info("Will process [" + file + "] to " + result); + } + try { + createDirectoryIfNecessary(result.getParentFile()); + } catch (IOException e) { + // don't care about it... + } + GWTJavaProcessor processor = new GWTJavaProcessor(); + processor.setVerbose(isVerbose()); + processor.process(file, result, getEncoding()); + return result; + } + + @Override + public void parseFile(File file) throws IOException { + + // clean the jsp to make it xml + + File processedFile = prepareFile(file); + + // process file to obtain + String line = null; + LineNumberReader lnr = new LineNumberReader(new InputStreamReader( + new FileInputStream(processedFile), getEncoding())); + try { + while ((line = lnr.readLine()) != null) { + parseLine(processedFile, line); + } + } catch (Exception e) { + if (line != null) { + getLog().error( + "could not parse line (" + lnr.getLineNumber() + ") '" + + line + "' of file " + file); + } + throw new ParserException(e); + } finally { + lnr.close(); + } + } + + @Override + public void parseLine(File file, String line) throws IOException { + + String key = line.trim(); + + if (key.isEmpty()) { + // no key detected on this line + return; + } + + // one key found in file, so file is marked as touched + setTouched(true); + // Found a i18n key, register it. + if (getLog().isDebugEnabled()) { + getLog().debug(file.getName() + " detected key = " + key); + } + registerKey(key); + } + } + + /** + * To Extract i18n keys from GWT java files. + * <p/> + * Says keep content only of those patterns : + * <ul> + * <li>{@code @Key("XXX")}</li> + * <li>{@code @LocalizableResource.Key("XXX")}</li> + * <li>{@code @com.google.gwt.i18n.client.LocalizableResource.Key("XXX")}</li> + * </ul> + * + * @author tchemit <chemit@codelutin.com> + * @since 2.1 + */ + public static class GWTJavaProcessor extends ProcessorHelper.AbstractParserProcessor { + + protected ProcessorHelper.KeyExtractor filter1 = + new ProcessorHelper.KeyExtractor( + "@Key(\"", + "\")" + ); + + protected ProcessorHelper.KeyExtractor filter2 = + new ProcessorHelper.KeyExtractor( + "@LocalizableResource.Key(\"", + "\")" + ); + + protected ProcessorHelper.KeyExtractor filter3 = + new ProcessorHelper.KeyExtractor( + "@com.google.gwt.i18n.client.LocalizableResource.Key(\"", + "\")" + ); + + + @Override + public void process(File filein, + File fileout, + String encoding) throws IOException { + + Set<String> keys = new HashSet<String>(); + extractKeys(filter1, filein, encoding, verbose, keys); + extractKeys(filter2, filein, encoding, verbose, keys); + extractKeys(filter3, filein, encoding, verbose, keys); + saveKeysToFile(fileout, keys); + } + + } +} \ No newline at end of file Property changes on: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserGWTJavaMojo.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJspMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJspMojo.java 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJspMojo.java 2011-01-04 13:43:41 UTC (rev 1837) @@ -29,6 +29,9 @@ import org.nuiton.i18n.plugin.parser.SourceEntry; import org.nuiton.io.FileUpdater; import org.nuiton.io.MirroredFileUpdater; +import org.nuiton.processor.ProcessorUtil; +import org.nuiton.processor.filters.DefaultFilter; +import org.nuiton.processor.filters.Filter; import javax.xml.parsers.DocumentBuilder; import javax.xml.xpath.XPath; @@ -195,4 +198,151 @@ protected String getKeyModifierEnd() { return "[\"\']"; } + + /** + * To transform jsp files to valid xml files. + * <p/> + * Says : + * <ul> + * <li>Remove jsp directive</li> + * <li>Remove jsp comment</li> + * <li>Remove any tags in attributes (used for example in struts)</li> + * <li>? Other thing</li> + * </ul> + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ + public static class JspFileProcessor extends ProcessorHelper.AbstractParserProcessor { + + protected ProcessorHelper.FragmentRemover remover1 = + new ProcessorHelper.FragmentRemover( + "<" + "%" + "-" + "-", + "-" + "-" + "%" + ">" + ); + + protected ProcessorHelper.FragmentRemover remover2 = + new ProcessorHelper.FragmentRemover( + "<" + "%", + "%" + ">" + ); + + public JspFileProcessor() { + setInputFilter( + new Filter[]{ + remover1, + remover2, + new JspAttributeWithTagFilter() + } + ); + } + + /** + * @param filein the source file to process + * @param fileout the output file to generate + * @param encoding encoding used to read and write files + * @throws IOException if any io problems while processing + * @since 1.0.4 + */ + @Override + public void process(File filein, + File fileout, + String encoding) throws IOException { + ProcessorUtil.doProcess(this, filein, fileout, encoding); + } + + /** + * To remove in attributes any sub tags inside it (used for example in + * struts) from jsp files. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ + public static class JspAttributeWithTagFilter extends DefaultFilter { + + private String header = "=" + "\"" + "<"; + + private String footer = "\"" + "/" + ">" + "\""; + + @Override + protected String performInFilter(String ch) { + return "=\"\""; + } + + @Override + protected String performOutFilter(String ch) { + return ch; + } + + protected String getHeader() { + return header; + } + + protected String getFooter() { + return footer; + } + } + + // /** + // * To remove jsp comment from files. + // * + // * @author tchemit <chemit@codelutin.com> + // * @since 2.0 + // */ + // public static class JspCommentFilter extends DefaultFilter { + // + // private String header = "<" + "%" + "-" + "-"; + // + // private String footer = "-" + "-" + "%" + ">"; + // + // @Override + // protected String performInFilter(String ch) { + // return EMPTY_STRING; + // } + // + // @Override + // protected String performOutFilter(String ch) { + // return ch; + // } + // + // protected String getHeader() { + // return header; + // } + // + // protected String getFooter() { + // return footer; + // } + // } + // + // /** + // * To remove jsp directive from files. + // * + // * @author tchemit <chemit@codelutin.com> + // * @since 2.0 + // */ + // public static class JspDirectiveFilter extends DefaultFilter { + // + // private String header = "<" + "%"; + // + // private String footer = "%" + ">"; + // + // @Override + // protected String performInFilter(String ch) { + // return EMPTY_STRING; + // } + // + // @Override + // protected String performOutFilter(String ch) { + // return ch; + // } + // + // protected String getHeader() { + // return header; + // } + // + // protected String getFooter() { + // return footer; + // } + // } + } } Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserTapestryMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserTapestryMojo.java 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserTapestryMojo.java 2011-01-04 13:43:41 UTC (rev 1837) @@ -40,12 +40,14 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; +import java.util.HashSet; +import java.util.Set; /** * To parse Tapestry templates to detect new I18n keys, says content of patterns : * {@code ${message:XXX}} and {@code ${format:XXX=...}}. - * * <p/> + * <p/> * <b>Note: </b> this goal must always be invoked before the {@code process-resources} * phase, otherwise all files will be considered as uptodate. * @@ -233,4 +235,65 @@ } } + /** + * To Extract i18n keys from tapestry template files. + * <p/> + * Says : + * <ul> + * <li>Remove all xml comments</li> + * <li>Keep content of {@code ${message:XXX}}</li> + * <li>Keep content of {@code ${format:XXX}}}</li> + * </ul> + * + * @author tchemit <chemit@codelutin.com> + * @since 2.1 + */ + public static class TapestryFileProcessor extends ProcessorHelper.AbstractParserProcessor { + + protected ProcessorHelper.FragmentRemover remover1 = + new ProcessorHelper.FragmentRemover( + "<" + "!" + "-" + "-", + "-" + "-" + ">" + ); + + protected ProcessorHelper.KeyExtractor extractor1 = + new ProcessorHelper.KeyExtractor( + "$" + "{" + "m" + "e" + "s" + "s" + "a" + "g" + "e" + ":", + "}" + ); + + protected ProcessorHelper.KeyExtractor extractor2 = + new ProcessorHelper.KeyExtractor( + "$" + "{" + "f" + "o" + "r" + "m" + "a" + "t" + ":", + "}") { + + @Override + protected String performInFilter(String ch) { + int i = ch.indexOf('='); + if (verbose) { + log.info("Detected key " + ch); + } + if (i == -1) { + return ""; + } + return ch.substring(0, i) + "\n"; + } + }; + + + public void process(File filein, + File fileout, + String encoding) throws IOException { + + // remove comment fragments + File fileTemp = new File(fileout.getAbsolutePath() + "-no-comment"); + removefragments(remover1, filein, fileTemp, encoding, false); + + Set<String> keys = new HashSet<String>(); + extractKeys(extractor1, fileTemp, encoding, verbose, keys); + extractKeys(extractor2, fileTemp, encoding, verbose, keys); + saveKeysToFile(fileout, keys); + } + + } } Deleted: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserXmlMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserXmlMojo.java 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserXmlMojo.java 2011-01-04 13:43:41 UTC (rev 1837) @@ -1,263 +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.plugin.parser.impl; - -import org.apache.maven.plugin.logging.Log; -import org.nuiton.i18n.plugin.parser.AbstractFileParser; -import org.nuiton.i18n.plugin.parser.AbstractI18nParserMojo; -import org.nuiton.i18n.plugin.parser.FileParser; -import org.nuiton.i18n.plugin.parser.ParserException; -import org.nuiton.io.SortedProperties; -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpression; -import javax.xml.xpath.XPathFactory; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; - -/** - * Récupération des chaines à traduire depuis les fichiers xml. - * - * @author julien - * @deprecated since 2.0, prefer use the {@link AbstractParserXmlMojo} instead. - */ -@Deprecated -public abstract class ParserXmlMojo extends AbstractI18nParserMojo { - - /** Taille du buffer pour les lectures/écritures */ - protected static final int BUFFER_SIZE = 8 * 1024; - - protected String rules; - - protected XPathFactory factory; - - protected DocumentBuilderFactory documentBuilderFactory; - - /** @return le fichier des rules */ - protected abstract String getFileRules(); - - /** @return le fichier des rules de base à toujours charger */ - protected abstract String getCoreFileRules(); - - protected abstract XmlFileParser newXmlFileParser(XPath xpath, - DocumentBuilder builder); - - @Override - public void init() throws Exception { - super.init(); - factory = XPathFactory.newInstance(); - rules = getRules(getFileRules()); - - documentBuilderFactory = DocumentBuilderFactory.newInstance(); - documentBuilderFactory.setNamespaceAware(true); - } - - @Override - public final FileParser newFileParser() { - - DocumentBuilder builder; - try { - // never forget this! - builder = documentBuilderFactory.newDocumentBuilder(); - } catch (ParserConfigurationException ex) { - throw new IllegalStateException( - "could not load DocumentBuilder for reason " + - ex.getMessage(), ex); - } - - XPath xpath = factory.newXPath(); - - return newXmlFileParser(xpath, builder); - } - - /** - * Récupère le xpath à partir d'un fichier - * - * @param fileRules le nom du fichier contant les règles - * @return le xpath à partir d'un fichier - */ - private String getRules(String fileRules) { - StringBuilder buffer = new StringBuilder(); - - try { - String readInputStream; - - // load core rules - readInputStream = loadRulesFile(getCoreFileRules()); - if (!silent && verbose) { - getLog().info("core rules : " + getCoreFileRules()); - } - buffer.append(readInputStream); - - if (!fileRules.equals(getCoreFileRules())) { - // add custom rules - readInputStream = loadRulesFile(fileRules); - if (!silent && verbose) { - getLog().info("custom rules : " + fileRules); - } - buffer.append(" | ").append(readInputStream); - } - } catch (IOException e) { - throw new ParserException(e); - } - - return buffer.toString(); - } - - private String loadRulesFile(String fileRules) throws IOException { - File f = new File(fileRules); - - InputStream inputStream; - if (f.exists()) { - // load from a file - try { - inputStream = new FileInputStream(f); - } catch (FileNotFoundException e) { - throw new ParserException(e); - } - } else { - // load from classpath - ClassLoader classLoader = getClass().getClassLoader(); - inputStream = classLoader.getResourceAsStream(fileRules); - } - if (inputStream == null) { - throw new ParserException( - "could not found file of rules : " + fileRules); - } - - inputStream = new BufferedInputStream(inputStream); - - try { - // Lecture - String readInputStream; - readInputStream = readInputStream(inputStream); - return readInputStream; - } catch (IOException e) { - throw new ParserException(e); - } finally { - inputStream.close(); - } - } - - /** - * Permet la lecture d'un InputStream et Suppressions. - * - * @param in le flux entrant - * @return le contenu du flux - * @throws IOException si problème de lecture dans flux entrant - */ - private String readInputStream(InputStream in) throws IOException { - StringBuilder sb = new StringBuilder(); - byte[] buffer = new byte[BUFFER_SIZE]; - while (in.read(buffer, 0, BUFFER_SIZE) != -1) { - String tmp = new String(buffer); - sb.append(tmp); - } - in.close(); - // Suppression - String txt = sb.toString().trim(); - // suppression des commentaires - txt = txt.replaceAll("#.*\n", ""); - // contruction du xpath avec des ou - txt = txt.replaceAll("\\s+", " | "); - // suppression des ou de début ee fin - txt = txt.replaceAll("(^ \\| )|( \\| $)", ""); - return txt; - } - - public static abstract class XmlFileParser extends AbstractFileParser { - - private final XPath xpath; - - private final String rules; - - private final DocumentBuilder builder; - - /** - * Fonction d'extraction de la chaine - * - * @param i18nString le clef i18n - * @return la chaine - */ - public abstract String extract(String i18nString); - - public XmlFileParser(Log log, - String encoding, - SortedProperties oldParser, - boolean showTouchedFiles, - String rules, - XPath xpath, - DocumentBuilder builder) { - super(log, encoding, oldParser, showTouchedFiles); - this.xpath = xpath; - this.rules = rules; - this.builder = builder; - - } - - @Override - public void parseFile(File file) throws IOException { - - try { - - // Recherche des clés à partir d'un xpath - - Document doc = builder.parse(file.getAbsolutePath()); - XPathExpression expression = xpath.compile(rules); - NodeList list = (NodeList) - expression.evaluate(doc, XPathConstants.NODESET); - - for (int index = 0; index < list.getLength(); index++) { - Node node = list.item(index); - parseLine(file, node.getTextContent()); - } - } catch (Exception e) { - throw new ParserException(e); - } - } - - @Override - public void parseLine(File file, String key) throws IOException { - - key = extract(key); - if (key != null) { - setTouched(true); - registerKey(key); - } - } - } -} Added: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ProcessorHelper.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ProcessorHelper.java (rev 0) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ProcessorHelper.java 2011-01-04 13:43:41 UTC (rev 1837) @@ -0,0 +1,243 @@ +package org.nuiton.i18n.plugin.parser.impl; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.processor.Processor; +import org.nuiton.processor.ProcessorUtil; +import org.nuiton.processor.filters.DefaultFilter; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +/** + * Helper of processor api. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public class ProcessorHelper extends ProcessorUtil { + + /** Logger */ + private static final Log log = LogFactory.getLog(ProcessorHelper.class); + + /** + * Abstract processor to be used with some smooth logic... + * + * @author tchemit <chemit@codelutin.com> + * @since 2.1 + */ + public abstract static class AbstractParserProcessor extends Processor { + + protected boolean verbose; + + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } + + public abstract void process(File filein, + File fileout, + String encoding) throws IOException; + + protected void removefragments(FragmentRemover filter, + File filein, + File fileout, + String encoding, + boolean verbose) throws IOException { + + filter.setVerbose(verbose || log.isDebugEnabled()); + setInputFilter(filter); + doProcess(this, filein, fileout, encoding); + } + + public void extractKeys(KeyExtractor filter, + File filein, + String encoding, + boolean verbose, + Set<String> result) throws IOException { + + filter.setVerbose(verbose); + setInputFilter(filter); + InputStream in = new FileInputStream(filein); + + try { + OutputStream out = new ByteArrayOutputStream(); + try { + doProcess(this, in, out, encoding); + String lines = out.toString(); + for (String line : lines.split("\n")) { + String key = line.trim(); + if (StringUtils.isNotBlank(key)) { + result.add(key); + } + } + } finally { + out.close(); + } + } finally { + in.close(); + } + } + + public void saveKeysToFile(File fileout, + Set<String> keys) throws IOException { + StringBuilder buffer = new StringBuilder(); + List<String> sortedKeys = new ArrayList<String>(keys); + Collections.sort(sortedKeys); + + for (String key : keys) { + buffer.append(key).append("\n"); + } + FileUtils.writeStringToFile(fileout, buffer.toString()); + } + } + + /** + * To extract all content inside the header and footer. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.1 + */ + public static class KeyExtractor extends DefaultFilter { + + /** Logger. */ + static protected final Log log = + LogFactory.getLog(KeyExtractor.class); + + protected String header; + + protected String footer; + + protected boolean verbose; + + public KeyExtractor(String header, String footer) { + this.header = header; + this.footer = footer; + } + + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } + + @Override + protected String performInFilter(String ch) { + if (verbose) { + log.info("Detected key " + ch); + } + return ch + "\n"; + } + + @Override + protected String performOutFilter(String ch) { + return EMPTY_STRING; + } + + @Override + protected String getHeader() { + return header; + } + + @Override + protected String getFooter() { + return footer; + } + } + + /** + * To remove all content inside the header and footer. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.1 + */ + public static class FragmentRemover extends DefaultFilter { + + /** Logger. */ + static private final Log log = + LogFactory.getLog(FragmentRemover.class); + + protected String header; + + protected String footer; + + protected boolean verbose; + + public FragmentRemover(String header, String footer) { + this.header = header; + this.footer = footer; + } + + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } + + @Override + protected String performInFilter(String ch) { + if (verbose) { + log.info("Will remove " + ch); + } + return EMPTY_STRING; + } + + @Override + protected String performOutFilter(String ch) { + return ch; + } + + @Override + protected String getHeader() { + return header; + } + + @Override + protected String getFooter() { + return footer; + } + } + + /** + * Launch the process of the given {@code processor} to the given streams. + * + * @param processor the processor to launch + * @param in the input file + * @param out the output file + * @param encoding encoding to use to read and write files. + * @throws IOException if any problems while processing file + * @since 2.1 remove this when will use processor >= 1.1 + */ + public static void doProcess(Processor processor, + InputStream in, + OutputStream out, + String encoding) throws IOException { + InputStreamReader input = new InputStreamReader(in, encoding); + try { + OutputStreamWriter output = new OutputStreamWriter(out, encoding); + try { + processor.process(input, output); + } catch (IOException eee) { + if (log.isErrorEnabled()) { + log.error( + "Error while processing file " + in + " to " + out + + " with processor " + processor, eee); + } + throw eee; + } finally { + output.close(); + } + } finally { + input.close(); + } + } + + +} Property changes on: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ProcessorHelper.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/TapestryFileProcessor.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/TapestryFileProcessor.java 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/TapestryFileProcessor.java 2011-01-04 13:43:41 UTC (rev 1837) @@ -1,192 +0,0 @@ -/* - * #%L - * I18n :: Maven Plugin - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 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.parser.impl; - -import org.apache.commons.io.FileUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.processor.Processor; -import org.nuiton.processor.ProcessorUtil; -import org.nuiton.processor.filters.DefaultFilter; -import org.nuiton.processor.filters.Filter; - -import java.io.File; -import java.io.IOException; - -/** - * To Extract i18n keys from tapestry template files. - * <p/> - * Says : - * <ul> - * <li>Remove all xml comments</li> - * <li>Keep content of {@code ${message:XXX}}</li> - * <li>Keep content of {@code ${format:XXX}}}</li> - * </ul> - * - * @author tchemit <chemit@codelutin.com> - * @since 2.1 - */ -public class TapestryFileProcessor extends Processor { - - /** Logger. */ - static private final Log log = - LogFactory.getLog(TapestryFileProcessor.class); - - protected boolean verbose; - - public void setVerbose(boolean verbose) { - this.verbose = verbose; - } - - public void process(File filein, - File fileout, - String encoding) throws IOException { - File fileTemp1 = new File(fileout.getAbsolutePath() + "~-message"); - setInputFilter(new Filter[]{ - new RemoveCommentFilter(), - new ExtractMessageFilter() - }); - ProcessorUtil.doProcess(this, filein, fileTemp1, encoding); - - File fileTemp2 = new File(fileout.getAbsolutePath() + "~-format"); - - setInputFilter(new Filter[]{ - new RemoveCommentFilter(), - new ExtractFormatFilter() - }); - ProcessorUtil.doProcess(this, filein, fileTemp2, encoding); - - String tmp1 = FileUtils.readFileToString(fileTemp1).trim(); - String tmp2 = FileUtils.readFileToString(fileTemp2).trim(); - if (log.isDebugEnabled()) { - log.debug("Keys from message:\n" + tmp1); - log.debug("Keys from format:\n" + tmp2); - } - FileUtils.writeStringToFile(fileout, tmp1 + "\n" + tmp2); - } - - /** - * To remove comments from tapestry template files. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.1 - */ - public class RemoveCommentFilter extends DefaultFilter { - - private String header = "<" + "!" + "-" + "-"; - - private String footer = "-" + "-" + ">"; - - @Override - protected String performInFilter(String ch) { - return EMPTY_STRING; - } - - @Override - protected String performOutFilter(String ch) { - return ch; - } - - protected String getHeader() { - return header; - } - - protected String getFooter() { - return footer; - } - } - - /** - * To obtain inside of {@code ${message:XXX}} from tapestry template files. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.1 - */ - public class ExtractMessageFilter extends DefaultFilter { - - private String header = "$" + "{" + "m" + "e" + "s" + "s" + "a" + "g" + "e" + ":"; - - private String footer = "}"; - - @Override - protected String performInFilter(String ch) { - if (verbose) { - log.info("Detected key " + ch); - } - return ch + "\n"; - } - - @Override - protected String performOutFilter(String ch) { - return EMPTY_STRING; - } - - protected String getHeader() { - return header; - } - - protected String getFooter() { - return footer; - } - } - - /** - * To obtain {@code XXX} data {@code ${format:XXX=YYY}} from tapestry template files. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.1 - */ - public class ExtractFormatFilter extends DefaultFilter { - - private String header = "$" + "{" + "f" + "o" + "r" + "m" + "a" + "t" + ":"; - - private String footer = "}"; - - @Override - protected String performInFilter(String ch) { - int i = ch.indexOf('='); - if (verbose) { - log.info("Detected key " + ch); - } - if (i == -1) { - return ""; - } - return ch.substring(0, i) + "\n"; - } - - @Override - protected String performOutFilter(String ch) { - return EMPTY_STRING; - } - - protected String getHeader() { - return header; - } - - protected String getFooter() { - return footer; - } - } -} Modified: trunk/nuiton-i18n/pom.xml =================================================================== --- trunk/nuiton-i18n/pom.xml 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/nuiton-i18n/pom.xml 2011-01-04 13:43:41 UTC (rev 1837) @@ -34,7 +34,7 @@ <parent> <groupId>org.nuiton</groupId> <artifactId>i18n</artifactId> - <version>2.0.2-SNAPSHOT</version> + <version>2.1-SNAPSHOT</version> </parent> <groupId>org.nuiton.i18n</groupId> Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2011-01-04 08:15:32 UTC (rev 1836) +++ trunk/pom.xml 2011-01-04 13:43:41 UTC (rev 1837) @@ -37,7 +37,7 @@ </parent> <artifactId>i18n</artifactId> - <version>2.0.2-SNAPSHOT</version> + <version>2.1-SNAPSHOT</version> <modules> <module>nuiton-i18n</module>