| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.commons.scxml.model; |
| 19 |
|
|
| 20 |
|
import java.util.Collection; |
| 21 |
|
|
| 22 |
|
import javax.xml.parsers.DocumentBuilderFactory; |
| 23 |
|
|
| 24 |
|
import org.apache.commons.logging.Log; |
| 25 |
|
import org.apache.commons.logging.LogFactory; |
| 26 |
|
import org.apache.commons.scxml.Context; |
| 27 |
|
import org.apache.commons.scxml.ErrorReporter; |
| 28 |
|
import org.apache.commons.scxml.Evaluator; |
| 29 |
|
import org.apache.commons.scxml.EventDispatcher; |
| 30 |
|
import org.apache.commons.scxml.PathResolver; |
| 31 |
|
import org.apache.commons.scxml.SCInstance; |
| 32 |
|
import org.apache.commons.scxml.SCXMLExpressionException; |
| 33 |
|
import org.apache.commons.scxml.SCXMLHelper; |
| 34 |
|
import org.apache.commons.scxml.TriggerEvent; |
| 35 |
|
import org.w3c.dom.Document; |
| 36 |
|
import org.w3c.dom.Node; |
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
public final class Assign extends Action implements PathResolverHolder { |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
private String name; |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
private String location; |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
private String src; |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
private String expr; |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
private PathResolver pathResolver; |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
public Assign() { |
| 76 |
48 |
super(); |
| 77 |
48 |
} |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
public String getName() { |
| 85 |
5 |
return name; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
public void setName(final String name) { |
| 94 |
33 |
this.name = name; |
| 95 |
33 |
} |
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
public String getExpr() { |
| 103 |
5 |
return expr; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
public void setExpr(final String expr) { |
| 112 |
43 |
this.expr = expr; |
| 113 |
43 |
} |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
public String getLocation() { |
| 121 |
5 |
return location; |
| 122 |
|
} |
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| 129 |
|
public void setLocation(final String location) { |
| 130 |
11 |
this.location = location; |
| 131 |
11 |
} |
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
public String getSrc() { |
| 139 |
0 |
return src; |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
public void setSrc(final String src) { |
| 148 |
1 |
this.src = src; |
| 149 |
1 |
} |
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
public PathResolver getPathResolver() { |
| 157 |
0 |
return pathResolver; |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
public void setPathResolver(final PathResolver pathResolver) { |
| 166 |
43 |
this.pathResolver = pathResolver; |
| 167 |
43 |
} |
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
public void execute(final EventDispatcher evtDispatcher, |
| 173 |
|
final ErrorReporter errRep, final SCInstance scInstance, |
| 174 |
|
final Log appLog, final Collection derivedEvents) |
| 175 |
|
throws ModelException, SCXMLExpressionException { |
| 176 |
31 |
State parentState = getParentState(); |
| 177 |
31 |
Context ctx = scInstance.getContext(parentState); |
| 178 |
31 |
Evaluator eval = scInstance.getEvaluator(); |
| 179 |
|
|
| 180 |
31 |
if (!SCXMLHelper.isStringEmpty(location)) { |
| 181 |
11 |
Node oldNode = eval.evalLocation(ctx, location); |
| 182 |
11 |
if (oldNode != null) { |
| 183 |
|
|
| 184 |
|
|
| 185 |
11 |
Node newNode = null; |
| 186 |
|
try { |
| 187 |
11 |
if (src != null && src.trim().length() > 0) { |
| 188 |
1 |
newNode = getSrcNode(); |
| 189 |
|
} else { |
| 190 |
10 |
newNode = eval.evalLocation(ctx, expr); |
| 191 |
|
} |
| 192 |
5 |
if (newNode != null) { |
| 193 |
|
|
| 194 |
5 |
for (Node child = newNode.getFirstChild(); |
| 195 |
12 |
child != null; |
| 196 |
7 |
child = child.getNextSibling()) { |
| 197 |
7 |
Node importedNode = oldNode.getOwnerDocument(). |
| 198 |
|
importNode(child, true); |
| 199 |
7 |
oldNode.appendChild(importedNode); |
| 200 |
|
} |
| 201 |
|
} |
| 202 |
6 |
} catch (SCXMLExpressionException see) { |
| 203 |
|
|
| 204 |
6 |
Object valueObject = eval.eval(ctx, expr); |
| 205 |
6 |
SCXMLHelper.setNodeValue(oldNode, valueObject.toString()); |
| 206 |
5 |
} |
| 207 |
11 |
TriggerEvent ev = new TriggerEvent(name + ".change", |
| 208 |
|
TriggerEvent.CHANGE_EVENT); |
| 209 |
11 |
derivedEvents.add(ev); |
| 210 |
|
} else { |
| 211 |
0 |
appLog.error("<assign>: location does not point to" |
| 212 |
|
+ " a <data> node"); |
| 213 |
|
} |
| 214 |
|
} else { |
| 215 |
|
|
| 216 |
20 |
if (!ctx.has(name)) { |
| 217 |
0 |
errRep.onError(ErrorReporter.UNDEFINED_VARIABLE, name |
| 218 |
|
+ " = null", parentState); |
| 219 |
|
} else { |
| 220 |
20 |
Object varObj = null; |
| 221 |
20 |
if (src != null && src.trim().length() > 0) { |
| 222 |
0 |
varObj = getSrcNode(); |
| 223 |
|
} else { |
| 224 |
20 |
varObj = eval.eval(ctx, expr); |
| 225 |
|
} |
| 226 |
20 |
ctx.set(name, varObj); |
| 227 |
20 |
TriggerEvent ev = new TriggerEvent(name + ".change", |
| 228 |
|
TriggerEvent.CHANGE_EVENT); |
| 229 |
20 |
derivedEvents.add(ev); |
| 230 |
|
} |
| 231 |
|
} |
| 232 |
31 |
} |
| 233 |
|
|
| 234 |
|
|
| 235 |
|
|
| 236 |
|
|
| 237 |
|
|
| 238 |
|
|
| 239 |
|
private Node getSrcNode() { |
| 240 |
1 |
String resolvedSrc = src; |
| 241 |
1 |
if (pathResolver != null) { |
| 242 |
1 |
resolvedSrc = pathResolver.resolvePath(src); |
| 243 |
|
} |
| 244 |
1 |
Document doc = null; |
| 245 |
|
try { |
| 246 |
1 |
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder(). |
| 247 |
|
parse(resolvedSrc); |
| 248 |
0 |
} catch (Throwable t) { |
| 249 |
0 |
org.apache.commons.logging.Log log = LogFactory. |
| 250 |
0 |
getLog(Assign.class); |
| 251 |
0 |
log.error(t.getMessage(), t); |
| 252 |
1 |
} |
| 253 |
1 |
if (doc == null) { |
| 254 |
0 |
return null; |
| 255 |
|
} |
| 256 |
1 |
return doc.getDocumentElement(); |
| 257 |
|
} |
| 258 |
|
|
| 259 |
|
} |