MockRace:
Now uses RaceState as its base class, rather than Race. This aligns it with VisualiserRace. RaceState contains the shared race state between the client and server, and stores most of its data in RaceDataSource etc..., instead of maintaining its own copy. Moved collider registry member to MockRace. XMLUtilities: Added documentation to two important functions, and updated them to use generics instead of Objects. #story[1188]
This commit is contained in:
@@ -20,10 +20,17 @@ import java.io.*;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 13/08/17.
|
||||
* Contains utility functions to convert between xml files and xml class objects.
|
||||
*/
|
||||
public class XMLUtilities {
|
||||
|
||||
|
||||
/**
|
||||
* Converts an XML class object to an XML string.
|
||||
* @param o The XML class object to convert.
|
||||
* @return String containing the serialised XML data.
|
||||
* @throws JAXBException Thrown if the object is cannot be serialised to XML.
|
||||
*/
|
||||
public static String classToXML(Object o) throws JAXBException {
|
||||
JAXBContext context = JAXBContext.newInstance(o.getClass());
|
||||
Marshaller jaxbMarshaller = context.createMarshaller();
|
||||
@@ -49,14 +56,26 @@ public class XMLUtilities {
|
||||
return xmlToClass(document, schemaURL, c);
|
||||
}
|
||||
|
||||
public static Object xmlToClass(InputStream i, URL schemaURL, Class c) throws ParserConfigurationException, IOException, SAXException, JAXBException {
|
||||
/**
|
||||
* Converts an XML file to an XML class (e.g., {@link shared.xml.Race.XMLRace}).
|
||||
* @param i The input stream for the XML file.
|
||||
* @param schemaURL URL for the XML schema.
|
||||
* @param c The XML class to convert to.
|
||||
* @param <T> The XML class to convert to.
|
||||
* @return The XML class object.
|
||||
* @throws ParserConfigurationException
|
||||
* @throws IOException
|
||||
* @throws SAXException
|
||||
* @throws JAXBException
|
||||
*/
|
||||
public static <T> T xmlToClass(InputStream i, URL schemaURL, Class<T> c) throws ParserConfigurationException, IOException, SAXException, JAXBException {
|
||||
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = parser.parse(i);
|
||||
|
||||
return xmlToClass(document, schemaURL, c);
|
||||
}
|
||||
|
||||
public static Object xmlToClass(Document document, URL schemaURL, Class c) throws ParserConfigurationException, IOException, SAXException, JAXBException {
|
||||
public static <T> T xmlToClass(Document document, URL schemaURL, Class<T> c) throws ParserConfigurationException, IOException, SAXException, JAXBException {
|
||||
JAXBContext jc = JAXBContext.newInstance(c);
|
||||
Unmarshaller unmarshaller = jc.createUnmarshaller();
|
||||
|
||||
@@ -64,7 +83,7 @@ public class XMLUtilities {
|
||||
Schema schema = sf.newSchema(schemaURL);
|
||||
unmarshaller.setSchema(schema);
|
||||
|
||||
return unmarshaller.unmarshal(new DOMSource(document));
|
||||
return (T) unmarshaller.unmarshal(new DOMSource(document));
|
||||
}
|
||||
|
||||
public static boolean validateXML(String file, URL schemaURL){
|
||||
|
||||
Reference in New Issue
Block a user