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.io.Serializable;
    20  import java.util.ArrayList;
    21  import java.util.List;
    22
    23  /***
    24   * An abstract base class for elements in SCXML that can serve as a
    25   * &lt;target&gt; for a &lt;transition&gt;, such as State or Parallel.
    26   *
    27   */
    28  public abstract class TransitionTarget implements Serializable {
    29
    30      /***
    31       * Identifier for this transition target. Other parts of the SCXML
    32       * document may refer to this &lt;state&gt; using this ID.
    33       */
    34      private String id;
    35
    36      /***
    37       * Optional property holding executable content to be run upon
    38       * entering this transition target.
    39       */
    40      private OnEntry onEntry;
    41
    42      /***
    43       * Optional property holding executable content to be run upon
    44       * exiting this transition target.
    45       */
    46      private OnExit onExit;
    47
    48      /***
    49       * Optional property holding the data model for this transition target.
    50       */
    51      private Datamodel datamodel;
    52
    53      /***
    54       * The parent of this transition target (may be null, if the parent
    55       * is the SCXML document root).
    56       */
    57      private TransitionTarget parent;
    58
    59      /***
    60       * List of history states owned by a given state (applies to non-leaf
    61       * states).
    62       */
    63      private List history;
    64
    65      /***
    66       * Constructor.
    67       */
    68      public TransitionTarget() {
    69          super();
    70          onEntry = new OnEntry(); //empty defaults
    71          onEntry.setParent(this);
    72          onExit = new OnExit();   //empty defaults
    73          onExit.setParent(this);
    74          parent = null;
    75          history = new ArrayList();
    76      }
    77
    78      /***
    79       * Get the identifier for this transition target (may be null).
    80       *
    81       * @return Returns the id.
    82       */
    83      public final String getId() {
    84          return id;
    85      }
    86
    87      /***
    88       * Set the identifier for this transition target.
    89       *
    90       * @param id The id to set.
    91       */
    92      public final void setId(final String id) {
    93          this.id = id;
    94      }
    95
    96      /***
    97       * Get the onentry property.
    98       *
    99       * @return Returns the onEntry.
    100      */
    101     public final OnEntry getOnEntry() {
    102         return onEntry;
    103     }
    104
    105     /***
    106      * Set the onentry property.
    107      *
    108      * @param onEntry The onEntry to set.
    109      */
    110     public final void setOnEntry(final OnEntry onEntry) {
    111         this.onEntry = onEntry;
    112         this.onEntry.setParent(this);
    113     }
    114
    115     /***
    116      * Get the onexit property.
    117      *
    118      * @return Returns the onExit.
    119      */
    120     public final OnExit getOnExit() {
    121         return onExit;
    122     }
    123
    124     /***
    125      * Set the onexit property.
    126      *
    127      * @param onExit The onExit to set.
    128      */
    129     public final void setOnExit(final OnExit onExit) {
    130         this.onExit = onExit;
    131         this.onExit.setParent(this);
    132     }
    133
    134     /***
    135      * Get the data model for this transition target.
    136      *
    137      * @return Returns the data model.
    138      */
    139     public final Datamodel getDatamodel() {
    140         return datamodel;
    141     }
    142
    143     /***
    144      * Set the data model for this transition target.
    145      *
    146      * @param datamodel The Datamodel to set.
    147      */
    148     public final void setDatamodel(final Datamodel datamodel) {
    149         this.datamodel = datamodel;
    150     }
    151
    152     /***
    153      * Get the parent TransitionTarget.
    154      *
    155      * @return Returns the parent state
    156      * (null if parent is &lt;scxml&gt; element)
    157      */
    158     public final TransitionTarget getParent() {
    159         return parent;
    160     }
    161
    162     /***
    163      * Set the parent TransitionTarget.
    164      *
    165      * @param parent The parent state to set
    166      */
    167     public final void setParent(final TransitionTarget parent) {
    168         this.parent = parent;
    169     }
    170
    171     /***
    172      * Get the parent State.
    173      *
    174      * @return The parent State
    175      */
    176     public final State getParentState() {
    177         TransitionTarget tt = this.getParent();
    178         if (tt == null) {
    179             return null;
    180         } else {
    181             if (tt instanceof State) {
    182                 return (State) tt;
    183             } else { //tt is Parallel
    184                 return tt.getParentState();
    185             }
    186         }
    187     }
    188
    189     /***
    190      * This method is used by XML digester.
    191      *
    192      * @param h
    193      *            History pseudo state
    194      *
    195      * @since 0.7
    196      */
    197     public final void addHistory(final History h) {
    198         history.add(h);
    199         h.setParent(this);
    200     }
    201
    202     /***
    203      * Does this state have a history pseudo state.
    204      *
    205      * @return boolean true if a given state contains at least one
    206      *                 history pseudo state
    207      *
    208      * @since 0.7
    209      */
    210     public final boolean hasHistory() {
    211         return (!history.isEmpty());
    212     }
    213
    214     /***
    215      * Get the list of history pseudo states for this state.
    216      *
    217      * @return a list of all history pseudo states contained by a given state
    218      *         (can be empty)
    219      * @see #hasHistory()
    220      *
    221      * @since 0.7
    222      */
    223     public final List getHistory() {
    224         return history;
    225     }
    226
    227 }
    228