Commit 1cdb3ef7 authored by Stefan Michel's avatar Stefan Michel

XML Parsing implementiert: speichern möglich, laden noch nicht

parent 3040f791
package application;
public class StringResource {
public static String getDTD() {
return "<!DOCTYPE terrain [\n" +
"\n" +
"<!ELEMENT terrain (name, hamstername, maxcols, maxrows, hamsterrow, hamstercol, hamsterdirection, hamstercorns, map)>\n" +
"<!ELEMENT name (#PCDATA)>\n" +
"<!ELEMENT hamstername (#PCDATA)>\n" +
"<!ELEMENT maxcols (#PCDATA)>\n" +
"<!ELEMENT maxrows (#PCDATA)>\n" +
"<!ELEMENT hamsterrow (#PCDATA)>\n" +
"<!ELEMENT hamstercol (#PCDATA)>\n" +
"<!ELEMENT hamsterdirection (#PCDATA)>\n" +
"<!ELEMENT hamstercorns (#PCDATA)>\n" +
"<!ELEMENT value (#PCDATA)>\n" +
"<!ELEMENT column (value+)>\n" +
"<!ELEMENT map (column+)>\n" +
"\n" +
"\n" +
"<!ATTLIST column index CDATA #REQUIRED>\n" +
"<!ATTLIST value index CDATA #REQUIRED>\n" +
"\n" +
"]>";
}
}
package controller;
import application.Main;
import application.StringResource;
import model.Terrain;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.io.*;
/**
......@@ -14,6 +18,7 @@ public class TerrainController {
private Terrain terrain;
public TerrainController(Terrain terrain) {
this.terrain = terrain;
}
......@@ -58,6 +63,78 @@ public class TerrainController {
}
}
public void parseXML() {
String terrainName = new SetTerrainNameDialogBox().setTerrainNameDialogBox();
try (FileOutputStream fileOutputStream = new FileOutputStream("terrains/" + terrainName + ".xml")) {
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(fileOutputStream);
writer.writeStartDocument("UTF-8", "1.0");
writer.writeDTD(StringResource.getDTD());
writer.writeStartElement("terrain");
writer.writeStartElement("name");
writer.writeCharacters(terrainName);
writer.writeEndElement();
writer.writeStartElement("hamstername");
writer.writeCharacters(terrain.getHamsterName());
writer.writeEndElement();
writer.writeStartElement("maxcols");
writer.writeCharacters(String.valueOf(terrain.getMaxCols()));
writer.writeEndElement();
writer.writeStartElement("maxrows");
writer.writeCharacters(String.valueOf(terrain.getMaxRows()));
writer.writeEndElement();
writer.writeStartElement("hamsterrow");
writer.writeCharacters(String.valueOf(terrain.getHamsterRow()));
writer.writeEndElement();
writer.writeStartElement("hamstercol");
writer.writeCharacters(String.valueOf(terrain.getHamsterCol()));
writer.writeEndElement();
writer.writeStartElement("hamsterdirection");
writer.writeCharacters(String.valueOf(terrain.getHamsterDirection()));
writer.writeEndElement();
writer.writeStartElement("hamstercorns");
writer.writeCharacters(String.valueOf(terrain.getHamsterCorns()));
writer.writeEndElement();
writer.writeStartElement("map");
for (int i = 0; i < terrain.getMaxCols(); i++) {
writer.writeStartElement("column");
writer.writeAttribute("index", String.valueOf(i));
for (int j = 0; j < terrain.getMaxRows(); j++) {
writer.writeStartElement("value");
writer.writeAttribute("index", String.valueOf(j));
writer.writeCharacters(String.valueOf(terrain.getCornAmount(i, j)));
writer.writeEndElement();
}
writer.writeEndElement();
}
writer.writeEndElement();
writer.writeEndElement();
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean deserializeTerrain(String name) {
if (name == null)
return false;
......
......@@ -231,6 +231,7 @@ public class MainViewCreationFX extends VBox implements Observer {
java.setOnAction(event -> ioController.saveFile("name"));
seri.setOnAction(event -> terrainController.serializeTerrain());
xml.setOnAction(event -> terrainController.parseXML());
Menu laden = new Menu("_Laden");
MenuItem xmlladen = new MenuItem("_XML");
......
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