bug fixes and rocket optimization
[debian/openrocket] / src / net / sf / openrocket / file / openrocket / OpenRocketLoader.java
index d9b57a8333e237274dfbca8ca01d2bfcda5ba7c7..a487e48c3c6988b76b96bb1b1b1f9b6356437757 100644 (file)
@@ -14,15 +14,18 @@ import net.sf.openrocket.aerodynamics.WarningSet;
 import net.sf.openrocket.database.Databases;
 import net.sf.openrocket.document.OpenRocketDocument;
 import net.sf.openrocket.document.Simulation;
-import net.sf.openrocket.document.StorageOptions;
 import net.sf.openrocket.document.Simulation.Status;
+import net.sf.openrocket.document.StorageOptions;
 import net.sf.openrocket.file.RocketLoadException;
 import net.sf.openrocket.file.RocketLoader;
 import net.sf.openrocket.file.simplesax.ElementHandler;
 import net.sf.openrocket.file.simplesax.PlainTextHandler;
 import net.sf.openrocket.file.simplesax.SimpleSAX;
+import net.sf.openrocket.logging.LogHelper;
 import net.sf.openrocket.material.Material;
 import net.sf.openrocket.motor.Motor;
+import net.sf.openrocket.motor.MotorDigest;
+import net.sf.openrocket.motor.ThrustCurveMotor;
 import net.sf.openrocket.rocketcomponent.BodyComponent;
 import net.sf.openrocket.rocketcomponent.BodyTube;
 import net.sf.openrocket.rocketcomponent.Bulkhead;
@@ -32,7 +35,9 @@ import net.sf.openrocket.rocketcomponent.Clusterable;
 import net.sf.openrocket.rocketcomponent.EllipticalFinSet;
 import net.sf.openrocket.rocketcomponent.EngineBlock;
 import net.sf.openrocket.rocketcomponent.ExternalComponent;
+import net.sf.openrocket.rocketcomponent.ExternalComponent.Finish;
 import net.sf.openrocket.rocketcomponent.FinSet;
+import net.sf.openrocket.rocketcomponent.FinSet.TabRelativePosition;
 import net.sf.openrocket.rocketcomponent.FreeformFinSet;
 import net.sf.openrocket.rocketcomponent.IllegalFinPointException;
 import net.sf.openrocket.rocketcomponent.InnerTube;
@@ -49,6 +54,7 @@ import net.sf.openrocket.rocketcomponent.ReferenceType;
 import net.sf.openrocket.rocketcomponent.RingComponent;
 import net.sf.openrocket.rocketcomponent.Rocket;
 import net.sf.openrocket.rocketcomponent.RocketComponent;
+import net.sf.openrocket.rocketcomponent.RocketComponent.Position;
 import net.sf.openrocket.rocketcomponent.ShockCord;
 import net.sf.openrocket.rocketcomponent.Stage;
 import net.sf.openrocket.rocketcomponent.Streamer;
@@ -58,15 +64,13 @@ import net.sf.openrocket.rocketcomponent.ThicknessRingComponent;
 import net.sf.openrocket.rocketcomponent.Transition;
 import net.sf.openrocket.rocketcomponent.TrapezoidFinSet;
 import net.sf.openrocket.rocketcomponent.TubeCoupler;
-import net.sf.openrocket.rocketcomponent.ExternalComponent.Finish;
-import net.sf.openrocket.rocketcomponent.FinSet.TabRelativePosition;
-import net.sf.openrocket.rocketcomponent.RocketComponent.Position;
 import net.sf.openrocket.simulation.FlightData;
 import net.sf.openrocket.simulation.FlightDataBranch;
 import net.sf.openrocket.simulation.FlightDataType;
 import net.sf.openrocket.simulation.FlightEvent;
-import net.sf.openrocket.simulation.GUISimulationConditions;
 import net.sf.openrocket.simulation.FlightEvent.Type;
+import net.sf.openrocket.simulation.GUISimulationConditions;
+import net.sf.openrocket.startup.Application;
 import net.sf.openrocket.unit.UnitGroup;
 import net.sf.openrocket.util.BugException;
 import net.sf.openrocket.util.Coordinate;
@@ -87,12 +91,15 @@ import org.xml.sax.SAXException;
  * 
  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
  */
