Copied remaining files into appropriate package. These need to be refactored and put into the shared package.
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package mock.dataInput;
|
||||
|
||||
import seng302.Model.Boat;
|
||||
import seng302.Model.Mark;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Boats Data
|
||||
*/
|
||||
public interface BoatDataSource {
|
||||
Map<Integer, Boat> getBoats();
|
||||
Map<Integer, Mark> getMarkerBoats();
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package mock.dataInput;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.SAXException;
|
||||
import seng302.Model.Boat;
|
||||
import seng302.Model.GPSCoordinate;
|
||||
import seng302.Model.Mark;
|
||||
import seng302.Model.Polars;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Xml Reader class for Boat XML used for the race
|
||||
*/
|
||||
public class BoatXMLReader extends XMLReader implements BoatDataSource {
|
||||
|
||||
private final Map<Integer, Boat> boatMap = new HashMap<>();
|
||||
private final Map<Integer, Mark> markerMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Polars table to assign to each boat.
|
||||
*/
|
||||
Polars boatPolars;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor for Boat XML
|
||||
*
|
||||
* @param filePath Name/path of file to read. Read as a resource.
|
||||
* @param boatPolars polars used by the boats
|
||||
* @throws IOException error
|
||||
* @throws SAXException error
|
||||
* @throws ParserConfigurationException error
|
||||
*/
|
||||
public BoatXMLReader(String filePath, Polars boatPolars) throws IOException, SAXException, ParserConfigurationException {
|
||||
super(filePath);
|
||||
this.boatPolars = boatPolars;
|
||||
read();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the XML
|
||||
*/
|
||||
public void read() {
|
||||
readSettings();
|
||||
readShapes();
|
||||
readBoats();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the Boats
|
||||
*/
|
||||
private void readBoats() {
|
||||
Element nBoats = (Element) doc.getElementsByTagName("Boats").item(0);
|
||||
for (int i = 0; i < nBoats.getChildNodes().getLength(); i++) {
|
||||
Node boat = nBoats.getChildNodes().item(i);
|
||||
if (boat.getNodeName().equals("Boat")) {
|
||||
readBoatNode(boat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ignored data
|
||||
*/
|
||||
private void readShapes() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Ignored data
|
||||
*/
|
||||
private void readSettings() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the node (XMl data node) is a Yacht or not
|
||||
* @param boatNode Node from the XML
|
||||
* @return Whether the node is a yacht node or not
|
||||
*/
|
||||
private boolean isYachtNode(Node boatNode) {
|
||||
return boatNode.getAttributes().getNamedItem("Type").getTextContent().toLowerCase().equals("yacht");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the information about one boat
|
||||
* Ignored values: ShapeID, StoweName, HullNum, Skipper, Type
|
||||
*/
|
||||
private void readBoatNode(Node boatNode) {
|
||||
int sourceID = Integer.parseInt(boatNode.getAttributes().getNamedItem("SourceID").getTextContent());
|
||||
String name = boatNode.getAttributes().getNamedItem("BoatName").getTextContent();
|
||||
|
||||
if (isYachtNode(boatNode)) readYacht(boatNode, sourceID, name);
|
||||
else readMark(boatNode, sourceID, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a Yacht Node
|
||||
* @param boatNode Node to be read
|
||||
* @param sourceID Source ID of the Yacht
|
||||
* @param name Name of the Boat
|
||||
*/
|
||||
private void readYacht(Node boatNode, int sourceID, String name) {
|
||||
String shortName = boatNode.getAttributes().getNamedItem("ShortName").getTextContent();
|
||||
if (exists(boatNode, "Country")) {
|
||||
String country = boatNode.getAttributes().getNamedItem("Country").getTextContent();
|
||||
boatMap.put(sourceID, new Boat(sourceID, name, country, this.boatPolars));
|
||||
} else {
|
||||
boatMap.put(sourceID, new Boat(sourceID, name, shortName, this.boatPolars));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read Marker Boats
|
||||
* @param boatNode Node to be read
|
||||
* @param sourceID Source ID of the boat
|
||||
* @param name Name of the Marker Boat
|
||||
*/
|
||||
private void readMark(Node boatNode, int sourceID, String name) {
|
||||
Node nCoord = ((Element)boatNode).getElementsByTagName("GPSposition").item(0);
|
||||
double x = Double.parseDouble(nCoord.getAttributes().getNamedItem("X").getTextContent());
|
||||
double y = Double.parseDouble(nCoord.getAttributes().getNamedItem("Y").getTextContent());
|
||||
Mark mark = new Mark(sourceID, name, new GPSCoordinate(y,x));
|
||||
markerMap.put(sourceID, mark);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the boats that are going to participate in this race
|
||||
* @return Dictionary of boats that are to participate in this race indexed by SourceID
|
||||
*/
|
||||
@Override
|
||||
public Map<Integer, Boat> getBoats() {
|
||||
return boatMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the marker BOats that are participating in this race
|
||||
* @return Dictionary of the Markers BOats that are in this race indexed by their Source ID.
|
||||
*/
|
||||
@Override
|
||||
public Map<Integer, Mark> getMarkerBoats() {
|
||||
return markerMap;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package mock.dataInput;
|
||||
|
||||
import seng302.Model.Boat;
|
||||
import seng302.Model.CompoundMark;
|
||||
import seng302.Model.GPSCoordinate;
|
||||
import seng302.Model.Leg;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Data Class for a Race
|
||||
*/
|
||||
public interface RaceDataSource {
|
||||
List<Boat> getBoats();
|
||||
|
||||
List<Leg> getLegs();
|
||||
|
||||
List<GPSCoordinate> getBoundary();
|
||||
|
||||
List<CompoundMark> getCompoundMarks();
|
||||
|
||||
int getRaceId();
|
||||
|
||||
String getRaceType();
|
||||
|
||||
ZonedDateTime getZonedDateTime();
|
||||
|
||||
GPSCoordinate getMapTopLeft();
|
||||
|
||||
GPSCoordinate getMapBottomRight();
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
package mock.dataInput;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
import seng302.Exceptions.StreamedCourseXMLException;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* XML Reader that reads in the race data required for this race
|
||||
*/
|
||||
public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
private static final double COORDINATEPADDING = 0.000;
|
||||
private GPSCoordinate mapTopLeft, mapBottomRight;
|
||||
private final List<GPSCoordinate> boundary = new ArrayList<>();
|
||||
private final Map<Integer,Element> compoundMarkMap = new HashMap<>();
|
||||
private final Map<Integer, Boat> participants = new HashMap<>();
|
||||
private final List<Leg> legs = new ArrayList<>();
|
||||
private final List<CompoundMark> compoundMarks = new ArrayList<>();
|
||||
private ZonedDateTime creationTimeDate;
|
||||
private ZonedDateTime raceStartTime;
|
||||
private int raceID;
|
||||
private String raceType;
|
||||
private boolean postpone;
|
||||
|
||||
private Map<Integer, Boat> boats;
|
||||
private Map<Integer, Mark> marks;
|
||||
|
||||
/**
|
||||
* Constructor for Streamed Race XML
|
||||
* @param filePath path of the file
|
||||
* @param boatData data for boats in race
|
||||
* @throws IOException error
|
||||
* @throws SAXException error
|
||||
* @throws ParserConfigurationException error
|
||||
* @throws ParseException error
|
||||
* @throws StreamedCourseXMLException error
|
||||
*/
|
||||
public RaceXMLReader(String filePath, BoatDataSource boatData) throws IOException, SAXException, ParserConfigurationException, ParseException, StreamedCourseXMLException {
|
||||
this(filePath, boatData, true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructor for Streamed Race XML
|
||||
* @param filePath file path to read
|
||||
* @param boatData data of the boats in race
|
||||
* @param read whether or not to read and store the files straight away.
|
||||
* @throws IOException error
|
||||
* @throws SAXException error
|
||||
* @throws ParserConfigurationException error
|
||||
* @throws ParseException error
|
||||
* @throws StreamedCourseXMLException error
|
||||
*/
|
||||
public RaceXMLReader(String filePath, BoatDataSource boatData, boolean read) throws IOException, SAXException, ParserConfigurationException, ParseException, StreamedCourseXMLException {
|
||||
super(filePath);
|
||||
this.boats = boatData.getBoats();
|
||||
this.marks = boatData.getMarkerBoats();
|
||||
if (read) {
|
||||
read();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* reads
|
||||
* @throws StreamedCourseXMLException error
|
||||
*/
|
||||
private void read() throws StreamedCourseXMLException {
|
||||
readRace();
|
||||
readParticipants();
|
||||
readCourse();
|
||||
}
|
||||
|
||||
/**
|
||||
* reads a race
|
||||
*/
|
||||
private void readRace() {
|
||||
DateTimeFormatter dateFormat = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
|
||||
Element settings = (Element) doc.getElementsByTagName("Race").item(0);
|
||||
NamedNodeMap raceTimeTag = doc.getElementsByTagName("RaceStartTime").item(0).getAttributes();
|
||||
|
||||
if (raceTimeTag.getNamedItem("Time") != null) dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
|
||||
|
||||
|
||||
raceID = Integer.parseInt(getTextValueOfNode(settings, "RaceID"));
|
||||
raceType = getTextValueOfNode(settings, "RaceType");
|
||||
|
||||
creationTimeDate = ZonedDateTime.parse(getTextValueOfNode(settings, "CreationTimeDate"), dateFormat);
|
||||
|
||||
if (raceTimeTag.getNamedItem("Time") != null) raceStartTime = ZonedDateTime.parse(raceTimeTag.getNamedItem("Time").getTextContent(), dateFormat);
|
||||
else raceStartTime = ZonedDateTime.parse(raceTimeTag.getNamedItem("Start").getTextContent(), dateFormat);
|
||||
|
||||
postpone = Boolean.parseBoolean(raceTimeTag.getNamedItem("Postpone").getTextContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads in the participants for htis race
|
||||
*/
|
||||
private void readParticipants() {
|
||||
Element nParticipants = (Element) doc.getElementsByTagName("Participants").item(0);
|
||||
nParticipants.getChildNodes().getLength();
|
||||
for (int i = 0; i < nParticipants.getChildNodes().getLength(); i++) {
|
||||
int sourceID;
|
||||
Node yacht = nParticipants.getChildNodes().item(i);
|
||||
if (yacht.getNodeName().equals("Yacht")) {
|
||||
if (exists(yacht, "SourceID")) {
|
||||
sourceID = Integer.parseInt(yacht.getAttributes().getNamedItem("SourceID").getTextContent());
|
||||
participants.put(sourceID, boats.get(sourceID));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* reads a course
|
||||
* @throws StreamedCourseXMLException error
|
||||
*/
|
||||
private void readCourse() throws StreamedCourseXMLException {
|
||||
readCompoundMarks();
|
||||
readCompoundMarkSequence();
|
||||
readCourseLimit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Indexes CompoundMark elements by their ID for use in generating the course, and populates list of Markers.
|
||||
* @see CompoundMark
|
||||
*/
|
||||
private void readCompoundMarks() throws StreamedCourseXMLException {
|
||||
Element nCourse = (Element) doc.getElementsByTagName("Course").item(0);
|
||||
for(int i = 0; i < nCourse.getChildNodes().getLength(); i++) {
|
||||
Node compoundMark = nCourse.getChildNodes().item(i);
|
||||
if(compoundMark.getNodeName().equals("CompoundMark")) {
|
||||
int compoundMarkID = getCompoundMarkID((Element) compoundMark);
|
||||
compoundMarkMap.put(compoundMarkID, (Element)compoundMark);
|
||||
compoundMarks.add(getCompoundMark(compoundMarkID));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a CompoundMark from the CompoundMark element with given ID.
|
||||
* @param compoundMarkID index of required CompoundMark element
|
||||
* @return generated CompoundMark
|
||||
* @throws StreamedCourseXMLException if CompoundMark element contains unhandled number of compoundMarks
|
||||
* @see CompoundMark
|
||||
*/
|
||||
private CompoundMark getCompoundMark(int compoundMarkID) throws StreamedCourseXMLException {
|
||||
Element compoundMark = compoundMarkMap.get(compoundMarkID);
|
||||
NodeList nMarks = compoundMark.getElementsByTagName("Mark");
|
||||
CompoundMark marker;
|
||||
|
||||
switch(nMarks.getLength()) {
|
||||
case 1: marker = new CompoundMark(getMark((Element)nMarks.item(0)));
|
||||
break;
|
||||
case 2: marker = new CompoundMark(getMark((Element)nMarks.item(0)), getMark((Element)nMarks.item(1))); break;
|
||||
default: throw new StreamedCourseXMLException();
|
||||
}
|
||||
|
||||
return marker;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a mark from an Element
|
||||
* @param mark Element the mark is suppose to be part of
|
||||
* @return a Mark that existed in the element
|
||||
*/
|
||||
private Mark getMark(Element mark) {
|
||||
int sourceID = Integer.parseInt(mark.getAttribute("SourceID"));
|
||||
return marks.get(sourceID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads "compoundMarkID" attribute of CompoundMark or Corner element
|
||||
* @param element with "compoundMarkID" attribute
|
||||
* @return value of "compoundMarkID" attribute
|
||||
*/
|
||||
private int getCompoundMarkID(Element element) {
|
||||
return Integer.parseInt(element.getAttribute("CompoundMarkID"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads "name" attribute of CompoundMark element with corresponding CompoundMarkID
|
||||
* @param compoundMarkID unique ID for CompoundMark element
|
||||
* @return value of "name" attribute
|
||||
*/
|
||||
private String getCompoundMarkName(int compoundMarkID) {
|
||||
return compoundMarkMap.get(compoundMarkID).getAttribute("Name");
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates list of legs given CompoundMarkSequence element and referenced CompoundMark elements.
|
||||
* @throws StreamedCourseXMLException if compoundMarks 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);
|
||||
CompoundMark lastCompoundMark = getCompoundMark(getCompoundMarkID(markXML));
|
||||
String legName = getCompoundMarkName(getCompoundMarkID(markXML));
|
||||
for(int i = 1; i < nCorners.getLength(); i++) {
|
||||
markXML = (Element)nCorners.item(i);
|
||||
CompoundMark currentCompoundMark = getCompoundMark(getCompoundMarkID(markXML));
|
||||
legs.add(new Leg(legName, lastCompoundMark, currentCompoundMark, i-1));
|
||||
lastCompoundMark = currentCompoundMark;
|
||||
legName = getCompoundMarkName(getCompoundMarkID(markXML));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the boundary limits of the course
|
||||
*/
|
||||
private void readCourseLimit() {
|
||||
Element nCourseLimit = (Element) doc.getElementsByTagName("CourseLimit").item(0);
|
||||
for(int i = 0; i < nCourseLimit.getChildNodes().getLength(); i++) {
|
||||
Node limit = nCourseLimit.getChildNodes().item(i);
|
||||
if (limit.getNodeName().equals("Limit")) {
|
||||
double lat = Double.parseDouble(limit.getAttributes().getNamedItem("Lat").getTextContent());
|
||||
double lon = Double.parseDouble(limit.getAttributes().getNamedItem("Lon").getTextContent());
|
||||
boundary.add(new GPSCoordinate(lat, lon));
|
||||
}
|
||||
}
|
||||
|
||||
double maxLatitude = boundary.stream().max(Comparator.comparingDouble(GPSCoordinate::getLatitude)).get().getLatitude() + COORDINATEPADDING;
|
||||
double maxLongitude = boundary.stream().max(Comparator.comparingDouble(GPSCoordinate::getLongitude)).get().getLongitude() + COORDINATEPADDING;
|
||||
double minLatitude = boundary.stream().min(Comparator.comparingDouble(GPSCoordinate::getLatitude)).get().getLatitude() + COORDINATEPADDING;
|
||||
double minLongitude = boundary.stream().min(Comparator.comparingDouble(GPSCoordinate::getLongitude)).get().getLongitude() + COORDINATEPADDING;
|
||||
|
||||
mapTopLeft = new GPSCoordinate(minLatitude, minLongitude);
|
||||
mapBottomRight = new GPSCoordinate(maxLatitude, maxLongitude);
|
||||
}
|
||||
|
||||
public List<GPSCoordinate> getBoundary() {
|
||||
return boundary;
|
||||
}
|
||||
|
||||
public GPSCoordinate getMapTopLeft() {
|
||||
return mapTopLeft;
|
||||
}
|
||||
|
||||
public GPSCoordinate getMapBottomRight() {
|
||||
return mapBottomRight;
|
||||
}
|
||||
|
||||
public List<Leg> getLegs() {
|
||||
return legs;
|
||||
}
|
||||
|
||||
public List<CompoundMark> getCompoundMarks() { return compoundMarks; }
|
||||
|
||||
public Double getPadding() {
|
||||
return COORDINATEPADDING;
|
||||
}
|
||||
|
||||
public ZonedDateTime getCreationTimeDate() {
|
||||
return creationTimeDate;
|
||||
}
|
||||
|
||||
public ZonedDateTime getZonedDateTime() {
|
||||
return raceStartTime;
|
||||
}
|
||||
|
||||
public int getRaceId() {
|
||||
return raceID;
|
||||
}
|
||||
|
||||
public String getRaceType() {
|
||||
return raceType;
|
||||
}
|
||||
|
||||
public boolean isPostpone() {
|
||||
return postpone;
|
||||
}
|
||||
|
||||
public List<Boat> getBoats() {
|
||||
return new ArrayList<>(participants.values());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package mock.dataInput;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
|
||||
/**
|
||||
* Base Reader for XML Files
|
||||
*/
|
||||
public abstract class XMLReader {
|
||||
|
||||
protected Document doc;
|
||||
|
||||
/**
|
||||
* Read in XML file
|
||||
* @param filePath filepath for XML file
|
||||
* @throws ParserConfigurationException If a document builder cannot be created.
|
||||
* @throws IOException If any IO errors occur while parsing the XML file.
|
||||
* @throws SAXException If any parse error occurs while parsing.
|
||||
*/
|
||||
public XMLReader(String filePath) throws ParserConfigurationException, IOException, SAXException {
|
||||
|
||||
InputSource fXmlFile;
|
||||
if (filePath.contains("<")) {
|
||||
fXmlFile = new InputSource();
|
||||
fXmlFile.setCharacterStream(new StringReader(filePath));
|
||||
|
||||
} else {
|
||||
fXmlFile = new InputSource(getClass().getClassLoader().getResourceAsStream(filePath));
|
||||
}
|
||||
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
doc = dBuilder.parse(fXmlFile);
|
||||
doc.getDocumentElement().normalize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Alternate constructor
|
||||
* @param xmlFile File to be read
|
||||
* @param isWholeFile boolean value whether entire file is being passed
|
||||
*/
|
||||
public XMLReader(String xmlFile, Boolean isWholeFile) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return Document data of the read-in XML
|
||||
* @return XML document
|
||||
*/
|
||||
public Document getDocument() {
|
||||
return doc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get content of a tag in an element
|
||||
* @param n Element to read tags from
|
||||
* @param tagName Name of the tag
|
||||
* @return Content of the tag
|
||||
*/
|
||||
public String getTextValueOfNode(Element n, String tagName) {
|
||||
return n.getElementsByTagName(tagName).item(0).getTextContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attributes for an element
|
||||
* @param n Element to read attributes from
|
||||
* @param attr Attributes of element
|
||||
* @return Attributes of element
|
||||
*/
|
||||
public String getAttribute(Element n, String attr) {
|
||||
return n.getAttribute(attr);
|
||||
}
|
||||
|
||||
protected boolean exists(Node node, String attribute) {
|
||||
return node.getAttributes().getNamedItem(attribute) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the contents of the XML FILe.
|
||||
* @param document holds all xml information
|
||||
* @return String representation of document
|
||||
* @throws TransformerException when document is malformed, and cannot be turned into a string
|
||||
*/
|
||||
public static String getContents(Document document) throws TransformerException {
|
||||
DOMSource source = new DOMSource(document);
|
||||
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
||||
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
StreamResult result = new StreamResult(stringWriter);
|
||||
transformer.transform(source, result);
|
||||
|
||||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user