1
|
import java.io.BufferedWriter;
|
2
|
import java.io.File;
|
3
|
import java.io.FileDescriptor;
|
4
|
import java.io.FileOutputStream;
|
5
|
import java.io.FileWriter;
|
6
|
import java.io.IOException;
|
7
|
import java.io.PrintStream;
|
8
|
import java.util.HashMap;
|
9
|
import java.util.List;
|
10
|
import java.util.concurrent.Semaphore;
|
11
|
import java.util.concurrent.atomic.AtomicBoolean;
|
12
|
import java.util.concurrent.atomic.AtomicReference;
|
13
|
import java.util.concurrent.locks.Lock;
|
14
|
import java.util.concurrent.locks.ReentrantLock;
|
15
|
|
16
|
import jdeserialize.content;
|
17
|
import jdeserialize.jdeserialize;
|
18
|
import io.Database.DB_Messenger;
|
19
|
import io.Database;
|
20
|
import io.FileWorker;
|
21
|
|
22
|
public class Converter extends Thread {
|
23
|
|
24
|
private IConversionResults ui;
|
25
|
private AtomicBoolean active;
|
26
|
|
27
|
// P-K without E semaphore.
|
28
|
private Lock entryLock;
|
29
|
private Semaphore inputAvailable;
|
30
|
|
31
|
// Input.
|
32
|
private File inputFile;
|
33
|
private DB_Messenger query;
|
34
|
|
35
|
// Key as enum?
|
36
|
private HashMap<String, String> htmlFormatting = new HashMap<String, String>();
|
37
|
private HashMap<String, String> jsonFormatting = new HashMap<String, String>();
|
38
|
|
39
|
public Converter(IConversionResults ui) {
|
40
|
this.ui = ui;
|
41
|
active = new AtomicBoolean(true);
|
42
|
entryLock = new ReentrantLock();
|
43
|
inputAvailable = new Semaphore(0);
|
44
|
fillInTestingDictionaries();
|
45
|
}
|
46
|
|
47
|
private void fillInTestingDictionaries() {
|
48
|
htmlFormatting.put("indent", "<span style=\"margin-left:2em;\">");
|
49
|
htmlFormatting.put("lineBreak", "<br/>");
|
50
|
htmlFormatting.put("classCol", "<span style=\"color:blue;font-weight:bold;\">");
|
51
|
htmlFormatting.put("fieldCol", "<span style=\"color:purple;\">");
|
52
|
htmlFormatting.put("valCol", "<span style=\"color:orange;\">");
|
53
|
htmlFormatting.put("keywordCol", "<span style=\"color:green;\">");
|
54
|
htmlFormatting.put("closeTagCol", "</span>");
|
55
|
|
56
|
jsonFormatting.put("indent", "\t");
|
57
|
jsonFormatting.put("lineBreak", "\n");
|
58
|
jsonFormatting.put("classCol", "");
|
59
|
jsonFormatting.put("fieldCol", "");
|
60
|
jsonFormatting.put("valCol", "");
|
61
|
jsonFormatting.put("keywordCol", "");
|
62
|
jsonFormatting.put("closeTagCol", "");
|
63
|
}
|
64
|
|
65
|
public void end() {
|
66
|
active.set(false);
|
67
|
setInput(null, null);
|
68
|
}
|
69
|
|
70
|
public void setInput(File inputFile, DB_Messenger query) {
|
71
|
entryLock.lock();
|
72
|
this.inputFile = inputFile;
|
73
|
this.query = query;
|
74
|
entryLock.unlock();
|
75
|
|
76
|
if (!inputAvailable.tryAcquire())
|
77
|
inputAvailable.release(); // MAX VALUE = 1!
|
78
|
}
|
79
|
|
80
|
public boolean getInput(AtomicReference<File> inputFile, AtomicReference<DB_Messenger> query) {
|
81
|
try {
|
82
|
inputAvailable.acquire();
|
83
|
} catch (InterruptedException e) {
|
84
|
return false;
|
85
|
}
|
86
|
|
87
|
entryLock.lock();
|
88
|
inputFile.set(this.inputFile);
|
89
|
query.set(this.query);
|
90
|
|
91
|
this.inputFile = null;
|
92
|
this.query = null;
|
93
|
entryLock.unlock();
|
94
|
|
95
|
return inputFile.get() != null || query.get() != null;
|
96
|
}
|
97
|
|
98
|
@Override
|
99
|
public void run() {
|
100
|
super.run();
|
101
|
while (active.get()) {
|
102
|
AtomicReference<File> inputFile = new AtomicReference<File>();
|
103
|
AtomicReference<DB_Messenger> query = new AtomicReference<DB_Messenger>();
|
104
|
|
105
|
if (!getInput(inputFile, query)) {
|
106
|
continue;
|
107
|
}
|
108
|
|
109
|
byte array[] = inputFile.get() != null ? getBytes(inputFile.get()) : getBytes(query.get());
|
110
|
StringBuilder html = new StringBuilder();
|
111
|
StringBuilder json = new StringBuilder();
|
112
|
|
113
|
try {
|
114
|
// Redirectovany system.out do null streamu. Mozno redirect do souboru
|
115
|
System.setOut(FileWorker.createRedirectStream());
|
116
|
convert(array, html, json);
|
117
|
} catch (Exception e) {
|
118
|
json.setLength(0);
|
119
|
html.setLength(0);
|
120
|
} finally {
|
121
|
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
|
122
|
}
|
123
|
|
124
|
//test file output
|
125
|
try {
|
126
|
BufferedWriter writer = new BufferedWriter(new FileWriter("html.txt"));
|
127
|
writer.write(html.toString().replace("<br/>", "\n")); // TODO pridan replace pro odsazovani - lepsi orientace v souboru.
|
128
|
writer.close();
|
129
|
|
130
|
BufferedWriter writer2 = new BufferedWriter(new FileWriter("json.txt"));
|
131
|
writer2.write(json.toString());
|
132
|
writer2.close();
|
133
|
} catch (IOException e) {
|
134
|
// TODO Auto-generated catch block
|
135
|
e.printStackTrace();
|
136
|
}
|
137
|
//test file output
|
138
|
|
139
|
ui.completed(html.toString(), json.toString(), array == null ? null : new String(array));
|
140
|
}
|
141
|
}
|
142
|
|
143
|
private byte[] getBytes(File input) {
|
144
|
byte array[];
|
145
|
try {
|
146
|
array = FileWorker.loadByteArray(input);
|
147
|
} catch (IOException e) {
|
148
|
ui.loadingInputFileError();
|
149
|
return null;
|
150
|
}
|
151
|
return array;
|
152
|
}
|
153
|
|
154
|
private byte[] getBytes(DB_Messenger query) {
|
155
|
return Database.getBlobBytes(query);
|
156
|
}
|
157
|
|
158
|
private void convert(byte[] buffer, StringBuilder html, StringBuilder json) throws Exception {
|
159
|
jdeserialize deserializer = new jdeserialize(buffer);
|
160
|
|
161
|
// gets the "contents" into an array - returnes the deserialization of all
|
162
|
// 'writes' into serialized object via objectOutputStream
|
163
|
List<content> cntnts = deserializer.getContent();
|
164
|
|
165
|
for (content cnt : cntnts) {
|
166
|
if (cnt != null) {
|
167
|
// Parametrizovany toJson pomoci dictionary
|
168
|
// Ciste HTML / Cisty JSON
|
169
|
json.append(cnt.toJson("", null, this.jsonFormatting, false));
|
170
|
html.append(cnt.toJson("", null, this.htmlFormatting, false));
|
171
|
}
|
172
|
}
|
173
|
}
|
174
|
|
175
|
}
|