better logging support
[debian/openrocket] / core / src / net / sf / openrocket / preset / xml / OpenRocketComponentSaver.java
1 package net.sf.openrocket.preset.xml;
2
3 import net.sf.openrocket.logging.LogHelper;
4 import net.sf.openrocket.material.Material;
5 import net.sf.openrocket.preset.ComponentPreset;
6 import net.sf.openrocket.startup.Application;
7
8 import javax.xml.bind.JAXBContext;
9 import javax.xml.bind.Marshaller;
10 import java.io.BufferedWriter;
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.io.OutputStreamWriter;
14 import java.io.StringWriter;
15 import java.util.List;
16
17 /**
18  * The active manager class that is the entry point for writing *.orc files.
19  */
20 public class OpenRocketComponentSaver {
21
22     /**
23      * The logger.
24      */
25     private static final LogHelper log = Application.getLogger();
26
27     /**
28      * This method marshals an OpenRocketDocument (OR design) to Rocksim-compliant XML.
29      *
30      * @param theMaterialList the list of materials to be included
31      * @param thePresetList   the list of presets to be included
32      *
33      * @return ORC-compliant XML
34      */
35     public String marshalToOpenRocketComponent(List<Material> theMaterialList, List<ComponentPreset> thePresetList) {
36
37         try {
38             JAXBContext binder = JAXBContext.newInstance(OpenRocketComponentDTO.class);
39             Marshaller marshaller = binder.createMarshaller();
40             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
41             StringWriter sw = new StringWriter();
42
43             marshaller.marshal(toOpenRocketComponentDTO(theMaterialList, thePresetList), sw);
44             return sw.toString();
45         }
46         catch (Exception e) {
47             log.error("Could not marshall a preset list. " + e.getMessage());
48         }
49
50         return null;
51     }
52
53     /**
54      * Write an XML representation of a list of presets.
55      *
56      * @param dest  the stream to write the data to
57      * @param theMaterialList the list of materials to be included
58      * @param thePresetList   the list of presets to be included
59      *
60      * @throws IOException thrown if the stream could not be written
61      */
62     public void save(OutputStream dest, List<Material> theMaterialList, List<ComponentPreset> thePresetList) throws IOException {
63         log.info("Saving .orc file");
64
65         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(dest, "UTF-8"));
66         writer.write(marshalToOpenRocketComponent(theMaterialList, thePresetList));
67         writer.flush();
68         writer.close();
69     }
70
71     /**
72      * Root conversion method.  It iterates over all subcomponents.
73      *
74      * @return a corresponding ORC representation
75      */
76     private OpenRocketComponentDTO toOpenRocketComponentDTO(List<Material> theMaterialList, List<ComponentPreset> thePresetList) {
77         OpenRocketComponentDTO rsd = new OpenRocketComponentDTO();
78
79         if (theMaterialList != null) {
80             for (Material material : theMaterialList) {
81                 rsd.addMaterial(new MaterialDTO(material));
82             }
83         }
84
85         if (thePresetList != null) {
86             for (ComponentPreset componentPreset : thePresetList) {
87                 rsd.addComponent(toComponentDTO(componentPreset));
88             }
89         }
90         return rsd;
91     }
92
93     /**
94      * Factory method that maps a preset to the corresponding DTO handler.
95      *
96      * @param thePreset  the preset for which a handler will be found
97      *
98      * @return a subclass of BaseComponentDTO that can be used for marshalling/unmarshalling a preset; null if not found
99      * for the preset type
100      */
101     private static BaseComponentDTO toComponentDTO(ComponentPreset thePreset) {
102         switch (thePreset.getType()) {
103             case BODY_TUBE:
104                 return new BodyTubeDTO(thePreset);
105             case TUBE_COUPLER:
106                 return new TubeCouplerDTO(thePreset);
107             case NOSE_CONE:
108                 return new NoseConeDTO(thePreset);
109             case TRANSITION:
110                 return new TransitionDTO(thePreset);
111             case BULK_HEAD:
112                 return new BulkHeadDTO(thePreset);
113             case CENTERING_RING:
114                 return new CenteringRingDTO(thePreset);
115             case ENGINE_BLOCK:
116                 return new EngineBlockDTO(thePreset);
117         }
118
119         return null;
120     }
121 }