1 |
03a2d08f
|
Michal Horký
|
import java.io.File;
|
2 |
e8ba39e7
|
horkmi
|
import java.io.FileDescriptor;
|
3 |
|
|
import java.io.FileOutputStream;
|
4 |
03a2d08f
|
Michal Horký
|
import java.io.IOException;
|
5 |
e8ba39e7
|
horkmi
|
import java.io.OutputStream;
|
6 |
|
|
import java.io.PrintStream;
|
7 |
|
|
import java.util.HashMap;
|
8 |
03a2d08f
|
Michal Horký
|
import java.util.List;
|
9 |
49fd8648
|
Michal Horký
|
import java.util.concurrent.Semaphore;
|
10 |
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
11 |
|
|
import java.util.concurrent.atomic.AtomicReference;
|
12 |
e8ba39e7
|
horkmi
|
import java.util.Map;
|
13 |
03a2d08f
|
Michal Horký
|
|
14 |
e8ba39e7
|
horkmi
|
import javafx.util.Pair;
|
15 |
03a2d08f
|
Michal Horký
|
import jdeserialize.content;
|
16 |
|
|
import jdeserialize.jdeserialize;
|
17 |
|
|
|
18 |
49fd8648
|
Michal Horký
|
import io.FileWorker;
|
19 |
|
|
|
20 |
03a2d08f
|
Michal Horký
|
public class Converter extends Thread {
|
21 |
e8ba39e7
|
horkmi
|
|
22 |
|
|
private IConversionResults ui;
|
23 |
49fd8648
|
Michal Horký
|
private AtomicBoolean active;
|
24 |
|
|
|
25 |
|
|
private AtomicReference<File> input;
|
26 |
|
|
private Semaphore inputAvailable;
|
27 |
e8ba39e7
|
horkmi
|
|
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 |
49fd8648
|
Michal Horký
|
|
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 |
e8ba39e7
|
horkmi
|
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 |
49fd8648
|
Michal Horký
|
}
|
58 |
03a2d08f
|
Michal Horký
|
|
59 |
49fd8648
|
Michal Horký
|
public void end() {
|
60 |
|
|
active.set(false);
|
61 |
|
|
setInput(null);
|
62 |
|
|
}
|
63 |
03a2d08f
|
Michal Horký
|
|
64 |
49fd8648
|
Michal Horký
|
public void setInput(File input) {
|
65 |
|
|
this.input.set(input);
|
66 |
|
|
inputAvailable.release();
|
67 |
03a2d08f
|
Michal Horký
|
}
|
68 |
|
|
|
69 |
|
|
@Override
|
70 |
|
|
public void run() {
|
71 |
|
|
super.run();
|
72 |
49fd8648
|
Michal Horký
|
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 |
03a2d08f
|
Michal Horký
|
|
94 |
e8ba39e7
|
horkmi
|
// TODO
|
95 |
|
|
String json, html;
|
96 |
03a2d08f
|
Michal Horký
|
try {
|
97 |
e8ba39e7
|
horkmi
|
//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 |
03a2d08f
|
Michal Horký
|
} catch (IOException e) {
|
103 |
49fd8648
|
Michal Horký
|
json = null;
|
104 |
e8ba39e7
|
horkmi
|
html = null;
|
105 |
03a2d08f
|
Michal Horký
|
}
|
106 |
49fd8648
|
Michal Horký
|
ui.completed(json);
|
107 |
03a2d08f
|
Michal Horký
|
}
|
108 |
|
|
|
109 |
e8ba39e7
|
horkmi
|
private Pair<String, String> convert(byte[] buffer) throws IOException {
|
110 |
e3d5fc53
|
@havlijan17
|
String json = "";
|
111 |
e8ba39e7
|
horkmi
|
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 |
49fd8648
|
Michal Horký
|
jdeserialize deserializer = new jdeserialize(buffer);
|
121 |
e8ba39e7
|
horkmi
|
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
|
122 |
49fd8648
|
Michal Horký
|
|
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 |
03a2d08f
|
Michal Horký
|
|
127 |
49fd8648
|
Michal Horký
|
for(content cnt : cntnts)
|
128 |
|
|
{
|
129 |
|
|
if(cnt != null)
|
130 |
e3d5fc53
|
@havlijan17
|
{
|
131 |
e8ba39e7
|
horkmi
|
//Parametrizovany toJson pomoci dictionary
|
132 |
|
|
//Ciste HTML / Cisty JSON
|
133 |
|
|
json += cnt.toJson("", null, this.jsonFormatting);
|
134 |
|
|
html += cnt.toJson("", null, this.htmlFormatting);
|
135 |
e3d5fc53
|
@havlijan17
|
}
|
136 |
03a2d08f
|
Michal Horký
|
}
|
137 |
49fd8648
|
Michal Horký
|
|
138 |
e8ba39e7
|
horkmi
|
return new Pair<String, String>(json, html);
|
139 |
03a2d08f
|
Michal Horký
|
}
|
140 |
|
|
|
141 |
|
|
}
|