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
|
// 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<>();
|
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) {
|
public MessageFileVerifier(String messagesFile) {
|
||||||
this.messagesFile = 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() {
|
public Set<String> getUnknownKeys() {
|
||||||
return unknownKeys;
|
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() {
|
public Map<String, Boolean> getMissingKeys() {
|
||||||
return missingKeys;
|
return missingKeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void analyze() {
|
private void verifyKeys() {
|
||||||
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()) {
|
||||||
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) {
|
if (line.indexOf(':') == -1) {
|
||||||
System.out.println("Skipping line in unknown format: '" + line + "'");
|
System.out.println("Skipping line in unknown format: '" + line + "'");
|
||||||
return;
|
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) {
|
public void addMissingKeys(Map<String, String> defaultMessages) {
|
||||||
List<String> keysToAdd = new ArrayList<>();
|
List<String> keysToAdd = new ArrayList<>();
|
||||||
|
|
||||||
for (Map.Entry<String, Boolean> entry : missingKeys.entrySet()) {
|
for (Map.Entry<String, Boolean> entry : missingKeys.entrySet()) {
|
||||||
if (Boolean.FALSE.equals(entry.getValue())) {
|
if (Boolean.FALSE.equals(entry.getValue()) && defaultMessages.get(entry.getKey()) != null) {
|
||||||
String defaultMessage = defaultMessages.get(entry.getKey());
|
keysToAdd.add(entry.getKey());
|
||||||
if (defaultMessage == null) {
|
|
||||||
System.out.println("Error: Key '" + entry.getKey() + "' not present in default messages");
|
|
||||||
} else {
|
|
||||||
keysToAdd.add(entry.getKey());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -17,7 +17,7 @@ import java.util.regex.Pattern;
|
|||||||
import static java.lang.String.format;
|
import static java.lang.String.format;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ljacqu write JavaDoc
|
* Entry point of the messages verifier.
|
||||||
*/
|
*/
|
||||||
public final class MessagesVerifierRunner {
|
public final class MessagesVerifierRunner {
|
||||||
|
|
||||||
@ -28,6 +28,7 @@ public final class MessagesVerifierRunner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
// Prompt user for options
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
System.out.println("Check a specific file only?");
|
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("- 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());
|
boolean addMissingKeys = "y".equals(scanner.nextLine());
|
||||||
scanner.close();
|
scanner.close();
|
||||||
|
|
||||||
|
// Set up needed objects
|
||||||
Map<String, String> defaultMessages = null;
|
Map<String, String> defaultMessages = null;
|
||||||
if (addMissingKeys) {
|
if (addMissingKeys) {
|
||||||
defaultMessages = constructDefaultMessages();
|
defaultMessages = constructDefaultMessages();
|
||||||
@ -49,11 +51,9 @@ public final class MessagesVerifierRunner {
|
|||||||
} else {
|
} else {
|
||||||
File customFile = new File(inputFile.replace(SOURCES_TAG, MESSAGES_FOLDER));
|
File customFile = new File(inputFile.replace(SOURCES_TAG, MESSAGES_FOLDER));
|
||||||
messageFiles = Collections.singletonList(customFile);
|
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) {
|
for (File file : messageFiles) {
|
||||||
System.out.println("Verifying '" + file.getName() + "'");
|
System.out.println("Verifying '" + file.getName() + "'");
|
||||||
MessageFileVerifier verifier = new MessageFileVerifier(file.getAbsolutePath());
|
MessageFileVerifier verifier = new MessageFileVerifier(file.getAbsolutePath());
|
||||||
@ -111,11 +111,11 @@ public final class MessagesVerifierRunner {
|
|||||||
if (line.startsWith("#") || line.trim().isEmpty()) {
|
if (line.startsWith("#") || line.trim().isEmpty()) {
|
||||||
continue;
|
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 + "'");
|
System.out.println("Warning! Unknown format in default messages file for line '" + line + "'");
|
||||||
} else {
|
} else {
|
||||||
String key = line.substring(0, line.indexOf(':'));
|
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;
|
return messages;
|
||||||
@ -145,6 +145,9 @@ public final class MessagesVerifierRunner {
|
|||||||
messageFiles.add(file);
|
messageFiles.add(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (messageFiles.isEmpty()) {
|
||||||
|
throw new RuntimeException("Error getting message files: list of files is empty");
|
||||||
|
}
|
||||||
return messageFiles;
|
return messageFiles;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user