Projekt

Obecné

Profil

Stáhnout (4.59 KB) Statistiky
| Větev: | Tag: | Revize:
1
import java.io.File;
2
import java.io.FileDescriptor;
3
import java.io.FileOutputStream;
4
import java.io.IOException;
5
import java.io.PrintStream;
6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.concurrent.Semaphore;
9
import java.util.concurrent.atomic.AtomicBoolean;
10
import java.util.concurrent.atomic.AtomicReference;
11
import java.util.concurrent.locks.Lock;
12
import java.util.concurrent.locks.ReentrantLock;
13

    
14
import jdeserialize.content;
15
import jdeserialize.jdeserialize;
16

    
17
import io.FileWorker;
18

    
19
public class Converter extends Thread {
20
    
21
    private IConversionResults ui;
22
	private AtomicBoolean active;
23
	
24
	// P-K without E semaphore.
25
	private Lock entryLock;
26
	private Semaphore inputAvailable;
27
    
28
	private File inputFile;
29
	private String inputString;
30
	private boolean testing;
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
	{
46
		htmlFormatting.put("indent", "<span style=\"margin-left:2em\">");
47
		htmlFormatting.put("lineBreak", "<br/>");
48
		htmlFormatting.put("classCol", "<span style=\"color:blue;font-weight:bold\">");
49
		htmlFormatting.put("fieldCol", "<span style=\"color:purple\">");
50
		htmlFormatting.put("valCol", "<span style=\"color:orange\">");
51
		htmlFormatting.put("keywordCol", "<span style=\"color:green\">");
52
		htmlFormatting.put("closeTagCol", "</span>");
53

    
54
		jsonFormatting.put("indent", "\t");
55
		jsonFormatting.put("lineBreak", "\n");
56
		jsonFormatting.put("classCol", "");
57
		jsonFormatting.put("fieldCol", "");
58
		jsonFormatting.put("valCol", "");
59
		jsonFormatting.put("keywordCol", "");
60
		jsonFormatting.put("closeTagCol", "");
61
	}
62
	
63
	public void end() {
64
		active.set(false);
65
		setInput(null, null, false);
66
	}
67
	
68
	public void setInput(File inputFile, String inputString, boolean testing) {
69
		entryLock.lock();
70
		this.inputFile = inputFile;
71
		this.inputString = inputString;
72
		this.testing = testing;
73
		entryLock.unlock();
74
		
75
		if (!inputAvailable.tryAcquire())
76
			inputAvailable.release(); // MAX VALUE = 1!
77
	}
78
	
79
	public boolean getInput(AtomicReference<File> inputFile, AtomicReference<String> inputString, AtomicBoolean testing) {
80
		try {
81
			inputAvailable.acquire();
82
		} catch (InterruptedException e) {
83
			return false;
84
		}
85
		
86
		entryLock.lock();
87
		inputFile.set(this.inputFile);
88
		inputString.set(this.inputString);
89
		testing.set(this.testing);
90
		
91
		this.inputFile = null;
92
		this.inputString = null;
93
		entryLock.unlock();
94
		
95
		return true;
96
	}
97
	
98
	@Override
99
	public void run() {
100
		super.run();
101
		while (active.get()) {
102
			AtomicReference<File> inputFile = new AtomicReference<File>();
103
			AtomicReference<String> inputString = new AtomicReference<String>();
104
			AtomicBoolean testing = new AtomicBoolean();
105
			
106
			if (!getInput(inputFile, inputString, testing)) {
107
				continue;
108
			}
109
			
110
			if (inputFile.get() != null) {
111
				processA(inputFile.get(), testing.get());
112
			} else if (inputString.get() != null) {
113
				processB(inputString.get().getBytes(), testing.get());
114
			}
115
		}
116
	}
117
	
118
	private void processA(File input, boolean testing) {
119
		byte array[];
120
		try {
121
			array = FileWorker.loadByteArray(input);
122
		} catch (IOException e) {
123
			ui.loadingInputFileError();
124
			return;
125
		}
126
		processB(array, testing);
127
	}
128
	
129
	private void processB(byte array[], boolean testing) {
130
		StringBuilder html = new StringBuilder();
131
		StringBuilder json = new StringBuilder();
132
		
133
		try {
134
			//Redirectovany system.out do null streamu. Mozno redirect do souboru
135
			System.setOut(FileWorker.createRedirectStream());
136
			convert(array, html, json);
137
		} catch (Exception e) {
138
			json.setLength(0);
139
			html.setLength(0);
140
		} finally {
141
			System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
142
		}
143
		
144
		ui.completed(html.toString(), json.toString(), testing);
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
		{
156
			if(cnt != null)
157
			{
158
				//Parametrizovany toJson pomoci dictionary
159
				//Ciste HTML / Cisty JSON
160
				json.append(cnt.toJson("", null, this.jsonFormatting));
161
				html.append(cnt.toJson("", null, this.htmlFormatting));
162
			}
163
		}
164
	}
165

    
166
}
(3-3/7)