unmarshal XML (*.orc) to ComponentPresets.

This commit is contained in:
Doug Pedrick 2012-04-25 02:26:13 +00:00
parent 93c7ea784c
commit 250a92dbe9
15 changed files with 231 additions and 7 deletions

View File

@ -280,7 +280,30 @@ public class ComponentPreset implements Comparable<ComponentPreset> {
return get(MANUFACTURER).toString() + "|" + get(PARTNO); return get(MANUFACTURER).toString() + "|" + get(PARTNO);
} }
/** @Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ComponentPreset that = (ComponentPreset) o;
if (digest != null ? !digest.equals(that.digest) : that.digest != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
return digest != null ? digest.hashCode() : 0;
}
/**
* Package scope so the factory can call it. * Package scope so the factory can call it.
*/ */
void computeDigest() { void computeDigest() {

View File

@ -24,6 +24,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -612,6 +613,12 @@ public class RocksimComponentFileLoader {
String xml = new OpenRocketComponentSaver().marshalToOpenRocketComponent(new ArrayList<Material>(materialMap.values()), allPresets); String xml = new OpenRocketComponentSaver().marshalToOpenRocketComponent(new ArrayList<Material>(materialMap.values()), allPresets);
System.err.println(xml); System.err.println(xml);
try {
List<ComponentPreset> presets = new OpenRocketComponentSaver().unmarshalFromOpenRocketComponent(new StringReader(xml));
}
catch (InvalidComponentPresetException e) {
e.printStackTrace();
}
} }
} }

View File

