JavaDocs improvement
This commit is contained in:
parent
4b71339758
commit
a70f658952
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
*
|
||||
* <p>
|
||||
* 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"
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* More file format can be added in the future by adding a new class that implement parser
|
||||
* and adding it to this file
|
||||
* </p>
|
||||
*
|
||||
* @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<FileParser, Boolean> 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<FileParser, Boolean> parser= getFileParser(file);
|
||||
parser.getKey().saveLevel(file, map, parser.getValue());
|
||||
}
|
||||
|
||||
private static Pair<FileParser, Boolean> 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, Boolean>(fileParser, save_data);
|
||||
}
|
||||
}
|
@ -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 {
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user