Merge branch 'develop' into releases/Release_2_11_2_Branch
[jalview.git] / utils / MessageBundleChecker.java
index c870f6d..b1927e1 100644 (file)
@@ -1,3 +1,4 @@
+
 /*
  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
  * Copyright (C) $$Year-Rel$$ The Jalview Authors
@@ -23,6 +24,7 @@ import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Properties;
 import java.util.Set;
 import java.util.TreeSet;
@@ -127,8 +129,9 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner
    */
   private void doMain(String srcPath) throws IOException
   {
-    System.out.println("Scanning " + srcPath
-            + " for calls to MessageManager\n");
+    System.out.println(
+            "Scanning " + srcPath + " for calls to MessageManager\n");
+
     sourcePath = srcPath;
     loadMessages();
     File dir = new File(srcPath);
@@ -157,18 +160,19 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner
    */
   private void reportResults()
   {
-    System.out.println("\nScanned " + javaCount + " source files");
     System.out.println(
-            "Messages.properties has " + messages.size()
-            + " keys");
+            "\nMessages.properties has " + messages.size() + " keys");
+    System.out.println("Scanned " + javaCount + " source files\n");
+
     if (!invalidKeys.isEmpty())
     {
       System.out.println("Found " + invalidKeys.size()
-              + " possibly invalid parameter call"
+              + " possibly invalid unmatched key(s) in source code"
               + (invalidKeys.size() > 1 ? "s" : ""));
     }
 
-    System.out.println("Keys not found, assumed constructed dynamically:");
+    System.out.println(
+            "Keys not found in source code, assumed constructed dynamically:");
     int dynamicCount = 0;
     for (String key : messageKeys)
     {
@@ -182,7 +186,7 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner
     if (dynamicCount < messageKeys.size())
     {
       System.out.println((messageKeys.size() - dynamicCount)
-              + " keys not found, possibly unused");
+              + " key(s) not found, possibly unused, or used indirectly (check code manually!)");
       for (String key : messageKeys)
       {
         if (!isDynamic(key))
@@ -192,7 +196,7 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner
       }
     }
     System.out
-            .println("(Run i18nAnt.xml to compare other message bundles)");
+            .println("\nRun i18nAnt.xml to compare other message bundles");
   }
 
   /**
@@ -284,11 +288,11 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner
    * @param lineNo
    * @param line
    */
-  private void inspectSourceLines(String path, int lineNo, String line)
+  private void inspectSourceLines(String path, int lineNo,
+          final String line)
   {
-    String lineNos = String
-            .format("%d-%d", lineNo, lineNo + bufferSize
-            - 1);
+    String lineNos = String.format("%d-%d", lineNo,
+            lineNo + bufferSize - 1);
     for (String method : METHODS)
     {
       int pos = line.indexOf(method);
@@ -328,10 +332,14 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner
 
       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);
+        if (!dynamicKeys.contains(key))
+        {
+//          System.out.println(String.format(
+//                  "Dynamic key \"" + key + "\" at %s line %s %s",
+//                  path.substring(sourcePath.length()), lineNos, line));
+          dynamicKeys.add(key);
+        }
         continue;
       }
 
@@ -344,8 +352,8 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner
 
       if (!(STRING_PATTERN.matcher(messageKey).matches()))
       {
-        System.out.println(String.format("Dynamic key at %s line %s %s",
-                path.substring(sourcePath.length()), lineNos, line));
+//        System.out.println(String.format("Dynamic key at %s line %s %s",
+//                path.substring(sourcePath.length()), lineNos, line));
         continue;
       }
 
@@ -356,16 +364,33 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner
 
       if (!this.messages.containsKey(messageKey))
       {
-        System.out.println(String.format(
-                "Unmatched key '%s' at line %s of %s", messageKey, lineNos,
-                path.substring(sourcePath.length())));
         if (!invalidKeys.contains(messageKey))
         {
+          // report each key the first time found only
+          System.out.println(String.format(
+                  "Unmatched key '%s' at line %s of %s", messageKey,
+                  lineNos, path.substring(sourcePath.length())));
           invalidKeys.add(messageKey);
         }
       }
       messageKeys.remove(messageKey);
     }
+
+    /*
+     * and a brute force scan for _any_ as yet unseen message key, to catch the
+     * cases where it is in a variable or a condition
+     */
+    if (line.contains("\""))
+    {
+      Iterator<String> it = messageKeys.iterator();
+      while (it.hasNext())
+      {
+        if (line.contains(it.next()))
+        {
+          it.remove(); // remove as 'seen'
+        }
+      }
+    }
   }
 
   /**
@@ -399,8 +424,8 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner
      */
     int commaPos = key.indexOf(",");
     int bracePos = key.indexOf(")");
-    int endPos = commaPos == -1 ? bracePos : (bracePos == -1 ? commaPos
-            : Math.min(commaPos, bracePos));
+    int endPos = commaPos == -1 ? bracePos
+            : (bracePos == -1 ? commaPos : Math.min(commaPos, bracePos));
     if (endPos == -1 && key.length() > 1 && key.endsWith("\""))
     {
       endPos = key.length();
@@ -417,8 +442,8 @@ public class MessageBundleChecker implements BufferedLineReader.LineCleaner
   void loadMessages() throws IOException
   {
     messages = new Properties();
-    FileReader reader = new FileReader(new File(sourcePath,
-            "../resources/lang/Messages.properties"));
+    FileReader reader = new FileReader(
+            new File(sourcePath, "../resources/lang/Messages.properties"));
     messages.load(reader);
     reader.close();