buildframework/helium/sf/java/core/src/com/nokia/helium/core/ant/types/VariableImpl.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 588 c7c26511138f
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
/*
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     2
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     3
* All rights reserved.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     4
* This component and the accompanying materials are made available
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     6
* which accompanies this distribution, and is available
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     8
*
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     9
* Initial Contributors:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    11
*
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    12
* Contributors:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    13
*
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    14
* Description: 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    15
*
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    16
*/
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.types;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    19
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    20
import java.util.ArrayList;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    21
import java.util.Arrays;
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    22
import java.util.List;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    23
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    24
import org.apache.tools.ant.BuildException;
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    25
import org.apache.tools.ant.types.DataType;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    26
import org.codehaus.plexus.util.StringUtils;
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    27
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    28
import com.nokia.helium.core.ant.MappedVariable;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    29
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    30
/**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    31
 * Helper class to store the command line variables
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    32
 * with name / value pair.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    33
 * @ant.type name="arg" category="Core"
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    34
 * @ant.type name="makeOption" category="Core"
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    35
 */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    36
public class VariableImpl extends DataType implements MappedVariable
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    37
{
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    38
    private String name;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    39
    private String value;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    40
    private String cmdLine;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    41
    
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    42
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    43
     * Set the name of the variable.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    44
     * @param name
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    45
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    46
    public void setName(String name) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    47
        this.name = name;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    48
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    49
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    50
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    51
     * Get the name of the variable. Will return name is name attribute is set
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    52
     * or first command line parameter if line is used.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    53
     * @return name.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    54
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    55
    public String getName() {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    56
        if ( cmdLine == null) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    57
            if (name == null ) { 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    58
                throw new BuildException( "'name' attribute must be defined");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    59
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    60
            if (value == null) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    61
                throw new BuildException( "'value' attribute must be defined");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    62
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    63
            return name;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    64
        } else {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    65
            if (name != null  && value != null) { 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    66
                throw new BuildException( "You can define either name, value or line attribute and not both");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    67
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    68
            String cmdPart = cmdLine.trim();
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    69
            String[] cmdArgs = cmdPart.split(" ");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    70
            return cmdArgs[0];
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    71
        }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    72
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    73
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    74
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    75
     * Set the value of the variable.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    76
     * @param value
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    77
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    78
    public void setValue(String value) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    79
        this.value = value;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    80
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    81
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    82
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    83
     * Helper function to set the command line string
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    84
     * @param line, string as input to command line.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    85
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    86
    public void setLine(String line) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    87
        cmdLine = line;
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
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    91
     * Get the value of the variable. Returns value if name/value are used, or the line attribute minus 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    92
     * the first command line parameter. 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    93
     * @return value.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    94
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    95
    public String getValue() {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    96
        if ( cmdLine == null) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    97
            if (name == null ) { 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    98
                throw new BuildException( "'name' attribute must be defined");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    99
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   100
            if (value == null) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   101
                throw new BuildException( "'value' attribute must be defined");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   102
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   103
            return value;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   104
        } else {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   105
            if (name != null  && value != null) { 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   106
                throw new BuildException( "You can define either name, value or line attribute but not both");
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
            String cmdPart = cmdLine.trim();
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   110
            List<String> cmdList = new ArrayList<String>(Arrays.asList(cmdPart.split(" ")));
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   111
            if (cmdList.size() > 0) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   112
                cmdList.remove(0);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   113
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   114
            return StringUtils.join(cmdList.toArray(), " "); // "-c armv5 -c foobar" : " armv5 -c foobar"
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   115
        }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   116
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   117
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   118
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   119
     * {@inheritDoc}
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   120
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   121
    public String getParameter() {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   122
        return getParameter("=");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   123
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   124
    
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   125
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   126
     * {@inheritDoc}
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   127
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   128
    public String getParameter(String separator) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   129
        if ( cmdLine == null) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   130
            if (name == null ) { 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   131
                throw new BuildException( "'name' attribute must be defined");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   132
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   133
            if (value == null) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   134
                throw new BuildException( "'value' attribute must be defined");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   135
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   136
            return name + separator + value;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   137
        } else {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   138
            if (name != null  && value != null) { 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   139
                throw new BuildException( "You can define either name, value or line attribute but not both");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   140
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   141
            return cmdLine;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   142
        }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   143
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   144
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   145
}