@ -1,11 +1,19 @@
package net.sf.openrocket.preset.xml; package net.sf.openrocket.preset.xml;
import net.sf.openrocket.database.Databases;
import net.sf.openrocket.material.Material;
import net.sf.openrocket.motor.Manufacturer;
import net.sf.openrocket.preset.ComponentPreset; import net.sf.openrocket.preset.ComponentPreset;
import net.sf.openrocket.preset.InvalidComponentPresetException;
import net.sf.openrocket.preset.TypedPropertyMap;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlValue;
import java.util.List;
/** /**
* Base class for the external representation of all component presets. * Base class for the external representation of all component presets.
@ -20,7 +28,7 @@ public abstract class BaseComponentDTO {
@XmlElement(name = "Description") @XmlElement(name = "Description")
private String description; private String description;
@XmlElement(name = "Material") @XmlElement(name = "Material")
private String material; private AnnotatedMaterialDTO material;
@XmlElement(name = "Mass") @XmlElement(name = "Mass")
private double mass; private double mass;
@ -41,7 +49,7 @@ public abstract class BaseComponentDTO {
setManufacturer(preset.getManufacturer().getSimpleName()); setManufacturer(preset.getManufacturer().getSimpleName());
setPartNo(preset.getPartNo()); setPartNo(preset.getPartNo());
setDescription(preset.get(ComponentPreset.DESCRIPTION)); setDescription(preset.get(ComponentPreset.DESCRIPTION));
setMaterial(preset.get(ComponentPreset.MATERIAL).getName()); setMaterial(new AnnotatedMaterialDTO(preset.get(ComponentPreset.MATERIAL)));
if (preset.has(ComponentPreset.MASS)) { if (preset.has(ComponentPreset.MASS)) {
setMass(preset.get(ComponentPreset.MASS)); setMass(preset.get(ComponentPreset.MASS));
} }
@ -71,11 +79,11 @@ public abstract class BaseComponentDTO {
description = theDescription; description = theDescription;
} }
public String getMaterial() { public AnnotatedMaterialDTO getMaterial() {
return material; return material;
} }
public void setMaterial(final String theMaterial) { public void setMaterial(final AnnotatedMaterialDTO theMaterial) {
material = theMaterial; material = theMaterial;
} }
@ -86,4 +94,39 @@ public abstract class BaseComponentDTO {
public void setMass(final double theMass) { public void setMass(final double theMass) {
mass = theMass; mass = theMass;
} }
public abstract ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException;
void addProps(TypedPropertyMap props, List<MaterialDTO> materialList) {
props.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer(manufacturer));
props.put(ComponentPreset.PARTNO, partNo);
props.put(ComponentPreset.DESCRIPTION, description);
props.put(ComponentPreset.MATERIAL, find(materialList, material));
props.put(ComponentPreset.MASS, mass);
}
private Material find(List<MaterialDTO> materialList, AnnotatedMaterialDTO dto) {
for (int i = 0; i < materialList.size(); i++) {
MaterialDTO materialDTO = materialList.get(i);
if (materialDTO.getType().name().equals(dto.type) && materialDTO.getName().equals(dto.material)) {
return materialDTO.asMaterial();
}
}
//Otherwise fallback and look at factory default materials.
return Databases.findMaterial(Material.Type.valueOf(material.type), material.material);
}
static class AnnotatedMaterialDTO {
@XmlAttribute(name = "Type")
private String type;
@XmlValue
private String material;
AnnotatedMaterialDTO() {}
AnnotatedMaterialDTO(Material theMaterial) {
type = theMaterial.getType().name();
material = theMaterial.getName();
}
}
} }

View File

@ -2,11 +2,15 @@
package net.sf.openrocket.preset.xml; package net.sf.openrocket.preset.xml;
import net.sf.openrocket.preset.ComponentPreset; import net.sf.openrocket.preset.ComponentPreset;
import net.sf.openrocket.preset.ComponentPresetFactory;
import net.sf.openrocket.preset.InvalidComponentPresetException;
import net.sf.openrocket.preset.TypedPropertyMap;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
/** /**
* Body tube preset XML handler. * Body tube preset XML handler.
@ -65,4 +69,19 @@ public class BodyTubeDTO extends BaseComponentDTO {
public void setLength(final double theLength) { public void setLength(final double theLength) {
length = theLength; length = theLength;
} }
public ComponentPreset asComponentPreset(java.util.List<MaterialDTO> materials) throws InvalidComponentPresetException {
return asComponentPreset(ComponentPreset.Type.BODY_TUBE, materials);
}
public ComponentPreset asComponentPreset(ComponentPreset.Type type, List<MaterialDTO> materials) throws InvalidComponentPresetException {
TypedPropertyMap props = new TypedPropertyMap();
addProps(props, materials);
props.put(ComponentPreset.INNER_DIAMETER, this.getInsideDiameter());
props.put(ComponentPreset.OUTER_DIAMETER, this.getOutsideDiameter());
props.put(ComponentPreset.LENGTH, this.getLength());
props.put(ComponentPreset.TYPE, type);
return ComponentPresetFactory.create(props);
}
} }

View File

@ -2,11 +2,15 @@
package net.sf.openrocket.preset.xml; package net.sf.openrocket.preset.xml;
import net.sf.openrocket.preset.ComponentPreset; import net.sf.openrocket.preset.ComponentPreset;
import net.sf.openrocket.preset.ComponentPresetFactory;
import net.sf.openrocket.preset.InvalidComponentPresetException;
import net.sf.openrocket.preset.TypedPropertyMap;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
/** /**
* Bulkhead preset XML handler. * Bulkhead preset XML handler.
@ -51,4 +55,15 @@ public class BulkHeadDTO extends BaseComponentDTO {
public void setLength(final double theLength) { public void setLength(final double theLength) {
length = theLength; length = theLength;
} }
public ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException {
TypedPropertyMap props = new TypedPropertyMap();
addProps(props, materials);
props.put(ComponentPreset.OUTER_DIAMETER, this.getOutsideDiameter());
props.put(ComponentPreset.LENGTH, this.getLength());
props.put(ComponentPreset.TYPE, ComponentPreset.Type.BULK_HEAD);
return ComponentPresetFactory.create(props);
}
} }

View File

@ -2,11 +2,15 @@
package net.sf.openrocket.preset.xml; package net.sf.openrocket.preset.xml;
import net.sf.openrocket.preset.ComponentPreset; import net.sf.openrocket.preset.ComponentPreset;
import net.sf.openrocket.preset.ComponentPresetFactory;
import net.sf.openrocket.preset.InvalidComponentPresetException;
import net.sf.openrocket.preset.TypedPropertyMap;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
/** /**
* Centering ring preset XML handler. * Centering ring preset XML handler.
@ -65,4 +69,19 @@ public class CenteringRingDTO extends BaseComponentDTO {
public void setLength(final double theLength) { public void setLength(final double theLength) {
length = theLength; length = theLength;
} }
public ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException {
return asComponentPreset(ComponentPreset.Type.CENTERING_RING, materials);
}
public ComponentPreset asComponentPreset(ComponentPreset.Type type, List<MaterialDTO> materials) throws InvalidComponentPresetException {
TypedPropertyMap props = new TypedPropertyMap();
addProps(props, materials);
props.put(ComponentPreset.INNER_DIAMETER, this.getInsideDiameter());
props.put(ComponentPreset.OUTER_DIAMETER, this.getOutsideDiameter());
props.put(ComponentPreset.LENGTH, this.getLength());
props.put(ComponentPreset.TYPE, type);
return ComponentPresetFactory.create(props);
}
} }

View File

@ -2,10 +2,12 @@
package net.sf.openrocket.preset.xml; package net.sf.openrocket.preset.xml;
import net.sf.openrocket.preset.ComponentPreset; import net.sf.openrocket.preset.ComponentPreset;
import net.sf.openrocket.preset.InvalidComponentPresetException;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
/** /**
* Engine block preset XML handler. * Engine block preset XML handler.
@ -31,4 +33,8 @@ public class EngineBlockDTO extends CenteringRingDTO {
super(thePreset); super(thePreset);
} }
@Override
public ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException {
return super.asComponentPreset(ComponentPreset.Type.ENGINE_BLOCK, materials);
}
} }

View File

@ -1,6 +1,7 @@
package net.sf.openrocket.preset.xml; package net.sf.openrocket.preset.xml;
import net.sf.openrocket.database.Databases;
import net.sf.openrocket.material.Material; import net.sf.openrocket.material.Material;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
@ -74,4 +75,8 @@ public class MaterialDTO {
public void setUom(final String theUom) { public void setUom(final String theUom) {
uom = theUom; uom = theUom;
} }
Material asMaterial() {
return Databases.findMaterial(type.getORMaterialType(), name, density, false);
}
} }

View File

@ -29,4 +29,7 @@ public enum MaterialTypeDTO {
return BULK; //default return BULK; //default
} }
public Material.Type getORMaterialType() {
return corollary;
}
} }

View File

@ -1,11 +1,15 @@
package net.sf.openrocket.preset.xml; package net.sf.openrocket.preset.xml;
import net.sf.openrocket.preset.ComponentPreset; import net.sf.openrocket.preset.ComponentPreset;
import net.sf.openrocket.preset.ComponentPresetFactory;
import net.sf.openrocket.preset.InvalidComponentPresetException;
import net.sf.openrocket.preset.TypedPropertyMap;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
/** /**
* A NoseCone preset XML handler. * A NoseCone preset XML handler.
@ -75,4 +79,17 @@ public class NoseConeDTO extends BaseComponentDTO {
public void setLength(final double theLength) { public void setLength(final double theLength) {
length = theLength; length = theLength;
} }
public ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException {
TypedPropertyMap props = new TypedPropertyMap();
addProps(props, materials);
props.put(ComponentPreset.SHAPE, shape.getORShape());
props.put(ComponentPreset.AFT_OUTER_DIAMETER, this.getShoulderDiameter());
props.put(ComponentPreset.AFT_SHOULDER_DIAMETER, this.getOutsideDiameter());
props.put(ComponentPreset.LENGTH, this.getLength());
props.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
return ComponentPresetFactory.create(props);
}
} }

View File

@ -1,5 +1,8 @@
package net.sf.openrocket.preset.xml; package net.sf.openrocket.preset.xml;
import net.sf.openrocket.preset.ComponentPreset;
import net.sf.openrocket.preset.InvalidComponentPresetException;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
@ -21,7 +24,7 @@ public class OpenRocketComponentDTO {
private final String version = "0.1"; private final String version = "0.1";
@XmlElementWrapper(name = "Materials") @XmlElementWrapper(name = "Materials")
@XmlElement(name = "Material") @XmlElement(name = "Material")
List<MaterialDTO> materials = new ArrayList<MaterialDTO>(); List<MaterialDTO> materials = new ArrayList<MaterialDTO>();
@XmlElementWrapper(name = "Components") @XmlElementWrapper(name = "Components")
@ -66,4 +69,12 @@ public class OpenRocketComponentDTO {
public void setComponents(final List<BaseComponentDTO> theComponents) { public void setComponents(final List<BaseComponentDTO> theComponents) {
components = theComponents; components = theComponents;
} }
public List<ComponentPreset> asComponentPresets() throws InvalidComponentPresetException {
List<ComponentPreset> result = new ArrayList<ComponentPreset>();
for (int i = 0; i < components.size(); i++) {
result.add(components.get(i).asComponentPreset(materials));
}
return result;
}
} }

View File

@ -3,14 +3,18 @@ package net.sf.openrocket.preset.xml;
import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.logging.LogHelper;
import net.sf.openrocket.material.Material; import net.sf.openrocket.material.Material;
import net.sf.openrocket.preset.ComponentPreset; import net.sf.openrocket.preset.ComponentPreset;
import net.sf.openrocket.preset.InvalidComponentPresetException;
import net.sf.openrocket.startup.Application; import net.sf.openrocket.startup.Application;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller; import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.List; import java.util.List;
@ -44,12 +48,29 @@ public class OpenRocketComponentSaver {
return sw.toString(); return sw.toString();
} }
catch (Exception e) { catch (Exception e) {
log.error("Could not marshall a preset list. " + e.getMessage()); log.error("Could not marshal a preset list. " + e.getMessage());
} }
return null; return null;
} }
public List<ComponentPreset> unmarshalFromOpenRocketComponent(Reader is) throws InvalidComponentPresetException {
return fromOpenRocketComponent(is).asComponentPresets();
}
private OpenRocketComponentDTO fromOpenRocketComponent(Reader is) {
try {
JAXBContext bind = JAXBContext.newInstance(OpenRocketComponentDTO.class);
Unmarshaller unmarshaller = bind.createUnmarshaller();
return (OpenRocketComponentDTO)unmarshaller.unmarshal(is);
}
catch (JAXBException e) {
e.printStackTrace();
log.error("Could not unmarshal the .orc file. " + e.getMessage());
}
return null;
}
/** /**
* Write an XML representation of a list of presets. * Write an XML representation of a list of presets.
* *
@ -90,6 +111,10 @@ public class OpenRocketComponentSaver {
return rsd; return rsd;
} }
public List<ComponentPreset> fromOpenRocketComponentDTO(OpenRocketComponentDTO dto) {
return null;
}
/** /**
* Factory method that maps a preset to the corresponding DTO handler. * Factory method that maps a preset to the corresponding DTO handler.
* *

View File

@ -34,4 +34,8 @@ public enum ShapeDTO {
} }
return ELLIPSOID; //default return ELLIPSOID; //default
} }
public Transition.Shape getORShape() {
return corollary;
}
} }

View File

@ -2,11 +2,15 @@
package net.sf.openrocket.preset.xml; package net.sf.openrocket.preset.xml;
import net.sf.openrocket.preset.ComponentPreset; import net.sf.openrocket.preset.ComponentPreset;
import net.sf.openrocket.preset.ComponentPresetFactory;
import net.sf.openrocket.preset.InvalidComponentPresetException;
import net.sf.openrocket.preset.TypedPropertyMap;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
/** /**
* Transition preset XML handler. * Transition preset XML handler.
@ -124,4 +128,20 @@ public class TransitionDTO extends BaseComponentDTO {
public void setLength(final double theLength) { public void setLength(final double theLength) {
length = theLength; length = theLength;
} }
public ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException {
TypedPropertyMap props = new TypedPropertyMap();
addProps(props, materials);
props.put(ComponentPreset.SHAPE, shape.getORShape());
props.put(ComponentPreset.FORE_OUTER_DIAMETER, this.getForeOutsideDiameter());
props.put(ComponentPreset.FORE_SHOULDER_DIAMETER, this.getForeShoulderDiameter());
props.put(ComponentPreset.FORE_SHOULDER_LENGTH, this.getForeShoulderLength());
props.put(ComponentPreset.AFT_OUTER_DIAMETER, this.getAftOutsideDiameter());
props.put(ComponentPreset.AFT_SHOULDER_DIAMETER, this.getAftShoulderDiameter());
props.put(ComponentPreset.AFT_SHOULDER_LENGTH, this.getAftShoulderLength());
props.put(ComponentPreset.LENGTH, this.getLength());
props.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
return ComponentPresetFactory.create(props);
}
} }

View File

@ -2,10 +2,12 @@
package net.sf.openrocket.preset.xml; package net.sf.openrocket.preset.xml;
import net.sf.openrocket.preset.ComponentPreset; import net.sf.openrocket.preset.ComponentPreset;
import net.sf.openrocket.preset.InvalidComponentPresetException;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
/** /**
* Tube coupler preset XML handler. * Tube coupler preset XML handler.
@ -30,4 +32,9 @@ public class TubeCouplerDTO extends BodyTubeDTO {
public TubeCouplerDTO(ComponentPreset thePreset) { public TubeCouplerDTO(ComponentPreset thePreset) {
super(thePreset); super(thePreset);
} }
@Override
public ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException {
return super.asComponentPreset(ComponentPreset.Type.TUBE_COUPLER, materials);
}
} }