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 * <log> SCXML element.
027 *
028 */
029public class Log extends Action {
030
031    /**
032     * Serial version UID.
033     */
034    private static final long serialVersionUID = 1L;
035
036    /**
037     * An expression evaluating to a string to be logged.
038     */
039    private String expr;
040
041    /**
042     * An expression which returns string which may be used, for example,
043     * to indicate the purpose of the log.
044     */
045    private String label;
046
047    /**
048     * Constructor.
049     */
050    public Log() {
051        super();
052    }
053
054    /**
055     * Get the log expression.
056     *
057     * @return Returns the expression.
058     */
059    public final String getExpr() {
060        return expr;
061    }
062
063    /**
064     * Set the log expression.
065     *
066     * @param expr The expr to set.
067     */
068    public final void setExpr(final String expr) {
069        this.expr = expr;
070    }
071
072    /**
073     * Get the log label.
074     *
075     * @return Returns the label.
076     */
077    public final String getLabel() {
078        return label;
079    }
080
081    /**
082     * Set the log label.
083     *
084     * @param label The label to set.
085     */
086    public final void setLabel(final String label) {
087        this.label = label;
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public void execute(ActionExecutionContext exctx) throws ModelException, SCXMLExpressionException {
095        Context ctx = exctx.getContext(getParentEnterableState());
096        Evaluator eval = exctx.getEvaluator();
097        ctx.setLocal(getNamespacesKey(), getNamespaces());
098        exctx.getAppLog().info(label + ": " + String.valueOf(getTextContentIfNodeResult(eval.eval(ctx, expr))));
099        ctx.setLocal(getNamespacesKey(), null);
100    }
101}
102