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