Projekt

Obecné

Profil

« Předchozí | Další » 

Revize d6ee4876

Přidáno uživatelem Michal Horký před asi 4 roky(ů)

re #7879

Converter.java a instance.java changed

Zobrazit rozdíly:

project/Deserializer/bcha.json
2 2
	class samples.ok.Simple : {
3 3
		"testDouble" : 1.0, 
4 4
		"testInt" : 123, 
5
		"intArray" : [ 5, 10, 15 ], 
5
		"intArray" : [  ], 
6 6
		"locInnerClassInstance" : {
7
			class samples.ok.Inner : {
8
				"vnitrni" : "INNER STRING AM I"
9
			}
10
		}, 
7
		class samples.ok.Inner : {
8
			"vnitrni" : "INNER STRING AM I"
9
		}
10
}, 
11 11
		"localEnum" : LOW, 
12 12
		"pozdrav" : "AHOJ", 
13 13
		"stringArray" : [ "A", "B", "C" ]
project/Deserializer/src/Converter.java
1 1
import java.io.File;
2
import java.io.FileDescriptor;
3
import java.io.FileOutputStream;
2 4
import java.io.IOException;
5
import java.io.OutputStream;
6
import java.io.PrintStream;
7
import java.util.HashMap;
3 8
import java.util.List;
4 9
import java.util.concurrent.Semaphore;
5 10
import java.util.concurrent.atomic.AtomicBoolean;
6 11
import java.util.concurrent.atomic.AtomicReference;
12
import java.util.Map;
7 13

  
14
import javafx.util.Pair;
8 15
import jdeserialize.content;
9 16
import jdeserialize.jdeserialize;
10 17

  
11 18
import io.FileWorker;
12 19

  
13 20
public class Converter extends Thread {
14
	
15
	private IConversionResults ui;
21
    
22
    private IConversionResults ui;
16 23
	private AtomicBoolean active;
17 24
	
18 25
	private AtomicReference<File> input;
19 26
	private Semaphore inputAvailable;
27
    
28
    //Key as enum?
29
	private HashMap<String, String> htmlFormatting = new HashMap<String, String>();
30
	private HashMap<String, String> jsonFormatting = new HashMap<String, String>();
20 31
	
21 32
	public Converter(IConversionResults ui) {
22 33
		this.ui = ui;
23 34
		active = new AtomicBoolean(true);
24 35
		input = new AtomicReference<File>(null);
25 36
		inputAvailable = new Semaphore(0);
37
        fillInTestingDictionaries();
38
	}
39

  
40
	private void fillInTestingDictionaries()
41
	{
42
		htmlFormatting.put("indent", "<span style=\"margin-left:2em\">");
43
		htmlFormatting.put("lineBreak", "<br/>");
44
		htmlFormatting.put("classCol", "<span style=\"color:blue;font-weight:bold\">");
45
		htmlFormatting.put("fieldCol", "<span style=\"color:purple\">");
46
		htmlFormatting.put("valCol", "<span style=\"color:orange\">");
47
		htmlFormatting.put("keywordCol", "<span style=\"color:green\">");
48
		htmlFormatting.put("closeTagCol", "</span>");
49

  
50
		jsonFormatting.put("indent", "\t");
51
		jsonFormatting.put("lineBreak", "\n");
52
		jsonFormatting.put("classCol", "");
53
		jsonFormatting.put("fieldCol", "");
54
		jsonFormatting.put("valCol", "");
55
		jsonFormatting.put("keywordCol", "");
56
		jsonFormatting.put("closeTagCol", "");
26 57
	}
27 58
	
28 59
	public void end() {
......
60 91
			return;
61 92
		}
62 93
		
63
		String json;
94
		// TODO
95
		String json, html;
64 96
		try {
65
			json = convert(array);
97
			//The Pair is not the best way
98
			Pair<String, String> returned = convert(FileWorker.loadByteArray(input));
99
			
100
			json = returned.getKey();
101
			html = returned.getValue();
66 102
		} catch (IOException e) {
67 103
			json = null;
104
			html = null;
68 105
		}
69 106
		ui.completed(json);
70 107
	}
