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 javafx.util.Pair;
|
12
|
import jdeserialize.content;
|
13
|
import jdeserialize.jdeserialize;
|
14
|
|
15
|
public class Converter extends Thread {
|
16
|
private Deserializer main;
|
17
|
|
18
|
private File input;
|
19
|
private File output;
|
20
|
|
21
|
//Key as enum?
|
22
|
private HashMap<String, String> htmlFormatting = new HashMap<String, String>();
|
23
|
private HashMap<String, String> jsonFormatting = new HashMap<String, String>();
|
24
|
|
25
|
public Converter(Deserializer main, File inputFilename, File outputFilename) {
|
26
|
this.main = main;
|
27
|
this.input = inputFilename;
|
28
|
this.output = outputFilename;
|
29
|
|
30
|
fillInTestingDictionaries();
|
31
|
}
|
32
|
|
33
|
private void fillInTestingDictionaries()
|
34
|
{
|
35
|
htmlFormatting.put("indent", "<span style=\"margin-left:2em\">");
|
36
|
htmlFormatting.put("lineBreak", "<br/>");
|
37
|
htmlFormatting.put("classCol", "<span style=\"color:blue;font-weight:bold\">");
|
38
|
htmlFormatting.put("fieldCol", "<span style=\"color:purple\">");
|
39
|
htmlFormatting.put("valCol", "<span style=\"color:orange\">");
|
40
|
htmlFormatting.put("keywordCol", "<span style=\"color:green\">");
|
41
|
htmlFormatting.put("closeTagCol", "</span>");
|
42
|
|
43
|
jsonFormatting.put("indent", "\t");
|
44
|
jsonFormatting.put("lineBreak", "\n");
|
45
|
jsonFormatting.put("classCol", "");
|
46
|
jsonFormatting.put("fieldCol", "");
|
47
|
jsonFormatting.put("valCol", "");
|
48
|
jsonFormatting.put("keywordCol", "");
|
49
|
jsonFormatting.put("closeTagCol", "");
|
50
|
}
|
51
|
|
52
|
@Override
|
53
|
public void run() {
|
54
|
super.run();
|
55
|
|
56
|
try {
|
57
|
//The Pair is not the best way
|
58
|
Pair<String, String> returned = convert(FileWorker.loadByteArray(input));
|
59
|
|
60
|
String json = returned.getKey();
|
61
|
String html = returned.getValue();
|
62
|
|
63
|
String title = "Uložení JSON";
|
64
|
if (FileWorker.saveJson(output, json)) {
|
65
|
System.out.println("The JSON file was saved correctly.");
|
66
|
Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku.");
|
67
|
} else {
|
68
|
System.out.println("There was an error saving the JSON file.");
|
69
|
Report.error(title, null, "Při ukládání JSON souboru nastala chyba.");
|
70
|
}
|
71
|
} catch (IOException e) {
|
72
|
System.out.println("There was an error loading the file.");
|
73
|
e.printStackTrace();
|
74
|
Report.error("Načítání souboru", null, "Při načítání souboru došlo k chybě.");
|
75
|
}
|
76
|
|
77
|
main.convertIsComplete();
|
78
|
}
|
79
|
|
80
|
private Pair<String, String> convert(byte[] buffer) {
|
81
|
String json = "";
|
82
|
String html = "";
|
83
|
|
84
|
try {
|
85
|
System.out.println("Deserialization begins.");
|
86
|
|
87
|
//Redirectovany system.out do null streamu. Mozno redirect do souboru
|
88
|
System.setOut(new PrintStream(OutputStream.nullOutputStream()));
|
89
|
System.out.println("TEST STRING NOT SHOWING");
|
90
|
jdeserialize deserializer = new jdeserialize(buffer);
|
91
|
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
|
92
|
|
93
|
// gets the "contents" into an array - returnes the deserialization of all
|
94
|
// 'writes' into serialized object via objectOutputStream
|
95
|
List<content> cntnts = deserializer.getContent();
|
96
|
|
97
|
for(content cnt : cntnts)
|
98
|
{
|
99
|
if(cnt != null)
|
100
|
{
|
101
|
//Parametrizovany toJson pomoci dictionary
|
102
|
//Ciste HTML / Cisty JSON
|
103
|
json += cnt.toJson("", null, this.jsonFormatting);
|
104
|
html += cnt.toJson("", null, this.htmlFormatting);
|
105
|
}
|
106
|
}
|
107
|
|
108
|
System.out.println("Task completed.");
|
109
|
|
110
|
//just for tests
|
111
|
System.out.println(json);
|
112
|
System.out.println("*********************\n");
|
113
|
System.out.println(html);
|
114
|
|
115
|
return new Pair<String, String>(json, html);
|
116
|
} catch (IOException e) {
|
117
|
System.out.println("An error occurred while processing!");
|
118
|
e.printStackTrace();
|
119
|
return null;
|
120
|
}
|
121
|
}
|
122
|
|
123
|
}
|