1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.oqt.core;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25
26 import net.sf.oqt.model.ResultVO;
27
28 import org.apache.maven.project.MavenProject;
29
30
31
32
33
34
35
36
37
38
39 public final class Serializer {
40
41
42
43 private final File resultFile;
44
45 private final File outputDir;
46
47
48
49
50
51
52
53
54
55 public Serializer(final MavenProject project) throws IOException {
56 File buildDir = new File(project.getBuild().getDirectory());
57 if (!buildDir.exists() && !buildDir.mkdir()) {
58 throw new IOException("Failed to create build dir at " + buildDir.getAbsolutePath());
59 }
60 resultFile = new File(project.getBuild().getDirectory() + "/querytranslator/output.ser");
61 outputDir = new File(project.getBuild().getDirectory() + "/querytranslator/");
62 if (!outputDir.exists() && !outputDir.mkdir()) {
63 throw new IOException("Failed to create the output directory in " + outputDir.getAbsolutePath());
64 }
65 }
66
67
68
69
70
71
72
73 public void serialize(final ResultVO result) throws IOException {
74 if (resultFile.exists()) {
75 if (!resultFile.delete()) {
76 throw new IOException("Failed to delete old report at " + resultFile.getAbsolutePath());
77 }
78 }
79 if (!resultFile.createNewFile()) {
80 throw new IOException("Failed to create report file at " + resultFile.getAbsolutePath());
81 }
82 FileOutputStream fos = null;
83 ObjectOutputStream out = null;
84 fos = new FileOutputStream(resultFile);
85 out = new ObjectOutputStream(fos);
86 out.writeObject(result);
87 out.close();
88 }
89
90
91
92
93
94
95
96
97 public ResultVO deserialize() throws IOException, ClassNotFoundException {
98 FileInputStream fis = null;
99 ObjectInputStream in = null;
100 if (!resultFile.exists()) {
101 return null;
102 }
103 fis = new FileInputStream(resultFile);
104 in = new ObjectInputStream(fis);
105 final Object o = in.readObject();
106 in.close();
107 return (ResultVO) o;
108 }
109
110 public void textOutput(String fileName, String stringOutput) throws IOException {
111 File outputFile = new File(outputDir.getAbsolutePath() + "/" + fileName);
112 FileWriter writer = new FileWriter(outputFile);
113 writer.write(stringOutput);
114 writer.flush();
115 writer.close();
116 }
117 }