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; 023import org.apache.commons.scxml2.TriggerEvent; 024 025/** 026 * The class in this SCXML object model that corresponds to the 027 * <var> SCXML element. 028 * 029 */ 030public class Var extends Action { 031 032 /** 033 * Serial version UID. 034 */ 035 private static final long serialVersionUID = 1L; 036 037 /** 038 * The name of the variable to be created. 039 */ 040 private String name; 041 042 /** 043 * The expression that evaluates to the initial value of the variable. 044 */ 045 private String expr; 046 047 /** 048 * Constructor. 049 */ 050 public Var() { 051 super(); 052 } 053 054 /** 055 * Get the expression that evaluates to the initial value 056 * of the variable. 057 * 058 * @return String Returns the expr. 059 */ 060 public final String getExpr() { 061 return expr; 062 } 063 064 /** 065 * Set the expression that evaluates to the initial value 066 * of the variable. 067 * 068 * @param expr The expr to set. 069 */ 070 public final void setExpr(final String expr) { 071 this.expr = expr; 072 } 073 074 /** 075 * Get the name of the (new) variable. 076 * 077 * @return String Returns the name. 078 */ 079 public final String getName() { 080 return name; 081 } 082 083 /** 084 * Set the name of the (new) variable. 085 * 086 * @param name The name to set. 087 */ 088 public final void setName(final String name) { 089 this.name = name; 090 } 091 092 /** 093 * {@inheritDoc} 094 */ 095 @Override 096 public void execute(ActionExecutionContext exctx) throws ModelException, SCXMLExpressionException { 097 Context ctx = exctx.getContext(getParentEnterableState()); 098 Evaluator eval = exctx.getEvaluator(); 099 ctx.setLocal(getNamespacesKey(), getNamespaces()); 100 Object varObj = eval.eval(ctx, expr); 101 ctx.setLocal(getNamespacesKey(), null); 102 ctx.setLocal(name, varObj); 103 if (exctx.getAppLog().isDebugEnabled()) { 104 exctx.getAppLog().debug("<var>: Defined variable '" + name 105 + "' with initial value '" + String.valueOf(varObj) + "'"); 106 } 107 TriggerEvent ev = new TriggerEvent(name + ".change", TriggerEvent.CHANGE_EVENT); 108 exctx.getInternalIOProcessor().addEvent(ev); 109 } 110} 111