1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml2.model;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.parsers.ParserConfigurationException;
26
27 import org.apache.commons.scxml2.Evaluator;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
30 import org.w3c.dom.Node;
31 import org.w3c.dom.NodeList;
32
33
34
35
36
37 public abstract class PayloadProvider extends Action {
38
39
40
41
42
43
44 private static class DataValueList extends ArrayList {
45 }
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 @SuppressWarnings("unchecked")
63 protected void addToPayload(final String attrName, final Object attrValue, Map<String, Object> payload) {
64 DataValueList valueList = null;
65 Object value = payload.get(attrName);
66 if (value != null) {
67 if (value instanceof DataValueList) {
68 valueList = (DataValueList)value;
69 }
70 else {
71 valueList = new DataValueList();
72 valueList.add(value);
73 payload.put(attrName, valueList);
74 }
75 }
76 value = clonePayloadValue(attrValue);
77 if (value instanceof List) {
78 if (valueList == null) {
79 valueList = new DataValueList();
80 payload.put(attrName, valueList);
81 }
82 valueList.addAll((List)value);
83 }
84 else if (valueList != null) {
85 valueList.add(value);
86 }
87 else {
88 payload.put(attrName, value);
89 }
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 @SuppressWarnings("unchecked")
106 protected Object clonePayloadValue(final Object value) {
107 if (value != null) {
108 if (value instanceof Node) {
109 return ((Node)value).cloneNode(true);
110 }
111 else if (value instanceof NodeList) {
112 NodeList nodeList = (NodeList)value;
113 ArrayList<Node> list = new ArrayList<Node>();
114 for (int i = 0, size = nodeList.getLength(); i < size; i++) {
115 list.add(nodeList.item(i).cloneNode(true));
116 }
117 return list;
118 }
119 else if (value instanceof List) {
120 ArrayList<Object> list = new ArrayList<Object>();
121 for (Object v : (List)value) {
122 list.add(clonePayloadValue(v));
123 }
124 return list;
125 }
126 else if (value instanceof Map) {
127 HashMap<Object, Object> map = new HashMap<Object, Object>();
128 for (Map.Entry<Object,Object> entry : ((Map<Object,Object>)value).entrySet()) {
129 map.put(entry.getKey(), clonePayloadValue(entry.getValue()));
130 }
131 return map;
132 }
133
134 }
135 return value;
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 protected Object makeEventPayload(final Evaluator evaluator, final Map<String, Object> payload)
153 throws ModelException {
154 if (payload != null && !payload.isEmpty() && Evaluator.XPATH_DATA_MODEL.equals(evaluator.getSupportedDatamodel())) {
155
156 try {
157 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
158 Element payloadNode = document.createElement("payload");
159 for (Map.Entry<String, Object> entry : payload.entrySet()) {
160 Element dataNode = document.createElement("data");
161 payloadNode.appendChild(dataNode);
162 dataNode.setAttribute("id", entry.getKey());
163 if (entry.getValue() instanceof Node) {
164 dataNode.appendChild(document.importNode((Node)entry.getValue(), true));
165 }
166 else if (entry.getValue() instanceof DataValueList) {
167 for (Object value : ((DataValueList)entry.getValue())) {
168 if (value instanceof Node) {
169 dataNode.appendChild(document.importNode((Node)entry.getValue(), true));
170 }
171 else {
172 dataNode.setTextContent(String.valueOf(value));
173 }
174 }
175 }
176 else if (entry.getValue() != null) {
177 dataNode.setTextContent(String.valueOf(entry.getValue()));
178 }
179 }
180 return payloadNode;
181 }
182 catch (ParserConfigurationException pce) {
183 throw new ModelException("Cannot instantiate a DocumentBuilder", pce);
184 }
185 }
186 return payload;
187 }
188 }