begining save data methods
Signed-off-by: Anthony Debucquoy <debucquoy.anthony@gmail.com>
This commit is contained in:
parent
43ef14abd8
commit
29c2fd7a0b
@ -10,11 +10,16 @@ import java.util.Arrays;
|
|||||||
|
|
||||||
public class BinaryParser implements FileParser {
|
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
|
* @return true if it is a binary File
|
||||||
*/
|
*/
|
||||||
static boolean isBinaryFile(File file) throws IOException {
|
public static boolean isBinaryFile(File file) throws IOException {
|
||||||
// TODO: 3/20/23 Need more testing
|
// TODO: 3/20/23 Need more testing
|
||||||
if (Files.probeContentType(file.toPath()) != null)
|
if (Files.probeContentType(file.toPath()) != null)
|
||||||
return false ;
|
return false ;
|
||||||
|
@ -18,6 +18,13 @@ public interface FileParser {
|
|||||||
*/
|
*/
|
||||||
Map getLevel(File file) throws IOException;
|
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
|
// TODO: 3/18/23 tonitch: Add getSavedData, for when piece could be added to map file
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,8 @@ public class FileParserFactory {
|
|||||||
public static FileParser createParser(File file) throws IOException {
|
public static FileParser createParser(File file) throws IOException {
|
||||||
if(BinaryParser.isBinaryFile(file))
|
if(BinaryParser.isBinaryFile(file))
|
||||||
return new BinaryParser();
|
return new BinaryParser();
|
||||||
else if (TxtParser.isTextFile(file))
|
else if (JsonParser.isJsonFile(file))
|
||||||
return new TxtParser();
|
return new JsonParser();
|
||||||
else
|
else
|
||||||
throw new NotSerializableException("This file format is not supported");
|
throw new NotSerializableException("This file format is not supported");
|
||||||
}
|
}
|
||||||
|
40
app/src/main/java/school_project/Parsers/JsonParser.java
Normal file
40
app/src/main/java/school_project/Parsers/JsonParser.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user