diff --git a/app/src/main/java/school_project/Parsers/BinaryParser.java b/app/src/main/java/school_project/Parsers/BinaryParser.java index 6cab224..fee1c7d 100644 --- a/app/src/main/java/school_project/Parsers/BinaryParser.java +++ b/app/src/main/java/school_project/Parsers/BinaryParser.java @@ -10,14 +10,19 @@ import java.util.Arrays; public class BinaryParser implements FileParser { + @Override + public void saveLevelData(File file, Map levelData) throws IOException { + // TODO: 3/20/23 + } + /** - * Check if the file is a binary file suited for parsing + * check if the file is a binary file suited for parsing * @return true if it is a binary File */ - static boolean isBinaryFile(File file) throws IOException { - // TODO: 3/20/23 Need more testing + public static boolean isBinaryFile(File file) throws IOException { + // TODO: 3/20/23 Need more testing if (Files.probeContentType(file.toPath()) != null) - return false; + return false ; FileInputStream r_file = new FileInputStream(file); byte[] content = r_file.readAllBytes(); diff --git a/app/src/main/java/school_project/Parsers/FileParser.java b/app/src/main/java/school_project/Parsers/FileParser.java index c0459f1..7df3566 100644 --- a/app/src/main/java/school_project/Parsers/FileParser.java +++ b/app/src/main/java/school_project/Parsers/FileParser.java @@ -18,6 +18,13 @@ public interface FileParser { */ Map getLevel(File file) throws IOException; + /** + * Save Map to a file with all it's data + * @param file the file where to save + * @param levelData the map to save + */ + void saveLevelData(File file, Map levelData) throws IOException; + // TODO: 3/18/23 tonitch: Add getSavedData, for when piece could be added to map file } diff --git a/app/src/main/java/school_project/Parsers/FileParserFactory.java b/app/src/main/java/school_project/Parsers/FileParserFactory.java index 149ea97..a5501b1 100644 --- a/app/src/main/java/school_project/Parsers/FileParserFactory.java +++ b/app/src/main/java/school_project/Parsers/FileParserFactory.java @@ -18,8 +18,8 @@ public class FileParserFactory { public static FileParser createParser(File file) throws IOException { if(BinaryParser.isBinaryFile(file)) return new BinaryParser(); - else if (TxtParser.isTextFile(file)) - return new TxtParser(); + else if (JsonParser.isJsonFile(file)) + return new JsonParser(); else throw new NotSerializableException("This file format is not supported"); } diff --git a/app/src/main/java/school_project/Parsers/JsonParser.java b/app/src/main/java/school_project/Parsers/JsonParser.java new file mode 100644 index 0000000..4331b0c --- /dev/null +++ b/app/src/main/java/school_project/Parsers/JsonParser.java @@ -0,0 +1,40 @@ +package school_project.Parsers; + +import school_project.Map; + +import java.io.*; +import java.nio.file.Files; + +public class JsonParser implements FileParser{ + + @Override + public void saveLevelData(File file, Map levelData) throws IOException { + FileOutputStream fileStream = new FileOutputStream(file); + ObjectOutputStream objectStream = new ObjectOutputStream(fileStream); + objectStream.writeObject(levelData); + + objectStream.close(); + fileStream.close(); + } + + /** + * Check if the file is a text file + * @return true if it is a text File + */ + public static boolean isJsonFile(File file) throws IOException { + System.out.println(Files.probeContentType(file.toPath())); + return Files.probeContentType(file.toPath()).equals("application/json"); + } + + @Override + public Map getLevel(File file) throws IOException { + FileInputStream fileStream = new FileInputStream(file); + ObjectInputStream objectStream = new ObjectInputStream(fileStream); + try { + return (Map) objectStream.readObject(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/app/src/main/java/school_project/Parsers/TxtParser.java b/app/src/main/java/school_project/Parsers/TxtParser.java deleted file mode 100644 index 65683bc..0000000 --- a/app/src/main/java/school_project/Parsers/TxtParser.java +++ /dev/null @@ -1,24 +0,0 @@ -package school_project.Parsers; - -import school_project.Map; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; - -public class TxtParser implements FileParser{ - /** - * Check if the file is a text file - * @return true if it is a text File - */ - static boolean isTextFile(File file) throws IOException { - return Files.probeContentType(file.toPath()).equals("text/plain"); - // TODO: 3/18/23 - } - - @Override - public Map getLevel(File file) { - return null; - // TODO: 3/18/23 - } -}