Home | History | Annotate | Download | only in ant
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.ant;
     18 
     19 import org.apache.tools.ant.BuildException;
     20 import org.apache.tools.ant.Task;
     21 import org.apache.tools.ant.taskdefs.Sequential;
     22 import org.apache.tools.ant.taskdefs.condition.And;
     23 
     24 /**
     25  * If (condition) then: {@link Sequential} else: {@link Sequential}.
     26  *
     27  * In XML:
     28  * <if condition="${prop with a boolean value}">
     29  *     <then>
     30  *     </then>
     31  *     <else>
     32  *     </else>
     33  * </if>
     34  *
     35  * or
     36  *
     37  * <if>
     38  *     <condition>
     39  *         ...
     40  *     </condition>
     41  *     <then>
     42  *     </then>
     43  *     <else>
     44  *     </else>
     45  * </if>
     46  *
     47  * both <then> and <else> behave like <sequential>.
     48  * <condition> behaves like an <and> condition.
     49  *
     50  * The presence of both <then> and <else> is not required, but one of them must be present.
     51  * <if condition="${some.condition}">
     52  *     <else>
     53  *     </else>
     54  * </if>
     55  * is perfectly valid.
     56  *
     57  */
     58 public class IfElseTask extends Task {
     59 
     60     private boolean mCondition;
     61     private boolean mConditionIsSet = false;
     62     private And mAnd;
     63     private Sequential mThen;
     64     private Sequential mElse;
     65 
     66     /**
     67      * Sets the condition value
     68      */
     69     public void setCondition(boolean condition) {
     70         mCondition = condition;
     71         mConditionIsSet = true;
     72     }
     73 
     74     /**
     75      * Creates and returns the <condition> node which is basically a <and>.
     76      */
     77     public Object createCondition() {
     78         if (mConditionIsSet) {
     79             throw new BuildException("Cannot use both condition attribute and <condition> element");
     80         }
     81 
     82         mAnd = new And();
     83         mAnd.setProject(getProject());
     84         return mAnd;
     85     }
     86 
     87     /**
     88      * Creates and returns the <then> {@link Sequential}
     89      */
     90     public Object createThen() {
     91         mThen = new Sequential();
     92         return mThen;
     93     }
     94 
     95     /**
     96      * Creates and returns the <else> {@link Sequential}
     97      */
     98     public Object createElse() {
     99         mElse = new Sequential();
    100         return mElse;
    101     }
    102 
    103     @Override
    104     public void execute() throws BuildException {
    105         if (mConditionIsSet == false && mAnd == null) {
    106             throw new BuildException("condition attribute or element must be set.");
    107         }
    108 
    109         if (mAnd != null) {
    110             mCondition = mAnd.eval();
    111         }
    112 
    113         // need at least one.
    114         if (mThen == null && mElse == null) {
    115             throw new BuildException("Need at least <then> or <else>");
    116         }
    117 
    118         if (mCondition) {
    119             if (mThen != null) {
    120                 mThen.execute();
    121             }
    122         } else {
    123             if (mElse != null) {
    124                 mElse.execute();
    125             }
    126         }
    127     }
    128 }
    129