Revize a5d16f0a
Přidáno uživatelem Michal Horký před asi 4 roky(ů)
project/Deserializer/src/Converter.java | ||
---|---|---|
2 | 2 |
import java.io.FileDescriptor; |
3 | 3 |
import java.io.FileOutputStream; |
4 | 4 |
import java.io.IOException; |
5 |
import java.io.OutputStream; |
|
6 | 5 |
import java.io.PrintStream; |
7 | 6 |
import java.util.HashMap; |
8 | 7 |
import java.util.List; |
9 | 8 |
import java.util.concurrent.Semaphore; |
10 | 9 |
import java.util.concurrent.atomic.AtomicBoolean; |
11 | 10 |
import java.util.concurrent.atomic.AtomicReference; |
12 |
import java.util.Map; |
|
11 |
import java.util.concurrent.locks.Lock; |
|
12 |
import java.util.concurrent.locks.ReentrantLock; |
|
13 | 13 |
|
14 |
import javafx.util.Pair; |
|
15 | 14 |
import jdeserialize.content; |
16 | 15 |
import jdeserialize.jdeserialize; |
17 | 16 |
|
... | ... | |
22 | 21 |
private IConversionResults ui; |
23 | 22 |
private AtomicBoolean active; |
24 | 23 |
|
25 |
private AtomicReference<File> input; |
|
24 |
// P-K without E semaphore. |
|
25 |
private Lock entryLock; |
|
26 | 26 |
private Semaphore inputAvailable; |
27 | 27 |
|
28 |
private File inputFile; |
|
29 |
private String inputString; |
|
30 |
private boolean testing; |
|
31 |
|
|
28 | 32 |
//Key as enum? |
29 | 33 |
private HashMap<String, String> htmlFormatting = new HashMap<String, String>(); |
30 | 34 |
private HashMap<String, String> jsonFormatting = new HashMap<String, String>(); |
... | ... | |
32 | 36 |
public Converter(IConversionResults ui) { |
33 | 37 |
this.ui = ui; |
34 | 38 |
active = new AtomicBoolean(true); |
35 |
input = new AtomicReference<File>(null);
|
|
39 |
entryLock = new ReentrantLock();
|
|
36 | 40 |
inputAvailable = new Semaphore(0); |
37 | 41 |
fillInTestingDictionaries(); |
38 | 42 |
} |
... | ... | |
58 | 62 |
|
59 | 63 |
public void end() { |
60 | 64 |
active.set(false); |
61 |
setInput(null); |
|
65 |
setInput(null, null, false);
|
|
62 | 66 |
} |
63 | 67 |
|
64 |
public void setInput(File input) { |
|
65 |
this.input.set(input); |
|
66 |
inputAvailable.release(); |
|
68 |
public void setInput(File inputFile, String inputString, boolean testing) { |
|
69 |
entryLock.lock(); |
|
70 |
this.inputFile = inputFile; |
|
71 |
this.inputString = inputString; |
|
72 |
this.testing = testing; |
|
73 |
entryLock.unlock(); |
|
74 |
|
|
75 |
if (!inputAvailable.tryAcquire()) |
|
76 |
inputAvailable.release(); // MAX VALUE = 1! |
|
77 |
} |
|
78 |
|
|
79 |
public boolean getInput(AtomicReference<File> inputFile, AtomicReference<String> inputString, AtomicBoolean testing) { |
|
80 |
try { |
|
81 |
inputAvailable.acquire(); |
|
82 |
} catch (InterruptedException e) { |
|
83 |
return false; |
|
84 |
} |
|
85 |
|
|
86 |
entryLock.lock(); |
|
87 |
inputFile.set(this.inputFile); |
|
88 |
inputString.set(this.inputString); |
|
89 |
testing.set(this.testing); |
|
90 |
|
|
91 |
this.inputFile = null; |
|
92 |
this.inputString = null; |
|
93 |
entryLock.unlock(); |
|
94 |
|
|
95 |
return true; |
|
67 | 96 |
} |
68 | 97 |
|
69 | 98 |
@Override |
70 | 99 |
public void run() { |
71 | 100 |
super.run(); |
72 | 101 |
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) { |
|
102 |
AtomicReference<File> inputFile = new AtomicReference<File>(); |
|
103 |
AtomicReference<String> inputString = new AtomicReference<String>(); |
|
104 |
AtomicBoolean testing = new AtomicBoolean(); |
|
105 |
|
|
106 |
if (!getInput(inputFile, inputString, testing)) { |
|
80 | 107 |
continue; |
81 | 108 |
} |
109 |
|
|
110 |
if (inputFile.get() != null) { |
|
111 |
processA(inputFile.get(), testing.get()); |
|
112 |
} else if (inputString.get() != null) { |
|
113 |
processB(inputString.get().getBytes(), testing.get()); |
|
114 |
} |
|
82 | 115 |
} |
83 | 116 |
} |
84 | 117 |
|
85 |
private void process(File input) {
|
|
118 |
private void processA(File input, boolean testing) {
|
|
86 | 119 |
byte array[]; |
87 | 120 |
try { |
88 | 121 |
array = FileWorker.loadByteArray(input); |
... | ... | |
90 | 123 |
ui.loadingInputFileError(); |
91 | 124 |
return; |
92 | 125 |
} |
126 |
processB(array, testing); |
|
127 |
} |
|
128 |
|
|
129 |
private void processB(byte array[], boolean testing) { |
|
130 |
StringBuilder html = new StringBuilder(); |
|
131 |
StringBuilder json = new StringBuilder(); |
|
93 | 132 |
|
94 |
// TODO |
|
95 |
String json, html; |
|
96 | 133 |
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;
|
|
134 |
//Redirectovany system.out do null streamu. Mozno redirect do souboru
|
|
135 |
System.setOut(FileWorker.createRedirectStream());
|
|
136 |
convert(array, html, json); |
|
137 |
} catch (Exception e) {
|
|
138 |
json.setLength(0);
|
|
139 |
html.setLength(0);
|
|
140 |
} finally {
|
|
141 |
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
|
|
105 | 142 |
} |
106 |
ui.completed(json); |
|
143 |
|
|
144 |
ui.completed(html.toString(), json.toString(), testing); |
|
107 | 145 |
} |
108 | 146 |
|
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"); |
|
147 |
private void convert(byte[] buffer, StringBuilder html, StringBuilder json) throws Exception { |
|
120 | 148 |
jdeserialize deserializer = new jdeserialize(buffer); |
121 |
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); |
|
122 |
|
|
149 |
|
|
123 | 150 |
// gets the "contents" into an array - returnes the deserialization of all |
124 | 151 |
// 'writes' into serialized object via objectOutputStream |
125 | 152 |
List<content> cntnts = deserializer.getContent(); |
... | ... | |
130 | 157 |
{ |
131 | 158 |
//Parametrizovany toJson pomoci dictionary |
132 | 159 |
//Ciste HTML / Cisty JSON |
133 |
json += cnt.toJson("", null, this.jsonFormatting);
|
|
134 |
html += cnt.toJson("", null, this.htmlFormatting);
|
|
160 |
json.append(cnt.toJson("", null, this.jsonFormatting));
|
|
161 |
html.append(cnt.toJson("", null, this.htmlFormatting));
|
|
135 | 162 |
} |
136 | 163 |
} |
137 |
|
|
138 |
return new Pair<String, String>(json, html); |
|
139 | 164 |
} |
140 | 165 |
|
141 | 166 |
} |
Také k dispozici: Unified diff
re #7879 release v0.2
release v0.2