Projekt

Obecné

Profil

Stáhnout (5 KB) Statistiky
| Větev: | Tag: | Revize:
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

    
19
import io.FileWorker;
20

    
21
public class Converter extends Thread {
22

    
23
	private IConversionResults ui;
24
	private AtomicBoolean active;
25

    
26
	// P-K without E semaphore.
27
	private Lock entryLock;
28
	private Semaphore inputAvailable;
29

    
30
	private File inputFile;
31
	private String inputString;
32
	private boolean testing;
33

    
34
	// Key as enum?
35
	private HashMap<String, String> htmlFormatting = new HashMap<String, String>();
36
	private HashMap<String, String> jsonFormatting = new HashMap<String, String>();
37

    
38
	public Converter(IConversionResults ui) {
39
		this.ui = ui;
40
		active = new AtomicBoolean(true);
41
		entryLock = new ReentrantLock();
42
		inputAvailable = new Semaphore(0);
43
		fillInTestingDictionaries();
44
	}
45

    
46
	private void fillInTestingDictionaries() {
47
		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
	}
63

    
64
	public void end() {
65
		active.set(false);
66
		setInput(null, null, false);
67
	}
68

    
69
	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

    
76
		if (!inputAvailable.tryAcquire())
77
			inputAvailable.release(); // MAX VALUE = 1!
78
	}
79

    
80
	public boolean getInput(AtomicReference<File> inputFile, AtomicReference<String> inputString,
81
			AtomicBoolean testing) {
82
		try {
83
			inputAvailable.acquire();
84
		} catch (InterruptedException e) {
85
			return false;
86
		}
87

    
88
		entryLock.lock();
89
		inputFile.set(this.inputFile);
90
		inputString.set(this.inputString);
91
		testing.set(this.testing);
92

    
93
		this.inputFile = null;
94
		this.inputString = null;
95
		entryLock.unlock();
96

    
97
		return true;
98
	}
99

    
100
	@Override
101
	public void run() {
102
		super.run();
103
		while (active.get()) {
104
			AtomicReference<File> inputFile = new AtomicReference<File>();
105
			AtomicReference<String> inputString = new AtomicReference<String>();
106
			AtomicBoolean testing = new AtomicBoolean();
107

    
108
			if (!getInput(inputFile, inputString, testing)) {
109
				continue;
110
			}
111

    
112
			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
		}
118
	}
119

    
120
	private void processA(File input, boolean testing) {
121
		byte array[];
122
		try {
123
			array = FileWorker.loadByteArray(input);
124
		} catch (IOException e) {
125
			ui.loadingInputFileError();
126
			return;
127
		}
128
		processB(array, testing);
129
	}
130

    
131
	private void processB(byte array[], boolean testing) {
132
		StringBuilder html = new StringBuilder();
133
		StringBuilder json = new StringBuilder();
134

    
135
		try {
136
			// Redirectovany system.out do null streamu. Mozno redirect do souboru
137
			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
		}
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

    
161
		ui.completed(html.toString(), json.toString(), testing);
162
	}
163

    
164
	private void convert(byte[] buffer, StringBuilder html, StringBuilder json) throws Exception {
165
		jdeserialize deserializer = new jdeserialize(buffer);
166

    
167
		// gets the "contents" into an array - returnes the deserialization of all
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
175
				json.append(cnt.toJson("", null, this.jsonFormatting, false));
176
				html.append(cnt.toJson("", null, this.htmlFormatting, false));
177
			}
178
		}
179
	}
180

    
181
}
(3-3/7)