From: Marco Zanon Date: Mon, 21 Mar 2022 10:14:08 +0000 (+0000) Subject: Refactored code to make use of try-with-resources. X-Git-Tag: 7.1.0~4 X-Git-Url: https://gitweb.marcozanon.com/?a=commitdiff_plain;h=9e0fc0ec8440f355fe9dfa2043f4f2c870706049;p=Macaco Refactored code to make use of try-with-resources. --- diff --git a/src/java/com/marcozanon/macaco/text/MTranslator.java b/src/java/com/marcozanon/macaco/text/MTranslator.java index eb074b7..4760d27 100644 --- a/src/java/com/marcozanon/macaco/text/MTranslator.java +++ b/src/java/com/marcozanon/macaco/text/MTranslator.java @@ -69,62 +69,57 @@ public class MTranslator extends MObject { 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()); - } - 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 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); } }