X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=core%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Ffile%2Fopenrocket%2Fimportt%2FOpenRocketLoader.java;h=d6c4e249347ded9bdac0d6a8c8a0f9361ff80b24;hb=77f2457dc781c8c517ddef157c18491ad770f6c6;hp=6a112f55d63df91dd353e7f7d99b83a1d9331e9e;hpb=9c8be94f5fb1a088eeb030dee26a5ec92415c37c;p=debian%2Fopenrocket diff --git a/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java b/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java index 6a112f55..d6c4e249 100644 --- a/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java +++ b/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java @@ -67,6 +67,7 @@ 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.simulation.customexpression.CustomExpression; import net.sf.openrocket.simulation.FlightData; import net.sf.openrocket.simulation.FlightDataBranch; import net.sf.openrocket.simulation.FlightDataType; @@ -648,6 +649,7 @@ class OpenRocketContentHandler extends AbstractElementHandler { private boolean rocketDefined = false; private boolean simulationsDefined = false; + private boolean datatypesDefined = false; public OpenRocketContentHandler(DocumentLoadingContext context) { this.context = context; @@ -655,7 +657,6 @@ class OpenRocketContentHandler extends AbstractElementHandler { this.doc = new OpenRocketDocument(rocket); } - public OpenRocketDocument getDocument() { if (!rocketDefined) return null; @@ -676,6 +677,15 @@ class OpenRocketContentHandler extends AbstractElementHandler { rocketDefined = true; return new ComponentParameterHandler(rocket, context); } + + if (element.equals("datatypes")){ + if (datatypesDefined) { + warnings.add(Warning.fromString("Multiple datatype blocks. Ignoring later ones.")); + return null; + } + datatypesDefined = true; + return new DatatypeHandler(this, context); + } if (element.equals("simulations")) { if (simulationsDefined) { @@ -696,6 +706,90 @@ class OpenRocketContentHandler extends AbstractElementHandler { +class DatatypeHandler extends AbstractElementHandler { + private final DocumentLoadingContext context; + private final OpenRocketContentHandler contentHandler; + private CustomExpressionHandler customExpressionHandler = null; + + public DatatypeHandler(OpenRocketContentHandler contentHandler, DocumentLoadingContext context) { + this.context = context; + this.contentHandler = contentHandler; + } + + @Override + public ElementHandler openElement(String element, + HashMap attributes, WarningSet warnings) + throws SAXException { + + if (element.equals("type") && attributes.get("source").equals("customexpression") ){ + customExpressionHandler = new CustomExpressionHandler(contentHandler, context); + return customExpressionHandler; + } + else { + warnings.add(Warning.fromString("Unknown datatype " + element + " defined, ignoring")); + } + + return this; + } + + @Override + public void closeElement(String element, HashMap attributes, String content, WarningSet warnings) throws SAXException { + attributes.remove("source"); + super.closeElement(element, attributes, content, warnings); + + if (customExpressionHandler != null){ + contentHandler.getDocument().addCustomExpression(customExpressionHandler.currentExpression); + } + + } + +} + +class CustomExpressionHandler extends AbstractElementHandler{ + private final DocumentLoadingContext context; + private final OpenRocketContentHandler contentHandler; + public CustomExpression currentExpression; + + public CustomExpressionHandler(OpenRocketContentHandler contentHandler, DocumentLoadingContext context) { + this.context = context; + this.contentHandler = contentHandler; + currentExpression = new CustomExpression(contentHandler.getDocument()); + + } + + @Override + public ElementHandler openElement(String element, + HashMap attributes, WarningSet warnings) + throws SAXException { + + return this; + } + + @Override + public void closeElement(String element, HashMap attributes, + String content, WarningSet warnings) throws SAXException { + + if (element.equals("type")) { + contentHandler.getDocument().addCustomExpression(currentExpression); + } + + if (element.equals("name")) { + currentExpression.setName(content); + } + + if (element.equals("symbol")) { + currentExpression.setSymbol(content); + } + + if (element.equals("unit") && attributes.get("unittype").equals("auto")) { + currentExpression.setUnit(content); + } + + if (element.equals("expression")){ + currentExpression.setExpression(content); + } + } +} /** * A handler that creates components from the corresponding elements. The control of the @@ -1201,6 +1295,7 @@ class SimulationsHandler extends AbstractElementHandler { } class SingleSimulationHandler extends AbstractElementHandler { + private final DocumentLoadingContext context; private final OpenRocketDocument doc; @@ -1209,7 +1304,7 @@ class SingleSimulationHandler extends AbstractElementHandler { private SimulationConditionsHandler conditionHandler; private FlightDataHandler dataHandler; - + private final List listeners = new ArrayList(); public SingleSimulationHandler(OpenRocketDocument doc, DocumentLoadingContext context) { @@ -1217,8 +1312,10 @@ class SingleSimulationHandler extends AbstractElementHandler { this.context = context; } - - + public OpenRocketDocument getDocument(){ + return doc; + } + @Override public ElementHandler openElement(String element, HashMap attributes, WarningSet warnings) { @@ -1230,7 +1327,7 @@ class SingleSimulationHandler extends AbstractElementHandler { conditionHandler = new SimulationConditionsHandler(doc.getRocket(), context); return conditionHandler; } else if (element.equals("flightdata")) { - dataHandler = new FlightDataHandler(context); + dataHandler = new FlightDataHandler(this, context); return dataHandler; } else { warnings.add("Unknown element '" + element + "', ignoring."); @@ -1288,13 +1385,11 @@ class SingleSimulationHandler extends AbstractElementHandler { Simulation simulation = new Simulation(doc.getRocket(), status, name, conditions, listeners, data); - + doc.addSimulation(simulation); } } - - - + class SimulationConditionsHandler extends AbstractElementHandler { private final DocumentLoadingContext context; private SimulationOptions conditions; @@ -1478,12 +1573,14 @@ class FlightDataHandler extends AbstractElementHandler { private FlightDataBranchHandler dataHandler; private WarningSet warningSet = new WarningSet(); private List branches = new ArrayList(); - + + private SingleSimulationHandler simHandler; private FlightData data; - public FlightDataHandler(DocumentLoadingContext context) { + public FlightDataHandler(SingleSimulationHandler simHandler, DocumentLoadingContext context) { this.context = context; + this.simHandler = simHandler; } public FlightData getFlightData() { @@ -1502,8 +1599,9 @@ class FlightDataHandler extends AbstractElementHandler { warnings.add("Illegal flight data definition, ignoring."); return null; } - dataHandler = new FlightDataBranchHandler(attributes.get("name"), - attributes.get("types"), context); + dataHandler = new FlightDataBranchHandler( attributes.get("name"), + attributes.get("types"), + simHandler, context); return dataHandler; } @@ -1599,18 +1697,59 @@ class FlightDataBranchHandler extends AbstractElementHandler { private final DocumentLoadingContext context; private final FlightDataType[] types; private final FlightDataBranch branch; - - public FlightDataBranchHandler(String name, String typeList, DocumentLoadingContext context) { + + private static final LogHelper log = Application.getLogger(); + private final SingleSimulationHandler simHandler; + + public FlightDataBranchHandler(String name, String typeList, SingleSimulationHandler simHandler, DocumentLoadingContext context) { + this.simHandler = simHandler; this.context = context; String[] split = typeList.split(","); types = new FlightDataType[split.length]; for (int i = 0; i < split.length; i++) { - types[i] = FlightDataType.getType(split[i], UnitGroup.UNITS_NONE); + String typeName = split[i]; + FlightDataType matching = findFlightDataType(typeName); + types[i] = matching; + //types[i] = FlightDataType.getType(typeName, matching.getSymbol(), matching.getUnitGroup()); } // TODO: LOW: May throw an IllegalArgumentException branch = new FlightDataBranch(name, types); } + + // Find the full flight data type given name only + // Note: this way of doing it requires that custom expressions always come before flight data in the file, + // not the nicest but this is always the case anyway. + private FlightDataType findFlightDataType(String name){ + + // Kevins version with lookup by key. Not using right now + /* + if ( key != null ) { + for (FlightDataType t : FlightDataType.ALL_TYPES){ + if (t.getKey().equals(key) ){ + return t; + } + } + } + */ + + // Look in built in types + for (FlightDataType t : FlightDataType.ALL_TYPES){ + if (t.getName().equals(name) ){ + return t; + } + } + + // Look in custom expressions + for (CustomExpression exp : simHandler.getDocument().getCustomExpressions()){ + if (exp.getName().equals(name) ){ + return exp.getType(); + } + } + + log.warn("Could not find the flight data type '"+name+"' used in the XML file. Substituted type with unknown symbol and units."); + return FlightDataType.getType(name, "Unknown", UnitGroup.UNITS_NONE); + } public FlightDataBranch getBranch() { branch.immute(); @@ -1939,134 +2078,137 @@ class ColorSetter implements Setter { ////ComponentPresetSetter - sets a ComponentPreset value class ComponentPresetSetter implements Setter { - private final Reflection.Method setMethod; - - public ComponentPresetSetter(Reflection.Method set) { - this.setMethod = set; - } +private final Reflection.Method setMethod; - @Override - public void set(RocketComponent c, String name, HashMap attributes, - WarningSet warnings) { -// FIXME - probably need more data in the warning messages - like what component preset... - String manufacturerName = attributes.get("manufacturer"); - if ( manufacturerName == null ) { - warnings.add(Warning.fromString("Invalid ComponentPreset, no manufacturer specified. Ignored")); - return; - } +public ComponentPresetSetter(Reflection.Method set) { + this.setMethod = set; +} - String productNo = attributes.get("partno"); - if ( productNo == null ) { - warnings.add(Warning.fromString("Invalid ComponentPreset, no partno specified. Ignored")); - return; - } +@Override +public void set(RocketComponent c, String name, HashMap attributes, + WarningSet warnings) { + // FIXME - probably need more data in the warning messages - like what component preset... + String manufacturerName = attributes.get("manufacturer"); + if ( manufacturerName == null ) { + warnings.add(Warning.fromString("Invalid ComponentPreset, no manufacturer specified. Ignored")); + return; + } - String digest = attributes.get("digest"); - if ( digest == null ) { - warnings.add(Warning.fromString("Invalid ComponentPreset, no digest specified.")); - } + String productNo = attributes.get("partno"); + if ( productNo == null ) { + warnings.add(Warning.fromString("Invalid ComponentPreset, no partno specified. Ignored")); + return; + } - String type = attributes.get("type"); - if ( type == null ) { - warnings.add(Warning.fromString("Invalid ComponentPreset, no type specified.")); - } + String digest = attributes.get("digest"); + if ( digest == null ) { + warnings.add(Warning.fromString("Invalid ComponentPreset, no digest specified.")); + } - List presets = Application.getComponentPresetDao().find( manufacturerName, productNo ); + String type = attributes.get("type"); + if ( type == null ) { + warnings.add(Warning.fromString("Invalid ComponentPreset, no type specified.")); + } - ComponentPreset matchingPreset = null; + List presets = Application.getComponentPresetDao().find( manufacturerName, productNo ); - for( ComponentPreset preset: presets ) { - if ( digest != null && preset.getDigest().equals(digest) ) { - // Found one with matching digest. Take it. - matchingPreset = preset; - break; - } - if ( type != null && preset.getType().name().equals(type) && matchingPreset != null) { - // Found the first one with matching type. - matchingPreset = preset; - } - } + ComponentPreset matchingPreset = null; - // Was any found? - if ( matchingPreset == null ) { - warnings.add(Warning.fromString("No matching ComponentPreset found")); - return; + for( ComponentPreset preset: presets ) { + if ( digest != null && preset.getDigest().equals(digest) ) { + // Found one with matching digest. Take it. + matchingPreset = preset; + break; } - - if ( digest != null && !matchingPreset.getDigest().equals(digest) ) { - warnings.add(Warning.fromString("ComponentPreset has wrong digest")); + if ( type != null && preset.getType().name().equals(type) && matchingPreset != null) { + // Found the first one with matching type. + matchingPreset = preset; } + } + + // Was any found? + if ( matchingPreset == null ) { + warnings.add(Warning.fromString("No matching ComponentPreset found " + manufacturerName + " " + productNo)); + return; + } - setMethod.invoke(c, matchingPreset); + if ( digest != null && !matchingPreset.getDigest().equals(digest) ) { + warnings.add(Warning.fromString("ComponentPreset has wrong digest")); } + + setMethod.invoke(c, matchingPreset); +} } ////MaterialSetter - sets a Material value class MaterialSetter implements Setter { - private final Reflection.Method setMethod; - private final Material.Type type; +private final Reflection.Method setMethod; +private final Material.Type type; - public MaterialSetter(Reflection.Method set, Material.Type type) { - this.setMethod = set; - this.type = type; - } - - @Override - public void set(RocketComponent c, String name, HashMap attributes, - WarningSet warnings) { +public MaterialSetter(Reflection.Method set, Material.Type type) { + this.setMethod = set; + this.type = type; +} - Material mat; +@Override +public void set(RocketComponent c, String name, HashMap attributes, + WarningSet warnings) { - // Check name != "" - name = name.trim(); - if (name.equals("")) { - warnings.add(Warning.fromString("Illegal material specification, ignoring.")); - return; - } + Material mat; - // Parse density - double density; - String str; - str = attributes.remove("density"); - if (str == null) { - warnings.add(Warning.fromString("Illegal material specification, ignoring.")); - return; - } - try { - density = Double.parseDouble(str); - } catch (NumberFormatException e) { - warnings.add(Warning.fromString("Illegal material specification, ignoring.")); - return; - } + // Check name != "" + name = name.trim(); + if (name.equals("")) { + warnings.add(Warning.fromString("Illegal material specification, ignoring.")); + return; + } - // Parse thickness - // double thickness = 0; - // str = attributes.remove("thickness"); - // try { - // if (str != null) - // thickness = Double.parseDouble(str); - // } catch (NumberFormatException e){ - // warnings.add(Warning.fromString("Illegal material specification, ignoring.")); - // return; - // } - - // Check type if specified - str = attributes.remove("type"); - if (str != null && !type.name().toLowerCase(Locale.ENGLISH).equals(str)) { - warnings.add(Warning.fromString("Illegal material type specified, ignoring.")); - return; - } + // Parse density + double density; + String str; + str = attributes.remove("density"); + if (str == null) { + warnings.add(Warning.fromString("Illegal material specification, ignoring.")); + return; + } + try { + density = Double.parseDouble(str); + } catch (NumberFormatException e) { + warnings.add(Warning.fromString("Illegal material specification, ignoring.")); + return; + } - mat = Databases.findMaterial(type, name, density, false); + // Parse thickness + // double thickness = 0; + // str = attributes.remove("thickness"); + // try { + // if (str != null) + // thickness = Double.parseDouble(str); + // } catch (NumberFormatException e){ + // warnings.add(Warning.fromString("Illegal material specification, ignoring.")); + // return; + // } - setMethod.invoke(c, mat); + // Check type if specified + str = attributes.remove("type"); + if (str != null && !type.name().toLowerCase(Locale.ENGLISH).equals(str)) { + warnings.add(Warning.fromString("Illegal material type specified, ignoring.")); + return; } + + String key = attributes.remove("key"); + + mat = Databases.findMaterial(type, key, name, density); + + setMethod.invoke(c, mat); +} } + class PositionSetter implements Setter { @Override