Projekt

Obecné

Profil

Stáhnout (2 KB) Statistiky
| Větev: | Tag: | Revize:
1
import java.io.File;
2
import java.io.IOException;
3
import java.util.List;
4

    
5
import org.codehaus.jackson.map.ObjectMapper;
6
import org.codehaus.jackson.map.ObjectWriter;
7

    
8
import jdeserialize.content;
9
import jdeserialize.jdeserialize;
10

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

    
45
		main.convertIsComplete();
46
	}
47
	
48
	private String convert(byte[] buffer) {
49
		try {
50
			System.out.println("Deserialization begins.");
51
			jdeserialize deserializer = new jdeserialize(buffer);
52
			
53
			// gets the "contents" into an array - returnes the deserialization of all
54
            // 'writes' into serialized object via objectOutputStream
55
            List<content> cntnts = deserializer.getContent();
56

    
57
            // testing with only first of the contents
58
            ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
59
            String json = ow.writeValueAsString(cntnts.get(0));
60
            
61
            System.out.println("Task completed.");
62
            return json;
63
		} catch (IOException e) {
64
			System.out.println("An error occurred while processing!");
65
			e.printStackTrace();
66
			return null;
67
		}
68
	}
69

    
70
}
(1-1/4)