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;
19
20 import java.io.BufferedInputStream;
21 import java.io.BufferedOutputStream;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28
29 import org.apache.log4j.Logger;
30 import org.eclipse.core.resources.IFile;
31 import org.eclipse.core.resources.IResource;
32 import org.eclipse.core.runtime.CoreException;
33
34 import pl.edu.agh.cast.Activator;
35 import pl.edu.agh.cast.model.visual.backward.IDiagram;
36 import pl.edu.agh.cast.model.visual.backward.IDiagramSettings;
37
38
39
40
41
42
43 public final class SerializationUtil {
44
45 private static final Logger LOG = Activator.getLogger();
46
47
48
49
50
51
52
53
54 public static IDiagramSettings readDiagramSettings(IFile file) {
55 ObjectInputStream ois = null;
56 try {
57 ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File(file.getLocationURI()))));
58 IDiagramSettings settings = (IDiagramSettings)ois.readObject();
59 ois.close();
60 ois = null;
61 return settings;
62 } catch (Exception e) {
63 LOG.error("Error occured while reading diagram settings from file "
64 + file.getName(), e);
65 } finally {
66 closeDiagramInput(file, ois);
67 }
68 return null;
69 }
70
71
72
73
74
75
76
77 private static void closeDiagramInput(IFile file, ObjectInputStream ois) {
78 if (ois != null) {
79 try {
80 ois.close();
81 } catch (IOException e) {
82 LOG.error("Error occured while closing diagram file after reading "
83 + file.getName(), e);
84 }
85 }
86 }
87
88
89
90
91
92
93
94
95 public static IDiagram readDiagram(IFile file) {
96 ObjectInputStream ois = null;
97 try {
98 ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File(file.getLocationURI()))));
99
100
101
102 ois.readObject();
103
104 IDiagram diagram = (IDiagram)ois.readObject();
105 ois.close();
106 ois = null;
107 return diagram;
108 } catch (Exception e) {
109 LOG.error("Error occured while reading diagram "
110 + file.getName(), e);
111 } finally {
112 closeDiagramInput(file, ois);
113 }
114 return null;
115 }
116
117
118
119
120
121
122
123
124 public static void serializeToFile(final IDiagram model) throws IOException {
125 serializeDiagram(model);
126 }
127
128
129
130
131
132
133
134
135 public static void serializeToFile(DiagramEditorInput input) throws IOException {
136 input.getDiagram().setFile(input.getFile());
137 serializeDiagram(input.getDiagram());
138 }
139
140
141
142
143
144
145
146 private static void serializeDiagram(final IDiagram diagram) throws IOException {
147
148 File f = new File(diagram.getFile().getLocationURI());
149 f.delete();
150
151 ObjectOutputStream oos = null;
152 try {
153 oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
154 oos.writeObject(diagram.getSettings());
155 oos.writeObject(diagram);
156 oos.close();
157 oos = null;
158 } catch (Exception e) {
159 LOG.error("Error occured while writing diagram to file "
160 + f.getName(), e);
161 } finally {
162 if (oos != null) {
163 try {
164 oos.close();
165 } catch (IOException e) {
166 LOG.error("Error occured while closing diagram file after writing "
167 + f.getName(), e);
168 }
169 }
170 }
171
172 try {
173 diagram.getFile().refreshLocal(IResource.DEPTH_ZERO, null);
174 } catch (CoreException e) {
175 LOG.error("Error refreshing file " + f.getName(), e);
176 }
177 }
178
179 }