mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2025-01-19 14:11:32 +01:00
Refine messages file verifier
- Fix minor open issues - Add documentation - Rename to [hopefully] more suitable names
This commit is contained in:
parent
186ef965ca
commit
c50c9efe83
@ -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<String, Boolean> 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<String> 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<String, Boolean> getMissingKeys() {
|
||||
return missingKeys;
|
||||
}
|
||||
|
||||
private void analyze() {
|
||||
findMissingKeys();
|
||||
}
|
||||
|
||||
private void findMissingKeys() {
|
||||
private void verifyKeys() {
|
||||
Set<String> messageKeys = getAllMessageKeys();
|
||||
List<String> 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<String> messageKeys) {
|
||||
private void processKeyInFile(String line, Set<String> 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<String, String> defaultMessages) {
|
||||
List<String> keysToAdd = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<String, Boolean> 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());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<String, String> 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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user