@ -6,14 +6,13 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList ;
import org.xml.sax.SAXException ;
import seng302.GPSCoordinate ;
import seng302.Model.CompoundMark ;
import seng302.Model.Leg ;
import seng302.Model.Marker ;
import seng302.XMLReader ;
import javax.xml.parsers.ParserConfigurationException ;
import java.io.IOException ;
import java.io.InputStream ;
import java.text.ParseException ;
import java.time.ZonedDateTime ;
import java.time.format.DateTimeFormatter ;
import java.util.* ;
@ -25,29 +24,16 @@ public class StreamedCourseXMLReader extends XMLReader {
private static final double COORDINATEPADDING = 0.000 ;
private GPSCoordinate mapTopLeft , mapBottomRight ;
private final List < GPSCoordinate > boundary = new ArrayList < > ( ) ;
private final Map < Integer , Element > compoundMark Map = new HashMap < > ( ) ;
private final Map < Integer , Element > compoundMark s = new HashMap < > ( ) ;
private final Map < Integer , StreamedBoat > participants = new HashMap < > ( ) ;
private final List < Leg > legs = new ArrayList < > ( ) ;
private final List < Compound Mark> co mpoundM arks = new ArrayList < > ( ) ;
private final List < Marker > marker s = new ArrayList < > ( ) ;
private ZonedDateTime creationTimeDate ;
private ZonedDateTime raceStartTime ;
private int raceID ;
private String raceType ;
private boolean postpone ;
/ * *
* Constructor for Streamed Race XML
* @param filePath path of the file
* @throws IOException error
* @throws SAXException error
* @throws ParserConfigurationException error
* @throws ParseException error
* @throws StreamedCourseXMLException error
* /
public StreamedCourseXMLReader ( String filePath ) throws IOException , SAXException , ParserConfigurationException , ParseException , StreamedCourseXMLException {
this ( filePath , true ) ;
}
/ * *
* Constructor for Streamed Race XML
* @param filePath file path to read
@ -55,10 +41,9 @@ public class StreamedCourseXMLReader extends XMLReader {
* @throws IOException error
* @throws SAXException error
* @throws ParserConfigurationException error
* @throws ParseException error
* @throws StreamedCourseXMLException error
* /
public StreamedCourseXMLReader ( String filePath , boolean read ) throws IOException , SAXException , ParserConfigurationException , ParseException, StreamedCourseXMLException {
public StreamedCourseXMLReader ( String filePath , boolean read ) throws IOException , SAXException , ParserConfigurationException , StreamedCourseXMLException {
super ( filePath ) ;
if ( read ) {
read ( ) ;
@ -71,10 +56,9 @@ public class StreamedCourseXMLReader extends XMLReader {
* @throws IOException error
* @throws SAXException error
* @throws ParserConfigurationException error
* @throws ParseException error
* @throws StreamedCourseXMLException error
* /
public StreamedCourseXMLReader ( InputStream xmlString ) throws IOException , SAXException , ParserConfigurationException , ParseException, StreamedCourseXMLException {
public StreamedCourseXMLReader ( InputStream xmlString ) throws IOException , SAXException , ParserConfigurationException , StreamedCourseXMLException {
super ( xmlString ) ;
read ( ) ;
}
@ -138,7 +122,7 @@ public class StreamedCourseXMLReader extends XMLReader {
/ * *
* Indexes CompoundMark elements by their ID for use in generating the course , and populates list of Markers .
* @see CompoundMark
* @see seng302. Model . Marker
* /
private void readCompoundMarks ( ) throws StreamedCourseXMLException {
Element nCourse = ( Element ) doc . getElementsByTagName ( "Course" ) . item ( 0 ) ;
@ -146,27 +130,28 @@ public class StreamedCourseXMLReader extends XMLReader {
Node compoundMark = nCourse . getChildNodes ( ) . item ( i ) ;
if ( compoundMark . getNodeName ( ) . equals ( "CompoundMark" ) ) {
int compoundMarkID = getCompoundMarkID ( ( Element ) compoundMark ) ;
compoundMark Map . put ( compoundMarkID , ( Element ) compoundMark ) ;
co mpoundM arks. add ( getMarker ( compoundMarkID ) ) ;
compoundMark s . put ( compoundMarkID , ( Element ) compoundMark ) ;
marker s. add ( getMarker ( compoundMarkID ) ) ;
}
}
}
/ * *
* Generates a Compound Mark from the CompoundMark element with given ID .
* Generates a Marker from the CompoundMark element with given ID .
* @param compoundMarkID index of required CompoundMark element
* @return generated Compound Mark
* @return generated Marker
* @throws StreamedCourseXMLException if CompoundMark element contains unhandled number of compoundMarks
* @see CompoundMark
* @see seng302. Model . Marker
* /
private Compound Mark getMarker ( int compoundMarkID ) throws StreamedCourseXMLException {
Element compoundMark = compoundMark Map . get ( compoundMarkID ) ;
private Marker getMarker ( int compoundMarkID ) throws StreamedCourseXMLException {
Element compoundMark = compoundMark s . get ( compoundMarkID ) ;
NodeList nMarks = compoundMark . getElementsByTagName ( "Mark" ) ;
Compound Mark marker ;
Marker marker ;
switch ( nMarks . getLength ( ) ) {
case 1 : marker = new CompoundMark ( getCoordinate ( ( Element ) nMarks . item ( 0 ) ) ) ; break ;
case 2 : marker = new CompoundMark ( getCoordinate ( ( Element ) nMarks . item ( 0 ) ) , getCoordinate ( ( Element ) nMarks . item ( 1 ) ) ) ; break ;
case 1 : marker = new Marker ( getCoordinate ( ( Element ) nMarks . item ( 0 ) ) , getSourceId ( ( Element ) nMarks . item ( 0 ) ) ) ; break ;
case 2 : marker = new Marker ( getCoordinate ( ( Element ) nMarks . item ( 0 ) ) , getCoordinate ( ( Element ) nMarks . item ( 1 ) ) ,
getSourceId ( ( Element ) nMarks . item ( 0 ) ) , getSourceId ( ( Element ) nMarks . item ( 1 ) ) ) ; break ;
default : throw new StreamedCourseXMLException ( ) ;
}
@ -179,6 +164,14 @@ public class StreamedCourseXMLReader extends XMLReader {
return new GPSCoordinate ( lat , lon ) ;
}
private int getSourceId ( Element mark ) {
String sourceId = mark . getAttribute ( "SourceID" ) ;
if ( sourceId . isEmpty ( ) ) {
return 0 ;
}
return Integer . parseInt ( sourceId ) ;
}
/ * *
* Reads "compoundMarkID" attribute of CompoundMark or Corner element
* @param element with "compoundMarkID" attribute
@ -194,24 +187,24 @@ public class StreamedCourseXMLReader extends XMLReader {
* @return value of "name" attribute
* /
private String getCompoundMarkName ( int compoundMarkID ) {
return compoundMark Map . get ( compoundMarkID ) . getAttribute ( "Name" ) ;
return compoundMark s . get ( compoundMarkID ) . getAttribute ( "Name" ) ;
}
/ * *
* Populates list of legs given CompoundMarkSequence element and referenced CompoundMark elements .
* @throws StreamedCourseXMLException if co mpoundM arks cannot be resolved from CompoundMark
* @throws StreamedCourseXMLException if marker s cannot be resolved from CompoundMark
* /
private void readCompoundMarkSequence ( ) throws StreamedCourseXMLException {
Element nCompoundMarkSequence = ( Element ) doc . getElementsByTagName ( "CompoundMarkSequence" ) . item ( 0 ) ;
NodeList nCorners = nCompoundMarkSequence . getElementsByTagName ( "Corner" ) ;
Element markXML = ( Element ) nCorners . item ( 0 ) ;
Compound Mark last Compound Mark = getMarker ( getCompoundMarkID ( markXML ) ) ;
Marker last Marker = getMarker ( getCompoundMarkID ( markXML ) ) ;
String legName = getCompoundMarkName ( getCompoundMarkID ( markXML ) ) ;
for ( int i = 1 ; i < nCorners . getLength ( ) ; i + + ) {
markXML = ( Element ) nCorners . item ( i ) ;
Compound Mark current Compound Mark = getMarker ( getCompoundMarkID ( markXML ) ) ;
legs . add ( new Leg ( legName , last Compound Mark, current Compound Mark, i - 1 ) ) ;
last Compound Mark = current Compound Mark;
Marker current Marker = getMarker ( getCompoundMarkID ( markXML ) ) ;
legs . add ( new Leg ( legName , last Marker , current Marker , i - 1 ) ) ;
last Marker = current Marker ;
legName = getCompoundMarkName ( getCompoundMarkID ( markXML ) ) ;
}
}
@ -253,16 +246,12 @@ public class StreamedCourseXMLReader extends XMLReader {
return legs ;
}
public List < Compound Mark> get Compound Marks( ) { return co mpoundM arks; }
public List < Marker > get Marker s( ) { return marker s; }
public Double getPadding ( ) {
return COORDINATEPADDING ;
}
public ZonedDateTime getCreationTimeDate ( ) {
return creationTimeDate ;
}
public ZonedDateTime getRaceStartTime ( ) {
return raceStartTime ;
}