View Javadoc

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.scxml.model;
18  
19  import java.util.Collection;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.scxml.Context;
23  import org.apache.commons.scxml.ErrorReporter;
24  import org.apache.commons.scxml.Evaluator;
25  import org.apache.commons.scxml.EventDispatcher;
26  import org.apache.commons.scxml.SCInstance;
27  import org.apache.commons.scxml.SCXMLExpressionException;
28  import org.apache.commons.scxml.TriggerEvent;
29  
30  /***
31   * The class in this SCXML object model that corresponds to the
32   * <var> SCXML element.
33   *
34   */
35  public class Var extends Action {
36  
37      /***
38       * Serial version UID.
39       */
40      private static final long serialVersionUID = 1L;
41  
42      /***
43       * The name of the variable to be created.
44       */
45      private String name;
46  
47      /***
48       * The expression that evaluates to the initial value of the variable.
49       */
50      private String expr;
51  
52      /***
53       * Constructor.
54       */
55      public Var() {
56          super();
57      }
58  
59      /***
60       * Get the expression that evaluates to the initial value
61       * of the variable.
62       *
63       * @return String Returns the expr.
64       */
65      public final String getExpr() {
66          return expr;
67      }
68  
69      /***
70       * Set the expression that evaluates to the initial value
71       * of the variable.
72       *
73       * @param expr The expr to set.
74       */
75      public final void setExpr(final String expr) {
76          this.expr = expr;
77      }
78  
79      /***
80       * Get the name of the (new) variable.
81       *
82       * @return String Returns the name.
83       */
84      public final String getName() {
85          return name;
86      }
87  
88      /***
89       * Set the name of the (new) variable.
90       *
91       * @param name The name to set.
92       */
93      public final void setName(final String name) {
94          this.name = name;
95      }
96  
97      /***
98       * {@inheritDoc}
99       */
100     public void execute(final EventDispatcher evtDispatcher,
101             final ErrorReporter errRep, final SCInstance scInstance,
102             final Log appLog, final Collection derivedEvents)
103     throws ModelException, SCXMLExpressionException {
104         Context ctx = scInstance.getContext(getParentState());
105         Evaluator eval = scInstance.getEvaluator();
106         ctx.setLocal(getNamespacesKey(), getNamespaces());
107         Object varObj = eval.eval(ctx, expr);
108         ctx.setLocal(getNamespacesKey(), null);
109         ctx.setLocal(name, varObj);
110         if (appLog.isDebugEnabled()) {
111             appLog.debug("<var>: Defined variable '" + name
112                 + "' with initial value '" + String.valueOf(varObj) + "'");
113         }
114         TriggerEvent ev = new TriggerEvent(name + ".change",
115                 TriggerEvent.CHANGE_EVENT);
116         derivedEvents.add(ev);
117     }
118 
119 }
120