buildframework/helium/tools/common/java/src/com/nokia/ant/taskdefs/GetValueFromVariableSet.java
changeset 1 be27ed110b50
child 179 d8ac696cc51f
equal deleted inserted replaced
0:044383f39525 1:be27ed110b50
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.ant.taskdefs;
       
    19 
       
    20 import org.apache.tools.ant.BuildException;
       
    21 import org.apache.tools.ant.Task;
       
    22 import org.apache.tools.ant.types.Reference;
       
    23 import java.util.Iterator;
       
    24 import java.util.Vector;
       
    25 import com.nokia.ant.types.VariableSet;
       
    26 import com.nokia.ant.types.Variable;
       
    27 
       
    28 
       
    29 /**
       
    30  * To retrive a variable value from a collection of variable set based on name, which contains property-value in pair.
       
    31  *  
       
    32  * <pre>
       
    33  * Example:
       
    34  * 
       
    35  * &lt;hlm:argSet id="test.variableSet"&gt;
       
    36  * &lt;variable name="v1" value="the_value_1"/&gt;
       
    37  *     &lt;variable name="v2" value="the_value_2"/&gt;
       
    38  *      &lt;variable name="v3" value="the_value_3"/&gt;
       
    39  * &lt;/hlm:argSet&gt;
       
    40  *       
       
    41  *  &lt;hlm:getVariableValue name="v3" property="v1.value"&gt;
       
    42  * &lt;hlm:argSet refid="test.variableSet"/&gt;
       
    43  * &lt;/hlm:getVariableValue&gt;
       
    44  * </pre>
       
    45  * @ant.task name="getVariableValue"
       
    46  */
       
    47 public class GetValueFromVariableSet extends Task {
       
    48     private String name;
       
    49     private String property;
       
    50     private Vector<VariableSet> variablesets = new Vector<VariableSet>();
       
    51 
       
    52     public void setName(String name) {
       
    53         this.name = name;
       
    54     }
       
    55 
       
    56     public void setProperty(String property) {
       
    57         this.property = property;
       
    58     }
       
    59 
       
    60     public void add(VariableSet vs) {
       
    61         variablesets.add(vs);
       
    62     }
       
    63 
       
    64     public VariableSet createVariableSet() {
       
    65         VariableSet vs = new VariableSet();
       
    66         variablesets.add(vs);
       
    67         return vs;
       
    68     }
       
    69 
       
    70     public void execute() {
       
    71         if (name == null)
       
    72             throw new BuildException("'name' attribute has not been defined.");
       
    73         if (property == null)
       
    74             throw new BuildException(
       
    75                     "'property' attribute has not been defined.");
       
    76 
       
    77         for (Iterator<VariableSet> vsit = variablesets.iterator(); vsit
       
    78                 .hasNext();) {
       
    79             VariableSet vs = vsit.next();
       
    80             if (vs.isReference()) {
       
    81                 Reference reference = vs.getRefid();
       
    82                 vs = (VariableSet)reference.getReferencedObject(getProject());
       
    83             }
       
    84             for (Iterator vit = vs.getVariables().iterator(); vit.hasNext();) {
       
    85                 Variable v = (Variable) vit.next();
       
    86                 if (v.getName().equals(name)) {
       
    87                     log("Setting '" + property + "' property to '" + v.getValue() + "'");
       
    88                     getProject().setProperty(property, v.getValue());
       
    89                     return;
       
    90                 }
       
    91             }
       
    92         }
       
    93 
       
    94         throw new BuildException("Could not find '" + name + "' variable.");
       
    95     }
       
    96 
       
    97 }