Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 9744d223

Přidáno uživatelem Michal Horký před více než 4 roky(ů)

#7767

Zprovoznění exportu do JSON. Přidáno zpracovávání binárních zazipovaných dat. Úprava struktury.

Zobrazit rozdíly:

demo_mh/Deserializer/src/deserialize/ClassDescription.java
3 3
import java.util.ArrayList;
4 4
import java.util.List;
5 5

  
6
import org.json.simple.JSONObject;
7

  
6 8
public class ClassDescription {
7 9

  
8
	final static byte SC_SERIALIZABLE = (byte) 0x02;
9
	final static byte SC_EXTERNALIZABLE = (byte) 0x04;
10
	private final static byte SC_SERIALIZABLE = (byte) 0x02;
11
	private final static byte SC_EXTERNALIZABLE = (byte) 0x04;
10 12
	
11 13
	String className;
12 14
	long serialVersionUID;
13 15
	byte flags;
14 16
	boolean isClass; // or enum
15
	List<String> variables = new ArrayList<String>();
17
	
18
	private List<ClassDescription> parents = new ArrayList<ClassDescription>();
19
	private int actParentIndex = -1;
20
	
21
	private List<Variable> variables = new ArrayList<Variable>();
22
	private int actVariableIndex = -1;
23
	
24
	public Variable nextVariable() {
25
		if (++actVariableIndex < variables.size()) {
26
			return variables.get(actVariableIndex);
27
		} else {
28
			return null;
29
		}
30
	}
31
	
32
	public void addParent(ClassDescription parent) {
33
		parents.add(parent);
34
		actParentIndex++;
35
	}
36
	
37
	public Variable getActVariable() {
38
		for (; actParentIndex >= 0; actParentIndex--) {
39
			Variable var = parents.get(actParentIndex).nextVariable();
40
			if (var != null) {
41
				return var;
42
			}
43
		}
44
		return null;
45
	}
46
	
47
	public void setVariables(List<Variable> variables) {
48
		this.variables = variables;
49
	}
50
	
51
	@SuppressWarnings("unchecked")
52
	public JSONObject getJSONObject() {
53
		int actIndex = ++actParentIndex;
54
		JSONObject obj = new JSONObject();
55
		
56
		JSONObject vars = new JSONObject();
57
		for (int j = 0; j < variables.size(); j++) {
58
			variables.get(j).putToJSON(vars);
59
		}
60
		obj.put("variables", vars);
61
		
62
		String i = "";
63
		if (flags == SC_SERIALIZABLE) {
64
			i = "java.io.Serializable";
65
		} else if (flags == SC_EXTERNALIZABLE) {
66
			i = "java.io.Externalizable";
67
		}
68
		obj.put("interface", i);
69
		
70
		String name;
71
		if (isClass) {
72
			name = "class";
73
		} else {
74
			name = "enum";
75
		}
76
		name += " " + className;
77
		obj.put("name", name);
78
		
79
		if ((actIndex + 1) < parents.size()) {
80
			obj.put("parent", parents.get(actIndex + 1).getJSONObject());
81
		}
82
		
83
		return obj;
84
	}
16 85
	
17 86
	@Override
18 87
	public String toString() {
88
		actParentIndex++;
19 89
		String retValue = "";
20 90
		
21 91
		if (isClass) {
......
25 95
		}
26 96
		
27 97
		retValue += " " + className;
98
		
99
		if ((actParentIndex + 1) < parents.size()) {
100
			retValue += " extends " + parents.get(actParentIndex + 1).className;
101
		}
102
		
28 103
		if (flags == SC_SERIALIZABLE) {
29 104
			retValue += " implements java.io.Serializable";
30 105
		} else if (flags == SC_EXTERNALIZABLE) {
......
36 111
			if (i > 0) {
37 112
				retValue += "\n";
38 113
			}
39
			retValue += "    " + variables.get(i) + ";";
114
			retValue += "    " + variables.get(i).toString() + ";";
115
		}
116
		retValue += "\n}\n";
117
		
118
		if ((actParentIndex + 1) < parents.size()) {
119
			retValue += parents.get(actParentIndex + 1).toString();
40 120
		}
41
		retValue += "\n}";
42 121
		
43 122
		return retValue;
44 123
	}

Také k dispozici: Unified diff