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;
private int javaCount;
- private HashSet<String> invalidKeys;
+ private Set<String> invalidKeys;
+
+ private Set<String> dynamicKeys;
/**
* Runs the scan given the path to the root of Java source directories
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);
System.out.println(srcPath + " not found");
return;
}
- invalidKeys = new HashSet<String>();
+
+ invalidKeys = new HashSet<>();
+ dynamicKeys = new HashSet<>();
+
if (dir.isDirectory())
{
scanDirectory(dir);
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;
}
/**
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",
messages.load(reader);
reader.close();
- messageKeys = new TreeSet<String>();
+ messageKeys = new TreeSet<>();
for (Object key : messages.keySet())
{
messageKeys.add((String) key);