Moving stuff from school pc to home

This commit is contained in:
Fan-Wu Yang
2017-08-13 23:03:16 +12:00
parent 8999cddf8b
commit 70d5447e88
25 changed files with 4995 additions and 282 deletions
@@ -23,7 +23,7 @@ public abstract class Race implements Runnable {
/**
* The source of race related data.
*/
protected RaceDataSource raceDataSource;
protected mock.xml.Race raceDataSource;
/**
* The source of boat related data.
@@ -134,7 +134,7 @@ public abstract class Race implements Runnable {
* @param regattaDataSource Data source for race related data (course name, location, timezone, etc...).
* @param latestMessages The collection of latest messages, which can be written to, or read from.
*/
public Race(BoatDataSource boatDataSource, RaceDataSource raceDataSource, RegattaDataSource regattaDataSource, LatestMessages latestMessages) {
public Race(BoatDataSource boatDataSource, mock.xml.Race raceDataSource, RegattaDataSource regattaDataSource, LatestMessages latestMessages) {
//Keep a reference to data sources.
this.raceDataSource = raceDataSource;
@@ -1,9 +1,26 @@
package shared.xml;
import mock.xml.Race;
import mock.xml.RaceXMLCreator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;
/**
* Created by fwy13 on 13/08/17.
@@ -21,16 +38,49 @@ public class XMLUtilities {
return sw.toString();
}
public static String xmlToClass(){
// JAXBContext jc = JAXBContext.newInstance(YourXMLClass.class);
// Unmarshaller unmarshaller = jc.createUnmarshaller();
//
// SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// URL schemaURL = // The URL to your XML Schema;
// Schema schema = sf.newSchema(schemaURL);
// unmarshaller.setSchema(schema);
//
// YourXMLClass yourXMLClass = (YourXMLClass) unmarshaller.unmarshal(xml);
public static Object xmlToClass(File file, URL schemaURL, Class c) throws ParserConfigurationException, IOException, SAXException, JAXBException {
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(file);
return xmlToClass(document, schemaURL, c);
}
public static Object xmlToClass(String xml, URL schemaURL, Class c) throws ParserConfigurationException, IOException, SAXException, JAXBException {
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(xml);
return xmlToClass(document, schemaURL, c);
}
public static Object xmlToClass(Document document, URL schemaURL, Class c) throws ParserConfigurationException, IOException, SAXException, JAXBException {
JAXBContext jc = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = jc.createUnmarshaller();
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(schemaURL);
unmarshaller.setSchema(schema);
return unmarshaller.unmarshal(new DOMSource(document));
}
public static boolean validateXML(String file, URL schemaURL){
try {
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File(file));
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(schemaURL);
Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));
} catch (ParserConfigurationException e) {
return false;
} catch (IOException e) {
return false;
} catch (SAXException e) {
return false;
}
return true;
}
}