From a70f6589529036672fed9ccbc745ae6ae1b82bf7 Mon Sep 17 00:00:00 2001 From: "Debucquoy Anthony (tonitch)" Date: Sat, 25 Mar 2023 16:59:25 +0100 Subject: [PATCH] JavaDocs improvement --- .../school_project/Parsers/BinaryParser.java | 26 ------- .../school_project/Parsers/FileParser.java | 5 ++ .../Parsers/FileParserFactory.java | 76 ++++++++++++++++--- .../{JsonParser.java => SerializeParser.java} | 2 +- .../Parsers/JsonParserTest.java | 34 --------- 5 files changed, 72 insertions(+), 71 deletions(-) rename app/src/main/java/school_project/Parsers/{JsonParser.java => SerializeParser.java} (95%) delete mode 100644 app/src/test/java/school_project/Parsers/JsonParserTest.java diff --git a/app/src/main/java/school_project/Parsers/BinaryParser.java b/app/src/main/java/school_project/Parsers/BinaryParser.java index 02fcb83..0e61870 100644 --- a/app/src/main/java/school_project/Parsers/BinaryParser.java +++ b/app/src/main/java/school_project/Parsers/BinaryParser.java @@ -11,32 +11,6 @@ import java.util.Arrays; public class BinaryParser implements FileParser { - /** - * check if the file is a binary file suited for parsing - * @return true if it is a binary File - */ - public static boolean isBinaryFile(File file) throws IOException { - if (Files.probeContentType(file.toPath()) != null) - return false ; - - FileInputStream r_file = new FileInputStream(file); - byte[] content = r_file.readAllBytes(); - - boolean header_found = false; - for (int i = 0; i < content.length; i++) { - if (!header_found) { - if (content[i] == 'S' && content[i + 1] == 'M' && content[i + 2] == 'S') { - header_found = true; - } - } else { - if (content[i] == 'S' && content[i + 1] == 'M' && content[i + 2] == 'E') { - return true; - } - } - } - return false; - } - @Override public Map getLevel(File file, boolean saved_data) throws IOException { Map ret; diff --git a/app/src/main/java/school_project/Parsers/FileParser.java b/app/src/main/java/school_project/Parsers/FileParser.java index e590194..c20d0bc 100644 --- a/app/src/main/java/school_project/Parsers/FileParser.java +++ b/app/src/main/java/school_project/Parsers/FileParser.java @@ -3,6 +3,7 @@ package school_project.Parsers; import school_project.Map; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; public interface FileParser { @@ -14,6 +15,8 @@ public interface FileParser { * @param saved_data does the saved data should be added to the map * @return Map Object parsed with file data * @see "TODO: Add Specification when done" + * @throws FileNotFoundException if the file was not found or was not accessible + * @throws IOException if an I/O occurs */ Map getLevel(File file, boolean saved_data) throws IOException; @@ -23,6 +26,8 @@ public interface FileParser { * @param file the file where to save * @param levelData the map to save * @param save_data should save the map data (need to be false only in development I think) + * @throws FileNotFoundException The file could not be created + * @throws IOException if an I/O occurs */ void saveLevel(File file, Map levelData, boolean save_data) throws IOException; } diff --git a/app/src/main/java/school_project/Parsers/FileParserFactory.java b/app/src/main/java/school_project/Parsers/FileParserFactory.java index a5501b1..a7f4f68 100644 --- a/app/src/main/java/school_project/Parsers/FileParserFactory.java +++ b/app/src/main/java/school_project/Parsers/FileParserFactory.java @@ -1,27 +1,83 @@ package school_project.Parsers; +import javafx.util.Pair; +import school_project.Map; + import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.NotSerializableException; /** * This is used to find the right parser to parser a save/level file. + * This should be the only right way to save/load a file! you can just use `Map loadMapFromFile(File)` to load a file + * and `void saveFileFromMap(File, Map)` to save a file * *

- * Mor file format can be added in the future by adding a new class that implement parser and making it recognisable by - * the CreateParser class + * there is currently 2 file format with 2 variation each (save file or level file) + * - BinaryParser + * - ".level" + * - ".slevel" + * - SerializeParser + * - ".serialized" + * - ".sserialized" + *

+ * + *

+ * More file format can be added in the future by adding a new class that implement parser + * and adding it to this file *

* * @author tonitch */ public class FileParserFactory { - public static FileParser createParser(File file) throws IOException { - if(BinaryParser.isBinaryFile(file)) - return new BinaryParser(); - else if (JsonParser.isJsonFile(file)) - return new JsonParser(); - else - throw new NotSerializableException("This file format is not supported"); + /** + * Load a file and return a map + * If this is a save map, return the map with its save data + * @param file file to get data from + * @return Map generated from the file + * @throws FileNotFoundException if the file was not found or was not accessible + * @throws IOException if an I/O occurs + */ + public static Map loadMapFromFile(File file) throws IOException { + Pair parser= getFileParser(file); + return parser.getKey().getLevel(file, parser.getValue()); } -} + /** + * Save a file in a specific format, this format is defined by the file extension + * This file extention could be: ".level", ".slevel", ".serialized", ".sserialized" + * for save file use the .s variations + * @param file file name to be saved to with the right extension + * @param map map file to save + * @throws NotSerializableException the file extension is not recognised + * @throws FileNotFoundException The file could not be created + * @throws IOException if an I/O occurs + */ + public static void saveFileFromMap(File file, Map map) throws IOException { + Pair parser= getFileParser(file); + parser.getKey().saveLevel(file, map, parser.getValue()); + } + + private static Pair getFileParser(File file) throws NotSerializableException { + FileParser fileParser; + boolean save_data; + + if (file.toString().toLowerCase().endsWith(".level")){ + fileParser = new BinaryParser(); + save_data = false; + }else if(file.toString().toLowerCase().endsWith(".slevel")){ + fileParser = new BinaryParser(); + save_data = true; + }else if(file.toString().toLowerCase().endsWith(".serialized")){ + fileParser = new SerializeParser(); + save_data = false; + }else if(file.toString().toLowerCase().endsWith(".sserialized")) { + fileParser = new SerializeParser(); + save_data = true; + }else { + throw new NotSerializableException("This file format is not supported"); + } + return new Pair(fileParser, save_data); + } +} \ No newline at end of file diff --git a/app/src/main/java/school_project/Parsers/JsonParser.java b/app/src/main/java/school_project/Parsers/SerializeParser.java similarity index 95% rename from app/src/main/java/school_project/Parsers/JsonParser.java rename to app/src/main/java/school_project/Parsers/SerializeParser.java index 12b350f..ed627ff 100644 --- a/app/src/main/java/school_project/Parsers/JsonParser.java +++ b/app/src/main/java/school_project/Parsers/SerializeParser.java @@ -5,7 +5,7 @@ import school_project.Map; import java.io.*; import java.nio.file.Files; -public class JsonParser implements FileParser{ +public class SerializeParser implements FileParser{ @Override public void saveLevel(File file, Map levelData, boolean save_data) throws IOException { diff --git a/app/src/test/java/school_project/Parsers/JsonParserTest.java b/app/src/test/java/school_project/Parsers/JsonParserTest.java deleted file mode 100644 index 8273a0d..0000000 --- a/app/src/test/java/school_project/Parsers/JsonParserTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package school_project.Parsers; - -import org.junit.jupiter.api.Test; -import school_project.Map; -import school_project.Piece; - -import java.io.File; -import java.io.IOException; - -import static org.junit.jupiter.api.Assertions.*; - -class JsonParserTest { - - @Test - void saveLevelData() throws IOException { - boolean[][] map_matrix = { - {true, true, true}, - {false, true, false}, - {true, true, true}, - }; - Map map = new Map(map_matrix); - boolean[][] piece1 = {{true, true, true}}; - boolean[][] piece2 = {{false, true, false}, {true, true, true}}; - map.addPiece(new Piece(piece1)); - map.addPiece(new Piece(piece2)); - JsonParser parser = new JsonParser(); - parser.saveLevel(new File("test.json"), map, false); - - FileParser p2 = FileParserFactory.createParser(new File("test.json")); - Map map2 = p2.getLevel(new File("test.json"), false); - assertArrayEquals(map2.getShape(), map.getShape()); - assertArrayEquals(map2.getUsedSpace(), map.getUsedSpace()); - } -} \ No newline at end of file