Commit ca1e1cf0 authored by Stefan Michel's avatar Stefan Michel

laden durch XML-Parsing jetzt möglich

parent a341396a
package controller;
import application.Main;
import application.StringResource;
import model.Terrain;
import org.w3c.dom.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
......@@ -37,15 +39,21 @@ public class TerrainController {
}
public void addWall(int col, int row) {
terrain.addWall(col, row);
synchronized (this) {
terrain.addWall(col, row);
}
}
public void moveHamster(int col, int row) {
terrain.setHamsterPostion(col, row);
synchronized (this) {
terrain.setHamsterPostion(col, row);
}
}
public void deleteTile(int col, int row) {
terrain.deleteTile(col, row);
synchronized (this) {
terrain.deleteTile(col, row);
}
}
public void serializeTerrain() {
......@@ -140,19 +148,6 @@ public class TerrainController {
}
void print(Element element, String indent) {
System.out.print(element.getNodeName());
NamedNodeMap attributes = element.getAttributes();
for (int i = 0; i < attributes.getLength(); ++i) {
Node attribute = attributes.item(i);
System.out.print(attribute.getNodeName());
}
}
void print(Document document) {
print(document.getDocumentElement(), "");
}
//abgewandelt aus https://stackoverflow.com/questions/7392646/parsing-xml-in-java-gettextcontent-and-getnodevalue-only-return-n-t-an
private String getValueByTag(String sTag, Element element) {
NodeList nlList = element.getElementsByTagName(sTag).item(0).getChildNodes();
......@@ -160,15 +155,21 @@ public class TerrainController {
return nValue.getNodeValue();
}
public void loadXML() {
/**
*
*
*/
// aus den folien UE20 und DOMDEMO2
public void loadXML(String path) {
String path = "terrains/niceguys.xml";
Terrain terrain = new Terrain();
Terrain rollback = new Terrain();
rollback.setUpTerrain(terrain);
// aus den folien UE20
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(false);
dbf.setIgnoringComments(true);
DocumentBuilder parser = dbf.newDocumentBuilder();
File input = new File(path);
......@@ -181,78 +182,77 @@ public class TerrainController {
terrain.setMaxCols(Integer.parseInt(getValueByTag("maxcols", docElement)));
terrain.setMaxRows(Integer.parseInt(getValueByTag("maxrows", docElement)));
terrain.setHamsterPostion(Integer.parseInt(getValueByTag("hamstercol", docElement)), Integer.parseInt(getValueByTag("hamsterrow", docElement)));
terrain.setHamsterDirection(Integer.parseInt(getValueByTag("hamstercorns", docElement)));
terrain.setHamsterDirection(Integer.parseInt(getValueByTag("hamsterdirection", docElement)));
int[][] map = new int[terrain.getMaxCols()][terrain.getMaxRows()];
NodeList columnList = docElement.getElementsByTagName("map").item(0).getChildNodes();
for (int i = 0; i < columnList.getLength(); i++) {
NodeList rows = columnList.item(i).getChildNodes();
for (int j = 0; j < rows.getLength(); j++) {
Node node = rows.item(j);
if (node.getNodeType() == Node.ELEMENT_NODE) {
int column = Integer.parseInt(columnList.item(i).getAttributes().getNamedItem("index").getNodeValue());
int row = Integer.parseInt(node.getAttributes().getNamedItem("index").getNodeValue());
System.out.print("Value [" + column + "] [" + row + "] : ");
map[column][row] = Integer.parseInt(node.getTextContent());
System.out.println(node.getTextContent());
}
}
System.out.println();
}
terrain.setMap(map);
} catch (ParserConfigurationException e) {
e.printStackTrace();
terrain = rollback;
} catch (SAXException e) {
e.printStackTrace();
terrain = rollback;
} catch (IOException e) {
e.printStackTrace();
terrain = rollback;
}
// try (FileInputStream fileInputStream = new FileInputStream("terrains/best.xml")) {
// Terrain terrain = new Terrain();
//
//
//
// TODO: kein plan??
// XMLInputFactory factory = XMLInputFactory.newInstance();
// XMLStreamReader parser = factory.createXMLStreamReader(fileInputStream);
// while (parser.hasNext()) {
// switch (parser.getEventType()) {
// case XMLStreamConstants.START_ELEMENT:
// System.out.println("START_ELEMENT: " + parser.getLocalName());
//
// for (int i = 0; i < parser.getAttributeCount(); i++)
// System.out.println(" Attribut: "
// + parser.getAttributeLocalName(i)
// + " Wert: " + parser.getAttributeValue(i));
// break;
// case XMLStreamConstants.CHARACTERS:
// System.out.println(" CHARACTERS: " + parser.getText());
// break;
// }
// parser.next();
// }
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (XMLStreamException e) {
// e.printStackTrace();
// }
}
public boolean deserializeTerrain(String name) {
if (name == null)
return false;
/**
* laed ein Terrain durch deserialisierung
*
* @param name
*/
public void deserializeTerrain(String name) {
Terrain terrain = null;
Terrain rollback = new Terrain();
rollback.setUpTerrain(terrain);
try (FileInputStream fileOutputStream = new FileInputStream(name)) {
ObjectInputStream objectInputStream = new ObjectInputStream(fileOutputStream);
terrain = (Terrain) objectInputStream.readObject();
Terrain tmpTerrain = (Terrain) objectInputStream.readObject();
terrain.setUpTerrain(tmpTerrain);
} catch (FileNotFoundException e) {
e.printStackTrace();
terrain = rollback;
} catch (IOException e) {
e.printStackTrace();
terrain = rollback;
} catch (ClassNotFoundException e) {
terrain = rollback;
e.printStackTrace();
}
if (terrain != null) {
Main.startNewStage(terrain.getHamsterName(), terrain);
return true;
}
terrain.setChanged();
terrain.notifyObservers();
return false;
}
}
......@@ -13,6 +13,8 @@ import java.util.Observable;
*/
public class Terrain extends Observable implements Serializable {
private static final long serialVersionUID = 20031996020202L;
public final static int NORTH = 0;
public final static int EAST = 1;
public final static int SOUTH = 2;
......@@ -286,4 +288,24 @@ public class Terrain extends Observable implements Serializable {
public void setName(String name) {
this.name = name;
}
private int[][] getMap() {
return map;
}
public void setMap(int[][] map) {
this.map = map;
setChanged();
notifyObservers();
}
public void setUpTerrain(Terrain newTerrain) {
this.setHamsterName(newTerrain.getHamsterName());
this.setName(newTerrain.getName());
this.setMaxCols(newTerrain.getMaxCols());
this.setMaxRows(newTerrain.getMaxRows());
this.setHamsterPostion(newTerrain.getHamsterCol(), newTerrain.getHamsterRow());
this.setHamsterDirection(newTerrain.getHamsterDirection());
this.setMap(newTerrain.getMap());
}
}
......@@ -239,8 +239,17 @@ public class MainViewCreationFX extends VBox implements Observer {
MenuItem seriladen = new MenuItem("_Deserialisieren");
laden.getItems().addAll(xmlladen, jaxbladen, seriladen);
seriladen.setOnAction(event -> openTerrainFileChooser());
xmlladen.setOnAction(event -> terrainController.loadXML());
seriladen.setOnAction(event -> {
File file = openTerrainFileChooser("Binary Files", "*.bin");
if (file != null)
terrainController.deserializeTerrain(file.toString());
});
xmlladen.setOnAction(event -> {
File file = openTerrainFileChooser("XML Files", "*.xml");
if (file != null)
terrainController.loadXML(file.toString());
});
Menu bildspeichern = new Menu("_Als Bild speichern");
MenuItem jpg = new MenuItem("_JPEG");
......@@ -334,20 +343,16 @@ public class MainViewCreationFX extends VBox implements Observer {
}
}
public void openTerrainFileChooser() {
public File openTerrainFileChooser(String extDescription, String... ext) {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new ExtensionFilter("Binary Files", "*.bin"));
fileChooser.getExtensionFilters().add(new ExtensionFilter(extDescription, ext));
fileChooser.setInitialDirectory(new File("terrains/"));
File file = fileChooser.showOpenDialog(this.getScene().getWindow());
if (file != null) {
// StringBuilder schneller, da nicht synchronized
StringBuilder cutName = new StringBuilder(file.getName());
cutName.delete(cutName.length() - 5, cutName.length());
if (terrainController.deserializeTerrain(file.toString())) {
Stage stage = (Stage) this.getScene().getWindow();
stage.close();
}
return file;
}
return null;
}
// ------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment