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