Projekt

Obecné

Profil

Stáhnout (3.16 KB) Statistiky
| Větev: | Tag: | Revize:
1
package deserialize;
2

    
3
import java.util.ArrayList;
4
import java.util.List;
5

    
6
import org.codehaus.jackson.JsonNode;
7
import org.codehaus.jackson.map.ObjectMapper;
8
import org.codehaus.jackson.node.ObjectNode;
9

    
10
public class ClassDescription {
11

    
12
	private final static byte SC_SERIALIZABLE = (byte) 0x02;
13
	private final static byte SC_EXTERNALIZABLE = (byte) 0x04;
14
	
15
	String className;
16
	long serialVersionUID;
17
	byte flags;
18
	boolean isClass; // or enum
19
	
20
	// Class is in the first place, parents are in the other positions.
21
	private List<ClassDescription> instances = new ArrayList<ClassDescription>();
22
	private int actParentIndex = -1;
23
	
24
	private List<Variable> variables = new ArrayList<Variable>();
25
	private int actVariableIndex = -1;
26
	
27
	
28
	
29
	public List<ClassDescription> getInstances() {
30
		return instances;
31
	}
32
	
33
	public List<Variable> getVariables() {
34
		return variables;
35
	}
36
	
37
	
38
	
39
	public Variable nextVariable() {
40
		if (++actVariableIndex < variables.size()) {
41
			return variables.get(actVariableIndex);
42
		} else {
43
			return null;
44
		}
45
	}
46
	
47
	public void addParent(ClassDescription parent) {
48
		instances.add(parent);
49
		actParentIndex++;
50
	}
51
	
52
	public Variable getActVariable() {
53
		for (; actParentIndex >= 0; actParentIndex--) {
54
			Variable var = instances.get(actParentIndex).nextVariable();
55
			if (var != null) {
56
				return var;
57
			}
58
		}
59
		return null;
60
	}
61
	
62
	public void setVariables(List<Variable> variables) {
63
		this.variables = variables;
64
	}
65
	
66
	public JsonNode getJsonNode() {
67
		int actIndex = ++actParentIndex;
68
		
69
		ObjectMapper mapper = new ObjectMapper();
70
		
71
		JsonNode childNode = mapper.createObjectNode();
72
		for (int j = 0; j < variables.size(); j++) {
73
			variables.get(j).putToJson((ObjectNode) childNode);
74
		}
75
		if ((actIndex + 1) < instances.size()) {
76
			((ObjectNode) childNode).put("parent", instances.get(actIndex + 1).getJsonNode());
77
		}
78

    
79
		String name;
80
		if (isClass) {
81
			name = "class";
82
		} else {
83
			name = "enum";
84
		}
85
		
86
		name += " " + className;
87
		
88
		if (actIndex < instances.size()) {
89
			name += " extends " + instances.get(actIndex).className;
90
		}
91
		
92
		if (flags == SC_SERIALIZABLE) {
93
			name += " implements java.io.Serializable";
94
		} else if (flags == SC_EXTERNALIZABLE) {
95
			name += " implements java.io.Externalizable";
96
		}
97
		
98
		JsonNode rootNode = mapper.createObjectNode();
99
		((ObjectNode) rootNode).put(name, childNode);
100
		return rootNode;
101
	}
102

    
103
	@Override
104
	public String toString() {
105
		actParentIndex++;
106
		String retValue = "";
107
		
108
		if (isClass) {
109
			retValue = "class";
110
		} else {
111
			retValue = "enum";
112
		}
113
		
114
		retValue += " " + className;
115
		
116
		if ((actParentIndex + 1) < instances.size()) {
117
			retValue += " extends " + instances.get(actParentIndex + 1).className;
118
		}
119
		
120
		if (flags == SC_SERIALIZABLE) {
121
			retValue += " implements java.io.Serializable";
122
		} else if (flags == SC_EXTERNALIZABLE) {
123
			retValue += " implements java.io.Externalizable";
124
		}
125
		
126
		retValue += "\n{\n";
127
		for (int i = 0; i < variables.size(); i++) {
128
			if (i > 0) {
129
				retValue += "\n";
130
			}
131
			retValue += "    " + variables.get(i).toString() + ";";
132
		}
133
		retValue += "\n}\n";
134
		
135
		if ((actParentIndex + 1) < instances.size()) {
136
			retValue += instances.get(actParentIndex + 1).toString();
137
		}
138
		
139
		return retValue;
140
	}
141
	
142
}
(2-2/9)