Revize e8ba39e7
Přidáno uživatelem Michal Horký před asi 4 roky(ů)
project/Deserializer/src/Converter.java | ||
---|---|---|
1 | 1 |
import java.io.File; |
2 |
import java.io.FileDescriptor; |
|
3 |
import java.io.FileOutputStream; |
|
2 | 4 |
import java.io.IOException; |
5 |
import java.io.OutputStream; |
|
6 |
import java.io.PrintStream; |
|
7 |
import java.util.HashMap; |
|
3 | 8 |
import java.util.List; |
4 | 9 |
import java.util.concurrent.Semaphore; |
5 | 10 |
import java.util.concurrent.atomic.AtomicBoolean; |
6 | 11 |
import java.util.concurrent.atomic.AtomicReference; |
12 |
import java.util.Map; |
|
7 | 13 |
|
14 |
import javafx.util.Pair; |
|
8 | 15 |
import jdeserialize.content; |
9 | 16 |
import jdeserialize.jdeserialize; |
10 | 17 |
|
11 | 18 |
import io.FileWorker; |
12 | 19 |
|
13 | 20 |
public class Converter extends Thread { |
14 |
|
|
15 |
private IConversionResults ui;
|
|
21 |
|
|
22 |
private IConversionResults ui;
|
|
16 | 23 |
private AtomicBoolean active; |
17 | 24 |
|
18 | 25 |
private AtomicReference<File> input; |
19 | 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>(); |
|
20 | 31 |
|
21 | 32 |
public Converter(IConversionResults ui) { |
22 | 33 |
this.ui = ui; |
23 | 34 |
active = new AtomicBoolean(true); |
24 | 35 |
input = new AtomicReference<File>(null); |
25 | 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", ""); |
|
26 | 57 |
} |
27 | 58 |
|
28 | 59 |
public void end() { |
... | ... | |
60 | 91 |
return; |
61 | 92 |
} |
62 | 93 |
|
63 |
String json; |
|
94 |
// TODO |
|
95 |
String json, html; |
|
64 | 96 |
try { |
65 |
json = convert(array); |
|
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(); |
|
66 | 102 |
} catch (IOException e) { |
67 | 103 |
json = null; |
104 |
html = null; |
|
68 | 105 |
} |
69 | 106 |
ui.completed(json); |
70 | 107 |
} |
71 | 108 |
|
72 |
private String convert(byte[] buffer) throws IOException {
|
|
109 |
private Pair<String, String> convert(byte[] buffer) throws IOException {
|
|
73 | 110 |
String json = ""; |
74 |
|
|
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"); |
|
75 | 120 |
jdeserialize deserializer = new jdeserialize(buffer); |
121 |
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); |
|
76 | 122 |
|
77 | 123 |
// gets the "contents" into an array - returnes the deserialization of all |
78 | 124 |
// 'writes' into serialized object via objectOutputStream |
79 | 125 |
List<content> cntnts = deserializer.getContent(); |
80 | 126 |
|
81 |
// testing with only first of the contents |
|
82 |
//ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); |
|
83 |
|
|
84 | 127 |
for(content cnt : cntnts) |
85 | 128 |
{ |
86 | 129 |
if(cnt != null) |
87 | 130 |
{ |
88 |
json += cnt.toString(); |
|
131 |
//Parametrizovany toJson pomoci dictionary |
|
132 |
//Ciste HTML / Cisty JSON |
|
133 |
json += cnt.toJson("", null, this.jsonFormatting); |
|
134 |
html += cnt.toJson("", null, this.htmlFormatting); |
|
89 | 135 |
} |
90 | 136 |
} |
91 | 137 |
|
92 |
return json;
|
|
138 |
return new Pair<String, String>(json, html);
|
|
93 | 139 |
} |
94 | 140 |
|
95 | 141 |
} |
Také k dispozici: Unified diff
re #7879
Converter.java a instance.java changed