* @author Lutz Gericke
* @author Christian Wiggert, Martin Linkhorst
*
*/
private void importVehicleTypes() throws XPathExpressionException {
SumoManager sumoManager = SumoManager.getInstance();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList vehicleTypes = (NodeList) xpath.evaluate("//vtype", inputSource, XPathConstants.NODESET);
int currentVehicleCount = 0;
int numVehicles = vehicleTypes.getLength();
for (int i = 0; i < vehicleTypes.getLength(); i++) {
org.w3c.dom.Node vehicleType = vehicleTypes.item(i);
NamedNodeMap vehicleAttributes = vehicleType.getAttributes();
// mandatory
String id = vehicleAttributes.getNamedItem("id").getTextContent();
if (sumoManager.getVehicleTypeById(id) != null) {
log.info("INFO: vehicle type \"" + id + "\" is already registered, skipping");
continue;
}
org.w3c.dom.Node lengthNode = vehicleAttributes.getNamedItem("length");
org.w3c.dom.Node maxSpeedNode = vehicleAttributes.getNamedItem("maxspeed");
org.w3c.dom.Node accelNode = vehicleAttributes.getNamedItem("accel");
org.w3c.dom.Node decelNode = vehicleAttributes.getNamedItem("decel");
org.w3c.dom.Node sigmaNode = vehicleAttributes.getNamedItem("sigma");
// optional
org.w3c.dom.Node colorNode = vehicleAttributes.getNamedItem("color");
float r = 255, g = 0, b = 0;
if (colorNode != null) {
String[] colors = colorNode.getTextContent().split(",");
r = Float.parseFloat(colors[0]);
g = Float.parseFloat(colors[1]);
b = Float.parseFloat(colors[2]);
}
NodeList typeChildren = vehicleType.getChildNodes();
for (int j = 0; j < typeChildren.getLength(); j++) {
org.w3c.dom.Node typeChild = typeChildren.item(j);
if (typeChild.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
NamedNodeMap childAttributes = typeChild.getAttributes();
org.w3c.dom.Node childLengthNode = childAttributes.getNamedItem("length");
org.w3c.dom.Node childMaxSpeedNode = childAttributes.getNamedItem("maxspeed");
org.w3c.dom.Node childAccelNode = childAttributes.getNamedItem("accel");
org.w3c.dom.Node childDecelNode = childAttributes.getNamedItem("decel");
org.w3c.dom.Node childSigmaNode = childAttributes.getNamedItem("sigma");
// optional
org.w3c.dom.Node childColorNode = childAttributes.getNamedItem("color");
try {
double length = Double.parseDouble((childLengthNode != null ? childLengthNode : lengthNode).getTextContent());
double maxSpeed = Double.parseDouble((childMaxSpeedNode != null ? childMaxSpeedNode : maxSpeedNode).getTextContent());
double accel = Double.parseDouble((childAccelNode != null ? childAccelNode : accelNode).getTextContent());
double decel = Double.parseDouble((childDecelNode != null ? childDecelNode : decelNode).getTextContent());
double sigma = Double.parseDouble((childSigmaNode != null ? childSigmaNode : sigmaNode).getTextContent());
if (childColorNode != null) {
String[] colors = childColorNode.getTextContent().split(",");
r = Float.parseFloat(colors[0]);
g = Float.parseFloat(colors[1]);
b = Float.parseFloat(colors[2]);
}
SumoVehicleType newVehicleType;
newVehicleType = new SumoVehicleType(id, accel, decel, length, maxSpeed, sigma);
newVehicleType.setColor(r, g, b);
sumoManager.addVehicleType(newVehicleType);
} catch (NullPointerException e) {
System.err.println("missing vehicle type attribute");
e.printStackTrace();
}