Message files verifier - refactoring

- Separate logic from output (still potential for improvement)
- Prompt user for options (single file mode / write missing keys or not)
This commit is contained in:
ljacqu 2015-12-10 19:18:05 +01:00
parent 5b92b30a96
commit 186ef965ca
4 changed files with 148 additions and 34 deletions

View File

@ -59,3 +59,5 @@ email_send: '[AuthMe] Recovery Email Send !'
country_banned: 'Your country is banned from this server' country_banned: 'Your country is banned from this server'
antibot_auto_enabled: '[AuthMe] AntiBotMod automatically enabled due to massive connections!' antibot_auto_enabled: '[AuthMe] AntiBotMod automatically enabled due to massive connections!'
antibot_auto_disabled: '[AuthMe] AntiBotMod automatically disabled after %m Minutes, hope invasion stopped' antibot_auto_disabled: '[AuthMe] AntiBotMod automatically disabled after %m Minutes, hope invasion stopped'
kick_antibot: 'AntiBot protection mode is enabled! You have to wait some minutes before joining the server.'
email_exists: '&cA recovery email was already sent! You can discard it and send a new one using the command below:'

View File

@ -21,7 +21,7 @@ usage_unreg: '&eSử dụng: /unregister mật-khẩu'
pwd_changed: '&cĐã đổi mật khẩu!' pwd_changed: '&cĐã đổi mật khẩu!'
user_unknown: '&cTài khoản này chưa được đăng kí' user_unknown: '&cTài khoản này chưa được đăng kí'
password_error: '&fMật khẩu không khớp' password_error: '&fMật khẩu không khớp'
unvalid_session: '&fPhiên đăng nhập không hồi đáp, vui lòng chờ phiên đăng nhập kết thúc' invalid_session: '&fPhiên đăng nhập không hồi đáp, vui lòng chờ phiên đăng nhập kết thúc'
reg_only: '&fChỉ cho phép người đã đăng kí! Hãy vào trang http://web-của.bạn/ để đăng kí' reg_only: '&fChỉ cho phép người đã đăng kí! Hãy vào trang http://web-của.bạn/ để đăng kí'
logged_in: '&cĐã đăng nhập!' logged_in: '&cĐã đăng nhập!'
logout: '&cThoát đăng nhập thành công' logout: '&cThoát đăng nhập thành công'

View File

