Created Schema for Race xml's and generated classes for reading xml with xjc, made the RaceXMLReader read Corner classes in as well as making a corner class #story[1092]

main
Fan-Wu Yang 8 years ago
parent a8701d8a1f
commit 4e8d4f2433

@ -3,6 +3,8 @@ package mock.app;
import mock.dataInput.PolarParser;
import mock.model.MockRace;
import mock.model.Polars;
import mock.xml.Race;
import mock.xml.RaceFactory;
import network.Messages.LatestMessages;
import shared.dataInput.*;
import shared.enums.XMLFileType;
@ -10,10 +12,15 @@ import shared.exceptions.InvalidBoatDataException;
import shared.exceptions.InvalidRaceDataException;
import shared.exceptions.InvalidRegattaDataException;
import shared.exceptions.XMLReaderException;
import shared.model.CompoundMark;
import shared.model.Constants;
import shared.model.Corner;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.TransformerException;
import java.io.IOException;
import java.io.*;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
@ -42,7 +49,7 @@ public class Event {
*/
private Event() {
try {
this.raceXML = getRaceXMLAtCurrentTime(XMLReader.readXMLFileToString("mock/mockXML/raceTest.xml", StandardCharsets.UTF_8));
this.raceXML = getRaceXMLAtCurrentTime(alterRaceToWind("mock/mockXML/raceSchemaTest.xml", 0));
this.boatXML = XMLReader.readXMLFileToString("mock/mockXML/boatsSinglePlayer.xml", StandardCharsets.UTF_8);
this.regattaXML = XMLReader.readXMLFileToString("mock/mockXML/regattaTest.xml", StandardCharsets.UTF_8);
this.xmlFileType = XMLFileType.Contents;
@ -58,6 +65,10 @@ public class Event {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
} catch (InvalidRaceDataException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
@ -130,4 +141,94 @@ public class Event {
return raceXML;
}
private Race copyRace(RaceXMLReader reader) throws InvalidRaceDataException, XMLReaderException {
mock.xml.RaceFactory raceFactory = new RaceFactory();
mock.xml.Race race = raceFactory.createRace();
race.setRaceID(String.valueOf(reader.getRaceId()));
race.setRaceType(String.valueOf(reader.getRaceType()));
race.setCreationTimeDate("CREATION_TIME");//this needs to be changed later
//Race Start Time
mock.xml.Race.RaceStartTime raceStartTime = raceFactory.createRaceRaceStartTime();
raceStartTime.setPostpone("false");
raceStartTime.setTime("START_TIME");//this needs to be changed later
race.setRaceStartTime(raceStartTime);
//Participants
Race.Participants participants = raceFactory.createRaceParticipants();
for (int i :reader.getParticipants()) {
Race.Participants.Yacht yacht = raceFactory.createRaceParticipantsYacht();
yacht.setSourceID(String.valueOf(i));
participants.getYacht().add(yacht); //java schema does not clone the array so this is "safe".
}
race.setParticipants(participants);
//compound marks sequence
Race.CompoundMarkSequence cms = raceFactory.createRaceCompoundMarkSequence();
for (Corner mark: reader.getCornersList()){
Race.CompoundMarkSequence.Corner corner = raceFactory.createRaceCompoundMarkSequenceCorner();
corner.setCompoundMarkID(String.valueOf(mark.getId()));
corner.setSeqID(String.valueOf(mark.getSeqID()));
cms.getCorner().add(corner);
}
race.setCompoundMarkSequence(cms);
//Course
Race.Course course = raceFactory.createRaceCourse();
for (CompoundMark mark: reader.getCompoundMarks()){
Race.Course.CompoundMark cm = raceFactory.createRaceCourseCompoundMark();
cm.setName(mark.getName());
cm.setCompoundMarkID(String.valueOf(mark.getId()));
if (mark.getMark1() != null){
Race.Course.CompoundMark.Mark m1 = raceFactory.createRaceCourseCompoundMarkMark();
m1.setName(mark.getMark1().getName());
m1.setTargetLat(String.valueOf(mark.getMark1().getPosition().getLatitude()));
m1.setTargetLng(String.valueOf(mark.getMark1().getPosition().getLongitude()));
m1.setSourceID(String.valueOf(mark.getMark1().getSourceID()));
cm.getMark().add(m1);
}
//make this not copy paste later but just need the concept working right now
if (mark.getMark2() != null){
Race.Course.CompoundMark.Mark m2 = raceFactory.createRaceCourseCompoundMarkMark();
m2.setName(mark.getMark1().getName());
m2.setTargetLat(String.valueOf(mark.getMark1().getPosition().getLatitude()));
m2.setTargetLng(String.valueOf(mark.getMark1().getPosition().getLongitude()));
m2.setSourceID(String.valueOf(mark.getMark1().getSourceID()));
cm.getMark().add(m2);
}
course.getCompoundMark().add(cm);
}
race.setCourse(course);
//Course Limit
Race.CourseLimit courseLimit = raceFactory.createRaceCourseLimit();
for (int i = 0; i < reader.getBoundary().size(); i++){
Race.CourseLimit.Limit limit = raceFactory.createRaceCourseLimitLimit();
limit.setSeqID(String.valueOf(i+1));
limit.setLat(String.valueOf(reader.getBoundary().get(i).getLatitude()));
limit.setLon(String.valueOf(reader.getBoundary().get(i).getLongitude()));
courseLimit.getLimit().add(limit);
}
race.setCourseLimit(courseLimit);
return race;
}
private String alterRaceToWind(String s, double degrees) throws XMLReaderException, InvalidRaceDataException, JAXBException {
RaceXMLReader reader = new RaceXMLReader(s, XMLFileType.ResourcePath);
mock.xml.Race race = copyRace(reader);
JAXBContext context = JAXBContext.newInstance(mock.xml.Race.class);
Marshaller jaxbMarshaller = context.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(race, sw);
return sw.toString();
}
}

@ -0,0 +1,127 @@
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2017.08.03 at 02:28:57 AM NZST
//
package mock.xml;
import javax.xml.bind.annotation.XmlRegistry;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the race package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: race
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link Race }
*
*/
public Race createRace() {
return new Race();
}
/**
* Create an instance of {@link Race.CourseLimit }
*
*/
public Race.CourseLimit createRaceCourseLimit() {
return new Race.CourseLimit();
}
/**
* Create an instance of {@link Race.Course }
*
*/
public Race.Course createRaceCourse() {
return new Race.Course();
}
/**
* Create an instance of {@link Race.Course.CompoundMark }
*
*/
public Race.Course.CompoundMark createRaceCourseCompoundMark() {
return new Race.Course.CompoundMark();
}
/**
* Create an instance of {@link Race.CompoundMarkSequence }
*
*/
public Race.CompoundMarkSequence createRaceCompoundMarkSequence() {
return new Race.CompoundMarkSequence();
}
/**
* Create an instance of {@link Race.Participants }
*
*/
public Race.Participants createRaceParticipants() {
return new Race.Participants();
}
/**
* Create an instance of {@link Race.RaceStartTime }
*
*/
public Race.RaceStartTime createRaceRaceStartTime() {
return new Race.RaceStartTime();
}
/**
* Create an instance of {@link Race.CourseLimit.Limit }
*
*/
public Race.CourseLimit.Limit createRaceCourseLimitLimit() {
return new Race.CourseLimit.Limit();
}
/**
* Create an instance of {@link Race.Course.CompoundMark.Mark }
*
*/
public Race.Course.CompoundMark.Mark createRaceCourseCompoundMarkMark() {
return new Race.Course.CompoundMark.Mark();
}
/**
* Create an instance of {@link Race.CompoundMarkSequence.Corner }
*
*/
public Race.CompoundMarkSequence.Corner createRaceCompoundMarkSequenceCorner() {
return new Race.CompoundMarkSequence.Corner();
}
/**
* Create an instance of {@link Race.Participants.Yacht }
*
*/
public Race.Participants.Yacht createRaceParticipantsYacht() {
return new Race.Participants.Yacht();
}
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,127 @@
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2017.08.03 at 01:07:33 AM NZST
//
package mock.xml;
import javax.xml.bind.annotation.XmlRegistry;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the main.java.mock.xml package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class RaceFactory {
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: main.java.mock.xml
*
*/
public RaceFactory() {
}
/**
* Create an instance of {@link Race }
*
*/
public Race createRace() {
return new Race();
}
/**
* Create an instance of {@link Race.CourseLimit }
*
*/
public Race.CourseLimit createRaceCourseLimit() {
return new Race.CourseLimit();
}
/**
* Create an instance of {@link Race.Course }
*
*/
public Race.Course createRaceCourse() {
return new Race.Course();
}
/**
* Create an instance of {@link Race.Course.CompoundMark }
*
*/
public Race.Course.CompoundMark createRaceCourseCompoundMark() {
return new Race.Course.CompoundMark();
}
/**
* Create an instance of {@link Race.CompoundMarkSequence }
*
*/
public Race.CompoundMarkSequence createRaceCompoundMarkSequence() {
return new Race.CompoundMarkSequence();
}
/**
* Create an instance of {@link Race.Participants }
*
*/
public Race.Participants createRaceParticipants() {
return new Race.Participants();
}
/**
* Create an instance of {@link Race.RaceStartTime }
*
*/
public Race.RaceStartTime createRaceRaceStartTime() {
return new Race.RaceStartTime();
}
/**
* Create an instance of {@link Race.CourseLimit.Limit }
*
*/
public Race.CourseLimit.Limit createRaceCourseLimitLimit() {
return new Race.CourseLimit.Limit();
}
/**
* Create an instance of {@link Race.Course.CompoundMark.Mark }
*
*/
public Race.Course.CompoundMark.Mark createRaceCourseCompoundMarkMark() {
return new Race.Course.CompoundMark.Mark();
}
/**
* Create an instance of {@link Race.CompoundMarkSequence.Corner }
*
*/
public Race.CompoundMarkSequence.Corner createRaceCompoundMarkSequenceCorner() {
return new Race.CompoundMarkSequence.Corner();
}
/**
* Create an instance of {@link Race.Participants.Yacht }
*
*/
public Race.Participants.Yacht createRaceParticipantsYacht() {
return new Race.Participants.Yacht();
}
}

@ -51,6 +51,10 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
*/
private final List<Leg> legs = new ArrayList<>();
/**
* List of corners in the race
*/
private final List<Corner> cornersList = new ArrayList<>();
/**
* The time that the race.xml file was created.
@ -331,6 +335,10 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
//Gets the ID number of this corner element.
int cornerID = getCompoundMarkID(cornerElement);
int cornerSeq = Integer.parseInt(getAttribute(cornerElement, "SeqID"));
cornersList.add(new Corner(cornerID, cornerSeq));
//Gets the CompoundMark associated with this corner.
CompoundMark lastCompoundMark = this.compoundMarkMap.get(cornerID);
@ -346,6 +354,9 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
//Gets the ID number of this corner element.
cornerID = getCompoundMarkID(cornerElement);
cornerSeq = Integer.parseInt(getAttribute(cornerElement, "SeqID"));
cornersList.add(new Corner(cornerID, cornerSeq));
//Gets the CompoundMark associated with this corner.
CompoundMark currentCompoundMark = this.compoundMarkMap.get(cornerID);
@ -460,4 +471,8 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
public List<Integer> getParticipants() {
return participants;
}
public List<Corner> getCornersList() {
return cornersList;
}
}

@ -0,0 +1,23 @@
package shared.model;
/**
* Created by Gondr on 3/08/2017.
*/
public class Corner {
private int id;
private int seqID;
public Corner(int id, int seqID){
this.id = id;
this.seqID = seqID;
}
public int getId() {
return id;
}
public int getSeqID() {
return seqID;
}
}

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Race>
<RaceID>5326</RaceID>
<RaceType>FLEET</RaceType>
<CreationTimeDate>2017-08-03T02:13:14+1200</CreationTimeDate>
<RaceStartTime Postpone="false" Time="2017-08-03T02:08:10+1200"/>
<Participants>
<Yacht SourceID="125"/>
</Participants>
<CompoundMarkSequence>
<Corner CompoundMarkID="1" SeqID="1"/>
<Corner CompoundMarkID="2" SeqID="2"/>
<Corner CompoundMarkID="4" SeqID="3"/>
<Corner CompoundMarkID="3" SeqID="4"/>
<Corner CompoundMarkID="4" SeqID="5"/>
<Corner CompoundMarkID="5" SeqID="6"/>
</CompoundMarkSequence>
<Course>
<CompoundMark CompoundMarkID="1" Name="Start Line">
<Mark SeqId="1" Name="PRO" TargetLat="32.296577" TargetLng="-64.854304" SourceID="101"/>
<Mark SeqId="2" Name="PIN" TargetLat="32.293771" TargetLng="-64.855242" SourceID="102"/>
</CompoundMark>
<CompoundMark CompoundMarkID="2" Name="Marker 1">
<Mark Name="Marker1" TargetLat="32.293039" TargetLng="-64.843983" SourceID="103"/>
</CompoundMark>
<CompoundMark CompoundMarkID="3" Name="Windward Gate">
<Mark Name="WGL" SeqId="1" TargetLat="32.28468" TargetLng="-64.850045" SourceID="104"/>
<Mark Name="WGR" SeqId="2" TargetLat="32.280164" TargetLng="-64.847591" SourceID="105"/>
</CompoundMark>
<CompoundMark CompoundMarkID="4" Name="Leeward Gate">
<Mark Name="LGL" SeqId="1" TargetLat="32.309693" TargetLng="-64.835249" SourceID="106"/>
<Mark Name="LGR" SeqId="2" TargetLat="32.308046" TargetLng="-64.831785" SourceID="107"/>
</CompoundMark>
<CompoundMark CompoundMarkID="5" Name="Finish Line">
<Mark Name="FL" SeqId="1" TargetLat="32.317379" TargetLng="-64.839291" SourceID="108"/>
<Mark Name="FR" SeqId="2" TargetLat="32.317257" TargetLng="-64.83626" SourceID="109"/>
</CompoundMark>
</Course>
<CourseLimit>
<Limit Lat="32.313922" Lon="-64.837168" SeqID="1"/>
<Limit Lat="32.317379" Lon="-64.839291" SeqID="2"/>
<Limit Lat="32.317911" Lon="-64.836996" SeqID="3"/>
<Limit Lat="32.317257" Lon="-64.83626" SeqID="4"/>
<Limit Lat="32.304273" Lon="-64.822834" SeqID="5"/>
<Limit Lat="32.279097" Lon="-64.841545" SeqID="6"/>
<Limit Lat="32.279604" Lon="-64.849871" SeqID="7"/>
<Limit Lat="32.289545" Lon="-64.854162" SeqID="8"/>
<Limit Lat="32.290198" Lon="-64.858711" SeqID="9"/>
<Limit Lat="32.297164" Lon="-64.856394" SeqID="10"/>
<Limit Lat="32.296148" Lon="-64.849184" SeqID="11"/>
</CourseLimit>
</Race>

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Race">
<xs:complexType>
<xs:sequence>
<xs:element name="RaceID" type="xs:string"/>
<xs:element name="RaceType" type="xs:string"/>
<xs:element name="CreationTimeDate" type="xs:string"/>
<xs:element name="RaceStartTime">
<xs:complexType>
<xs:attribute name="Postpone" type="xs:string" use="required"/>
<xs:attribute name="Time" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="Participants">
<xs:complexType>
<xs:sequence>
<xs:element name="Yacht" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="SourceID" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CompoundMarkSequence">
<xs:complexType>
<xs:sequence>
<xs:element name="Corner" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="CompoundMarkID" type="xs:string" use="required"/>
<xs:attribute name="SeqID" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Course">
<xs:complexType>
<xs:sequence>
<xs:element name="CompoundMark" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Mark" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="SeqId" type="xs:string"/>
<xs:attribute name="Name" type="xs:string" use="required"/>
<xs:attribute name="TargetLat" type="xs:string" use="required"/>
<xs:attribute name="TargetLng" type="xs:string" use="required"/>
<xs:attribute name="SourceID" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="CompoundMarkID" type="xs:string" use="required"/>
<xs:attribute name="Name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CourseLimit">
<xs:complexType>
<xs:sequence>
<xs:element name="Limit" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="Lat" type="xs:string" use="required"/>
<xs:attribute name="Lon" type="xs:string" use="required"/>
<xs:attribute name="SeqID" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Loading…
Cancel
Save