1 package net.sf.openrocket.preset.xml;
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;
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;
18 * The active manager class that is the entry point for writing *.orc files.
20 public class OpenRocketComponentSaver {
25 private static final LogHelper log = Application.getLogger();
28 * This method marshals an OpenRocketDocument (OR design) to Rocksim-compliant XML.
30 * @param theMaterialList the list of materials to be included
31 * @param thePresetList the list of presets to be included
33 * @return ORC-compliant XML
35 public String marshalToOpenRocketComponent(List<Material> theMaterialList, List<ComponentPreset> thePresetList) {
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();
43 marshaller.marshal(toOpenRocketComponentDTO(theMaterialList, thePresetList), sw);
47 log.error("Could not marshall a preset list. " + e.getMessage());
54 * Write an XML representation of a list of presets.
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
60 * @throws IOException thrown if the stream could not be written
62 public void save(OutputStream dest, List<Material> theMaterialList, List<ComponentPreset> thePresetList) throws IOException {
63 log.info("Saving .orc file");
65 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(dest, "UTF-8"));
66 writer.write(marshalToOpenRocketComponent(theMaterialList, thePresetList));
72 * Root conversion method. It iterates over all subcomponents.
74 * @return a corresponding ORC representation
76 private OpenRocketComponentDTO toOpenRocketComponentDTO(List<Material> theMaterialList, List<ComponentPreset> thePresetList) {
77 OpenRocketComponentDTO rsd = new OpenRocketComponentDTO();
79 if (theMaterialList != null) {
80 for (Material material : theMaterialList) {
81 rsd.addMaterial(new MaterialDTO(material));
85 if (thePresetList != null) {
86 for (ComponentPreset componentPreset : thePresetList) {
87 rsd.addComponent(toComponentDTO(componentPreset));
94 * Factory method that maps a preset to the corresponding DTO handler.
96 * @param thePreset the preset for which a handler will be found
98 * @return a subclass of BaseComponentDTO that can be used for marshalling/unmarshalling a preset; null if not found
101 private static BaseComponentDTO toComponentDTO(ComponentPreset thePreset) {
102 switch (thePreset.getType()) {
104 return new BodyTubeDTO(thePreset);
106 return new TubeCouplerDTO(thePreset);
108 return new NoseConeDTO(thePreset);
110 return new TransitionDTO(thePreset);
112 return new BulkHeadDTO(thePreset);
114 return new CenteringRingDTO(thePreset);
116 return new EngineBlockDTO(thePreset);