71 108
	
72
	private String convert(byte[] buffer) throws IOException {
109
	private Pair<String, String> convert(byte[] buffer) throws IOException {
73 110
		String json = "";
74
		
111
		String html = "";
112

  
113
		//Redirectovany system.out do null streamu. Mozno redirect do souboru
114
		System.setOut(new PrintStream(new OutputStream() {
115
            public void write(int b) {
116
            	// TODO nejspise do souboru.
117
            }
118
        }));
119
		System.out.println("TEST STRING NOT SHOWING");
75 120
		jdeserialize deserializer = new jdeserialize(buffer);
121
		System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
76 122
		
77 123
		// gets the "contents" into an array - returnes the deserialization of all
78 124
        // 'writes' into serialized object via objectOutputStream
79 125
        List<content> cntnts = deserializer.getContent();
80 126

  
81
        // testing with only first of the contents
82
		//ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
83
		
84 127
		for(content cnt : cntnts)
85 128
		{
86 129
			if(cnt != null)
87 130
			{
88
				json += cnt.toString();
131
				//Parametrizovany toJson pomoci dictionary
132
				//Ciste HTML / Cisty JSON
133
				json += cnt.toJson("", null, this.jsonFormatting);
134
				html += cnt.toJson("", null, this.htmlFormatting);
89 135
			}
90 136
		}
91 137
		
92
		return json;
138
		return new Pair<String, String>(json, html);
93 139
	}
94 140

  
95 141
}
project/Deserializer/src/jdeserialize/arrayobj.java
1 1
package jdeserialize;
2 2

  
3 3
/**
4
 * <p>Represents an array instance, including the values the comprise the array.  </p>
4
 * <p>
5
 * Represents an array instance, including the values the comprise the array.
6
 * </p>
5 7
 *
6
 * <p>Note that in arrays of primitives, the classdesc will be named "[x", where x is the
7
 * field type code representing the primitive type.  See jdeserialize.resolveJavaType()
8
 * for an example of analysis/generation of human-readable names from these class names.</p>
8
 * <p>
9
 * Note that in arrays of primitives, the classdesc will be named "[x", where x
10
 * is the field type code representing the primitive type. See
11
 * jdeserialize.resolveJavaType() for an example of analysis/generation of
12
 * human-readable names from these class names.
13
 * </p>
9 14
 */
10 15
public class arrayobj extends contentbase {
11 16
    /**
......
24 29
        this.classdesc = cd;
25 30
        this.data = data;
26 31
    }
27
    public String toString() {
32

  
33
    public @Override String toJson(String indentation) {
28 34
        StringBuilder sb = new StringBuilder();
29 35
        sb.append("[ ");
30
        for(Object el : this.data)
31
        {
32
            sb.append(el.toString());
33
            if(!el.equals(this.data.get(this.data.size()-1)))
34
            {
35
                sb.append(", ");
36

  
37
        for (Object el : this.data) {
38
            if (el != null && el instanceof content) {
39
                sb.append(((content)el).toJson(indentation));
40
                // el null!
41
                if (!el.equals(this.data.get(this.data.size() - 1))) {
42
                    sb.append(", ");
43
                }
36 44
            }
37 45
        }
38 46
        sb.append(" ]");
39 47
        return sb.toString();
40 48
    }
41
}
42 49

  
50
    public String toString() {
51
        return "[array " + jdeserialize.hex(handle) + " classdesc " + classdesc.toString() + ": " + data.toString()
52
                + "]";
53
    }
54
}
project/Deserializer/src/jdeserialize/content.java
64 64
     * @param value the new value to use
65 65
     */
66 66
    public void setIsExceptionObject(boolean value);
67

  
68
    public String toJson(String indentation);
67 69
}
68 70

  
project/Deserializer/src/jdeserialize/contentbase.java
24 24
    }
