throw new IllegalArgumentException("Invalid 'file': null or empty.");
}
//
- LineNumberReader buffer = null;
- try {
- buffer = new LineNumberReader(new InputStreamReader(new FileInputStream(file), MConstants.TEXT_ENCODING));
- }
- catch (FileNotFoundException exception) {
- throw new MTranslationFileParsingException("Could not open file.", exception);
- }
- catch (UnsupportedEncodingException exception) { // cannot happen
- }
- String message = null;
- String line = null;
- synchronized (this.getMessages()) {
- while (true) {
- try {
- line = buffer.readLine();
- }
- catch (IOException exception) {
- throw new MTranslationFileParsingException("Could not read file.", exception);
- }
- if (null == line) {
- break;
- }
- line = line.trim();
- if (MText.isEmpty(line)) {
- message = null;
- continue;
- }
- else if ((line.startsWith("#")) || (line.startsWith(";"))) {
- continue;
- }
- else if (null == message) {
- message = line;
- if (this.getMessages().containsKey(message)) {
- throw new MTranslationFileParsingException(String.format("Invalid line: %s: duplicated message: %s.", buffer.getLineNumber(), message));
+ try (LineNumberReader buffer = new LineNumberReader(new InputStreamReader(new FileInputStream(file), MConstants.TEXT_ENCODING))) {
+ String message = null;
+ String line = null;
+ synchronized (this.getMessages()) {
+ while (true) {
+ try {
+ line = buffer.readLine();
}
- this.getMessages().put(message, new LinkedHashMap<String, String>());
- }
- else {
- int a = line.indexOf("=");
- if (-1 == a) {
- throw new MTranslationFileParsingException(String.format("Invalid line: %s: string malformed: %s.", buffer.getLineNumber(), line));
+ catch (IOException exception) {
+ throw new MTranslationFileParsingException("Could not read file.", exception);
+ }
+ if (null == line) {
+ break;
+ }
+ line = line.trim();
+ if (MText.isEmpty(line)) {
+ message = null;
+ continue;
+ }
+ else if ((line.startsWith("#")) || (line.startsWith(";"))) {
+ continue;
+ }
+ else if (null == message) {
+ message = line;
+ if (this.getMessages().containsKey(message)) {
+ throw new MTranslationFileParsingException(String.format("Invalid line: %s: duplicated message: %s.", buffer.getLineNumber(), message));
+ }
+ this.getMessages().put(message, new LinkedHashMap<String, String>());
}
- String localeRepresentation = line.substring(0, a).trim();
- String translation = line.substring(a + 1).trim();
- if (this.getMessages().get(message).containsKey(localeRepresentation)) {
- throw new MTranslationFileParsingException(String.format("Invalid line: %s: duplicated translation for locale: %s.", buffer.getLineNumber(), message));
+ else {
+ int a = line.indexOf("=");
+ if (-1 == a) {
+ throw new MTranslationFileParsingException(String.format("Invalid line: %s: string malformed: %s.", buffer.getLineNumber(), line));
+ }
+ String localeRepresentation = line.substring(0, a).trim();
+ String translation = line.substring(a + 1).trim();
+ if (this.getMessages().get(message).containsKey(localeRepresentation)) {
+ throw new MTranslationFileParsingException(String.format("Invalid line: %s: duplicated translation for locale: %s.", buffer.getLineNumber(), message));
+ }
+ this.getMessages().get(message).put(localeRepresentation, translation);
}
- this.getMessages().get(message).put(localeRepresentation, translation);
}
}
}
- try {
- buffer.close();
+ catch (FileNotFoundException exception) {
+ throw new MTranslationFileParsingException("Could not open file.", exception);
+ }
+ catch (UnsupportedEncodingException exception) { // cannot happen
}
catch (IOException exception) {
- throw new MTranslationFileParsingException("Could not close file.", exception);
+ throw new MTranslationFileParsingException("Could not open / close file.", exception);
}
}