Revize 81ff7da4
Přidáno uživatelem Jan Havlíček před asi 4 roky(ů)
project/Deserializer/src/Converter.java | ||
---|---|---|
1 |
import java.io.BufferedWriter; |
|
1 | 2 |
import java.io.File; |
2 | 3 |
import java.io.FileDescriptor; |
3 | 4 |
import java.io.FileOutputStream; |
5 |
import java.io.FileWriter; |
|
4 | 6 |
import java.io.IOException; |
5 | 7 |
import java.io.PrintStream; |
6 | 8 |
import java.util.HashMap; |
... | ... | |
17 | 19 |
import io.FileWorker; |
18 | 20 |
|
19 | 21 |
public class Converter extends Thread { |
20 |
|
|
21 |
private IConversionResults ui;
|
|
22 |
|
|
23 |
private IConversionResults ui;
|
|
22 | 24 |
private AtomicBoolean active; |
23 |
|
|
25 |
|
|
24 | 26 |
// P-K without E semaphore. |
25 | 27 |
private Lock entryLock; |
26 | 28 |
private Semaphore inputAvailable; |
27 |
|
|
29 |
|
|
28 | 30 |
private File inputFile; |
29 | 31 |
private String inputString; |
30 | 32 |
private boolean testing; |
31 |
|
|
32 |
//Key as enum?
|
|
33 |
|
|
34 |
// Key as enum?
|
|
33 | 35 |
private HashMap<String, String> htmlFormatting = new HashMap<String, String>(); |
34 | 36 |
private HashMap<String, String> jsonFormatting = new HashMap<String, String>(); |
35 |
|
|
37 |
|
|
36 | 38 |
public Converter(IConversionResults ui) { |
37 | 39 |
this.ui = ui; |
38 | 40 |
active = new AtomicBoolean(true); |
39 | 41 |
entryLock = new ReentrantLock(); |
40 | 42 |
inputAvailable = new Semaphore(0); |
41 |
fillInTestingDictionaries();
|
|
43 |
fillInTestingDictionaries();
|
|
42 | 44 |
} |
43 | 45 |
|
44 |
private void fillInTestingDictionaries() |
|
45 |
{ |
|
46 |
private void fillInTestingDictionaries() { |
|
46 | 47 |
htmlFormatting.put("indent", "<span style=\"margin-left:2em\">"); |
47 | 48 |
htmlFormatting.put("lineBreak", "<br/>"); |
48 | 49 |
htmlFormatting.put("classCol", "<span style=\"color:blue;font-weight:bold\">"); |
... | ... | |
59 | 60 |
jsonFormatting.put("keywordCol", ""); |
60 | 61 |
jsonFormatting.put("closeTagCol", ""); |
61 | 62 |
} |
62 |
|
|
63 |
|
|
63 | 64 |
public void end() { |
64 | 65 |
active.set(false); |
65 | 66 |
setInput(null, null, false); |
66 | 67 |
} |
67 |
|
|
68 |
|
|
68 | 69 |
public void setInput(File inputFile, String inputString, boolean testing) { |
69 | 70 |
entryLock.lock(); |
70 | 71 |
this.inputFile = inputFile; |
71 | 72 |
this.inputString = inputString; |
72 | 73 |
this.testing = testing; |
73 | 74 |
entryLock.unlock(); |
74 |
|
|
75 |
|
|
75 | 76 |
if (!inputAvailable.tryAcquire()) |
76 | 77 |
inputAvailable.release(); // MAX VALUE = 1! |
77 | 78 |
} |
78 |
|
|
79 |
public boolean getInput(AtomicReference<File> inputFile, AtomicReference<String> inputString, AtomicBoolean testing) { |
|
79 |
|
|
80 |
public boolean getInput(AtomicReference<File> inputFile, AtomicReference<String> inputString, |
|
81 |
AtomicBoolean testing) { |
|
80 | 82 |
try { |
81 | 83 |
inputAvailable.acquire(); |
82 | 84 |
} catch (InterruptedException e) { |
83 | 85 |
return false; |
84 | 86 |
} |
85 |
|
|
87 |
|
|
86 | 88 |
entryLock.lock(); |
87 | 89 |
inputFile.set(this.inputFile); |
88 | 90 |
inputString.set(this.inputString); |
89 | 91 |
testing.set(this.testing); |
90 |
|
|
92 |
|
|
91 | 93 |
this.inputFile = null; |
92 | 94 |
this.inputString = null; |
93 | 95 |
entryLock.unlock(); |
94 |
|
|
96 |
|
|
95 | 97 |
return true; |
96 | 98 |
} |
97 |
|
|
99 |
|
|
98 | 100 |
@Override |
99 | 101 |
public void run() { |
100 | 102 |
super.run(); |
... | ... | |
102 | 104 |
AtomicReference<File> inputFile = new AtomicReference<File>(); |
103 | 105 |
AtomicReference<String> inputString = new AtomicReference<String>(); |
104 | 106 |
AtomicBoolean testing = new AtomicBoolean(); |
105 |
|
|
107 |
|
|
106 | 108 |
if (!getInput(inputFile, inputString, testing)) { |
107 | 109 |
continue; |
108 | 110 |
} |
109 |
|
|
111 |
|
|
110 | 112 |
if (inputFile.get() != null) { |
111 | 113 |
processA(inputFile.get(), testing.get()); |
112 | 114 |
} else if (inputString.get() != null) { |
... | ... | |
114 | 116 |
} |
115 | 117 |
} |
116 | 118 |
} |
117 |
|
|
119 |
|
|
118 | 120 |
private void processA(File input, boolean testing) { |
119 | 121 |
byte array[]; |
120 | 122 |
try { |
... | ... | |
125 | 127 |
} |
126 | 128 |
processB(array, testing); |
127 | 129 |
} |
128 |
|
|
130 |
|
|
129 | 131 |
private void processB(byte array[], boolean testing) { |
130 | 132 |
StringBuilder html = new StringBuilder(); |
131 | 133 |
StringBuilder json = new StringBuilder(); |
132 |
|
|
134 |
|
|
133 | 135 |
try { |
134 |
//Redirectovany system.out do null streamu. Mozno redirect do souboru |
|
136 |
// Redirectovany system.out do null streamu. Mozno redirect do souboru
|
|
135 | 137 |
System.setOut(FileWorker.createRedirectStream()); |
136 | 138 |
convert(array, html, json); |
137 | 139 |
} catch (Exception e) { |
... | ... | |
140 | 142 |
} finally { |
141 | 143 |
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); |
142 | 144 |
} |
143 |
|
|
145 |
|
|
146 |
//test file output |
|
147 |
try { |
|
148 |
BufferedWriter writer = new BufferedWriter(new FileWriter("html.txt")); |
|
149 |
writer.write(html.toString()); |
|
150 |
writer.close(); |
|
151 |
|
|
152 |
BufferedWriter writer2 = new BufferedWriter(new FileWriter("json.txt")); |
|
153 |
writer2.write(json.toString()); |
|
154 |
writer2.close(); |
|
155 |
} catch (IOException e) { |
|
156 |
// TODO Auto-generated catch block |
|
157 |
e.printStackTrace(); |
|
158 |
} |
|
159 |
//test file output |
|
160 |
|
|
144 | 161 |
ui.completed(html.toString(), json.toString(), testing); |
145 | 162 |
} |
146 |
|
|
163 |
|
|
147 | 164 |
private void convert(byte[] buffer, StringBuilder html, StringBuilder json) throws Exception { |
148 | 165 |
jdeserialize deserializer = new jdeserialize(buffer); |
149 | 166 |
|
150 | 167 |
// gets the "contents" into an array - returnes the deserialization of all |
151 |
// 'writes' into serialized object via objectOutputStream |
|
152 |
List<content> cntnts = deserializer.getContent(); |
|
153 |
|
|
154 |
for(content cnt : cntnts) |
|
155 |
{ |
|
156 |
if(cnt != null) |
|
157 |
{ |
|
158 |
//Parametrizovany toJson pomoci dictionary |
|
159 |
//Ciste HTML / Cisty JSON |
|
168 |
// 'writes' into serialized object via objectOutputStream |
|
169 |
List<content> cntnts = deserializer.getContent(); |
|
170 |
|
|
171 |
for (content cnt : cntnts) { |
|
172 |
if (cnt != null) { |
|
173 |
// Parametrizovany toJson pomoci dictionary |
|
174 |
// Ciste HTML / Cisty JSON |
|
160 | 175 |
json.append(cnt.toJson("", null, this.jsonFormatting, false)); |
161 | 176 |
html.append(cnt.toJson("", null, this.htmlFormatting, false)); |
162 | 177 |
} |
Také k dispozici: Unified diff
re #7891 Indentation new issue found.
WebView interpretation of html does not respond to actual HTML