buildframework/helium/sf/java/core/src/com/nokia/helium/core/ant/taskdefs/GetValueFromVariableSetTask.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 588 c7c26511138f
child 645 b8d81fa19e7d
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     1
/*
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     2
 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     3
 * All rights reserved.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     4
 * This component and the accompanying materials are made available
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     6
 * which accompanies this distribution, and is available
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     8
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     9
 * Initial Contributors:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    10
 * Nokia Corporation - initial contribution.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    11
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    12
 * Contributors:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    13
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    14
 * Description: 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    15
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    16
 */
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    17
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    18
package com.nokia.helium.core.ant.taskdefs;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    19
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    20
import java.util.Vector;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    21
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    22
import org.apache.tools.ant.BuildException;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    23
import org.apache.tools.ant.Task;
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    24
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    25
import com.nokia.helium.core.ant.MappedVariable;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    26
import com.nokia.helium.core.ant.VariableMap;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    27
import com.nokia.helium.core.ant.types.VariableSet;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    28
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    29
/**
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    30
 * To retrive a variable value from a collection of variable set based on name, which contains
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    31
 * property-value in pair.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    32
 * 
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    33
 * <pre>
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    34
 * Example:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    35
 * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    36
 * &lt;hlm:argSet id="test.variableSet"&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    37
 * &lt;variable name="v1" value="the_value_1"/&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    38
 *     &lt;variable name="v2" value="the_value_2"/&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    39
 *      &lt;variable name="v3" value="the_value_3"/&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    40
 * &lt;/hlm:argSet&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    41
 *       
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    42
 *  &lt;hlm:getVariableValue name="v3" property="v1.value"&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    43
 * &lt;hlm:argSet refid="test.variableSet"/&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    44
 * &lt;/hlm:getVariableValue&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    45
 * </pre>
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    46
 * 
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    47
 * @ant.task name="getVariableValue"
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    48
 */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    49
public class GetValueFromVariableSetTask extends Task {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    50
    private String name;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    51
    private String property;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    52
    private boolean failOnError = true;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    53
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    54
    private Vector<VariableMap> variableMaps = new Vector<VariableMap>();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    55
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    56
    public void setName(String name) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    57
        this.name = name;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    58
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    59
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    60
    /**
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    61
     * Helper function to set failonerror attribute for the task.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    62
     * 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    63
     * @param failStatus, if true will fail the build if no variable is found for matching name.
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    64
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    65
    public void setFailOnError(boolean failStatus) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    66
        failOnError = failStatus;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    67
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    68
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    69
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    70
     * Helper function to store the name of the property where the value to be stored
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    71
     * 
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    72
     * @param property name of the property where the result to be stored
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    73
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    74
    public void setProperty(String property) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    75
        this.property = property;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    76
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    77
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    78
    /**
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    79
     * Helper function to create the VariableIFImpl object.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    80
     * 
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    81
     * @return created VariableIFImpl instance
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    82
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    83
    public VariableSet createVariableIFImpl() {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    84
        VariableSet var = new VariableSet();
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    85
        add(var);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    86
        return var;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    87
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    88
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    89
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    90
     * Helper function to add the newly created variable set. Called by ant.
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    91
     * 
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    92
     * @param vs variable set to be added.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    93
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    94
    public void add(VariableMap vs) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    95
        variableMaps.add(vs);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    96
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    97
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    98
    public VariableMap getVariableInterface() {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    99
        if (variableMaps.isEmpty()) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   100
            throw new BuildException("variable interface cannot be null");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   101
        }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   102
        if (variableMaps.size() > 1) {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   103
            throw new BuildException("maximum one variable interface can be set");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   104
        }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   105
        return variableMaps.elementAt(0);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   106
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   107
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   108
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   109
     * Task to get the name / value pair
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   110
     * 
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   111
     * @return return the name / value pair for the variable set.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   112
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   113
    public void execute() {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   114
        if (name == null) {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   115
            throw new BuildException("'name' attribute has not been defined.");
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   116
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   117
        if (property == null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   118
            throw new BuildException("'property' attribute has not been defined.");
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   119
        }
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   120
        VariableMap variableMap = getVariableInterface();
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   121
        for (MappedVariable var : variableMap.getVariables()) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   122
            if (var.getName().equals(name)) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   123
                getProject().setProperty(property, var.getValue());
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   124
                return;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   125
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   126
        }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   127
        if (failOnError) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   128
            throw new BuildException("Could not find '" + name + "' variable.");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   129
        }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   130
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   131
}