View Javadoc

    1   /*
    2    *
    3    *   Copyright 2005 The Apache Software Foundation.
    4    *
    5    *  Licensed under the Apache License, Version 2.0 (the "License");
    6    *  you may not use this file except in compliance with the License.
    7    *  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   */
    18  package org.apache.commons.scxml.model;
    19
    20  import java.util.ArrayList;
    21  import java.util.Collection;
    22  import java.util.Iterator;
    23  import java.util.List;
    24
    25  import org.apache.commons.logging.Log;
    26  import org.apache.commons.scxml.Context;
    27  import org.apache.commons.scxml.ErrorReporter;
    28  import org.apache.commons.scxml.Evaluator;
    29  import org.apache.commons.scxml.EventDispatcher;
    30  import org.apache.commons.scxml.SCInstance;
    31  import org.apache.commons.scxml.SCXMLExpressionException;
    32
    33  /***
    34   * The class in this SCXML object model that corresponds to the
    35   * &lt;if&gt; SCXML element, which serves as a container for conditionally
    36   * executed elements. &lt;else&gt; and &lt;elseif&gt; can optionally
    37   * appear within an &lt;if&gt; as immediate children, and serve to partition
    38   * the elements within an &lt;if&gt;.
    39   *
    40   */
    41  public class If extends Action {
    42
    43      /***
    44       * An conditional expression which can be evaluated to true or false.
    45       */
    46      private String cond;
    47
    48      /***
    49       * The set of executable elements (those that inheriting from
    50       * Action) that are contained in this &lt;if&gt; element.
    51       */
    52      private List actions;
    53
    54      /***
    55       * The boolean value that dictates whether the particular child action
    56       * should be executed.
    57       */
    58      private boolean execute;
    59
    60      /***
    61       * Constructor.
    62       */
    63      public If() {
    64          super();
    65          this.actions = new ArrayList();
    66          this.execute = false;
    67      }
    68
    69      /***
    70       * Get the executable actions contained in this &lt;if&gt;.
    71       *
    72       * @return Returns the actions.
    73       */
    74      public final List getActions() {
    75          return actions;
    76      }
    77
    78      /***
    79       * Add an Action to the list of executable actions contained in
    80       * this &lt;if&gt;.
    81       *
    82       * @param action The action to add.
    83       */
    84      public final void addAction(final Action action) {
    85          if (action != null) {
    86              this.actions.add(action);
    87          }
    88      }
    89
    90      /***
    91       * Get the conditional expression.
    92       *
    93       * @return Returns the cond.
    94       */
    95      public final String getCond() {
    96          return cond;
    97      }
    98
    99      /***
    100      * Set the conditional expression.
    101      *
    102      * @param cond The cond to set.
    103      */
    104     public final void setCond(final String cond) {
    105         this.cond = cond;
    106     }
    107
    108     /***
    109      * {@inheritDoc}
    110      */
    111     public void execute(final EventDispatcher evtDispatcher,
    112             final ErrorReporter errRep, final SCInstance scInstance,
    113             final Log appLog, final Collection derivedEvents)
    114     throws ModelException, SCXMLExpressionException {
    115         State parentState = getParentState();
    116         Context ctx = scInstance.getContext(parentState);
    117         Evaluator eval = scInstance.getEvaluator();
    118         execute = eval.evalCond(ctx, cond).booleanValue();
    119         // The "if" statement is a "container"
    120         for (Iterator ifiter = actions.iterator(); ifiter.hasNext();) {
    121             Action aa = (Action) ifiter.next();
    122             if (execute && !(aa instanceof ElseIf)
    123                     && !(aa instanceof Else)) {
    124                 aa.execute(evtDispatcher, errRep, scInstance, appLog,
    125                     derivedEvents);
    126             } else if (execute
    127                     && (aa instanceof ElseIf || aa instanceof Else)) {
    128                 break;
    129             } else if (aa instanceof Else) {
    130                 execute = true;
    131             } else if (aa instanceof ElseIf) {
    132                 execute = eval.evalCond(ctx, ((ElseIf) aa).getCond())
    133                         .booleanValue();
    134             }
    135         }
    136     }
    137
    138 }
    139