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.
*/
void computeDigest() {
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.StringReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
String xml = new OpenRocketComponentSaver().marshalToOpenRocketComponent(new ArrayList<Material>(materialMap.values()), allPresets);
System.err.println(xml);
+ try {
+ List<ComponentPreset> presets = new OpenRocketComponentSaver().unmarshalFromOpenRocketComponent(new StringReader(xml));
+ }
+ catch (InvalidComponentPresetException e) {
+ e.printStackTrace();
+ }
}
}
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.InvalidComponentPresetException;
+import net.sf.openrocket.preset.TypedPropertyMap;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
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.
@XmlElement(name = "Description")
private String description;
@XmlElement(name = "Material")
- private String material;
+ private AnnotatedMaterialDTO material;
@XmlElement(name = "Mass")
private double mass;
setManufacturer(preset.getManufacturer().getSimpleName());
setPartNo(preset.getPartNo());
setDescription(preset.get(ComponentPreset.DESCRIPTION));
- setMaterial(preset.get(ComponentPreset.MATERIAL).getName());
+ setMaterial(new AnnotatedMaterialDTO(preset.get(ComponentPreset.MATERIAL)));
if (preset.has(ComponentPreset.MASS)) {
setMass(preset.get(ComponentPreset.MASS));
}
description = theDescription;
}
- public String getMaterial() {
+ public AnnotatedMaterialDTO getMaterial() {
return material;
}
- public void setMaterial(final String theMaterial) {
+ public void setMaterial(final AnnotatedMaterialDTO theMaterial) {
material = theMaterial;
}
public void setMass(final double 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();
+ }
+ }
}
package net.sf.openrocket.preset.xml;
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.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
+import java.util.List;
/**
* Body tube preset XML handler.
public void setLength(final double 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);
+ }
}
package net.sf.openrocket.preset.xml;
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.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
+import java.util.List;
/**
* Bulkhead preset XML handler.
public void setLength(final double 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);
+ }
+
}
package net.sf.openrocket.preset.xml;
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.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
+import java.util.List;
/**
* Centering ring preset XML handler.
public void setLength(final double 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);
+ }
}
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.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
+import java.util.List;
/**
* Engine block preset XML handler.
super(thePreset);
}
+ @Override
+ public ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException {
+ return super.asComponentPreset(ComponentPreset.Type.ENGINE_BLOCK, materials);
+ }
}
package net.sf.openrocket.preset.xml;
+import net.sf.openrocket.database.Databases;
import net.sf.openrocket.material.Material;
import javax.xml.bind.annotation.XmlAccessType;
public void setUom(final String theUom) {
uom = theUom;
}
+
+ Material asMaterial() {
+ return Databases.findMaterial(type.getORMaterialType(), name, density, false);
+ }
}
return BULK; //default
}
+ public Material.Type getORMaterialType() {
+ return corollary;
+ }
}
package net.sf.openrocket.preset.xml;
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.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
+import java.util.List;
/**
* A NoseCone preset XML handler.
public void setLength(final double 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);
+ }
+
}
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.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
private final String version = "0.1";
@XmlElementWrapper(name = "Materials")
- @XmlElement(name = "Material")
+ @XmlElement(name = "Material")
List<MaterialDTO> materials = new ArrayList<MaterialDTO>();
@XmlElementWrapper(name = "Components")
public void setComponents(final List<BaseComponentDTO> 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;
+ }
}
import net.sf.openrocket.logging.LogHelper;
import net.sf.openrocket.material.Material;
import net.sf.openrocket.preset.ComponentPreset;
+import net.sf.openrocket.preset.InvalidComponentPresetException;
import net.sf.openrocket.startup.Application;
import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
+import java.io.Reader;
import java.io.StringWriter;
import java.util.List;
return sw.toString();
}
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;
}
+ 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.
*
return rsd;
}
+ public List<ComponentPreset> fromOpenRocketComponentDTO(OpenRocketComponentDTO dto) {
+ return null;
+ }
+
/**
* Factory method that maps a preset to the corresponding DTO handler.
*
}
return ELLIPSOID; //default
}
+
+ public Transition.Shape getORShape() {
+ return corollary;
+ }
}
package net.sf.openrocket.preset.xml;
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.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
+import java.util.List;
/**
* Transition preset XML handler.
public void setLength(final double 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);
+ }
}
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.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
+import java.util.List;
/**
* Tube coupler preset XML handler.
public TubeCouplerDTO(ComponentPreset thePreset) {
super(thePreset);
}
+
+ @Override
+ public ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException {
+ return super.asComponentPreset(ComponentPreset.Type.TUBE_COUPLER, materials);
+ }
}