From c50c9efe8390a06bcb980ae0807626ed05407d65 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Thu, 10 Dec 2015 19:31:56 +0100 Subject: [PATCH] Refine messages file verifier - Fix minor open issues - Add documentation - Rename to [hopefully] more suitable names --- src/tools/messages/MessageFileVerifier.java | 44 ++++++++++++------- .../messages/MessagesVerifierRunner.java | 15 ++++--- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/tools/messages/MessageFileVerifier.java b/src/tools/messages/MessageFileVerifier.java index 3319fbd22..af06298d7 100644 --- a/src/tools/messages/MessageFileVerifier.java +++ b/src/tools/messages/MessageFileVerifier.java @@ -23,30 +23,43 @@ public class MessageFileVerifier { // Map with the missing key and a boolean indicating whether or not it was added to the file by this object private final Map missingKeys = new HashMap<>(); + /** + * Create a verifier that verifies the given messages file. + * + * @param messagesFile The messages file to process + */ public MessageFileVerifier(String messagesFile) { this.messagesFile = messagesFile; - analyze(); + verifyKeys(); } + /** + * Return the list of unknown keys, i.e. the list of keys present in the file that are not + * part of the {@link MessageKey} enum. + * + * @return List of unknown keys + */ public Set getUnknownKeys() { return unknownKeys; } + /** + * Return the list of missing keys, i.e. all keys that are part of {@link MessageKey} but absent + * in the messages file. + * + * @return The list of missing keys in the file + */ public Map getMissingKeys() { return missingKeys; } - private void analyze() { - findMissingKeys(); - } - - private void findMissingKeys() { + private void verifyKeys() { Set messageKeys = getAllMessageKeys(); List fileLines = FileUtils.readLinesFromFile(messagesFile); for (String line : fileLines) { // Skip comments and empty lines if (!line.startsWith("#") && !line.trim().isEmpty()) { - verifyKeyInFile(line, messageKeys); + processKeyInFile(line, messageKeys); } } @@ -56,7 +69,7 @@ public class MessageFileVerifier { } } - private void verifyKeyInFile(String line, Set messageKeys) { + private void processKeyInFile(String line, Set messageKeys) { if (line.indexOf(':') == -1) { System.out.println("Skipping line in unknown format: '" + line + "'"); return; @@ -70,17 +83,16 @@ public class MessageFileVerifier { } } + /** + * Add missing keys to the file with the provided default (English) message. + * + * @param defaultMessages The collection of default messages + */ public void addMissingKeys(Map defaultMessages) { List keysToAdd = new ArrayList<>(); - for (Map.Entry entry : missingKeys.entrySet()) { - if (Boolean.FALSE.equals(entry.getValue())) { - String defaultMessage = defaultMessages.get(entry.getKey()); - if (defaultMessage == null) { - System.out.println("Error: Key '" + entry.getKey() + "' not present in default messages"); - } else { - keysToAdd.add(entry.getKey()); - } + if (Boolean.FALSE.equals(entry.getValue()) && defaultMessages.get(entry.getKey()) != null) { + keysToAdd.add(entry.getKey()); } } diff --git a/src/tools/messages/MessagesVerifierRunner.java b/src/tools/messages/MessagesVerifierRunner.java index 1d6e743c0..be0313990 100644 --- a/src/tools/messages/MessagesVerifierRunner.java +++ b/src/tools/messages/MessagesVerifierRunner.java @@ -17,7 +17,7 @@ import java.util.regex.Pattern; import static java.lang.String.format; /** - * TODO: ljacqu write JavaDoc + * Entry point of the messages verifier. */ public final class MessagesVerifierRunner { @@ -28,6 +28,7 @@ public final class MessagesVerifierRunner { } public static void main(String[] args) { + // Prompt user for options Scanner scanner = new Scanner(System.in); System.out.println("Check a specific file only?"); System.out.println("- Empty line will check all files in the resources messages folder (default)"); @@ -38,6 +39,7 @@ public final class MessagesVerifierRunner { boolean addMissingKeys = "y".equals(scanner.nextLine()); scanner.close(); + // Set up needed objects Map defaultMessages = null; if (addMissingKeys) { defaultMessages = constructDefaultMessages(); @@ -49,11 +51,9 @@ public final class MessagesVerifierRunner { } else { File customFile = new File(inputFile.replace(SOURCES_TAG, MESSAGES_FOLDER)); messageFiles = Collections.singletonList(customFile); - if (messageFiles.isEmpty()) { - throw new RuntimeException("Error getting message files: list of files is empty"); - } } + // Verify the given files for (File file : messageFiles) { System.out.println("Verifying '" + file.getName() + "'"); MessageFileVerifier verifier = new MessageFileVerifier(file.getAbsolutePath()); @@ -111,11 +111,11 @@ public final class MessagesVerifierRunner { if (line.startsWith("#") || line.trim().isEmpty()) { continue; } - if (line.indexOf(':') == -1) { + if (line.indexOf(':') == -1 || line.indexOf(':') == line.length() - 1) { System.out.println("Warning! Unknown format in default messages file for line '" + line + "'"); } else { String key = line.substring(0, line.indexOf(':')); - messages.put(key, line.substring(line.indexOf(':') + 1)); // fixme: may throw exception + messages.put(key, line.substring(line.indexOf(':') + 1)); } } return messages; @@ -145,6 +145,9 @@ public final class MessagesVerifierRunner { messageFiles.add(file); } } + if (messageFiles.isEmpty()) { + throw new RuntimeException("Error getting message files: list of files is empty"); + } return messageFiles; } }