mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Added XML Generation
- Implemented a wrapper for Apache Freemake - Implemented the regatta XML generator Tags: #story[984]
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user