Projekt

Obecné

Profil

Stáhnout (4.49 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
import io.FileWorker;
19

    
20
public class Converter extends Thread {
21

    
22
	private IConversionResults ui;
23
	private AtomicBoolean active;
24

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

    
29
	// Input.
30
	private File inputFile;
31

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

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

    
44
	private void fillInTestingDictionaries() {
45
		htmlFormatting.put("indent", "<span style=\"margin-left:2em;\">");
46
		htmlFormatting.put("lineBreak", "<br/>");
47
		htmlFormatting.put("classCol", "<span style=\"color:blue;font-weight:bold;\">");
48
		htmlFormatting.put("fieldCol", "<span style=\"color:purple;\">");
49
		htmlFormatting.put("valCol", "<span style=\"color:orange;\">");
50
		htmlFormatting.put("keywordCol", "<span style=\"color:green;\">");
51
		htmlFormatting.put("closeTagCol", "</span>");
52

    
53
		jsonFormatting.put("indent", "\t");
54
		jsonFormatting.put("lineBreak", "\n");
55
		jsonFormatting.put("classCol", "");
56
		jsonFormatting.put("fieldCol", "");
57
		jsonFormatting.put("valCol", "");
58
		jsonFormatting.put("keywordCol", "");
59
		jsonFormatting.put("closeTagCol", "");
60
	}
61

    
62
	public void end() {
63
		active.set(false);
64
		setInput(null);
65
	}
66
	
67
	public void setInput(File inputFile) {
68
		entryLock.lock();
69
		this.inputFile = inputFile;
70
		entryLock.unlock();
71

    
72
		if (!inputAvailable.tryAcquire())
73
			inputAvailable.release(); // MAX VALUE = 1!
74
	}
75
	
76
	public boolean getInput(AtomicReference<File> inputFile) {
77
		try {
78
			inputAvailable.acquire();
79
		} catch (InterruptedException e) {
80
			return false;
81
		}
82

    
83
		entryLock.lock();
84
		inputFile.set(this.inputFile);
85
		
86
		this.inputFile = null;
87
		entryLock.unlock();
88

    
89
		return inputFile.get() != null;
90
	}
91

    
92
	@Override
93
	public void run() {
94
		super.run();
95
		while (active.get()) {
96
			AtomicReference<File> inputFile = new AtomicReference<File>();
97

    
98
			if (!getInput(inputFile)) {
99
				continue;
100
			}
101

    
102
			byte array[] = getBytes(inputFile.get());
103
			StringBuilder html = new StringBuilder();
104
			StringBuilder json = new StringBuilder();
105
			
106
			try {
107
				// Redirectovany system.out do null streamu. Mozno redirect do souboru
108
				System.setOut(FileWorker.createRedirectStream());
109
				convert(array, html, json);
110
			} catch (Exception e) {
111
				json.setLength(0);
112
				html.setLength(0);
113
			} finally {
114
				System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
115
			}
116

    
117
			//test file output
118
			try {
119
				BufferedWriter writer = new BufferedWriter(new FileWriter("html.txt"));
120
				writer.write(html.toString().replace("<br/>", "\n")); // TODO pridan replace pro odsazovani - lepsi orientace v souboru.
121
				writer.close();
122

    
123
				BufferedWriter writer2 = new BufferedWriter(new FileWriter("json.txt"));
124
				writer2.write(json.toString());
125
				writer2.close();
126
			} catch (IOException e) {
127
				// TODO Auto-generated catch block
128
				e.printStackTrace();
129
			}
130
			//test file output
131
			
132
			ui.completed(html.toString(), json.toString(), new String(array));
133
		}
134
	}
135

    
136
	private byte[] getBytes(File input) {
137
		byte array[];
138
		try {
139
			array = FileWorker.loadByteArray(input);
140
		} catch (IOException e) {
141
			ui.loadingInputFileError();
142
			return null;
143
		}
144
		return array;
145
	}
146
	
147
	private void convert(byte[] buffer, StringBuilder html, StringBuilder json) throws Exception {
148
		jdeserialize deserializer = new jdeserialize(buffer);
149

    
150
		// 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
			if (cnt != null) {
156
				// Parametrizovany toJson pomoci dictionary
157
				// Ciste HTML / Cisty JSON
158
				json.append(cnt.toJson("", null, this.jsonFormatting, false));
159
				html.append(cnt.toJson("", null, this.htmlFormatting, false));
160
			}
161
		}
162
	}
163

    
164
}
(2-2/6)