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.concurrent.Semaphore;
|
10
|
import java.util.concurrent.atomic.AtomicBoolean;
|
11
|
import java.util.concurrent.atomic.AtomicReference;
|
12
|
import java.util.Map;
|
13
|
|
14
|
import javafx.util.Pair;
|
15
|
import jdeserialize.content;
|
16
|
import jdeserialize.jdeserialize;
|
17
|
|
18
|
import io.FileWorker;
|
19
|
|
20
|
public class Converter extends Thread {
|
21
|
|
22
|
private IConversionResults ui;
|
23
|
private AtomicBoolean active;
|
24
|
|
25
|
private AtomicReference<File> input;
|
26
|
private Semaphore inputAvailable;
|
27
|
|
28
|
//Key as enum?
|
29
|
private HashMap<String, String> htmlFormatting = new HashMap<String, String>();
|
30
|
private HashMap<String, String> jsonFormatting = new HashMap<String, String>();
|
31
|
|
32
|
public Converter(IConversionResults ui) {
|
33
|
this.ui = ui;
|
34
|
active = new AtomicBoolean(true);
|
35
|
input = new AtomicReference<File>(null);
|
36
|
inputAvailable = new Semaphore(0);
|
37
|
fillInTestingDictionaries();
|
38
|
}
|
39
|
|
40
|
private void fillInTestingDictionaries()
|
41
|
{
|
42
|
htmlFormatting.put("indent", "<span style=\"margin-left:2em\">");
|
43
|
htmlFormatting.put("lineBreak", "<br/>");
|
44
|
htmlFormatting.put("classCol", "<span style=\"color:blue;font-weight:bold\">");
|
45
|
htmlFormatting.put("fieldCol", "<span style=\"color:purple\">");
|
46
|
htmlFormatting.put("valCol", "<span style=\"color:orange\">");
|
47
|
htmlFormatting.put("keywordCol", "<span style=\"color:green\">");
|
48
|
htmlFormatting.put("closeTagCol", "</span>");
|
49
|
|
50
|
jsonFormatting.put("indent", "\t");
|
51
|
jsonFormatting.put("lineBreak", "\n");
|
52
|
jsonFormatting.put("classCol", "");
|
53
|
jsonFormatting.put("fieldCol", "");
|
54
|
jsonFormatting.put("valCol", "");
|
55
|
jsonFormatting.put("keywordCol", "");
|
56
|
jsonFormatting.put("closeTagCol", "");
|
57
|
}
|
58
|
|
59
|
public void end() {
|
60
|
active.set(false);
|
61
|
setInput(null);
|
62
|
}
|
63
|
|
64
|
public void setInput(File input) {
|
65
|
this.input.set(input);
|
66
|
inputAvailable.release();
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
public void run() {
|
71
|
super.run();
|
72
|
while (active.get()) {
|
73
|
try {
|
74
|
inputAvailable.acquire();
|
75
|
File input = this.input.get();
|
76
|
if (input != null) {
|
77
|
process(input);
|
78
|
}
|
79
|
} catch (InterruptedException e) {
|
80
|
continue;
|
81
|
}
|
82
|
}
|
83
|
}
|
84
|
|
85
|
private void process(File input) {
|
86
|
byte array[];
|
87
|
try {
|
88
|
array = FileWorker.loadByteArray(input);
|
89
|
} catch (IOException e) {
|
90
|
ui.loadingInputFileError();
|
91
|
return;
|
92
|
}
|
93
|
|
94
|
// TODO
|
95
|
String json, html;
|
96
|
try {
|
97
|
//The Pair is not the best way
|
98
|
Pair<String, String> returned = convert(FileWorker.loadByteArray(input));
|
99
|
|
100
|
json = returned.getKey();
|
101
|
html = returned.getValue();
|
102
|
} catch (IOException e) {
|
103
|
json = null;
|
104
|
html = null;
|
105
|
}
|
106
|
ui.completed(json);
|
107
|
}
|
108
|
|
109
|
private Pair<String, String> convert(byte[] buffer) throws IOException {
|
110
|
String json = "";
|
111
|
String html = "";
|
112
|
|
113
|
//Redirectovany system.out do null streamu. Mozno redirect do souboru
|
114
|
System.setOut(new PrintStream(new OutputStream() {
|
115
|
public void write(int b) {
|
116
|
// TODO nejspise do souboru.
|
117
|
}
|
118
|
}));
|
119
|
System.out.println("TEST STRING NOT SHOWING");
|
120
|
jdeserialize deserializer = new jdeserialize(buffer);
|
121
|
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
|
122
|
|
123
|
// gets the "contents" into an array - returnes the deserialization of all
|
124
|
// 'writes' into serialized object via objectOutputStream
|
125
|
List<content> cntnts = deserializer.getContent();
|
126
|
|
127
|
for(content cnt : cntnts)
|
128
|
{
|
129
|
if(cnt != null)
|
130
|
{
|
131
|
//Parametrizovany toJson pomoci dictionary
|
132
|
//Ciste HTML / Cisty JSON
|
133
|
json += cnt.toJson("", null, this.jsonFormatting);
|
134
|
html += cnt.toJson("", null, this.htmlFormatting);
|
135
|
}
|
136
|
}
|
137
|
|
138
|
return new Pair<String, String>(json, html);
|
139
|
}
|
140
|
|
141
|
}
|