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 * <cancel> SCXML element.
027 *
028 */
029public class Cancel extends Action {
030
031    /**
032     * Serial version UID.
033     */
034    private static final long serialVersionUID = 1L;
035
036    /**
037     * Constructor.
038     */
039    public Cancel() {
040        super();
041    }
042
043    /**
044     * The ID of the send message that should be cancelled.
045     */
046    private String sendid;
047
048    /**
049     * The expression that evaluates to the ID of the send message that should be cancelled.
050     */
051    private String sendidexpr;
052
053    /**
054     * Get the ID of the send message that should be cancelled.
055     *
056     * @return Returns the sendid.
057     */
058    public String getSendid() {
059        return sendid;
060    }
061
062    /**
063     * Set the ID of the send message that should be cancelled.
064     *
065     * @param sendid The sendid to set.
066     */
067    public void setSendid(final String sendid) {
068        this.sendid = sendid;
069    }
070
071    /**
072     * Get the expression that evaluates to the ID of the send message that should be cancelled.
073     * 
074     * @return the expression that evaluates to the ID of the send message that should be cancelled.
075     */
076    public String getSendidexpr() {
077        return sendidexpr;
078    }
079
080    /**
081     * Set the expression that evaluates to the ID of the send message that should be cancelled.
082     * 
083     * @param sendidexpr the expression that evaluates to the ID of the send message that should be cancelled.
084     */
085    public void setSendidexpr(String sendidexpr) {
086        this.sendidexpr = sendidexpr;
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    @Override
093    public void execute(ActionExecutionContext exctx) throws ModelException, SCXMLExpressionException {
094        EnterableState parentState = getParentEnterableState();
095        Context ctx = exctx.getContext(parentState);
096        ctx.setLocal(getNamespacesKey(), getNamespaces());
097        Evaluator eval = exctx.getEvaluator();
098
099        String sendidValue = sendid;
100        if (sendidValue == null && sendidexpr != null) {
101            sendidValue = (String) getTextContentIfNodeResult(eval.eval(ctx, sendidexpr));
102            if ((sendidValue == null || sendidValue.trim().length() == 0)
103                    && exctx.getAppLog().isWarnEnabled()) {
104                exctx.getAppLog().warn("<send>: sendid expression \"" + sendidexpr
105                        + "\" evaluated to null or empty String");
106            }
107        }
108
109        exctx.getEventDispatcher().cancel(sendidValue);
110    }
111}
112