summaryrefslogtreecommitdiffstats
path: root/src/bindings/swig/java/org/uscxml/Data.java
blob: f3f21f34d7fd41f6ecb7a3db6b97e4d512d99de8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package org.uscxml;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Data {
	public Map<String, Data> compound = new HashMap<String, Data>();
	public List<Data> array = new LinkedList<Data>();
	public String atom;
	public Type type = Type.INTERPRETED;

	public enum Type {
	    VERBATIM, INTERPRETED 
	}

	public static Data fromJSON(String jsonString) {
		return new Data(DataNative.fromJSON(jsonString));
	}
	
	public Data() {
	}
	
	public Data(String atom, Data.Type type) {
		this.atom = atom;
		this.type = type;
	}
	
	public Data(DataNative nativeData) {
		if (!nativeData.getCompound().empty()) {
			// data is a key value compound
			for(String key : nativeData.getCompound()) {
				this.compound.put(key, new Data(nativeData.getCompound().get(key)));
			}
		} else if (!nativeData.getArray().isEmpty()) {
			// data is an array
			for (int i = 0; i < nativeData.getArray().size(); i++) {
				this.array.add(new Data(nativeData.getArray().get(i)));
			}
		} else {
			// data is a single atom
			this.atom = nativeData.getAtom();
			if (nativeData.getType() == DataNative.Type.INTERPRETED) {
				this.type = Type.INTERPRETED;
			} else {
				this.type = Type.VERBATIM;				
			}
		}
	}
	
	@Override
	public String toString() {
		return toJSON();
	}

	public String toJSON() {
		DataNative nativeData = toNative(this);
		return DataNative.toJSON(nativeData);
	}

	public static DataNative toNative(Data data) {
		DataNative nativeData = new DataNative();
		//nativeData.swigCMemOwn = false;
		if (data.compound != null && !data.compound.isEmpty()) {
			DataMap nativeDataMap = new DataMap();
			for (String key : data.compound.keySet()) {
				nativeDataMap.set(key, toNative(data.compound.get(key)));
			}
			nativeData.setCompound(nativeDataMap);
		} else if (data.array != null && !data.array.isEmpty()) {
			DataList nativeDataList = new DataList();
			for (Data item : data.array) {
				nativeDataList.add(toNative(item));
			}
			nativeData.setArray(nativeDataList);
		} else if (data.atom != null) {
			nativeData.setAtom(data.atom);
			if (data.type == Type.INTERPRETED) {
				nativeData.setType(DataNative.Type.INTERPRETED);
			} else {
				nativeData.setType(DataNative.Type.VERBATIM);				
			}
		}
		return nativeData;
	}

}