1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.backward.resources.xml;
19
20 import java.io.BufferedWriter;
21 import java.io.File;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.io.ObjectOutputStream;
25 import java.io.Writer;
26 import java.util.List;
27
28 import org.apache.log4j.Logger;
29
30 import pl.edu.agh.cast.Activator;
31 import pl.edu.agh.cast.backward.resources.IExporter;
32 import pl.edu.agh.cast.model.visual.backward.IDiagram;
33
34 import com.thoughtworks.xstream.XStream;
35
36
37
38
39
40
41 public class XMLExporter implements IExporter {
42
43 private static final String XML_ENCODING_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
44 + System.getProperty("line.separator");
45
46 private static final Logger LOG = Activator.getLogger();
47
48
49
50
51 private XStream xstream;
52
53
54
55
56 public XMLExporter() {
57 this(null);
58 }
59
60
61
62
63
64
65
66 public XMLExporter(Class<?>[] classesWithAnnotations) {
67 this.xstream = XStreamInitializer.createXStreamInstance(classesWithAnnotations);
68 LOG.debug("XMLProjectExporter created.");
69 }
70
71
72
73
74
75
76 public void export(String projectName, List<IDiagram> diagrams, File file) throws IOException {
77 LOG.debug("Exporting project to file: " + file.getAbsolutePath());
78 Writer fileWriter = new BufferedWriter(new FileWriter(file));
79 fileWriter.write(XML_ENCODING_HEADER);
80 xstream.toXML(new XMLProjectContainer(projectName, diagrams), fileWriter);
81 fileWriter.close();
82 }
83
84
85
86
87
88
89 public void export(IDiagram diagram, File file) throws IOException {
90 LOG.debug("Exporting a diagram to file: " + file.getAbsolutePath());
91 Writer fileWriter = new BufferedWriter(new FileWriter(file));
92 fileWriter.write(XML_ENCODING_HEADER);
93 ObjectOutputStream objectOutputStream = xstream.createObjectOutputStream(fileWriter);
94 objectOutputStream.writeObject(diagram.getSettings());
95 objectOutputStream.writeObject(diagram);
96 objectOutputStream.close();
97 fileWriter.close();
98 }
99
100
101
102
103
104
105
106 public void updateXStreamAnnotataions(Class<?> clazz) {
107 xstream.processAnnotations(clazz);
108 }
109
110 }