From 453f3ba1cf27f427b70f0c64c1c4f3dc9216de19 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Wed, 22 Nov 2017 12:25:13 +0000 Subject: [PATCH] JAL-1424 enhanced to report dynamic keys separately --- utils/MessageBundleChecker.java | 74 +++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 11 deletions(-) diff --git a/utils/MessageBundleChecker.java b/utils/MessageBundleChecker.java index 4489a93..c870f6d 100644 --- a/utils/MessageBundleChecker.java +++ b/utils/MessageBundleChecker.java @@ -24,6 +24,7 @@ import java.io.FileReader; import java.io.IOException; import java.util.HashSet; import java.util.Properties; +import java.util.Set; import java.util.TreeSet; import java.util.regex.Pattern; @@ -89,7 +90,9 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner private int javaCount; - private HashSet invalidKeys; + private Set invalidKeys; + + private Set dynamicKeys; /** * Runs the scan given the path to the root of Java source directories @@ -125,7 +128,7 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner private void doMain(String srcPath) throws IOException { System.out.println("Scanning " + srcPath - + " for calls to MessageManager"); + + " for calls to MessageManager\n"); sourcePath = srcPath; loadMessages(); File dir = new File(srcPath); @@ -134,7 +137,10 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner System.out.println(srcPath + " not found"); return; } - invalidKeys = new HashSet(); + + invalidKeys = new HashSet<>(); + dynamicKeys = new HashSet<>(); + if (dir.isDirectory()) { scanDirectory(dir); @@ -152,17 +158,60 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner private void reportResults() { System.out.println("\nScanned " + javaCount + " source files"); - System.out.println("Message.properties has " + messages.size() + System.out.println( + "Messages.properties has " + messages.size() + " keys"); - System.out.println("Found " + invalidKeys.size() - + " possibly invalid parameter calls"); + if (!invalidKeys.isEmpty()) + { + System.out.println("Found " + invalidKeys.size() + + " possibly invalid parameter call" + + (invalidKeys.size() > 1 ? "s" : "")); + } - System.out.println(messageKeys.size() - + " keys not found, either unused or constructed dynamically"); + System.out.println("Keys not found, assumed constructed dynamically:"); + int dynamicCount = 0; for (String key : messageKeys) { - System.out.println(" " + key); + if (isDynamic(key)) + { + System.out.println(" " + key); + dynamicCount++; + } + } + + if (dynamicCount < messageKeys.size()) + { + System.out.println((messageKeys.size() - dynamicCount) + + " keys not found, possibly unused"); + for (String key : messageKeys) + { + if (!isDynamic(key)) + { + System.out.println(" " + key); + } + } + } + System.out + .println("(Run i18nAnt.xml to compare other message bundles)"); + } + + /** + * Answers true if the key starts with one of the recorded dynamic key stubs, + * else false + * + * @param key + * @return + */ + private boolean isDynamic(String key) + { + for (String dynamic : dynamicKeys) + { + if (key.startsWith(dynamic)) + { + return true; + } } + return false; } /** @@ -275,14 +324,17 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner continue; } + String messageKey = getMessageKey(method, methodArgs); + if (METHOD3 == method) { System.out.println(String.format("Dynamic key at %s line %s %s", path.substring(sourcePath.length()), lineNos, line)); + String key = messageKey.substring(1, messageKey.length() - 1); + dynamicKeys.add(key); continue; } - String messageKey = getMessageKey(method, methodArgs); if (messageKey == null) { System.out.println(String.format("Trouble parsing %s line %s %s", @@ -370,7 +422,7 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner messages.load(reader); reader.close(); - messageKeys = new TreeSet(); + messageKeys = new TreeSet<>(); for (Object key : messages.keySet()) { messageKeys.add((String) key); -- 1.7.10.2