2 package net.sf.openrocket.preset.xml;
4 import javax.xml.bind.Marshaller;
5 import javax.xml.bind.Unmarshaller;
6 import javax.xml.bind.annotation.XmlAccessType;
7 import javax.xml.bind.annotation.XmlAccessorType;
8 import javax.xml.bind.annotation.XmlAttribute;
9 import javax.xml.bind.annotation.XmlElement;
10 import javax.xml.bind.annotation.XmlRootElement;
12 import net.sf.openrocket.database.Databases;
13 import net.sf.openrocket.material.Material;
14 import net.sf.openrocket.util.Chars;
17 * XML handler for materials.
19 @XmlRootElement(name = "Material")
20 @XmlAccessorType(XmlAccessType.FIELD)
21 public class MaterialDTO {
23 @XmlElement(name = "Name")
25 @XmlElement(name = "Density")
26 private double density;
27 @XmlElement(name = "Type")
28 private MaterialTypeDTO type;
29 @XmlAttribute(name = "UnitsOfMeasure")
33 * Default constructor.
35 public MaterialDTO() {
38 public MaterialDTO(final Material theMaterial) {
39 this(theMaterial.getName(), theMaterial.getDensity(), MaterialTypeDTO.asDTO(theMaterial.getType()),
40 theMaterial.getType().getUnitGroup().getDefaultUnit().toString());
43 public MaterialDTO(final String theName, final double theDensity, final MaterialTypeDTO theType, final String theUom) {
50 public String getName() {
54 public void setName(final String theName) {
58 public double getDensity() {
62 public void setDensity(final double theDensity) {
66 public MaterialTypeDTO getType() {
70 public void setType(final MaterialTypeDTO theType) {
74 public String getUom() {
78 public void setUom(final String theUom) {
82 Material asMaterial() {
83 return Material.newMaterial(type.getORMaterialType(), name, density, true);
88 * Special directive to the JAXB system. After the object is parsed from xml,
89 * we replace the '2' with Chars.SQUARED, and '3' with Chars.CUBED. Just the
90 * opposite transformation as doen in beforeMarshal.
94 @SuppressWarnings("unused")
95 private void afterUnmarshal( Unmarshaller unmarshaller, Object parent ) {
97 uom = uom.replace('2', Chars.SQUARED);
98 uom = uom.replace('3', Chars.CUBED);
103 * Special directive to the JAXB system. Before the object is serialized into xml,
104 * we strip out the special unicode characters for cubed and squared so they appear
105 * as simple "3" and "2" chars. The reverse transformation is done in afterUnmarshal.
108 @SuppressWarnings("unused")
109 private void beforeMarshal( Marshaller marshaller ) {
111 uom = uom.replace(Chars.SQUARED, '2');
112 uom = uom.replace(Chars.CUBED, '3');