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