1 package net.sf.openrocket.preset.xml;
3 import javax.xml.bind.Marshaller;
4 import javax.xml.bind.Unmarshaller;
5 import javax.xml.bind.annotation.XmlAccessType;
6 import javax.xml.bind.annotation.XmlAccessorType;
7 import javax.xml.bind.annotation.XmlAttribute;
8 import javax.xml.bind.annotation.XmlElement;
9 import javax.xml.bind.annotation.XmlRootElement;
11 import net.sf.openrocket.database.Databases;
12 import net.sf.openrocket.material.Material;
13 import net.sf.openrocket.util.Chars;
16 * XML handler for materials.
18 @XmlRootElement(name = "Material")
19 @XmlAccessorType(XmlAccessType.FIELD)
20 public class MaterialDTO {
22 @XmlElement(name = "Name")
24 @XmlElement(name = "Density")
25 private double density;
26 @XmlElement(name = "Type")
27 private MaterialTypeDTO type;
28 @XmlAttribute(name = "UnitsOfMeasure")
32 * Default constructor.
34 public MaterialDTO() {
37 public MaterialDTO(final Material theMaterial) {
38 this(theMaterial.getName(), theMaterial.getDensity(), MaterialTypeDTO.asDTO(theMaterial.getType()),
39 theMaterial.getType().getUnitGroup().getDefaultUnit().toString());
42 public MaterialDTO(final String theName, final double theDensity, final MaterialTypeDTO theType, final String theUom) {
49 public String getName() {
53 public void setName(final String theName) {
57 public double getDensity() {
61 public void setDensity(final double theDensity) {
65 public MaterialTypeDTO getType() {
69 public void setType(final MaterialTypeDTO theType) {
73 public String getUom() {
77 public void setUom(final String theUom) {
81 Material asMaterial() {
82 return Databases.findMaterial(type.getORMaterialType(), name, density);
87 * Special directive to the JAXB system. After the object is parsed from xml,
88 * we replace the '2' with Chars.SQUARED, and '3' with Chars.CUBED. Just the
89 * opposite transformation as doen in beforeMarshal.
93 @SuppressWarnings("unused")
94 private void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
96 uom = uom.replace('2', Chars.SQUARED);
97 uom = uom.replace('3', Chars.CUBED);
102 * Special directive to the JAXB system. Before the object is serialized into xml,
103 * we strip out the special unicode characters for cubed and squared so they appear
104 * as simple "3" and "2" chars. The reverse transformation is done in afterUnmarshal.
107 @SuppressWarnings("unused")
108 private void beforeMarshal(Marshaller marshaller) {
110 uom = uom.replace(Chars.SQUARED, '2');
111 uom = uom.replace(Chars.CUBED, '3');