From 26fe25586aba7cbe57e0cdbf19108ee6335d4d93 Mon Sep 17 00:00:00 2001
From: Jan Gruteser <jan.gruteser@hhu.de>
Date: Wed, 30 Oct 2024 08:44:23 +0100
Subject: [PATCH] simplify file creation in Translator

---
 src/main/java/de/tla2bAst/Translator.java | 71 ++++++-----------------
 1 file changed, 18 insertions(+), 53 deletions(-)

diff --git a/src/main/java/de/tla2bAst/Translator.java b/src/main/java/de/tla2bAst/Translator.java
index 7fe4d4f..c3ce3fb 100644
--- a/src/main/java/de/tla2bAst/Translator.java
+++ b/src/main/java/de/tla2bAst/Translator.java
@@ -39,6 +39,8 @@ import java.util.List;
 import java.util.stream.Collectors;
 
 public class Translator implements TranslationGlobals {
+	private static final String GENERATED_BY_TLA2B_HEADER = "/*@ generated by TLA2B ";
+
 	private String parentPath;
 	private File moduleFile;
 	private File configFile;
@@ -106,14 +108,11 @@ public class Translator implements TranslationGlobals {
 		dir.mkdirs();
 		dir.deleteOnExit();
 
-		try {
-			moduleFile = new File("temp/" + moduleName + ".tla");
-			moduleFile.createNewFile();
-			try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(moduleFile.toPath()), StandardCharsets.UTF_8))) {
-				out.write(moduleString);
-			}
+		moduleFile = new File("temp/" + moduleName + ".tla");
+		try (BufferedWriter out = Files.newBufferedWriter(moduleFile.toPath(), StandardCharsets.UTF_8)) {
+			out.write(moduleString);
 		} catch (IOException e) {
-			e.printStackTrace();
+			System.err.println("Error while writing file '" + moduleFile.getAbsolutePath() + "':\n" + e.getMessage());
 		}
 	}
 
@@ -121,14 +120,10 @@ public class Translator implements TranslationGlobals {
 		modelConfig = null;
 		if (configString != null) {
 			configFile = new File("temp/" + moduleName + ".cfg");
-			try {
-				configFile.createNewFile();
-				try (BufferedWriter out = new BufferedWriter(
-						new OutputStreamWriter(Files.newOutputStream(configFile.toPath()), StandardCharsets.UTF_8))) {
-					out.write(configString);
-				}
+			try (BufferedWriter out = Files.newBufferedWriter(configFile.toPath(), StandardCharsets.UTF_8)) {
+				out.write(configString);
 			} catch (IOException e) {
-				e.printStackTrace();
+				System.err.println("Error while writing file '" + configFile.getAbsolutePath() + "':\n" + e.getMessage());
 			}
 		}
 	}
@@ -210,44 +205,26 @@ public class Translator implements TranslationGlobals {
 	}
 
 	public void createProbFile() {
-		String fileName = FileUtils.removeExtension(moduleFile.getAbsolutePath());
-		fileName = fileName + ".prob";
-		File probFile;
-		probFile = new File(fileName);
-		try {
-			probFile.createNewFile();
-		} catch (IOException e) {
-			System.err.printf("Could not create File %s.%n", probFile.getName());
-			System.exit(-1);
-		}
+		File probFile = new File(FileUtils.removeExtension(moduleFile.getAbsolutePath()) + ".prob");
 
 		BParser bParser = new BParser();
-
-		try {
+		try (BufferedWriter outWriter = Files.newBufferedWriter(probFile.toPath(), StandardCharsets.UTF_8)) {
 			bParser.getDefinitions().addDefinitions(getBDefinitions());
 			final RecursiveMachineLoader rml = parseAllMachines(getBAST(), getModuleFile(), bParser);
-
-			PrintWriter outWriter = new PrintWriter(probFile, "UTF-8");
 			rml.printAsProlog(new PrologTermOutput(outWriter, false));
-			outWriter.close();
 			System.out.println(probFile.getAbsolutePath() + " created.");
-
-		} catch (BCompoundException | FileNotFoundException | PreParseException | UnsupportedEncodingException e) {
+		} catch (BCompoundException | IOException | PreParseException e) {
 			System.err.println(e.getMessage());
 			System.exit(-1);
 		}
 	}
 
 	public void createMachineFile() {
-		String bFile = FileUtils.removeExtension(moduleFile.getAbsolutePath()) + "_tla.txt";
-
-		File machineFile = new File(bFile);
+		File machineFile = new File(FileUtils.removeExtension(moduleFile.getAbsolutePath()) + "_tla.txt");
 		if (machineFile.exists()) {
-			try {
-				BufferedReader in = new BufferedReader(new FileReader(machineFile));
+			try (BufferedReader in = new BufferedReader(new FileReader(machineFile))) {
 				String firstLine = in.readLine();
-				in.close();
-				if (firstLine != null && !firstLine.startsWith("/*@ generated by TLA2B ")) {
+				if (firstLine != null && !firstLine.startsWith(GENERATED_BY_TLA2B_HEADER)) {
 					System.err.println("Error: File " + machineFile.getName() + " already exists"
 						+ " and was not generated by TLA2B.\n" + "Delete or move this file.");
 					System.exit(-1);
@@ -258,28 +235,16 @@ public class Translator implements TranslationGlobals {
 			}
 		}
 
-		try {
-			machineFile.createNewFile();
-		} catch (IOException e) {
-			System.err.printf("Could not create File %s.%n", machineFile.getName());
-			System.exit(-1);
-		}
-
 		PrettyPrinter pp = new PrettyPrinter();
 		pp.setRenaming(new SuffixIdentifierRenaming());
 		getBAST().apply(pp);
-		String result = "/*@ generated by TLA2B " + VERSION_NUMBER + " */\n" + pp.getPrettyPrint();
-
-		try {
-			try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(machineFile.toPath()), StandardCharsets.UTF_8))) {
-				out.write(result);
-			}
+		try (BufferedWriter out = Files.newBufferedWriter(machineFile.toPath(), StandardCharsets.UTF_8)) {
+			out.write(GENERATED_BY_TLA2B_HEADER + VERSION_NUMBER + " */\n" + pp.getPrettyPrint());
 			System.out.println("B-Machine " + machineFile.getAbsolutePath() + " created.");
 		} catch (IOException e) {
-			System.err.println("Error while creating file '" + machineFile.getAbsolutePath() + "'.");
+			System.err.println("Error while creating file '" + machineFile.getAbsolutePath() + "':\n" + e.getMessage());
 			System.exit(-1);
 		}
-
 	}
 
 	public RecursiveMachineLoader parseAllMachines(final Start ast, final File f, final BParser bparser) throws BCompoundException {
-- 
GitLab