1 |
03a2d08f
|
Michal Horký
|
import java.io.File;
|
2 |
|
|
import java.io.IOException;
|
3 |
|
|
import java.util.List;
|
4 |
|
|
|
5 |
|
|
import org.codehaus.jackson.map.ObjectMapper;
|
6 |
|
|
import org.codehaus.jackson.map.ObjectWriter;
|
7 |
|
|
|
8 |
|
|
import jdeserialize.content;
|
9 |
|
|
import jdeserialize.jdeserialize;
|
10 |
|
|
|
11 |
|
|
public class Converter extends Thread {
|
12 |
|
|
|
13 |
|
|
private Deserializer main;
|
14 |
|
|
|
15 |
|
|
private File input;
|
16 |
|
|
private File output;
|
17 |
|
|
|
18 |
|
|
public Converter(Deserializer main, File inputFilename, File outputFilename) {
|
19 |
|
|
this.main = main;
|
20 |
|
|
this.input = inputFilename;
|
21 |
|
|
this.output = outputFilename;
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
@Override
|
25 |
|
|
public void run() {
|
26 |
|
|
super.run();
|
27 |
|
|
|
28 |
|
|
try {
|
29 |
|
|
String json = convert(FileWorker.loadByteArray(input));
|
30 |
|
|
|
31 |
b639237c
|
Michal Horký
|
String title = "Uložení JSON";
|
32 |
03a2d08f
|
Michal Horký
|
if (FileWorker.saveJson(output, json)) {
|
33 |
|
|
System.out.println("The JSON file was saved correctly.");
|
34 |
b639237c
|
Michal Horký
|
Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku.");
|
35 |
03a2d08f
|
Michal Horký
|
} else {
|
36 |
|
|
System.out.println("There was an error saving the JSON file.");
|
37 |
b639237c
|
Michal Horký
|
Report.error(title, null, "Při ukládání JSON souboru nastala chyba.");
|
38 |
03a2d08f
|
Michal Horký
|
}
|
39 |
|
|
} catch (IOException e) {
|
40 |
|
|
System.out.println("There was an error loading the file.");
|
41 |
|
|
e.printStackTrace();
|
42 |
b639237c
|
Michal Horký
|
Report.error("Načítání souboru", null, "Při načítání souboru došlo k chybě.");
|
43 |
03a2d08f
|
Michal Horký
|
}
|
44 |
|
|
|
45 |
|
|
main.convertIsComplete();
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
private String convert(byte[] buffer) {
|
49 |
|
|
try {
|
50 |
|
|
System.out.println("Deserialization begins.");
|
51 |
|
|
jdeserialize deserializer = new jdeserialize(buffer);
|
52 |
|
|
|
53 |
|
|
// gets the "contents" into an array - returnes the deserialization of all
|
54 |
|
|
// 'writes' into serialized object via objectOutputStream
|
55 |
|
|
List<content> cntnts = deserializer.getContent();
|
56 |
|
|
|
57 |
|
|
// testing with only first of the contents
|
58 |
|
|
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
|
59 |
|
|
String json = ow.writeValueAsString(cntnts.get(0));
|
60 |
|
|
|
61 |
|
|
System.out.println("Task completed.");
|
62 |
|
|
return json;
|
63 |
|
|
} catch (IOException e) {
|
64 |
|
|
System.out.println("An error occurred while processing!");
|
65 |
|
|
e.printStackTrace();
|
66 |
|
|
return null;
|
67 |
|
|
}
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
}
|