Projekt

Obecné

Profil

Stáhnout (4.93 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 query;
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 query, boolean testing) {
70
		entryLock.lock();
71
		this.inputFile = inputFile;
72
		this.query = query;
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> query, AtomicBoolean testing) {
81
		try {
82
			inputAvailable.acquire();
83
		} catch (InterruptedException e) {
84
			return false;
85
		}
86

    
87
		entryLock.lock();
88
		inputFile.set(this.inputFile);
89
		query.set(this.query);
90
		testing.set(this.testing);
91

    
92
		this.inputFile = null;
93
		this.query = null;
94
		entryLock.unlock();
95

    
96
		return true;
97
	}
98

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

    
107
			if (!getInput(inputFile, query, testing)) {
108
				continue;
109
			}
110

    
111
			if (inputFile.get() != null) {
112
				processA(inputFile.get(), testing.get());
113
			} else if (query.get() != null) {
114
				processB(query.get().getBytes(), testing.get());
115
			}
116
		}
117
	}
118

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

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

    
134
		try {
135
			// Redirectovany system.out do null streamu. Mozno redirect do souboru
136
			System.setOut(FileWorker.createRedirectStream());
137
			convert(array, html, json);
138
		} catch (Exception e) {
139
			json.setLength(0);
140
			html.setLength(0);
141
		} finally {
142
			System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
143
		}
144

    
145
		//test file output
146
		try {
147
			BufferedWriter writer = new BufferedWriter(new FileWriter("html.txt"));
148
			writer.write(html.toString());
149
			writer.close();
150

    
151
			BufferedWriter writer2 = new BufferedWriter(new FileWriter("json.txt"));
152
			writer2.write(json.toString());
153
			writer2.close();
154
		} catch (IOException e) {
155
			// TODO Auto-generated catch block
156
			e.printStackTrace();
157
		}
158
		//test file output
159

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

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

    
166
		// gets the "contents" into an array - returnes the deserialization of all
167
		// 'writes' into serialized object via objectOutputStream
168
		List<content> cntnts = deserializer.getContent();
169

    
170
		for (content cnt : cntnts) {
171
			if (cnt != null) {
172
				// Parametrizovany toJson pomoci dictionary
173
				// Ciste HTML / Cisty JSON
174
				json.append(cnt.toJson("", null, this.jsonFormatting, false));
175
				html.append(cnt.toJson("", null, this.htmlFormatting, false));
176
			}
177
		}
178
	}
179

    
180
}
(3-3/7)