Projekt

Obecné

Profil

Stáhnout (2.58 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.OutputStream;
6
import java.io.PrintStream;
7
import java.util.List;
8

    
9
import jdeserialize.content;
10
import jdeserialize.jdeserialize;
11

    
12
public class Converter extends Thread {
13
	
14
	private Deserializer main;
15
	
16
	private File input;
17
	private File output;
18
	
19
	public Converter(Deserializer main, File inputFilename, File outputFilename) {
20
		this.main = main;
21
		this.input = inputFilename;
22
		this.output = outputFilename;
23
	}
24
	
25
	@Override
26
	public void run() {
27
		super.run();
28
		
29
		try {
30
			String json = convert(FileWorker.loadByteArray(input));
31
			
32
			String title = "Uložení JSON";
33
			if (FileWorker.saveJson(output, json)) {
34
				System.out.println("The JSON file was saved correctly.");
35
				Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku.");
36
			} else {
37
				System.out.println("There was an error saving the JSON file.");
38
				Report.error(title, null, "Při ukládání JSON souboru nastala chyba.");
39
			}
40
		} catch (IOException e) {
41
			System.out.println("There was an error loading the file.");
42
			e.printStackTrace();
43
			Report.error("Načítání souboru", null, "Při načítání souboru došlo k chybě.");
44
		}
45

    
46
		main.convertIsComplete();
47
	}
48
	
49
	private String convert(byte[] buffer) {
50
		String json = "";
51
		try {
52
			System.out.println("Deserialization begins.");
53

    
54
			//Redirectovany system.out do null streamu. Mozno redirect do souboru
55
			System.setOut(new PrintStream(OutputStream.nullOutputStream()));
56
			System.out.println("TEST STRING NOT SHOWING");
57
			jdeserialize deserializer = new jdeserialize(buffer);
58
			System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
59
			
60
			// gets the "contents" into an array - returnes the deserialization of all
61
            // 'writes' into serialized object via objectOutputStream
62
            List<content> cntnts = deserializer.getContent();
63

    
64
            // testing with only first of the contents
65
			//ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
66
			
67
			for(content cnt : cntnts)
68
			{
69
				if(cnt != null)
70
				{
71
					//Parametrizovany toJson pomoci dictionary
72
					//replace VS parametr
73
					//Pustit 2X s parametrem nenabori vnitrni data (kde muzou byt html znacky)
74
					//Ciste HTML / Cisty JSON
75
					json += cnt.toJson("", null);
76
				}
77
			}
78

    
79
			System.out.println("Task completed.");
80
			
81
			//just for tests
82
			System.out.println(json);
83
			
84
			return json;
85
		} catch (IOException e) {
86
			System.out.println("An error occurred while processing!");
87
			e.printStackTrace();
88
			return null;
89
		}
90
	}
91

    
92
}
(1-1/4)