create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / preset / xml / MaterialDTO.java
1 package net.sf.openrocket.preset.xml;
2
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;
10
11 import net.sf.openrocket.database.Databases;
12 import net.sf.openrocket.material.Material;
13 import net.sf.openrocket.util.Chars;
14
15 /**
16  * XML handler for materials.
17  */
18 @XmlRootElement(name = "Material")
19 @XmlAccessorType(XmlAccessType.FIELD)
20 public class MaterialDTO {
21         
22         @XmlElement(name = "Name")
23         private String name;
24         @XmlElement(name = "Density")
25         private double density;
26         @XmlElement(name = "Type")
27         private MaterialTypeDTO type;
28         @XmlAttribute(name = "UnitsOfMeasure")
29         private String uom;
30         
31         /**
32          * Default constructor.
33          */
34         public MaterialDTO() {
35         }
36         
37         public MaterialDTO(final Material theMaterial) {
38                 this(theMaterial.getName(), theMaterial.getDensity(), MaterialTypeDTO.asDTO(theMaterial.getType()),
39                                 theMaterial.getType().getUnitGroup().getDefaultUnit().toString());
40         }
41         
42         public MaterialDTO(final String theName, final double theDensity, final MaterialTypeDTO theType, final String theUom) {
43                 name = theName;
44                 density = theDensity;
45                 type = theType;
46                 uom = theUom;
47         }
48         
49         public String getName() {
50                 return name;
51         }
52         
53         public void setName(final String theName) {
54                 name = theName;
55         }
56         
57         public double getDensity() {
58                 return density;
59         }
60         
61         public void setDensity(final double theDensity) {
62                 density = theDensity;
63         }
64         
65         public MaterialTypeDTO getType() {
66                 return type;
67         }
68         
69         public void setType(final MaterialTypeDTO theType) {
70                 type = theType;
71         }
72         
73         public String getUom() {
74                 return uom;
75         }
76         
77         public void setUom(final String theUom) {
78                 uom = theUom;
79         }
80         
81         Material asMaterial() {
82                 return Databases.findMaterial(type.getORMaterialType(), name, density);
83         }
84         
85         
86         /**
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.
90          * @param unmarshaller
91          * @param parent
92          */
93         @SuppressWarnings("unused")
94         private void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
95                 if (uom != null) {
96                         uom = uom.replace('2', Chars.SQUARED);
97                         uom = uom.replace('3', Chars.CUBED);
98                 }
99         }
100         
101         /**
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.
105          * @param marshaller
106          */
107         @SuppressWarnings("unused")
108         private void beforeMarshal(Marshaller marshaller) {
109                 if (uom != null) {
110                         uom = uom.replace(Chars.SQUARED, '2');
111                         uom = uom.replace(Chars.CUBED, '3');
112                 }
113         }
114 }