mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Added XML Generation
- Implemented a wrapper for Apache Freemake - Implemented the regatta XML generator Tags: #story[984]
This commit is contained in:
@@ -36,6 +36,13 @@
|
||||
<version>2.7.13</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
|
||||
<dependency>
|
||||
<groupId>org.freemarker</groupId>
|
||||
<artifactId>freemarker</artifactId>
|
||||
<version>2.3.26-incubating</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -40,7 +40,7 @@ public class GameServerThread implements Runnable, Observer, ClientConnectionDel
|
||||
runner.start();
|
||||
}
|
||||
|
||||
static void serverLog(String message, int logLevel){
|
||||
public static void serverLog(String message, int logLevel){
|
||||
if(logLevel <= LOG_LEVEL){
|
||||
System.out.println("[SERVER] " + message);
|
||||
}
|
||||
|
||||
@@ -59,7 +59,4 @@ public class GameState {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,9 +2,13 @@ package seng302.gameServerWithThreading;
|
||||
|
||||
import seng302.gameServer.GameStages;
|
||||
import seng302.gameServer.GameState;
|
||||
import seng302.models.mark.Mark;
|
||||
import seng302.models.mark.MarkType;
|
||||
import seng302.models.stream.PacketBufferDelegate;
|
||||
import seng302.models.stream.StreamParser;
|
||||
import seng302.models.stream.packets.StreamPacket;
|
||||
import seng302.models.xml.Regatta;
|
||||
import seng302.models.xml.XMLGenerator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
@@ -40,6 +44,17 @@ public class MainServerThread extends Thread implements PacketBufferDelegate{
|
||||
|
||||
|
||||
public void run() {
|
||||
// @TODO remove before commit
|
||||
XMLGenerator g = new XMLGenerator();
|
||||
|
||||
Regatta r = new Regatta("Test regatta", 1.123, 4.456);
|
||||
r.setMagneticVariation(12.1);
|
||||
r.setUtcOffset(12);
|
||||
|
||||
g.setRegatta(r);
|
||||
|
||||
System.out.println("g.getRegattaAsXml() = " + g.getRegattaAsXml());
|
||||
|
||||
//You should handle interrupts in some way, so that the thread won't keep on forever if you exit the app.
|
||||
while (!isInterrupted()) {
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package seng302.models.xml;
|
||||
|
||||
import seng302.models.mark.Mark;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Boats {
|
||||
private List<Mark> marks;
|
||||
|
||||
public Boats(){
|
||||
marks = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addMark(Mark m){
|
||||
marks.add(m);
|
||||
}
|
||||
|
||||
public List<Mark> getMarks(){
|
||||
return this.marks;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package seng302.models.xml;
|
||||
|
||||
public class Race {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package seng302.models.xml;
|
||||
|
||||
public class Regatta {
|
||||
private final Double DEFAULT_ALTITUDE = 0d;
|
||||
private final Integer DEFAULT_REGATTA_ID = 0;
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String courseName;
|
||||
|
||||
private Double latitude;
|
||||
private Double longitude;
|
||||
private Double altitude;
|
||||
|
||||
private Integer utcOffset;
|
||||
private Double magneticVariation;
|
||||
|
||||
public Regatta(String name, Double latitude, Double longitude) {
|
||||
this.name = name;
|
||||
this.id = DEFAULT_REGATTA_ID;
|
||||
this.courseName = name;
|
||||
|
||||
this.latitude = latitude;
|
||||
this.longitude = longitude;
|
||||
this.altitude = DEFAULT_ALTITUDE;
|
||||
|
||||
this.utcOffset = 0;
|
||||
this.magneticVariation = 0d;
|
||||
}
|
||||
|
||||
public void setMagneticVariation(Double magneticVariation){
|
||||
this.magneticVariation = magneticVariation;
|
||||
}
|
||||
|
||||
public void setUtcOffset(Integer offset){
|
||||
this.utcOffset = offset;
|
||||
}
|
||||
|
||||
/*
|
||||
NOTE!! The following getters must follow the JavaBean standard (getPropertyName()), and must be public.
|
||||
*/
|
||||
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getCourseName(){
|
||||
return courseName;
|
||||
}
|
||||
|
||||
public Integer getRegattaId(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public Double getLatitude() {
|
||||
return latitude;
|
||||
}
|
||||
|
||||
public Double getLongitude() {
|
||||
return longitude;
|
||||
}
|
||||
|
||||
public Double getAltitude() {
|
||||
return altitude;
|
||||
}
|
||||
|
||||
public Integer getUtcOffset(){
|
||||
return utcOffset;
|
||||
}
|
||||
|
||||
public Double getMagneticVariation(){
|
||||
return magneticVariation;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package seng302.models.xml;
|
||||
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import seng302.server.messages.XMLMessageSubType;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public class XMLGenerator {
|
||||
private static final String XML_TEMPLATE_DIR = "/server_config/xml_templates";
|
||||
private static final String REGATTA_TEMPLATE_NAME = "regatta.ftlh";
|
||||
private Configuration configuration;
|
||||
private Regatta regatta;
|
||||
|
||||
/**
|
||||
* Set up a configuration instance for Apache Freemake
|
||||
*/
|
||||
private void setupConfiguration() {
|
||||
configuration = new Configuration(Configuration.VERSION_2_3_26);
|
||||
|
||||
try {
|
||||
configuration.setDirectoryForTemplateLoading(new File(getClass().getResource(XML_TEMPLATE_DIR).toURI()));
|
||||
} catch (IOException e){
|
||||
System.out.println("[FATAL] Server could not read XML templates");
|
||||
} catch (URISyntaxException e) {
|
||||
System.out.println("[FATAL] Xml template directory URI is invalid");
|
||||
} catch (NullPointerException e){
|
||||
System.out.println("[FATAL] Server could not load XML Template directory, ensure this directory isn't empty");
|
||||
}
|
||||
}
|
||||
|
||||
public XMLGenerator(){
|
||||
setupConfiguration();
|
||||
}
|
||||
|
||||
public void setRegatta(Regatta regatta){
|
||||
this.regatta = regatta;
|
||||
}
|
||||
|
||||
private String parseToXmlString(String templateName, XMLMessageSubType type) throws IOException, TemplateException {
|
||||
Template template;
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
OutputStreamWriter writer = new OutputStreamWriter(os);
|
||||
|
||||
template = configuration.getTemplate(templateName);
|
||||
|
||||
switch (type) {
|
||||
case REGATTA:
|
||||
template.process(regatta, writer);
|
||||
break;
|
||||
|
||||
case BOAT:
|
||||
template.process(regatta, writer);
|
||||
break;
|
||||
|
||||
case RACE:
|
||||
template.process(regatta, writer);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
try {
|
||||
return os.toString("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
System.out.println("[FATAL] UTF-8 Not supported");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String getRegattaAsXml(){
|
||||
String result = null;
|
||||
|
||||
if (regatta == null) return null;
|
||||
|
||||
try {
|
||||
result = parseToXmlString(REGATTA_TEMPLATE_NAME, XMLMessageSubType.REGATTA);
|
||||
} catch (TemplateException e) {
|
||||
System.out.println("[FATAL] Error parsing regatta");
|
||||
} catch (IOException e) {
|
||||
System.out.println("[FATAL] Error reading regatta");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<BoatConfig>
|
||||
<Modified>2012-05-17T07:49:40+0200</Modified>
|
||||
<Version>12</Version>
|
||||
|
||||
<Settings>
|
||||
<RaceBoatType Type="AC45" />
|
||||
<BoatDimension BoatLength="14.019" HullLength="13.449" />
|
||||
<ZoneSize MarkZoneSize="40.347" CourseZoneSize="40.347" />
|
||||
<ZoneLimits Limit1="200" Limit2="100" Limit3="40.347" Limit4="0" Limit5="-100" />
|
||||
</Settings>
|
||||
|
||||
<BoatShapes>
|
||||
<#-- Not used -->
|
||||
</BoatShapes>
|
||||
|
||||
<Boats>
|
||||
<#list marks as mark>
|
||||
<Boat Type="Mark" SourceID="${mark.sourceId}" ShapeID="1" HullNum="${mark.hullNumber}" StoweName="${mark.stoweName}" ShortName="${mark.shortName}"
|
||||
BoatName="${mark.boatName}">
|
||||
<GPSposition Z="${mark.zpos}" Y="${mark.ypos}" X="${mark.xpos}" />
|
||||
<FlagPosition Z="${mark.zpos}" Y="${mark.ypos}" X="${mark.xpos}" />
|
||||
</Boat>
|
||||
</#list>
|
||||
|
||||
<#list boats as boat>
|
||||
<Boat Type="Yacht" SourceID="${boat.sourceId}" ShapeID="4" HullNum="${boat.hullNumber}" StoweName="${boat.stoweName}" ShortName="${boat.shortName}"
|
||||
BoatName="${boat.boatName}" Country="${boat.country}">
|
||||
|
||||
<GPSposition Z="${boat.zpos}" Y="${boat.ypos}" X="${boat.xpos}" />
|
||||
<MastTop Z="${boat.zpos}" Y="${boat.ypos}" X="${boat.xpos}"/>
|
||||
</Boat>
|
||||
</#list>
|
||||
</Boats>
|
||||
</BoatConfig>
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RegattaConfig>
|
||||
<RegattaID>${regattaId}</RegattaID>
|
||||
<RegattaName>${name}</RegattaName>
|
||||
<CourseName>${courseName}</CourseName>
|
||||
<CentralLatitude>${latitude}</CentralLatitude>
|
||||
<CentralLongitude>${longitude}</CentralLongitude>
|
||||
<CentralAltitude>${altitude}</CentralAltitude>
|
||||
<UtcOffset>${utcOffset}</UtcOffset>
|
||||
<MagneticVariation>${magneticVariation}</MagneticVariation>
|
||||
</RegattaConfig>
|
||||
Reference in New Issue
Block a user