@ -2,47 +2,61 @@ package messages;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import utils.FileUtils; import utils.FileUtils;
import utils.ToolsConstants;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
/** /**
* Verifies that a message file has all keys as given in {@link MessageKey}. * Verifies a message file's keys to ensure that it is in sync with {@link MessageKey}, i.e. that the file contains
* all keys and that it doesn't have any unknown ones.
*/ */
public class MessageFileVerifier { public class MessageFileVerifier {
public static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/"; private static final char NEW_LINE = '\n';
private static final String NEW_LINE = "\n";
private final String messagesFile; private final String messagesFile;
private final Map<String, String> defaultMessages; private final Set<String> unknownKeys = new HashSet<>();
// Map with the missing key and a boolean indicating whether or not it was added to the file by this object
private final Map<String, Boolean> missingKeys = new HashMap<>();
public MessageFileVerifier(Map<String, String> defaultMessages, String messagesFile) { public MessageFileVerifier(String messagesFile) {
this.messagesFile = messagesFile; this.messagesFile = messagesFile;
this.defaultMessages = defaultMessages; analyze();
} }
public void verify(boolean addMissingKeys) { public Set<String> getUnknownKeys() {
return unknownKeys;
}
public Map<String, Boolean> getMissingKeys() {
return missingKeys;
}
private void analyze() {
findMissingKeys();
}
private void findMissingKeys() {
Set<String> messageKeys = getAllMessageKeys(); Set<String> messageKeys = getAllMessageKeys();
List<String> fileLines = FileUtils.readLinesFromFile(messagesFile); List<String> fileLines = FileUtils.readLinesFromFile(messagesFile);
for (String line : fileLines) { for (String line : fileLines) {
// Skip comments and empty lines // Skip comments and empty lines
if (!line.startsWith("#") && !line.trim().isEmpty()) { if (!line.startsWith("#") && !line.trim().isEmpty()) {
handleMessagesLine(line, messageKeys); verifyKeyInFile(line, messageKeys);
} }
} }
if (messageKeys.isEmpty()) { // All keys that remain are keys that are absent in the file
System.out.println("Found all message keys"); for (String missingKey : messageKeys) {
} else { missingKeys.put(missingKey, false);
handleMissingKeys(messageKeys, addMissingKeys);
} }
} }
private void handleMessagesLine(String line, Set<String> messageKeys) { private void verifyKeyInFile(String line, Set<String> messageKeys) {
if (line.indexOf(':') == -1) { if (line.indexOf(':') == -1) {
System.out.println("Skipping line in unknown format: '" + line + "'"); System.out.println("Skipping line in unknown format: '" + line + "'");
return; return;
@ -52,22 +66,40 @@ public class MessageFileVerifier {
if (messageKeys.contains(key)) { if (messageKeys.contains(key)) {
messageKeys.remove(key); messageKeys.remove(key);
} else { } else {
System.out.println("Warning: Unknown key '" + key + "' for line '" + line + "'"); unknownKeys.add(key);
} }
} }
private void handleMissingKeys(Set<String> missingKeys, boolean addMissingKeys) { public void addMissingKeys(Map<String, String> defaultMessages) {
for (String key : missingKeys) { List<String> keysToAdd = new ArrayList<>();
if (addMissingKeys) {
String defaultMessage = defaultMessages.get(key); for (Map.Entry<String, Boolean> entry : missingKeys.entrySet()) {
FileUtils.appendToFile(messagesFile, NEW_LINE + key + ":" + defaultMessage); if (Boolean.FALSE.equals(entry.getValue())) {
System.out.println("Added missing key '" + key + "' to file"); String defaultMessage = defaultMessages.get(entry.getKey());
if (defaultMessage == null) {
System.out.println("Error: Key '" + entry.getKey() + "' not present in default messages");
} else { } else {
System.out.println("Error: Missing key '" + key + "'"); keysToAdd.add(entry.getKey());
} }
} }
} }
// Very ugly way of verifying if the last char in the file is a new line, in which case we won't start by
// adding a new line to the file. It's grossly inefficient but with the scale of the messages file it's fine
String fileContents = FileUtils.readFromFile(messagesFile);
String contentsToAdd = "";
if (fileContents.charAt(fileContents.length() - 1) == NEW_LINE) {
contentsToAdd += NEW_LINE;
}
// We know that all keys in keysToAdd are safe to retrieve and add
for (String keyToAdd : keysToAdd) {
contentsToAdd += keyToAdd + ":" + defaultMessages.get(keyToAdd) + NEW_LINE;
missingKeys.put(keyToAdd, true);
}
FileUtils.appendToFile(messagesFile, contentsToAdd);
}
private static Set<String> getAllMessageKeys() { private static Set<String> getAllMessageKeys() {
Set<String> messageKeys = new HashSet<>(MessageKey.values().length); Set<String> messageKeys = new HashSet<>(MessageKey.values().length);
for (MessageKey key : MessageKey.values()) { for (MessageKey key : MessageKey.values()) {

View File

@ -1,40 +1,110 @@
package messages; package messages;
import fr.xephi.authme.util.StringUtils;
import utils.FileUtils; import utils.FileUtils;
import utils.ToolsConstants;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static java.lang.String.format;
/** /**
* TODO: ljacqu write JavaDoc * TODO: ljacqu write JavaDoc
*/ */
public final class MessagesVerifierRunner { public final class MessagesVerifierRunner {
public static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
private static final String SOURCES_TAG = "{msgdir}";
private MessagesVerifierRunner() { private MessagesVerifierRunner() {
} }
public static void main(String[] args) { public static void main(String[] args) {
final Map<String, String> defaultMessages = constructDefaultMessages(); Scanner scanner = new Scanner(System.in);
final List<File> messagesFiles = getMessagesFiles(); System.out.println("Check a specific file only?");
System.out.println("- Empty line will check all files in the resources messages folder (default)");
System.out.println(format("- %s will be replaced to the messages folder %s", SOURCES_TAG, MESSAGES_FOLDER));
String inputFile = scanner.nextLine();
if (messagesFiles.isEmpty()) { System.out.println("Add any missing keys to files? ['y' = yes]");
throw new RuntimeException("Error getting messages file: list of files is empty"); boolean addMissingKeys = "y".equals(scanner.nextLine());
scanner.close();
Map<String, String> defaultMessages = null;
if (addMissingKeys) {
defaultMessages = constructDefaultMessages();
} }
for (File file : messagesFiles) { List<File> messageFiles;
if (StringUtils.isEmpty(inputFile)) {
messageFiles = getMessagesFiles();
} 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");
}
}
for (File file : messageFiles) {
System.out.println("Verifying '" + file.getName() + "'"); System.out.println("Verifying '" + file.getName() + "'");
MessageFileVerifier messageFileVerifier = new MessageFileVerifier(defaultMessages, file.getAbsolutePath()); MessageFileVerifier verifier = new MessageFileVerifier(file.getAbsolutePath());
messageFileVerifier.verify(false); if (addMissingKeys) {
System.out.println(); verifyFileAndAddKeys(verifier, defaultMessages);
} else {
verifyFile(verifier);
}
}
}
private static void verifyFile(MessageFileVerifier verifier) {
Map<String, Boolean> missingKeys = verifier.getMissingKeys();
if (missingKeys.isEmpty()) {
System.out.println(" No missing keys");
} else {
System.out.println(" Missing keys: " + missingKeys.keySet());
}
Set<String> unknownKeys = verifier.getUnknownKeys();
if (!unknownKeys.isEmpty()) {
System.out.println(" Unknown keys: " + unknownKeys);
}
}
private static void verifyFileAndAddKeys(MessageFileVerifier verifier, Map<String, String> defaultMessages) {
Map<String, Boolean> missingKeys = verifier.getMissingKeys();
if (missingKeys.isEmpty()) {
System.out.println(" No missing keys");
} else {
verifier.addMissingKeys(defaultMessages);
missingKeys = verifier.getMissingKeys();
List<String> addedKeys = getKeysWithValue(Boolean.TRUE, missingKeys);
System.out.println("Could add missing keys " + addedKeys);
List<String> unsuccessfulKeys = getKeysWithValue(Boolean.FALSE, missingKeys);
if (!unsuccessfulKeys.isEmpty()) {
System.out.println(" Warning! Could not add all missing keys (problem with loading " +
"default messages?)");
System.out.println(" Could not add keys " + unsuccessfulKeys);
}
}
Set<String> unknownKeys = verifier.getUnknownKeys();
if (!unknownKeys.isEmpty()) {
System.out.println(" Unknown keys: " + unknownKeys);
} }
} }
private static Map<String, String> constructDefaultMessages() { private static Map<String, String> constructDefaultMessages() {
String defaultMessagesFile = MessageFileVerifier.MESSAGES_FOLDER + "messages_en.yml"; String defaultMessagesFile = MESSAGES_FOLDER + "messages_en.yml";
List<String> lines = FileUtils.readLinesFromFile(defaultMessagesFile); List<String> lines = FileUtils.readLinesFromFile(defaultMessagesFile);
Map<String, String> messages = new HashMap<>(lines.size()); Map<String, String> messages = new HashMap<>(lines.size());
for (String line : lines) { for (String line : lines) {
@ -51,9 +121,19 @@ public final class MessagesVerifierRunner {
return messages; return messages;
} }
private static <K, V> List<K> getKeysWithValue(V value, Map<K, V> map) {
List<K> result = new ArrayList<>();
for (Map.Entry<K, V> entry : map.entrySet()) {
if (value.equals(entry.getValue())) {
result.add(entry.getKey());
}
}
return result;
}
private static List<File> getMessagesFiles() { private static List<File> getMessagesFiles() {
final Pattern messageFilePattern = Pattern.compile("messages_[a-z]{2,3}\\.yml"); final Pattern messageFilePattern = Pattern.compile("messages_[a-z]{2,3}\\.yml");
File folder = new File(MessageFileVerifier.MESSAGES_FOLDER); File folder = new File(MESSAGES_FOLDER);
File[] files = folder.listFiles(); File[] files = folder.listFiles();
if (files == null) { if (files == null) {
throw new RuntimeException("Could not read files from folder '" + folder.getName() + "'"); throw new RuntimeException("Could not read files from folder '" + folder.getName() + "'");