Coverage Report - org.apache.commons.scxml.model.If

Classes in this File Line Coverage Branch Coverage Complexity
If
97% 
100% 
2

 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  
  * <if> SCXML element, which serves as a container for conditionally
 35  
  * executed elements. <else> and <elseif> can optionally
 36  
  * appear within an <if> as immediate children, and serve to partition
 37  
  * the elements within an <if>.
 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 <if> 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  15
         super();
 69  15
         this.actions = new ArrayList();
 70  15
         this.execute = false;
 71  15
     }
 72  
 
 73  
     /**
 74  
      * Get the executable actions contained in this <if>.
 75  
      *
 76  
      * @return Returns the actions.
 77  
      */
 78  
     public final List getActions() {
 79  4
         return actions;
 80  
     }
 81  
 
 82  
     /**
 83  
      * Add an Action to the list of executable actions contained in
 84  
      * this <if>.
 85  
      *
 86  
      * @param action The action to add.
 87  
      */
 88  
     public final void addAction(final Action action) {
 89  34
         if (action != null) {
 90  34
             this.actions.add(action);
 91  
         }
 92  34
     }
 93  
 
 94  
     /**
 95  
      * Get the conditional expression.
 96  
      *
 97  
      * @return Returns the cond.
 98  
      */
 99  
     public final String getCond() {
 100  4
         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  15
         this.cond = cond;
 110  15
     }
 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  9
         State parentState = getParentState();
 120  9
         Context ctx = scInstance.getContext(parentState);
 121  9
         Evaluator eval = scInstance.getEvaluator();
 122  9
         ctx.setLocal(getNamespacesKey(), getNamespaces());
 123  9
         execute = eval.evalCond(ctx, cond).booleanValue();
 124  9
         ctx.setLocal(getNamespacesKey(), null);
 125  
         // The "if" statement is a "container"
 126  9
         for (Iterator ifiter = actions.iterator(); ifiter.hasNext();) {
 127  19
             Action aa = (Action) ifiter.next();
 128  19
             if (execute && !(aa instanceof ElseIf)
 129  
                     && !(aa instanceof Else)) {
 130  9
                 aa.execute(evtDispatcher, errRep, scInstance, appLog,
 131  
                     derivedEvents);
 132  10
             } else if (execute
 133  
                     && (aa instanceof ElseIf || aa instanceof Else)) {
 134  0
                 break;
 135  8
             } else if (aa instanceof Else) {
 136  1
                 execute = true;
 137  7
             } else if (aa instanceof ElseIf) {
 138  3
                 ctx.setLocal(getNamespacesKey(), getNamespaces());
 139  3
                 execute = eval.evalCond(ctx, ((ElseIf) aa).getCond())
 140  
                         .booleanValue();
 141  3
                 ctx.setLocal(getNamespacesKey(), null);
 142  
             }
 143  
         }
 144  9
     }
 145  
 
 146  
 }
 147