Refactored code to make use of try-with-resources.
authorMarco Zanon <info@marcozanon.com>
Mon, 21 Mar 2022 10:14:08 +0000 (10:14 +0000)
committerMarco Zanon <info@marcozanon.com>
Mon, 21 Mar 2022 10:14:08 +0000 (10:14 +0000)
7.x/src/java/com/marcozanon/macaco/text/MTranslator.java

index eb074b7fde31e30f961b4b4f17514685d3909e86..4760d27637dae7b1242e0806987c3a455e688482 100644 (file)
@@ -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<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);
         }
     }