Author: tchemit Date: 2013-03-08 10:50:19 +0100 (Fri, 08 Mar 2013) New Revision: 2599 Url: http://nuiton.org/projects/jaxx/repository/revisions/2599 Log: fixes #2575: Add a strict mode on generate-help-ids mojo Modified: trunk/jaxx-maven-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpIdsMojo.java Modified: trunk/jaxx-maven-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpIdsMojo.java =================================================================== --- trunk/jaxx-maven-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpIdsMojo.java 2013-03-08 09:50:09 UTC (rev 2598) +++ trunk/jaxx-maven-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpIdsMojo.java 2013-03-08 09:50:19 UTC (rev 2599) @@ -54,7 +54,7 @@ defaultPhase = LifecyclePhase.PROCESS_SOURCES, requiresProject = true) public class GenerateHelpIdsMojo extends AbstractGenerateHelpMojo { - public static final String INPUT_FILENAME_FORMAT = "%s_%s_%s.properties"; + public static final String INPUT_FILENAME_FORMAT = "%s-%s_%s.properties"; /** * Flag to merge ids into input directory. @@ -67,6 +67,23 @@ protected boolean mergeIdsToInput; /** + * Flag to remove obsolete ids into input files. + * <p/> + * <strong>Important Note:</strong> Be sure to use this after a clean, or + * using with {@code jaxx.force} property to get all ids of any jaxx files + * detected, otherwise you could loose some data. USE WITH CAUTION. + * <p/> + * <strong>Note:</strong> Only used when {@code mergeIdsToInput} parameter + * is on. + * + * @since 2.5.12 + */ + @Parameter(property = "jaxx.strictMode", + defaultValue = "false", + required = true) + protected boolean strictMode; + + /** * Directory where to merge (create) input files. * <p/> * <strong>Note:</strong> Only used when {@code mergeIdsToInput} parameter @@ -75,15 +92,15 @@ * @since 2.5.12 */ @Parameter(property = "jaxx.inputHelpDirectory", - defaultValue = "src/main/resources/help", + defaultValue = "src/main/help", required = true) - private File inputHelpDirectory; + protected File inputHelpDirectory; /** * Prefix of input files. * <p/> - * <strong>Note:</strong> {@code _locale.properties} will be added to each - * generated file. + * <strong>Note:</strong> {@code -locale.properties} will be added to each + * generated file (example: {@code helpMapping-fr_FR.properties}). * <p/> * <strong>Note:</strong> Only used when {@code mergeIdsToInput} parameter * is on. @@ -93,7 +110,7 @@ @Parameter(property = "jaxx.inputHelpFilenamePrefix", defaultValue = "helpMapping", required = true) - private String inputHelpFilenamePrefix; + protected String inputHelpFilenamePrefix; /** help ids to react. */ protected Set<String> helpIds; @@ -133,12 +150,16 @@ boolean b = super.checkSkip(); if (b) { - if (helpIds.isEmpty()) { -// if (isVerbose()) { + if (strictMode) { + + // always launch goal + getLog().info("Strict mode is on, will use all detected ids."); + + } else if (helpIds.isEmpty()) { + // no ids detected in this compilation round getLog().info("No help ids to treate, will skip goal."); -// } - return false; + b = false; } } return b; @@ -198,29 +219,59 @@ } } Set<String> existingId = p.stringPropertyNames(); + Set<String> newIds = new HashSet<String>(); + + SortedProperties resultProperties = p; + + // get all detected ids for (String helpId : helpIds) { newIds.add(removeQuote(helpId)); } - newIds.removeAll(existingId); - if (!newIds.isEmpty()) { - getLog().info("Add " + newIds.size() + " keys."); + if (strictMode) { + // remove all obsolete ids + for (String s : existingId) { + if (!newIds.contains(s)) { + resultProperties.remove(s); + } + } + + // update existing ids + existingId.retainAll(newIds); + } else { + + // remove all existing ids (no need to treat them again) + newIds.removeAll(existingId); + } + + if (newIds.isEmpty()) { + + getLog().info("No keys to add."); + } else { + + if (strictMode) { + getLog().info("Regenerate files with " + newIds.size() + " keys."); + } else { + getLog().info("Add " + newIds.size() + " keys."); + } + for (String helpId : newIds) { - p.put(helpId, ""); + if (!resultProperties.containsKey(helpId)) { + resultProperties.put(helpId, ""); + } } Writer writer = Files.newWriter(inputFile, Charsets.UTF_8); try { - p.store(writer, "Generated by " + getClass().getName()); + resultProperties.store(writer, "Generated by " + getClass().getName()); writer.close(); } finally { Closeables.closeQuietly(writer); } } - } }