XMLBusNetworkUtil { @SuppressWarnings("checkstyle:npathcomplexity") public static Node writeBusNetwork(BusNetwork busNetwork, XMLBuilder builder, XMLResources resources) throws IOException { final Element element = builder.createElement(NODE_BUSNETWORK); writeGISElementAttributes(element, busNetwork, builder, resources); final Integer color = busNetwork.getRawColor(); if (color != null) { element.setAttribute(ATTR_COLOR, toColor(color)); } final Element stopsNode = builder.createElement(NODE_BUSSTOPS); for (final BusStop stop : busNetwork.busStops()) { final Node stopNode = writeBusStop(stop, builder, resources); if (stopNode != null) { stopsNode.appendChild(stopNode); } } if (stopsNode.getChildNodes().getLength() > 0) { element.appendChild(stopsNode); } final Element hubsNode = builder.createElement(NODE_BUSHUBS); for (final BusHub hub : busNetwork.busHubs()) { final Node hubNode = writeBusHub(hub, builder, resources); if (hubNode != null) { hubsNode.appendChild(hubNode); } } if (hubsNode.getChildNodes().getLength() > 0) { element.appendChild(hubsNode); } final Element linesNode = builder.createElement(NODE_BUSLINES); for (final BusLine line : busNetwork.busLines()) { final Node lineNode = writeBusLine(line, builder, resources); if (lineNode != null) { linesNode.appendChild(lineNode); } } if (linesNode.getChildNodes().getLength() > 0) { element.appendChild(linesNode); } return element; } private  XMLBusNetworkUtil(); @SuppressWarnings("checkstyle:npathcomplexity") static Node writeBusNetwork(BusNetwork busNetwork, XMLBuilder builder, XMLResources resources); static BusNetwork readBusNetwork(Element xmlNode, RoadNetwork roadNetwork,
			PathBuilder pathBuilder, XMLResources resources); static final String NODE_BUSHALTS; static final String NODE_BUSHALT; static final String NODE_BUSHUBS; static final String NODE_BUSHUB; static final String NODE_BUSITINERARIES; static final String NODE_BUSITINERARY; static final String NODE_BUSLINES; static final String NODE_BUSLINE; static final String NODE_BUSNETWORK; static final String NODE_BUSSTOPS; static final String NODE_BUSSTOP; static final String NODE_ROADS; static final String ATTR_ROADID; }
@Test public void writeBusNetworkBusNetworkXMLBuilderXMLResources() throws Exception { BusNetwork busNetwork = new BusNetwork(this.network); BusStop stop1 = new BusStop("Stop1"); stop1.setPosition(new GeoLocationPoint(935665.0, 2311306.9)); busNetwork.addBusStop(stop1); BusStop stop2 = new BusStop("Stop2"); stop2.setPosition(new GeoLocationPoint(935665.0, 2311306.9)); busNetwork.addBusStop(stop2); BusLine line1 = new BusLine("Line1"); busNetwork.addBusLine(line1); BusItinerary itinerary1 = new BusItinerary("Itinerary1"); line1.addBusItinerary(itinerary1); RoadSegment road1 = this.network.getRoadSegment(GeoId.valueOf("ccf6f6d64c911eec7e16dd51c3385a28#935646;2311307;935665;2311320")); itinerary1.addRoadSegment(road1); RoadSegment road2 = this.network.getRoadSegment(GeoId.valueOf("67b453c1f5f2a55c3e91aa0468fc861a#935579;2311319;935647;2311395")); itinerary1.addRoadSegment(road2); RoadSegment road3 = this.network.getRoadSegment(GeoId.valueOf("42bc6f398c0552f66138fe953a9a52d8#935561;2311395;935580;2311432")); itinerary1.addRoadSegment(road3); BusItineraryHalt halt1 = itinerary1.addBusHalt("Halt1", BusItineraryHaltType.SYSTEMATIC_STOP); halt1.setBusStop(stop1); BusItineraryHalt halt2 = itinerary1.addBusHalt("Halt1", BusItineraryHaltType.SYSTEMATIC_STOP); halt2.setBusStop(stop2); busNetwork.revalidate(); PathBuilder pathBuilder = new SimplePathBuilder(); XMLResources res = new XMLResources(pathBuilder); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); XMLBuilder xmlBuilder = new XMLBuilder() { @Override public Document getDocument() throws DOMException { return doc; } }; Element root = xmlBuilder.createElement("root"); doc.appendChild(root); Node result = XMLBusNetworkUtil.writeBusNetwork(busNetwork, xmlBuilder, res); root.appendChild(result); String actual; try (StringWriter sw = new StringWriter()) { XMLUtil.writeXML(doc, sw); sw.close(); actual = sw.toString(); } assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + "<root><busNetwork geoId=\"" + busNetwork.getGeoId().toString() + "\" id=\"" + busNetwork.getUUID().toString() + "\" name=\"\"><stops><stop geoId=\"6ebac3fcff724d6ebe66e7f67f77a2e9#935665;2311307;935665;2311307\" " + "id=\"" + stop1.getUUID().toString() + "\" name=\"Stop1\" x=\"935665.0\" y=\"2311307.0\"/>" + "<stop geoId=\"6ebac3fcff724d6ebe66e7f67f77a2e9#935665;2311307;935665;2311307\" " + "id=\"" + stop2.getUUID().toString() + "\" name=\"Stop2\" x=\"935665.0\" " + "y=\"2311307.0\"/></stops><lines><line geoId=\"" + line1.getGeoId().toString() + "\" id=\"" + line1.getUUID().toString() + "\" name=\"Line1\"><itineraries>" + "<itinerary geoId=\"fbee950d00913708b1d6bece061b87c7#935558;2311303;935669;2311435\" " + "id=\"" + itinerary1.getUUID().toString() + "\" name=\"Itinerary1\"><roads>" + "<road geoId=\"ccf6f6d64c911eec7e16dd51c3385a28#935646;2311307;935665;2311320\" " + "id=\"" + road1.getUUID().toString() + "\"/><road " + "geoId=\"67b453c1f5f2a55c3e91aa0468fc861a#935579;2311319;935647;2311395\" " + "id=\"" + road2.getUUID().toString() + "\"/><road " + "geoId=\"42bc6f398c0552f66138fe953a9a52d8#935561;2311395;935580;2311432\" " + "id=\"" + road3.getUUID().toString() + "\"/></roads><halts><halt " + "geoId=\"" + halt1.getGeoId().toString() + "\" id=\"" + halt1.getUUID().toString() + "\" " + "name=\"Halt1\" stopId=\"" + stop1.getUUID().toString() + "\" " + "type=\"SYSTEMATIC_STOP\"/><halt geoId=\"" + halt2.getGeoId().toString() + "\" " + "id=\"" + halt2.getUUID().toString() + "\" name=\"Halt1\" " + "stopId=\"" + stop2.getUUID().toString() + "\" type=\"SYSTEMATIC_STOP\"/>" + "</halts></itinerary></itineraries></line></lines></busNetwork></root>", actual); }