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.io.IOException;
20
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.parsers.FactoryConfigurationError;
23 import javax.xml.parsers.ParserConfigurationException;
24
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.commons.scxml2.ActionExecutionContext;
27 import org.apache.commons.scxml2.Context;
28 import org.apache.commons.scxml2.Evaluator;
29 import org.apache.commons.scxml2.PathResolver;
30 import org.apache.commons.scxml2.SCXMLExpressionException;
31 import org.w3c.dom.*;
32 import org.xml.sax.SAXException;
33
34
35
36
37
38
39 public class Assign extends Action implements PathResolverHolder {
40
41
42
43
44 private static final long serialVersionUID = 1L;
45
46
47
48
49
50 private String location;
51
52
53
54
55 private String src;
56
57
58
59
60 private String expr;
61
62
63
64
65 private Evaluator.AssignType type;
66
67
68
69
70 private String attr;
71
72
73
74
75 private PathResolver pathResolver;
76
77
78
79
80 public Assign() {
81 super();
82 }
83
84
85
86
87
88
89 public String getExpr() {
90 return expr;
91 }
92
93
94
95
96
97
98 public void setExpr(final String expr) {
99 this.expr = expr;
100 }
101
102
103
104
105
106
107 public String getLocation() {
108 return location;
109 }
110
111
112
113
114
115
116 public void setLocation(final String location) {
117 this.location = location;
118 }
119
120
121
122
123
124
125 public String getSrc() {
126 return src;
127 }
128
129
130
131
132
133
134 public void setSrc(final String src) {
135 this.src = src;
136 }
137
138
139
140
141
142
143 public PathResolver getPathResolver() {
144 return pathResolver;
145 }
146
147
148
149
150
151
152 public void setPathResolver(final PathResolver pathResolver) {
153 this.pathResolver = pathResolver;
154 }
155
156 public Evaluator.AssignType getType() {
157 return type;
158 }
159
160 public void setType(final Evaluator.AssignType type) {
161 this.type = type;
162 }
163
164 public String getAttr() {
165 return attr;
166 }
167
168 public void setAttr(final String attr) {
169 this.attr = attr;
170 }
171
172
173
174
175 @Override
176 public void execute(ActionExecutionContext exctx) throws ModelException, SCXMLExpressionException {
177 EnterableState parentState = getParentEnterableState();
178 Context ctx = exctx.getContext(parentState);
179 Evaluator evaluator = exctx.getEvaluator();
180 ctx.setLocal(getNamespacesKey(), getNamespaces());
181 Object data;
182 if (src != null && src.trim().length() > 0) {
183 data = getSrcNode();
184 } else {
185 data = evaluator.eval(ctx, expr);
186 }
187
188 evaluator.evalAssign(ctx, location, data, type, attr);
189 if (exctx.getAppLog().isDebugEnabled()) {
190 exctx.getAppLog().debug("<assign>: '" + location + "' updated");
191 }
192
193
194
195
196
197
198
199
200
201 ctx.setLocal(getNamespacesKey(), null);
202 }
203
204
205
206
207
208
209 private Node getSrcNode() {
210 String resolvedSrc = src;
211 if (pathResolver != null) {
212 resolvedSrc = pathResolver.resolvePath(src);
213 }
214 Document doc = null;
215 try {
216 doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(resolvedSrc);
217 } catch (FactoryConfigurationError t) {
218 logError(t);
219 } catch (SAXException e) {
220 logError(e);
221 } catch (IOException e) {
222 logError(e);
223 } catch (ParserConfigurationException e) {
224 logError(e);
225 }
226 if (doc == null) {
227 return null;
228 }
229 return doc.getDocumentElement();
230 }
231
232
233
234
235 private void logError(Throwable throwable) {
236 org.apache.commons.logging.Log log = LogFactory.
237 getLog(Assign.class);
238 log.error(throwable.getMessage(), throwable);
239 }
240 }