Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lapr.project.utils;
- /**
- *
- * @author asus
- */
- import java.util.ArrayList;
- import java.util.List;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import lapr.project.model.Energy;
- import lapr.project.model.Gear;
- import lapr.project.model.Motorization;
- import lapr.project.model.Project;
- import lapr.project.model.Regime;
- import lapr.project.model.Throttle;
- import lapr.project.model.Vehicle;
- import lapr.project.model.VehicleList;
- import lapr.project.model.VelocityLimit;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- public class ImportVehicle {
- private String ROOT_ELEMENT_NAME = "vehicle_list";
- private String VEHICLE_ELEMENT_NAME = "vehicle";
- private XMLParser parser;
- private Project project;
- private VehicleList vehicleList;
- private static final String STR_TYPE = "type";
- private static final String STR_TOLL_CLASS = "toll_class";
- private static final String STR_MOTORIZATION = "motorization";
- private static final String STR_FUEL = "fuel";
- private static final String STR_MASS = "mass";
- private static final String STR_LOAD = "load";
- private static final String STR_DRAG = "drag";
- private static final String STR_FRONTAL_AREA = "frontal_area";
- private static final String STR_RRC = "rrc";
- private static final String STR_WHEEL_SIZE = "wheel_size";
- private static final String STR_VELOCITY_LIMIT_LIST = "velocity_limit_list";
- // add child nodes
- private static final String STR_ENERGY = "energy";
- // add child nodes
- public ImportVehicle() {
- vehicleList = new VehicleList();
- }
- public ImportVehicle(Project project) {
- this.project = project;
- vehicleList = project.getVehicleList();
- }
- public List<Vehicle> getVehicleList() {
- return vehicleList.getVehicleList();
- }
- public void addVehicle(Vehicle v){
- vehicleList.getVehicleList().add(v);
- }
- public void setVehicleList(VehicleList vehicleList) {
- this.vehicleList = vehicleList;
- }
- public void importVehiclesFromXMLFile(String str) throws Exception {
- Motorization m = new Motorization();
- try {
- parser = new XMLParser();
- Node root = parser.readXMLElementFromFile(str);
- NodeList vehiclesList = root.getChildNodes();
- for (int i = 0; i < vehiclesList.getLength(); i++) {
- Vehicle v = new Vehicle();
- Node vehicle = vehiclesList.item(i);
- String vehicleName = vehicle.getNodeName();
- System.out.println(vehicleName);
- NodeList vehicleSpecs = vehicle.getChildNodes();
- if(vehicleName.equalsIgnoreCase("vehicle")){
- v.setName(vehicle.getAttributes().getNamedItem("name").getNodeValue());
- v.setDescription(vehicle.getAttributes().getNamedItem("description").getNodeValue());
- for (int j = 0; j < vehicleSpecs.getLength(); j++) {
- Node tmp = vehicleSpecs.item(i);
- switch (tmp.getNodeName()) {
- case STR_TYPE:
- v.setType(tmp.getTextContent());
- break;
- case STR_TOLL_CLASS:
- v.setTollClass(Integer.parseInt(tmp.getTextContent()));
- break;
- case STR_MOTORIZATION:
- m.setType(tmp.getTextContent());
- break;
- case STR_FUEL:
- m.setFuel(tmp.getTextContent());
- break;
- case STR_MASS:
- v.setMass(Float.parseFloat(tmp.getTextContent()));
- break;
- case STR_LOAD:
- v.setLoad(Float.parseFloat(tmp.getTextContent()));
- break;
- case STR_DRAG:
- v.setDragCoefficient(Float.parseFloat(tmp.getTextContent()));
- break;
- case STR_FRONTAL_AREA:
- v.setFrontalArea(Float.parseFloat(tmp.getTextContent()));
- break;
- case STR_WHEEL_SIZE:
- v.setWheelSize(Float.parseFloat(tmp.getTextContent()));
- break;
- case STR_VELOCITY_LIMIT_LIST:
- NodeList velocityLimitList = tmp.getChildNodes();
- readVelocity(velocityLimitList, v);
- break;
- case STR_ENERGY:
- NodeList energyChildren = tmp.getChildNodes();
- readEnergy(energyChildren);
- break;
- default:
- break;
- }
- }
- v.setMotorization(m);
- System.out.println(v);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void readEnergy(NodeList nList) {
- Gear g; Throttle t; Regime r;
- Energy e = new Energy();
- for (int i = 0; i < nList.getLength(); i++) {
- Node tmp = nList.item(i);
- switch (tmp.getNodeName()) {
- case "min_rpm":
- e.setMinRPM(Integer.parseInt(tmp.getTextContent()));
- break;
- case "max_rpm":
- e.setMaxRPM(Integer.parseInt(tmp.getTextContent()));
- break;
- case "final_drive_ratio":
- e.setFinalDriveRatio(Integer.parseInt(tmp.getTextContent()));
- break;
- case "gear_list":
- NodeList gearList = tmp.getChildNodes();
- for (int j = 0; j < gearList.getLength(); j++) {
- g = new Gear();
- Node tmpGear = gearList.item(j);
- g.setRatio(Float.parseFloat(tmpGear.getFirstChild().getTextContent()));
- g.setId(Integer.parseInt(tmpGear.getAttributes().getNamedItem("id").getTextContent()));
- }
- break;
- case "throttle_list":
- NodeList throttleList = tmp.getChildNodes();
- for (int j = 0; j < throttleList.getLength(); j++) {
- t = new Throttle();
- Node tmpThrottle = throttleList.item(j);
- t.setId(Integer.parseInt(tmpThrottle.getAttributes().getNamedItem("id").getTextContent()));
- NodeList regimeList = tmpThrottle.getChildNodes();
- for (int k = 0; k < regimeList.getLength(); k++) {
- r = new Regime();
- Node tmpRegime = regimeList.item(k);
- NodeList regimeSpecs = tmpRegime.getChildNodes();
- for (int l = 0; l < regimeSpecs.getLength(); l++) {
- Node regimeSpec = regimeSpecs.item(l);
- switch (regimeSpec.getNodeName()) {
- case "torque":
- r.setTorque(Integer.parseInt(regimeSpec.getTextContent()));
- break;
- case "rpm_low":
- r.setLowRPM(Integer.parseInt(regimeSpec.getTextContent()));
- break;
- case "rpm_high":
- r.setHighRPM(Integer.parseInt(regimeSpec.getTextContent()));
- break;
- case "SFC":
- r.setSfc(Integer.parseInt(regimeSpec.getTextContent()));
- break;
- default:
- throw new AssertionError();
- }
- }
- }
- }
- break;
- default:
- break;
- }
- }
- }
- private void readVelocity(NodeList nList,Vehicle v) {
- VelocityLimit vel;
- for (int i = 0; i < nList.getLength(); i++) {
- vel = new VelocityLimit();
- Node tmp = nList.item(i);
- switch (tmp.getNodeName()) {
- case "segment_type":
- vel.setSegment_type(tmp.getTextContent());
- break;
- case "limit":
- vel.setLimit(Integer.parseInt(tmp.getTextContent()));
- break;
- default:
- break;
- }
- v.addVelocityLimit(vel);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement