Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.54 KB | None | 0 0
  1. package lapr.project.utils;
  2.  
  3. /**
  4.  *
  5.  * @author asus
  6.  */
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import javax.xml.parsers.DocumentBuilder;
  10. import javax.xml.parsers.DocumentBuilderFactory;
  11. import javax.xml.parsers.ParserConfigurationException;
  12. import lapr.project.model.Energy;
  13. import lapr.project.model.Gear;
  14. import lapr.project.model.Motorization;
  15. import lapr.project.model.Project;
  16. import lapr.project.model.Regime;
  17. import lapr.project.model.Throttle;
  18. import lapr.project.model.Vehicle;
  19. import lapr.project.model.VehicleList;
  20. import lapr.project.model.VelocityLimit;
  21. import org.w3c.dom.Document;
  22. import org.w3c.dom.Element;
  23. import org.w3c.dom.Node;
  24. import org.w3c.dom.NodeList;
  25.  
  26. public class ImportVehicle {
  27.  
  28.     private String ROOT_ELEMENT_NAME = "vehicle_list";
  29.  
  30.     private String VEHICLE_ELEMENT_NAME = "vehicle";
  31.  
  32.     private XMLParser parser;
  33.  
  34.     private Project project;
  35.    
  36.    
  37.     private VehicleList vehicleList;
  38.  
  39.     private static final String STR_TYPE = "type";
  40.     private static final String STR_TOLL_CLASS = "toll_class";
  41.     private static final String STR_MOTORIZATION = "motorization";
  42.     private static final String STR_FUEL = "fuel";
  43.     private static final String STR_MASS = "mass";
  44.     private static final String STR_LOAD = "load";
  45.     private static final String STR_DRAG = "drag";
  46.     private static final String STR_FRONTAL_AREA = "frontal_area";
  47.     private static final String STR_RRC = "rrc";
  48.     private static final String STR_WHEEL_SIZE = "wheel_size";
  49.     private static final String STR_VELOCITY_LIMIT_LIST = "velocity_limit_list";
  50.     // add child nodes
  51.     private static final String STR_ENERGY = "energy";
  52.     // add child nodes
  53.  
  54.         public ImportVehicle() {
  55.             vehicleList = new VehicleList();
  56.      }
  57.  
  58.    
  59.    
  60.       public ImportVehicle(Project project) {
  61.        
  62.         this.project = project;
  63.         vehicleList = project.getVehicleList();
  64.     }
  65.     public List<Vehicle> getVehicleList() {
  66.      
  67.         return vehicleList.getVehicleList();
  68.     }
  69.    
  70.     public void addVehicle(Vehicle v){
  71.        
  72.         vehicleList.getVehicleList().add(v);
  73.     }
  74.  
  75.     public void setVehicleList(VehicleList vehicleList) {
  76.        
  77.         this.vehicleList = vehicleList;
  78.     }
  79.  
  80.  
  81.     public void importVehiclesFromXMLFile(String str) throws Exception {
  82.         Motorization m = new Motorization();
  83.  
  84.        
  85.        try {
  86.             parser = new XMLParser();
  87.  
  88.             Node root = parser.readXMLElementFromFile(str);
  89.  
  90.             NodeList vehiclesList = root.getChildNodes();
  91.  
  92.             for (int i = 0; i < vehiclesList.getLength(); i++) {
  93.  
  94.                Vehicle v = new Vehicle();
  95.  
  96.                 Node vehicle = vehiclesList.item(i);
  97.                  
  98.                 String vehicleName = vehicle.getNodeName();
  99.                 System.out.println(vehicleName);
  100.                 NodeList vehicleSpecs = vehicle.getChildNodes();
  101.  
  102.                
  103.                 if(vehicleName.equalsIgnoreCase("vehicle")){
  104.                 v.setName(vehicle.getAttributes().getNamedItem("name").getNodeValue());
  105.                
  106.                 v.setDescription(vehicle.getAttributes().getNamedItem("description").getNodeValue());
  107.                    
  108.                
  109.                 for (int j = 0; j < vehicleSpecs.getLength(); j++) {
  110.  
  111.                     Node tmp = vehicleSpecs.item(i);
  112.  
  113.                        
  114.                            
  115.                     switch (tmp.getNodeName()) {
  116.                         case STR_TYPE:
  117.                             v.setType(tmp.getTextContent());
  118.                             break;
  119.                         case STR_TOLL_CLASS:
  120.                             v.setTollClass(Integer.parseInt(tmp.getTextContent()));
  121.                             break;
  122.                         case STR_MOTORIZATION:
  123.                             m.setType(tmp.getTextContent());
  124.                             break;
  125.                         case STR_FUEL:
  126.                             m.setFuel(tmp.getTextContent());
  127.                             break;
  128.                         case STR_MASS:
  129.                             v.setMass(Float.parseFloat(tmp.getTextContent()));
  130.                             break;
  131.                         case STR_LOAD:
  132.                             v.setLoad(Float.parseFloat(tmp.getTextContent()));
  133.                             break;
  134.                         case STR_DRAG:
  135.                             v.setDragCoefficient(Float.parseFloat(tmp.getTextContent()));
  136.                             break;
  137.                         case STR_FRONTAL_AREA:
  138.                             v.setFrontalArea(Float.parseFloat(tmp.getTextContent()));
  139.                             break;
  140.                         case STR_WHEEL_SIZE:
  141.                             v.setWheelSize(Float.parseFloat(tmp.getTextContent()));
  142.                             break;
  143.                         case STR_VELOCITY_LIMIT_LIST:
  144.                             NodeList velocityLimitList = tmp.getChildNodes();
  145.                             readVelocity(velocityLimitList, v);
  146.                             break;
  147.                         case STR_ENERGY:
  148.                             NodeList energyChildren = tmp.getChildNodes();
  149.                             readEnergy(energyChildren);
  150.                             break;
  151.                         default:
  152.                             break;
  153.                     }
  154.                 }
  155.                v.setMotorization(m);
  156.                System.out.println(v);
  157.                
  158.                 }
  159.            
  160.             }
  161.            
  162.         } catch (Exception e) {
  163.             e.printStackTrace();
  164. }
  165.        
  166.     }
  167.  
  168.    
  169.    
  170.     private void readEnergy(NodeList nList) {
  171.         Gear g; Throttle t; Regime r;
  172.         Energy e = new Energy();
  173.         for (int i = 0; i < nList.getLength(); i++) {
  174.             Node tmp = nList.item(i);
  175.             switch (tmp.getNodeName()) {
  176.                 case "min_rpm":
  177.                     e.setMinRPM(Integer.parseInt(tmp.getTextContent()));
  178.                     break;
  179.                 case "max_rpm":
  180.                     e.setMaxRPM(Integer.parseInt(tmp.getTextContent()));
  181.                     break;
  182.                 case "final_drive_ratio":
  183.                     e.setFinalDriveRatio(Integer.parseInt(tmp.getTextContent()));
  184.                     break;
  185.                 case "gear_list":
  186.                     NodeList gearList = tmp.getChildNodes();
  187.                     for (int j = 0; j < gearList.getLength(); j++) {
  188.                         g = new Gear();
  189.                         Node tmpGear = gearList.item(j);
  190.                         g.setRatio(Float.parseFloat(tmpGear.getFirstChild().getTextContent()));
  191.                         g.setId(Integer.parseInt(tmpGear.getAttributes().getNamedItem("id").getTextContent()));
  192.                     }
  193.                     break;
  194.                 case "throttle_list":
  195.                     NodeList throttleList = tmp.getChildNodes();
  196.                     for (int j = 0; j < throttleList.getLength(); j++) {
  197.                         t = new Throttle();
  198.                         Node tmpThrottle = throttleList.item(j);
  199.                         t.setId(Integer.parseInt(tmpThrottle.getAttributes().getNamedItem("id").getTextContent()));
  200.                         NodeList regimeList = tmpThrottle.getChildNodes();
  201.                         for (int k = 0; k < regimeList.getLength(); k++) {
  202.                             r = new Regime();
  203.                             Node tmpRegime = regimeList.item(k);
  204.                             NodeList regimeSpecs = tmpRegime.getChildNodes();
  205.                             for (int l = 0; l < regimeSpecs.getLength(); l++) {
  206.                                 Node regimeSpec = regimeSpecs.item(l);
  207.                                 switch (regimeSpec.getNodeName()) {
  208.                                     case "torque":
  209.                                         r.setTorque(Integer.parseInt(regimeSpec.getTextContent()));
  210.                                         break;
  211.                                     case "rpm_low":
  212.                                         r.setLowRPM(Integer.parseInt(regimeSpec.getTextContent()));
  213.                                         break;
  214.                                     case "rpm_high":
  215.                                         r.setHighRPM(Integer.parseInt(regimeSpec.getTextContent()));
  216.                                         break;
  217.                                     case "SFC":
  218.                                         r.setSfc(Integer.parseInt(regimeSpec.getTextContent()));
  219.                                         break;
  220.                                     default:
  221.                                         throw new AssertionError();
  222.                                 }
  223.                             }
  224.                         }
  225.                     }
  226.                     break;
  227.                 default:
  228.                     break;
  229.             }
  230.         }
  231.     }
  232.    
  233.     private void readVelocity(NodeList nList,Vehicle v) {
  234.         VelocityLimit vel;
  235.         for (int i = 0; i < nList.getLength(); i++) {
  236.             vel = new VelocityLimit();
  237.             Node tmp = nList.item(i);
  238.             switch (tmp.getNodeName()) {
  239.                 case "segment_type":
  240.                     vel.setSegment_type(tmp.getTextContent());
  241.                     break;
  242.                 case "limit":
  243.                     vel.setLimit(Integer.parseInt(tmp.getTextContent()));
  244.                     break;
  245.                 default:
  246.                 break;
  247.             }
  248.             v.addVelocityLimit(vel);
  249.         }
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement