1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.scxml2.model;
18
19 import org.apache.commons.scxml2.ActionExecutionContext;
20 import org.apache.commons.scxml2.Context;
21 import org.apache.commons.scxml2.Evaluator;
22 import org.apache.commons.scxml2.SCXMLExpressionException;
23
24 /**
25 * The class in this SCXML object model that corresponds to the
26 * <script> SCXML element.
27 *
28 * TODO src attribute support
29 */
30 public class Script extends Action implements BodyContainer {
31
32 /**
33 * Serial version UID.
34 */
35 private static final long serialVersionUID = 1L;
36
37 private boolean globalScript;
38 private String body;
39
40 /**
41 * Constructor.
42 */
43 public Script() {
44 super();
45 }
46
47 public boolean isGlobalScript() {
48 return globalScript;
49 }
50
51 public void setGlobalScript(final boolean globalScript) {
52 this.globalScript = globalScript;
53 }
54
55 @Override
56 public String getBody() {
57 return body;
58 }
59
60 @Override
61 public void setBody(String body) {
62 this.body = body;
63 }
64
65 /**
66 * Get the script to execute.
67 *
68 * @return The script to execute.
69 */
70 public String getScript() {
71 return body;
72 }
73
74 /**
75 * {@inheritDoc}
76 */
77 @Override
78 public void execute(ActionExecutionContext exctx) throws ModelException, SCXMLExpressionException {
79 Context ctx = isGlobalScript() ? exctx.getGlobalContext() : exctx.getContext(getParentEnterableState());
80 ctx.setLocal(getNamespacesKey(), getNamespaces());
81 Evaluator eval = exctx.getEvaluator();
82 eval.evalScript(ctx, getScript());
83 ctx.setLocal(getNamespacesKey(), null);
84 }
85
86 }
87