Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/AbstractI18nParser.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/AbstractI18nParser.java:1.1 --- /dev/null Sat Jan 5 13:06:56 2008 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/core/AbstractI18nParser.java Sat Jan 5 13:06:51 2008 @@ -0,0 +1,176 @@ +/* +* \#\#% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, +* Tony Chemit +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* 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 Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* \#\#% */ +package org.codelutin.i18n.plugin.core; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.codelutin.i18n.plugin.ui.KeysModifier; +import org.codelutin.util.FileUtil; +import org.codelutin.util.StringUtil; + +import java.io.File; +import java.io.FileInputStream; +import java.util.Properties; + +/** + * Abstract implementation for parsing goal. + * + * @author tony + */ +public abstract class AbstractI18nParser extends AbstractI18nPlugin implements Parser { + + /** @return the outGetter to use for the instance (java.getter,...) */ + protected abstract String getOutGetter(); + + /** @return the starting regex expression to catch keys in key modifier */ + protected abstract String getKeyModifierStart(); + + /** @return the ending regex expression to catch keys in key modifier */ + protected abstract String getKeyModifierEnd(); + + /** @return the default includes to add to directory scanner */ + protected abstract String[] getDefaultIncludes(); + + /** @return the default src directory to use in directory scanner */ + protected abstract File getDefaultBasedir(); + + /** + * @description Source entries (src+includes+excludes) . + * @parameter expression="${i18n.entries}" + */ + protected SourceEntry[] entries; + + /** + * @description verbose + * @parameter expression="${maven.verbose}" + * @readonly + */ + protected boolean verbose; + + protected Properties result; + protected Properties oldParser; + protected Properties oldLanguage; + + protected int fileTreated = 0; + + protected long t0; + + public void init() { + t0 = System.nanoTime(); + result = new Properties(); + oldParser = new Properties(); + oldLanguage = new Properties(); + out.mkdirs(); + // Événements + if (keysModifier) { + addParserEvent(KeysModifier.getInstance(getKeyModifierStart(), getKeyModifierEnd())); + } + } + + /* + * (non-Javadoc) + * @see org.apache.maven.plugin.AbstractMojo#execute() + */ + public void execute() throws MojoExecutionException, MojoFailureException { + + init(); + + try { + // Reprise sur un ancien parsing + File oldParserFile = new File(out.getAbsolutePath() + File.separatorChar + getOutGetter()); + oldParserFile.createNewFile(); + oldParser.load(new FileInputStream(oldParserFile)); + + File saveFile = new File(out.getAbsolutePath() + File.separatorChar + getOutGetter() + "~"); + FileUtil.copy(oldParserFile, saveFile); + + // Anciennes clés disponnibles + File oldLanguageFile = new File(src.getAbsolutePath() + File.separatorChar + "language-" + bundles[0] + ".properties"); + oldLanguageFile.createNewFile(); + oldLanguage.load(new FileInputStream(oldLanguageFile)); + + // Parsing + parse(); + + // Suppression du fichier sauvegarder + saveFile.delete(); + + } catch (Exception e) { + log.error("Error code parsing ", e); + throw new MojoFailureException("Error code parsing"); + } + + log.info(getLogEntry(" success [treated file(s) : " + fileTreated + "]", fileTreated-1, 0, t0)); + } + + public void parse() { + if (entries == null || entries.length == 0) { + // add the default sourceEntry + entries = new SourceEntry[]{new SourceEntry()}; + } + long t00 = System.nanoTime(); + for (SourceEntry entry : entries) { + // get found files + String[] foundFiles = entry.getIncludedFiles(getDefaultBasedir(), getDefaultIncludes()); + long t000 = System.nanoTime(); + + log.info(getLogEntry(" parse <" + entry + "> [incoming file(s) : " + foundFiles.length + "]", 0, 0, 0)); + // launch parser for found files + parseEntry(entry.getBasedir(), foundFiles); + fileTreated += foundFiles.length; + if (verbose) { + log.debug(getLogEntry(" success <" + entry.getBasedir() + "> [treated file(s) : " + foundFiles.length + "]", foundFiles.length - 1, t000, t00)); + } + t00 = System.nanoTime(); + } + } + + protected void parseEntry(File basedir, String[] files) { + long t00 = System.nanoTime(); + for (int i = 0; i < files.length; i++) { + String file1 = files[i]; + long t000 = System.nanoTime(); + String fileName = basedir.getAbsolutePath() + File.separator + file1; + File file = new File(fileName); + for (ParserEvent event : events) { + event.eventChangeFile(file); + } + parseFile(file); + if (verbose) { + log.debug(getLogEntry(fileName, i, t000, t00)); + } + for (ParserEvent event : events) { + event.eventNextFile(file); + } + } + } + + protected String getLogEntry(String msg, int nbFiles, long time, long all) { + long now = System.nanoTime(); + long delta = now - time; + String s = getClass().getSimpleName() + " : " + msg; + if (time > 0) { + s += " (" + StringUtil.convertTime(delta) + ")"; + } + if (all > 0 && nbFiles>-1) { + s += "(total time:" + StringUtil.convertTime(now - all) + ") ( ~ " + StringUtil.convertTime(((now - all) / (nbFiles + 1))) + " /file)"; + } + return s; + } +}