Projekt

Obecné

Profil

Stáhnout (3.2 KB) Statistiky
| Větev: | Tag: | Revize:
1 03a2d08f Michal Horký
import java.io.File;
2 53949177 @havlijan17
import java.io.FileDescriptor;
3
import java.io.FileOutputStream;
4 03a2d08f Michal Horký
import java.io.IOException;
5 53949177 @havlijan17
import java.io.OutputStream;
6
import java.io.PrintStream;
7 02390102 @havlijan17
import java.util.HashMap;
8 03a2d08f Michal Horký
import java.util.List;
9 02390102 @havlijan17
import java.util.Map;
10 03a2d08f Michal Horký
11
import jdeserialize.content;
12
import jdeserialize.jdeserialize;
13
14
public class Converter extends Thread {
15
	private Deserializer main;
16
	
17
	private File input;
18
	private File output;
19 02390102 @havlijan17
20
	//Key as enum?
21
	private HashMap<String, String> html = new HashMap<String, String>();
22
	private HashMap<String, String> json = new HashMap<String, String>();
23 03a2d08f Michal Horký
	
24
	public Converter(Deserializer main, File inputFilename, File outputFilename) {
25
		this.main = main;
26
		this.input = inputFilename;
27
		this.output = outputFilename;
28 02390102 @havlijan17
		
29
		fillInTestingDictionaries();
30
	}
31
32
	private void fillInTestingDictionaries()
33
	{
34
		html.put("indent", "");
35
		html.put("lineBreak", "");
36
		html.put("classCol", "");
37
		html.put("fieldCol", "");
38
		html.put("valCol", "");
39
		html.put("keywordCol", "");
40
41
		json.put("indent", "\t");
42
		json.put("lineBreak", "\n");
43
		json.put("classCol", "");
44
		json.put("fieldCol", "");
45
		json.put("valCol", "");
46
		json.put("keywordCol", "");
47 03a2d08f Michal Horký
	}
48
	
49
	@Override
50
	public void run() {
51
		super.run();
52
		
53
		try {
54
			String json = convert(FileWorker.loadByteArray(input));
55
			
56 b639237c Michal Horký
			String title = "Uložení JSON";
57 03a2d08f Michal Horký
			if (FileWorker.saveJson(output, json)) {
58
				System.out.println("The JSON file was saved correctly.");
59 b639237c Michal Horký
				Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku.");
60 03a2d08f Michal Horký
			} else {
61
				System.out.println("There was an error saving the JSON file.");
62 b639237c Michal Horký
				Report.error(title, null, "Při ukládání JSON souboru nastala chyba.");
63 03a2d08f Michal Horký
			}
64
		} catch (IOException e) {
65
			System.out.println("There was an error loading the file.");
66
			e.printStackTrace();
67 b639237c Michal Horký
			Report.error("Načítání souboru", null, "Při načítání souboru došlo k chybě.");
68 03a2d08f Michal Horký
		}
69
70
		main.convertIsComplete();
71
	}
72
	
73
	private String convert(byte[] buffer) {
74 e3d5fc53 @havlijan17
		String json = "";
75 03a2d08f Michal Horký
		try {
76
			System.out.println("Deserialization begins.");
77 53949177 @havlijan17
78
			//Redirectovany system.out do null streamu. Mozno redirect do souboru
79
			System.setOut(new PrintStream(OutputStream.nullOutputStream()));
80
			System.out.println("TEST STRING NOT SHOWING");
81 03a2d08f Michal Horký
			jdeserialize deserializer = new jdeserialize(buffer);
82 53949177 @havlijan17
			System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
83 03a2d08f Michal Horký
			
84
			// gets the "contents" into an array - returnes the deserialization of all
85
            // 'writes' into serialized object via objectOutputStream
86
            List<content> cntnts = deserializer.getContent();
87
88
            // testing with only first of the contents
89 171c20d6 @havlijan17
			//ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
90 e3d5fc53 @havlijan17
			
91
			for(content cnt : cntnts)
92
			{
93
				if(cnt != null)
94
				{
95 53949177 @havlijan17
					//Parametrizovany toJson pomoci dictionary
96
					//replace VS parametr
97
					//Pustit 2X s parametrem nenabori vnitrni data (kde muzou byt html znacky)
98
					//Ciste HTML / Cisty JSON
99 02390102 @havlijan17
					json += cnt.toJson("", null, this.json);
100 e3d5fc53 @havlijan17
				}
101
			}
102
103
			System.out.println("Task completed.");
104
			
105
			//just for tests
106 a0511590 @havlijan17
			System.out.println(json);
107 e3d5fc53 @havlijan17
			
108
			return json;
109 03a2d08f Michal Horký
		} catch (IOException e) {
110
			System.out.println("An error occurred while processing!");
111
			e.printStackTrace();
112
			return null;
113
		}
114
	}
115
116
}