Projekt

Obecné

Profil

Stáhnout (3.98 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.List;
9
import java.util.concurrent.Semaphore;
10
import java.util.concurrent.atomic.AtomicBoolean;
11
import java.util.concurrent.atomic.AtomicReference;
12
import java.util.concurrent.locks.Lock;
13
import java.util.concurrent.locks.ReentrantLock;
14

    
15
import jdeserialize.content;
16
import jdeserialize.jdeserialize;
17
import io.Database.DB_Messenger;
18
import io.Database;
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
	// Input.
31
	private File inputFile;
32
	private DB_Messenger query;
33

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

    
41
	public void end() {
42
		active.set(false);
43
		setInput(null, null);
44
	}
45
	
46
	public void setInput(File inputFile, DB_Messenger query) {
47
		entryLock.lock();
48
		this.inputFile = inputFile;
49
		this.query = query;
50
		entryLock.unlock();
51

    
52
		if (!inputAvailable.tryAcquire())
53
			inputAvailable.release(); // MAX VALUE = 1!
54
	}
55
	
56
	public boolean getInput(AtomicReference<File> inputFile, AtomicReference<DB_Messenger> query) {
57
		try {
58
			inputAvailable.acquire();
59
		} catch (InterruptedException e) {
60
			return false;
61
		}
62

    
63
		entryLock.lock();
64
		inputFile.set(this.inputFile);
65
		query.set(this.query);
66
		
67
		this.inputFile = null;
68
		this.query = null;
69
		entryLock.unlock();
70

    
71
		return inputFile.get() != null || query.get() != null;
72
	}
73

    
74
	@Override
75
	public void run() {
76
		super.run();
77
		while (active.get()) {
78
			AtomicReference<File> inputFile = new AtomicReference<File>();
79
			AtomicReference<DB_Messenger> query = new AtomicReference<DB_Messenger>();
80

    
81
			if (!getInput(inputFile, query)) {
82
				continue;
83
			}
84

    
85
			byte array[] = getBytes(inputFile.get(), query.get());
86
			StringBuilder html = new StringBuilder();
87
			StringBuilder json = new StringBuilder();
88
			
89
			try {
90
				// Redirectovany system.out do null streamu. Mozno redirect do souboru
91
				System.setOut(FileWorker.createRedirectStream());
92
				convert(array, html, json);
93
			} catch (Exception e) {
94
				json.setLength(0);
95
				html.setLength(0);
96
			} finally {
97
				System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
98
			}
99

    
100
			//test file output
101
			try {
102
				BufferedWriter writer = new BufferedWriter(new FileWriter("html.txt"));
103
				writer.write(html.toString().replace("<br/>", "\n")); // TODO pridan replace pro odsazovani - lepsi orientace v souboru.
104
				writer.close();
105

    
106
				BufferedWriter writer2 = new BufferedWriter(new FileWriter("json.txt"));
107
				writer2.write(json.toString());
108
				writer2.close();
109
			} catch (IOException e) {
110
				// TODO Auto-generated catch block
111
				e.printStackTrace();
112
			}
113
			//test file output
114
			
115
			ui.completed(html.toString(), json.toString(), array == null ? null : new String(array));
116
		}
117
	}
118

    
119
	private byte[] getBytes(File file, DB_Messenger query) {
120
		byte array[];
121
		try {
122
			if (file != null) {
123
				array = FileWorker.loadFileContent(file);
124
			} else {
125
				array = Database.getBlobBytes(query);
126
			}
127
			array = FileWorker.unzipFile(array);
128
		} catch (IOException e) {
129
			e.printStackTrace();
130
			ui.loadingInputFileError();
131
			return null;
132
		}
133
		return array;
134
	}
135
	
136
	private void convert(byte[] buffer, StringBuilder html, StringBuilder json) throws Exception {
137
		jdeserialize deserializer = new jdeserialize(buffer);
138

    
139
		// gets the "contents" into an array - returnes the deserialization of all
140
		// 'writes' into serialized object via objectOutputStream
141
		List<content> cntnts = deserializer.getContent();
142

    
143
		for (content cnt : cntnts) {
144
			if (cnt != null) {
145
				// Parametrizovany toJson pomoci dictionary
146
				// Ciste HTML / Cisty JSON
147
				json.append(cnt.toJson("", null, false));
148
				html.append(cnt.toJson("", null, false));
149
			}
150
		}
151
	}
152

    
153
}
(2-2/6)