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