-
 public class OpenRocketLoader extends RocketLoader {
+       private static final LogHelper log = Application.getLogger();
+       
        
        @Override
        public OpenRocketDocument loadFromStream(InputStream source) throws RocketLoadException,
                        IOException {
+               log.info("Loading .ork file");
+               
                InputSource xmlSource = new InputSource(source);
                OpenRocketHandler handler = new OpenRocketHandler();
                
@@ -100,6 +107,7 @@ public class OpenRocketLoader extends RocketLoader {
                try {
                        SimpleSAX.readXML(xmlSource, handler, warnings);
                } catch (SAXException e) {
+                       log.warn("Malformed XML in input");
                        throw new RocketLoadException("Malformed XML in input.", e);
                }
                
@@ -139,6 +147,7 @@ public class OpenRocketLoader extends RocketLoader {
                doc.getDefaultStorageOptions().setExplicitlySet(false);
                
                doc.clearUndo();
+               log.info("Loading done");
                return doc;
        }
        
@@ -149,7 +158,7 @@ public class OpenRocketLoader extends RocketLoader {
 class DocumentConfig {
        
        /* Remember to update OpenRocketSaver as well! */
-       public static final String[] SUPPORTED_VERSIONS = { "0.9", "1.0", "1.1" };
+       public static final String[] SUPPORTED_VERSIONS = { "0.9", "1.0", "1.1", "1.2" };
        
 
        ////////  Component constructors
@@ -235,9 +244,9 @@ class DocumentConfig {
                
                // BodyTube
                setters.put("BodyTube:radius", new DoubleSetter(
-                               Reflection.findMethodStatic(BodyTube.class, "setRadius", double.class),
+                               Reflection.findMethodStatic(BodyTube.class, "setOuterRadius", double.class),
                                "auto",
-                               Reflection.findMethodStatic(BodyTube.class, "setRadiusAutomatic", boolean.class)));
+                               Reflection.findMethodStatic(BodyTube.class, "setOuterRadiusAutomatic", boolean.class)));
                
                // Transition
                setters.put("Transition:shape", new EnumSetter<Transition.Shape>(
@@ -320,7 +329,7 @@ class DocumentConfig {
                
                // LaunchLug
                setters.put("LaunchLug:radius", new DoubleSetter(
-                               Reflection.findMethodStatic(LaunchLug.class, "setRadius", double.class)));
+                               Reflection.findMethodStatic(LaunchLug.class, "setOuterRadius", double.class)));
                setters.put("LaunchLug:length", new DoubleSetter(
                                Reflection.findMethodStatic(LaunchLug.class, "setLength", double.class)));
                setters.put("LaunchLug:thickness", new DoubleSetter(
@@ -744,7 +753,6 @@ class ComponentParameterHandler extends ElementHandler {
                        Setter s = DocumentConfig.setters.get(setterKey);
                        if (s != null) {
                                // Setter found
-                               System.out.println("Calling with key " + setterKey);
                                s.set(component, content, attributes, warnings);
                                break;
                        }
@@ -969,6 +977,7 @@ class MotorHandler extends ElementHandler {
        private Motor.Type type = null;
        private String manufacturer = null;
        private String designation = null;
+       private String digest = null;
        private double diameter = Double.NaN;
        private double length = Double.NaN;
        private double delay = Double.NaN;
@@ -988,24 +997,68 @@ class MotorHandler extends ElementHandler {
                        warnings.add(Warning.fromString("No motor specified, ignoring."));
                        return null;
                }
-               Motor[] motors = Databases.findMotors(type, manufacturer, designation, diameter, length);
-               if (motors.length == 0) {
+               
+               List<ThrustCurveMotor> motors = Application.getMotorSetDatabase().findMotors(type, manufacturer,
+                               designation, diameter, length);
+               
+               // No motors
+               if (motors.size() == 0) {
                        String str = "No motor with designation '" + designation + "'";
                        if (manufacturer != null)
                                str += " for manufacturer '" + manufacturer + "'";
-                       warnings.add(Warning.fromString(str + " found."));
+                       str += " found.";
+                       warnings.add(str);
                        return null;
                }
-               if (motors.length > 1) {
+               
+               // One motor
+               if (motors.size() == 1) {
+                       ThrustCurveMotor m = motors.get(0);
+                       if (digest != null && !MotorDigest.digestMotor(m).equals(digest)) {
+                               String str = "Motor with designation '" + designation + "'";
+                               if (manufacturer != null)
+                                       str += " for manufacturer '" + manufacturer + "'";
+                               str += " has differing thrust curve than the original.";
+                               warnings.add(str);
+                       }
+                       return m;
+               }
+               
+               // Multiple motors, check digest for which one to use
+               if (digest != null) {
+                       
+                       // Check for motor with correct digest
+                       for (ThrustCurveMotor m : motors) {
+                               if (MotorDigest.digestMotor(m).equals(digest)) {
+                                       return m;
+                               }
+                       }
+                       String str = "Motor with designation '" + designation + "'";
+                       if (manufacturer != null)
+                               str += " for manufacturer '" + manufacturer + "'";
+                       str += " has differing thrust curve than the original.";
+                       warnings.add(str);
+                       
+               } else {
+                       
+                       // No digest, check for preferred digest (OpenRocket <= 1.1.0)
+                       // TODO: MEDIUM: This should only be done for document versions 1.1 and below
+                       for (ThrustCurveMotor m : motors) {
+                               if (PreferredMotorDigests.DIGESTS.contains(MotorDigest.digestMotor(m))) {
+                                       return m;
+                               }
+                       }
+                       
                        String str = "Multiple motors with designation '" + designation + "'";
                        if (manufacturer != null)
                                str += " for manufacturer '" + manufacturer + "'";
-                       warnings.add(Warning.fromString(str + " found, one chosen arbitrarily."));
+                       str += " found, one chosen arbitrarily.";
+                       warnings.add(str);
+                       
                }
-               return motors[0];
+               return motors.get(0);
        }
        
-       
        /**
         * Return the delay to use for the motor.
         */
@@ -1029,7 +1082,7 @@ class MotorHandler extends ElementHandler {
                        // Motor type
                        type = null;
                        for (Motor.Type t : Motor.Type.values()) {
-                               if (t.name().toLowerCase().equals(content)) {
+                               if (t.name().toLowerCase().equals(content.trim())) {
                                        type = t;
                                        break;
                                }
@@ -1041,19 +1094,24 @@ class MotorHandler extends ElementHandler {
                } else if (element.equals("manufacturer")) {
                        
                        // Manufacturer
-                       manufacturer = content;
+                       manufacturer = content.trim();
                        
                } else if (element.equals("designation")) {
                        
                        // Designation
-                       designation = content;
+                       designation = content.trim();
+                       
+               } else if (element.equals("digest")) {
+                       
+                       // Digest
+                       digest = content.trim();
                        
                } else if (element.equals("diameter")) {
                        
                        // Diameter
                        diameter = Double.NaN;
                        try {
-                               diameter = Double.parseDouble(content);
+                               diameter = Double.parseDouble(content.trim());
                        } catch (NumberFormatException e) {
                                // Ignore
                        }
@@ -1066,7 +1124,7 @@ class MotorHandler extends ElementHandler {
                        // Length
                        length = Double.NaN;
                        try {
-                               length = Double.parseDouble(content);
+                               length = Double.parseDouble(content.trim());
                        } catch (NumberFormatException ignore) {
                        }
                        
@@ -1082,7 +1140,7 @@ class MotorHandler extends ElementHandler {
                                delay = Motor.PLUGGED;
                        } else {
                                try {
-                                       delay = Double.parseDouble(content);
+                                       delay = Double.parseDouble(content.trim());
                                } catch (NumberFormatException ignore) {
                                }
                                
@@ -1475,8 +1533,9 @@ class FlightDataHandler extends ElementHandler {
                        } catch (NumberFormatException ignore) {
                        }
                        
+                       // TODO: HIGH: Store and load launchRodVelocity
                        data = new FlightData(maxAltitude, maxVelocity, maxAcceleration, maxMach,
-                                       timeToApogee, flightTime, groundHitVelocity);
+                                       timeToApogee, flightTime, groundHitVelocity, Double.NaN);
                }
                
                data.getWarningSet().addAll(warningSet);
@@ -1610,6 +1669,7 @@ class StringSetter implements Setter {
                setMethod = set;
        }
        
+       @Override
        public void set(RocketComponent c, String s, HashMap<String, String> attributes,
                        WarningSet warnings) {
                setMethod.invoke(c, s);
@@ -1624,6 +1684,7 @@ class IntSetter implements Setter {
                setMethod = set;
        }
        
+       @Override
        public void set(RocketComponent c, String s, HashMap<String, String> attributes,
                        WarningSet warnings) {
                try {
@@ -1644,6 +1705,7 @@ class BooleanSetter implements Setter {
                setMethod = set;
        }
        
+       @Override
        public void set(RocketComponent c, String s, HashMap<String, String> attributes,
                        WarningSet warnings) {
                
@@ -1708,6 +1770,7 @@ class DoubleSetter implements Setter {
        }
        
        
+       @Override
        public void set(RocketComponent c, String s, HashMap<String, String> attributes,
                        WarningSet warnings) {
                
@@ -1739,6 +1802,7 @@ class OverrideSetter implements Setter {
                this.enabledMethod = enabledMethod;
        }
        
+       @Override
        public void set(RocketComponent c, String s, HashMap<String, String> attributes,
                        WarningSet warnings) {
                
@@ -1785,6 +1849,7 @@ class ColorSetter implements Setter {
                setMethod = set;
        }
        
+       @Override
        public void set(RocketComponent c, String s, HashMap<String, String> attributes,
                        WarningSet warnings) {
                
@@ -1832,6 +1897,7 @@ class MaterialSetter implements Setter {
                this.type = type;
        }
        
+       @Override
        public void set(RocketComponent c, String name, HashMap<String, String> attributes,
                        WarningSet warnings) {
                
@@ -1888,6 +1954,7 @@ class MaterialSetter implements Setter {
 
 class PositionSetter implements Setter {
        
+       @Override
        public void set(RocketComponent c, String value, HashMap<String, String> attributes,
                        WarningSet warnings) {
                
@@ -1963,6 +2030,7 @@ class FinTabPositionSetter extends DoubleSetter {
 
 class ClusterConfigurationSetter implements Setter {
        
+       @Override
        public void set(RocketComponent component, String value, HashMap<String, String> attributes,
                        WarningSet warnings) {