1 |
03a2d08f
|
Michal Horký
|
import java.io.File;
|
2 |
53949177
|
@havlijan17
|
import java.io.FileDescriptor;
|
3 |
|
|
import java.io.FileOutputStream;
|
4 |
03a2d08f
|
Michal Horký
|
import java.io.IOException;
|
5 |
53949177
|
@havlijan17
|
import java.io.OutputStream;
|
6 |
|
|
import java.io.PrintStream;
|
7 |
03a2d08f
|
Michal Horký
|
import java.util.List;
|
8 |
|
|
|
9 |
|
|
import jdeserialize.content;
|
10 |
|
|
import jdeserialize.jdeserialize;
|
11 |
|
|
|
12 |
|
|
public class Converter extends Thread {
|
13 |
|
|
|
14 |
|
|
private Deserializer main;
|
15 |
|
|
|
16 |
|
|
private File input;
|
17 |
|
|
private File output;
|
18 |
|
|
|
19 |
|
|
public Converter(Deserializer main, File inputFilename, File outputFilename) {
|
20 |
|
|
this.main = main;
|
21 |
|
|
this.input = inputFilename;
|
22 |
|
|
this.output = outputFilename;
|
23 |
|
|
}
|
24 |
|
|
|
25 |
|
|
@Override
|
26 |
|
|
public void run() {
|
27 |
|
|
super.run();
|
28 |
|
|
|
29 |
|
|
try {
|
30 |
|
|
String json = convert(FileWorker.loadByteArray(input));
|
31 |
|
|
|
32 |
b639237c
|
Michal Horký
|
String title = "Uložení JSON";
|
33 |
03a2d08f
|
Michal Horký
|
if (FileWorker.saveJson(output, json)) {
|
34 |
|
|
System.out.println("The JSON file was saved correctly.");
|
35 |
b639237c
|
Michal Horký
|
Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku.");
|
36 |
03a2d08f
|
Michal Horký
|
} else {
|
37 |
|
|
System.out.println("There was an error saving the JSON file.");
|
38 |
b639237c
|
Michal Horký
|
Report.error(title, null, "Při ukládání JSON souboru nastala chyba.");
|
39 |
03a2d08f
|
Michal Horký
|
}
|
40 |
|
|
} catch (IOException e) {
|
41 |
|
|
System.out.println("There was an error loading the file.");
|
42 |
|
|
e.printStackTrace();
|
43 |
b639237c
|
Michal Horký
|
Report.error("Načítání souboru", null, "Při načítání souboru došlo k chybě.");
|
44 |
03a2d08f
|
Michal Horký
|
}
|
45 |
|
|
|
46 |
|
|
main.convertIsComplete();
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
private String convert(byte[] buffer) {
|
50 |
e3d5fc53
|
@havlijan17
|
String json = "";
|
51 |
03a2d08f
|
Michal Horký
|
try {
|
52 |
|
|
System.out.println("Deserialization begins.");
|
53 |
53949177
|
@havlijan17
|
|
54 |
|
|
//Redirectovany system.out do null streamu. Mozno redirect do souboru
|
55 |
|
|
System.setOut(new PrintStream(OutputStream.nullOutputStream()));
|
56 |
|
|
System.out.println("TEST STRING NOT SHOWING");
|
57 |
03a2d08f
|
Michal Horký
|
jdeserialize deserializer = new jdeserialize(buffer);
|
58 |
53949177
|
@havlijan17
|
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
|
59 |
03a2d08f
|
Michal Horký
|
|
60 |
|
|
// gets the "contents" into an array - returnes the deserialization of all
|
61 |
|
|
// 'writes' into serialized object via objectOutputStream
|
62 |
|
|
List<content> cntnts = deserializer.getContent();
|
63 |
|
|
|
64 |
|
|
// testing with only first of the contents
|
65 |
171c20d6
|
@havlijan17
|
//ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
|
66 |
e3d5fc53
|
@havlijan17
|
|
67 |
|
|
for(content cnt : cntnts)
|
68 |
|
|
{
|
69 |
|
|
if(cnt != null)
|
70 |
|
|
{
|
71 |
53949177
|
@havlijan17
|
//Parametrizovany toJson pomoci dictionary
|
72 |
|
|
//replace VS parametr
|
73 |
|
|
//Pustit 2X s parametrem nenabori vnitrni data (kde muzou byt html znacky)
|
74 |
|
|
//Ciste HTML / Cisty JSON
|
75 |
e48f57d1
|
@havlijan17
|
json += cnt.toJson("", null);
|
76 |
e3d5fc53
|
@havlijan17
|
}
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
System.out.println("Task completed.");
|
80 |
|
|
|
81 |
|
|
//just for tests
|
82 |
a0511590
|
@havlijan17
|
System.out.println(json);
|
83 |
e3d5fc53
|
@havlijan17
|
|
84 |
|
|
return json;
|
85 |
03a2d08f
|
Michal Horký
|
} catch (IOException e) {
|
86 |
|
|
System.out.println("An error occurred while processing!");
|
87 |
|
|
e.printStackTrace();
|
88 |
|
|
return null;
|
89 |
|
|
}
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
}
|