25 25
    public void validate() throws ValidityException {
26 26
    }
27

  
28
    public @Override String toJson(String indetation)
29
    {
30
        return this.toString();
31
    }
27 32
}
28 33

  
project/Deserializer/src/jdeserialize/enumobj.java
31 31
        this.classdesc = cd;
32 32
        this.value = so;
33 33
    }
34
    public String toString() {
34
    public @Override String toJson(String indetation) {
35 35
        return this.value.value;
36 36
    }
37

  
38
    public String toString(){
39
        return "[enum " + jdeserialize.hex(handle) + ": " + value.value + "]";
40
    }
37 41
}
project/Deserializer/src/jdeserialize/instance.java
26 26
        this.fielddata = new HashMap<classdesc, Map<field, Object>>();
27 27
    }
28 28

  
29
    
30
    public @Override String toJson(String indentation, Map<classdesc, Map<field, Object>> fielddata, Map<String, String> formatting)
31
    {
32
        return this.classdesc.toJson(indentation, this.fielddata, formatting);
33
    }
34
    
29 35
    public String toString() {
30 36
        StringBuffer sb = new StringBuffer();
31

  
32
        // ClassName
33
        sb.append("{\n\tclass ").append(classdesc.name);
34
        // extends
35
        if (classdesc.superclass != null) {
36
            sb.append(" extends ").append(classdesc.superclass.name);
37
        }
38
        // implements
39
        if (classdesc.interfaces != null) {
40
            sb.append(" implements ");
41
            for (String str : classdesc.interfaces) {
42
                sb.append(str);
43
            }
44
        }
45
        sb.append(" : {"); // ending of first line
46

  
47
        for (field f : classdesc.fields) {
48
            // v this.fielddata najit element, jehoz key == classdesc
49
            // v tomto prvku fielddata najdu value (element) podle key == f
50
            // jeho value je chtena hodnota
51
            String val = this.fielddata.get(classdesc).get(f).toString();
52
            sb.append("\n\t\t");
53
            // Datový typ není potřeba - json jej nepoužívá!
54
            sb.append("\"")./* append(f.type.getJavaType()).append(" "). */append(f.name).append("\"");
55
            sb.append(" : ");
56
            sb.append(val);
57

  
58
            if (!f.equals(classdesc.fields[classdesc.fields.length - 1])) {
59
                sb.append(", ");
60
            }
61
        }
62

  
63
        sb.append("\n\t}\n}");
64

  
37
        sb.append(classdesc.name).append(' ').append("_h").append(jdeserialize.hex(handle))
38
            .append(" = r_").append(jdeserialize.hex(classdesc.handle)).append(";  ");
39
        //sb.append("// [instance " + jdeserialize.hex(handle) + ": " + jdeserialize.hex(classdesc.handle) + "/" + classdesc.name).append("]");
65 40
        return sb.toString();
66 41
    }
67 42

  
project/Deserializer/src/jdeserialize/jdeserialize.java
837 837
                    break;
838 838
                }
839 839
                content c = read_Content(tc, dis, true);
840
                System.out.println("read: " + c.toString());
840
                //System.out.println("read: " + c.toString());
841 841
                if(c != null && c.isExceptionObject()) {
842 842
                    c = new exceptionstate(c, lis.getRecordedData());
843 843
                }
project/Deserializer/src/jdeserialize/stringobj.java
15 15
        }
16 16
        return x;
17 17
    }
18
    public String toString() {
18

  
19
    public @Override String toJson(String indetation)
20
    {
19 21
        return "\"" + this.value + "\"";
20 22
    }
23

  
24
    public String toString() {
25
        return "[String " + jdeserialize.hex(handle) + ": \"" + value + "\"]";
26
    }
21 27
    /**
22 28
     * Constructor.
23 29
     *

Také k dispozici: Unified diff