001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.scxml2.model;
018
019import org.apache.commons.scxml2.ActionExecutionContext;
020import org.apache.commons.scxml2.Context;
021import org.apache.commons.scxml2.Evaluator;
022import org.apache.commons.scxml2.SCXMLExpressionException;
023
024/**
025 * The class in this SCXML object model that corresponds to the
026 * <script> SCXML element.
027 *
028 * TODO src attribute support
029 */
030public class Script extends Action implements BodyContainer {
031
032    /**
033     * Serial version UID.
034     */
035    private static final long serialVersionUID = 1L;
036
037    private boolean globalScript;
038    private String body;
039
040    /**
041     * Constructor.
042     */
043    public Script() {
044        super();
045    }
046
047    public boolean isGlobalScript() {
048        return globalScript;
049    }
050
051    public void setGlobalScript(final boolean globalScript) {
052        this.globalScript = globalScript;
053    }
054
055    @Override
056    public String getBody() {
057        return body;
058    }
059
060    @Override
061    public void setBody(String body) {
062        this.body = body;
063    }
064
065    /**
066     * Get the script to execute.
067     *
068     * @return The script to execute.
069     */
070    public String getScript() {
071        return body;
072    }
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public void execute(ActionExecutionContext exctx) throws ModelException, SCXMLExpressionException {
079        Context ctx = isGlobalScript() ? exctx.getGlobalContext() : exctx.getContext(getParentEnterableState());
080        ctx.setLocal(getNamespacesKey(), getNamespaces());
081        Evaluator eval = exctx.getEvaluator();
082        eval.evalScript(ctx, getScript());
083        ctx.setLocal(getNamespacesKey(), null);
084    }
085
086}
087