Projekt

Obecné

Profil

Stáhnout (5 KB) Statistiky
| Větev: | Tag: | Revize:
1 81ff7da4 @havlijan17
import java.io.BufferedWriter;
2 03a2d08f Michal Horký
import java.io.File;
3 e8ba39e7 horkmi
import java.io.FileDescriptor;
4
import java.io.FileOutputStream;
5 81ff7da4 @havlijan17
import java.io.FileWriter;
6 03a2d08f Michal Horký
import java.io.IOException;
7 e8ba39e7 horkmi
import java.io.PrintStream;
8
import java.util.HashMap;
9 03a2d08f Michal Horký
import java.util.List;
10 49fd8648 Michal Horký
import java.util.concurrent.Semaphore;
11
import java.util.concurrent.atomic.AtomicBoolean;
12
import java.util.concurrent.atomic.AtomicReference;
13 a5d16f0a horkmi
import java.util.concurrent.locks.Lock;
14
import java.util.concurrent.locks.ReentrantLock;
15 03a2d08f Michal Horký
16
import jdeserialize.content;
17
import jdeserialize.jdeserialize;
18
19 49fd8648 Michal Horký
import io.FileWorker;
20
21 03a2d08f Michal Horký
public class Converter extends Thread {
22 81ff7da4 @havlijan17
23
	private IConversionResults ui;
24 49fd8648 Michal Horký
	private AtomicBoolean active;
25 81ff7da4 @havlijan17
26 a5d16f0a horkmi
	// P-K without E semaphore.
27
	private Lock entryLock;
28 49fd8648 Michal Horký
	private Semaphore inputAvailable;
29 81ff7da4 @havlijan17
30 a5d16f0a horkmi
	private File inputFile;
31
	private String inputString;
32
	private boolean testing;
33 81ff7da4 @havlijan17
34
	// Key as enum?
35 e8ba39e7 horkmi
	private HashMap<String, String> htmlFormatting = new HashMap<String, String>();
36
	private HashMap<String, String> jsonFormatting = new HashMap<String, String>();
37 81ff7da4 @havlijan17
38 49fd8648 Michal Horký
	public Converter(IConversionResults ui) {
39
		this.ui = ui;
40
		active = new AtomicBoolean(true);
41 a5d16f0a horkmi
		entryLock = new ReentrantLock();
42 49fd8648 Michal Horký
		inputAvailable = new Semaphore(0);
43 81ff7da4 @havlijan17
		fillInTestingDictionaries();
44 e8ba39e7 horkmi
	}
45
46 81ff7da4 @havlijan17
	private void fillInTestingDictionaries() {
47 e8ba39e7 horkmi
		htmlFormatting.put("indent", "<span style=\"margin-left:2em\">");
48
		htmlFormatting.put("lineBreak", "<br/>");
49
		htmlFormatting.put("classCol", "<span style=\"color:blue;font-weight:bold\">");
50
		htmlFormatting.put("fieldCol", "<span style=\"color:purple\">");
51
		htmlFormatting.put("valCol", "<span style=\"color:orange\">");
52
		htmlFormatting.put("keywordCol", "<span style=\"color:green\">");
53
		htmlFormatting.put("closeTagCol", "</span>");
54
55
		jsonFormatting.put("indent", "\t");
56
		jsonFormatting.put("lineBreak", "\n");
57
		jsonFormatting.put("classCol", "");
58
		jsonFormatting.put("fieldCol", "");
59
		jsonFormatting.put("valCol", "");
60
		jsonFormatting.put("keywordCol", "");
61
		jsonFormatting.put("closeTagCol", "");
62 49fd8648 Michal Horký
	}
63 81ff7da4 @havlijan17
64 49fd8648 Michal Horký
	public void end() {
65
		active.set(false);
66 a5d16f0a horkmi
		setInput(null, null, false);
67 49fd8648 Michal Horký
	}
68 81ff7da4 @havlijan17
69 a5d16f0a horkmi
	public void setInput(File inputFile, String inputString, boolean testing) {
70
		entryLock.lock();
71
		this.inputFile = inputFile;
72
		this.inputString = inputString;
73
		this.testing = testing;
74
		entryLock.unlock();
75 81ff7da4 @havlijan17
76 a5d16f0a horkmi
		if (!inputAvailable.tryAcquire())
77
			inputAvailable.release(); // MAX VALUE = 1!
78
	}
79 81ff7da4 @havlijan17
80
	public boolean getInput(AtomicReference<File> inputFile, AtomicReference<String> inputString,
81
			AtomicBoolean testing) {
82 a5d16f0a horkmi
		try {
83
			inputAvailable.acquire();
84
		} catch (InterruptedException e) {
85
			return false;
86
		}
87 81ff7da4 @havlijan17
88 a5d16f0a horkmi
		entryLock.lock();
89
		inputFile.set(this.inputFile);
90
		inputString.set(this.inputString);
91
		testing.set(this.testing);
92 81ff7da4 @havlijan17
93 a5d16f0a horkmi
		this.inputFile = null;
94
		this.inputString = null;
95
		entryLock.unlock();
96 81ff7da4 @havlijan17
97 a5d16f0a horkmi
		return true;
98 03a2d08f Michal Horký
	}
99 81ff7da4 @havlijan17
100 03a2d08f Michal Horký
	@Override
101
	public void run() {
102
		super.run();
103 49fd8648 Michal Horký
		while (active.get()) {
104 a5d16f0a horkmi
			AtomicReference<File> inputFile = new AtomicReference<File>();
105
			AtomicReference<String> inputString = new AtomicReference<String>();
106
			AtomicBoolean testing = new AtomicBoolean();
107 81ff7da4 @havlijan17
108 a5d16f0a horkmi
			if (!getInput(inputFile, inputString, testing)) {
109 49fd8648 Michal Horký
				continue;
110
			}
111 81ff7da4 @havlijan17
112 a5d16f0a horkmi
			if (inputFile.get() != null) {
113
				processA(inputFile.get(), testing.get());
114
			} else if (inputString.get() != null) {
115
				processB(inputString.get().getBytes(), testing.get());
116
			}
117 49fd8648 Michal Horký
		}
118
	}
119 81ff7da4 @havlijan17
120 a5d16f0a horkmi
	private void processA(File input, boolean testing) {
121 49fd8648 Michal Horký
		byte array[];
122
		try {
123
			array = FileWorker.loadByteArray(input);
124
		} catch (IOException e) {
125
			ui.loadingInputFileError();
126
			return;
127
		}
128 a5d16f0a horkmi
		processB(array, testing);
129
	}
130 81ff7da4 @havlijan17
131 a5d16f0a horkmi
	private void processB(byte array[], boolean testing) {
132
		StringBuilder html = new StringBuilder();
133
		StringBuilder json = new StringBuilder();
134 81ff7da4 @havlijan17
135 03a2d08f Michal Horký
		try {
136 81ff7da4 @havlijan17
			// Redirectovany system.out do null streamu. Mozno redirect do souboru
137 a5d16f0a horkmi
			System.setOut(FileWorker.createRedirectStream());
138
			convert(array, html, json);
139
		} catch (Exception e) {
140
			json.setLength(0);
141
			html.setLength(0);
142
		} finally {
143
			System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
144 03a2d08f Michal Horký
		}
145 81ff7da4 @havlijan17
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
161 a5d16f0a horkmi
		ui.completed(html.toString(), json.toString(), testing);
162 03a2d08f Michal Horký
	}
163 81ff7da4 @havlijan17
164 a5d16f0a horkmi
	private void convert(byte[] buffer, StringBuilder html, StringBuilder json) throws Exception {
165 49fd8648 Michal Horký
		jdeserialize deserializer = new jdeserialize(buffer);
166 a5d16f0a horkmi
167 49fd8648 Michal Horký
		// gets the "contents" into an array - returnes the deserialization of all
168 81ff7da4 @havlijan17
		// '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
175 a6f38344 @havlijan17
				json.append(cnt.toJson("", null, this.jsonFormatting, false));
176
				html.append(cnt.toJson("", null, this.htmlFormatting, false));
177 e3d5fc53 @havlijan17
			}
178 03a2d08f Michal Horký
		}
179
	}
180
181
}