Author: tchemit Date: 2010-03-09 17:32:25 +0100 (Tue, 09 Mar 2010) New Revision: 1709 Log: - clean code - do little improvment on code Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/AbstractI18nMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/GenerateMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/GetterMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/AbstractI18nParserMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/I18nLogger.java 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/SourceEntry.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/AbstractI18nMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/AbstractI18nMojo.java 2010-03-08 18:35:57 UTC (rev 1708) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/AbstractI18nMojo.java 2010-03-09 16:32:25 UTC (rev 1709) @@ -142,6 +142,8 @@ public void init() throws Exception { if (verbose) { + // in verbose mode, no silent + silent = false; getLog().info("config - verbose mode is on"); } locales = I18nUtil.parseLocales(bundles); Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/GenerateMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/GenerateMojo.java 2010-03-08 18:35:57 UTC (rev 1708) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/GenerateMojo.java 2010-03-09 16:32:25 UTC (rev 1709) @@ -66,6 +66,14 @@ */ protected boolean keepBackup; + /** + * Conserve les fichiers de getters. + * + * @parameter expression="${i18n.keepGetters}" default-value="false" + */ + protected boolean keepGetters; + + @Override protected boolean checkSkip() { if (!needGeneration()) { @@ -89,6 +97,8 @@ // Merge File bundleSrc = getI18nFile(src, artifactId, locale, false); File bundleOut = getI18nFile(out, artifactId, locale, false); + File bundleGetterOut = getI18nFile( + out, artifactId + GetterMojo.FROM_GETTERS, locale, false); SortedProperties propertiesSrc = new SortedProperties(encoding); @@ -103,7 +113,8 @@ // bundle out, all the bundle src keys propertiesOut.putAll(propertiesSrc); } - propertiesOut.load(bundleOut); + propertiesOut.load(bundleGetterOut); +// propertiesOut.load(bundleOut); // Parcours des clés for (Object key : propertiesOut.keySet()) { @@ -150,6 +161,10 @@ getLog().info("copy bundle " + locale + " to src"); } } + + if (!keepGetters) { + deleteFile(bundleGetterOut); + } } } } Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/GetterMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/GetterMojo.java 2010-03-08 18:35:57 UTC (rev 1708) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/GetterMojo.java 2010-03-09 16:32:25 UTC (rev 1709) @@ -38,6 +38,15 @@ */ public class GetterMojo extends AbstractI18nMojo { + /** + * Conserve les fichiers de getters. + * + * @parameter expression="${i18n.keepGetters}" default-value="false" + */ + protected boolean keepGetters; + + protected static final String FROM_GETTERS = "-fromGetters"; + @Override protected boolean checkSkip() { @@ -58,10 +67,14 @@ getLog().info("config - locales : " + Arrays.toString(locales)); } - File bundleGetters = new File(out.getAbsolutePath() + - File.separatorChar + artifactId + - ".properties"); - createNewFile(bundleGetters); + File bundleGetters = new File(out.getAbsoluteFile(), + artifactId + ".properties"); + + createDirectoryIfNecessary(bundleGetters.getParentFile()); +// createNewFile(bundleGetters); + + SortedProperties propertiesOut = new SortedProperties(getEncoding()); + DirectoryScanner ds = new DirectoryScanner(); ds.setBasedir(out); ds.setIncludes(new String[]{"*.getter"}); @@ -72,8 +85,17 @@ for (String file : files) { long t0 = System.nanoTime(); File bundleGetter = getGetterFile(out, file, false); - concactProperties(bundleGetter, bundleGetters); - if (genSrc) { + + // chargement du getter + SortedProperties propertiesIn = + new SortedProperties(getEncoding()).load(bundleGetter); + + // ajout des entrées dans le bundle + propertiesOut.putAll(propertiesIn); +// concactProperties(bundleGetter, bundleGetters); + + if (!keepGetters) { +// if (genSrc) { deleteFile(bundleGetter); } if (!silent) { @@ -83,18 +105,24 @@ } } + // sauvegarde du fichier des getters + propertiesOut.store(bundleGetters); + // Création des bundles for (Locale locale : locales) { if (getLog().isDebugEnabled()) { getLog().debug("generate bundle for locale " + locale); } - File bundleOut = getI18nFile(out, artifactId, locale, false); + File bundleOut = getI18nFile(out, artifactId + FROM_GETTERS, locale, false); +// File bundleOut = getI18nFile(out, artifactId, locale, false); copyFile(bundleGetters, bundleOut); if (!silent && verbose) { getLog().info("generate bundle " + locale); } } - deleteFile(bundleGetters); + if (!keepGetters) { + deleteFile(bundleGetters); + } } /** @@ -104,7 +132,9 @@ * @param out le fichier sortant * @throws IOException si problème pendant la sauvegarde ou fichier non * trouvé. + * @deprecated since 1.1, this is no more used */ + @Deprecated protected void concactProperties(File in, File out) throws IOException { SortedProperties propertiesIn = new SortedProperties(encoding).load(in); SortedProperties propertiesOut = Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/AbstractI18nParserMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/AbstractI18nParserMojo.java 2010-03-08 18:35:57 UTC (rev 1708) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/AbstractI18nParserMojo.java 2010-03-09 16:32:25 UTC (rev 1709) @@ -127,7 +127,7 @@ protected List<File> treadedFiles; /** logger verbeux */ - protected I18nLogger verboseLog = new I18nLogger(this); + protected I18nLogger verboseLog; public boolean isStrictMode() { return strictMode; @@ -139,6 +139,7 @@ @Override public void init() throws Exception { + verboseLog = new I18nLogger(this); super.init(); t0 = System.nanoTime(); result = new SortedProperties(encoding); @@ -171,9 +172,9 @@ } treadedFiles = new ArrayList<File>(); - if (!silent && verbose) { - showTouchedFiles = true; - } +// if (!silent && verbose) { +// showTouchedFiles = true; +// } } @Override @@ -229,9 +230,6 @@ @Override public void parse() throws IOException { if (treateDefaultEntry) { - if (verbose && getLog().isInfoEnabled()) { - getLog().info("add default entry"); - } addDefaultEntry(); } long t00 = System.nanoTime(); @@ -251,11 +249,13 @@ long t000 = System.nanoTime(); int nbFiles = entry.getFiles().length; - if (!silent && verbose) { + if (!silent) { +// if (!silent && verbose) { vLog.infoEntry("start", vLog.getLogEntry( "[incoming file(s) : " + entry.getFoudFiles() + "]", 0, 0, 0)); } + int nbTreatedFilesBefore = treadedFiles.size(); // launch parser for found files parseEntry(entry); @@ -268,10 +268,16 @@ } } fileTreated += nbFiles; - if (!silent && verbose) { - vLog.infoEntry("end", vLog.getLogEntry( - "[treated file(s) : " + nbFiles + "]", nbFiles, t000, - t00)); + if (!silent) { +// if (!silent && verbose) { + int nbTreatedFilesAfter = treadedFiles.size() - + nbTreatedFilesBefore; + vLog.infoEntry( + "end", + vLog.getLogEntry( + "[treated file(s) : " + nbTreatedFilesAfter + + "/" + nbFiles + "]", nbFiles, t000, + t00)); } t00 = System.nanoTime(); } Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/I18nLogger.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/I18nLogger.java 2010-03-08 18:35:57 UTC (rev 1708) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/I18nLogger.java 2010-03-09 16:32:25 UTC (rev 1709) @@ -104,10 +104,10 @@ * @return la chaine de log formatée */ public String getLogEntry(String msg, int nbFiles, long time, long all) { - long now = System.nanoTime(); + long now = System.nanoTime() ; long delta = now - time; String s = msg; - if (time > 0) { + if (time > 0 && all == 0) { s += " (" + PluginHelper.convertTime(delta) + ")"; } if (all > 0) { Modified: 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 2010-03-08 18:35:57 UTC (rev 1708) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/Parser.java 2010-03-09 16:32:25 UTC (rev 1709) @@ -44,15 +44,17 @@ * Parse sur un fichier * * @param file le fichier à parser + * @throws IOException if any pb */ - void parseFile(File file); + 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); + void parseLine(File file, String line)throws IOException; } Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/SourceEntry.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/SourceEntry.java 2010-03-08 18:35:57 UTC (rev 1708) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/SourceEntry.java 2010-03-09 16:32:25 UTC (rev 1709) @@ -166,7 +166,7 @@ if (includes != null) { sb.append(", includes:").append(Arrays.toString(includes)); } - if (excludes != null) { + if (excludes != null && excludes.length>0) { sb.append(", excludes:").append(Arrays.toString(excludes)); } return sb.toString(); 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 2010-03-08 18:35:57 UTC (rev 1708) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJavaMojo.java 2010-03-09 16:32:25 UTC (rev 1709) @@ -25,10 +25,7 @@ import org.nuiton.io.FileUpdaterHelper; import org.nuiton.processor.filters.I18nFilter; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStreamReader; -import java.io.LineNumberReader; +import java.io.*; /** * Récupération des chaine a traduire depuis les fichiers java. @@ -105,23 +102,28 @@ } @Override - public void parseFile(File srcFile) { - LineNumberReader lnr; + public void parseFile(File srcFile) throws IOException { String line = null; + LineNumberReader lnr = new LineNumberReader(new InputStreamReader( + new FileInputStream(srcFile))); try { - lnr = new LineNumberReader(new InputStreamReader( - new FileInputStream(srcFile))); - - while (lnr.ready()) { - line = lnr.readLine(); + while ((line = lnr.readLine())!=null) { parseLine(srcFile, line); } +// while (lnr.ready()) { +// line = lnr.readLine(); +// parseLine(srcFile, line); +// } } catch (Exception e) { if (line != null) { - getLog().error("could not parse line " + line); + getLog().error( + "could not parse line (" + lnr.getLineNumber() + ") '" + + line + "' of file " + srcFile); } throw new ParserException(e); + } finally { + lnr.close(); } } @@ -130,6 +132,7 @@ String keysSet = filter.parse(line); if (!keysSet.equals(I18nFilter.EMPTY_STRING)) { + // au moins une clef i18n a ete detectee dans le fichier touchFile = true; // Found a set of i18n Strings, split it. String[] keys = keysSet.split("="); @@ -139,11 +142,17 @@ event.eventChangeKey(key, !oldLanguage.containsKey(key)); keyModified = event.eventGetRealKey(); } - if (oldParser.containsKey(key)) { - result.put(keyModified, oldParser.get(key)); - } else { - result.put(keyModified, key); + Object value = oldParser.get(key); + if (value == null) { + // nouvelle clef du parser, on utilise la clef comme valeur + value = key; } + result.put(keyModified,value); +// if (oldParser.containsKey(key)) { +// result.put(keyModified, oldParser.get(key)); +// } else { +// result.put(keyModified, key); +// } } } }