summaryrefslogtreecommitdiffstats
path: root/embedding/java/src/org/uscxml/datamodel/ecmascript/ECMAData.java
blob: 035da61a12427574d06144146905b1933624291a (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package org.uscxml.datamodel.ecmascript;

import org.mozilla.javascript.Scriptable;
import org.uscxml.Data;

public class ECMAData implements Scriptable {

	protected Data data;
    protected Scriptable parent;
    protected Scriptable prototype;

	public ECMAData(Data data) {
		this.data = data;
	}

	@Override
	public String getClassName() {
		return "Data";
	}

	public Object unwrap(Data data) {
		if (data.getAtom().length() > 0) {
			return data.getAtom();
		}
		return new ECMAData(data);
		
	}
	
	@Override
	public Object get(String name, Scriptable start) {
		if (data.hasKey(name))
			return unwrap(data.at(name));
		return NOT_FOUND;
	}

	@Override
	public Object get(int index, Scriptable start) {
		if (data.getArray().size() > index)
			return unwrap(data.item(index));
		return NOT_FOUND;
	}

	@Override
	public boolean has(String name, Scriptable start) {
		return data.hasKey(name);
	}

	@Override
	public boolean has(int index, Scriptable start) {
		return data.getArray().size() > index;
	}

	@Override
	public void put(String name, Scriptable start, Object value) {
	}

	@Override
	public void put(int index, Scriptable start, Object value) {
	}

	@Override
	public void delete(String name) {
	}

	@Override
	public void delete(int index) {
	}

	@Override
	public Scriptable getPrototype() {
		return prototype;
	}

	@Override
	public void setPrototype(Scriptable prototype) {
		this.prototype = prototype;
	}

	@Override
	public Scriptable getParentScope() {
		return parent;
	}

	@Override
	public void setParentScope(Scriptable parent) {
		this.parent = parent;		
	}

	@Override
	public Object[] getIds() {
		return data.getCompound().keySet().toArray();
	}

	@Override
	public Object getDefaultValue(Class<?> hint) {
        return "[object Data]";
	}

	@Override
	public boolean hasInstance(Scriptable instance) {
        Scriptable proto = instance.getPrototype();
        while (proto != null) {
            if (proto.equals(this))
                return true;
            proto = proto.getPrototype();
        }
        return false;
	}

}