588
|
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.helium.core.ant.types;
|
|
19 |
|
|
20 |
import java.util.ArrayList;
|
628
|
21 |
import java.util.Arrays;
|
588
|
22 |
import java.util.List;
|
|
23 |
|
628
|
24 |
import org.apache.tools.ant.BuildException;
|
588
|
25 |
import org.apache.tools.ant.types.DataType;
|
628
|
26 |
import org.codehaus.plexus.util.StringUtils;
|
588
|
27 |
|
|
28 |
import com.nokia.helium.core.ant.MappedVariable;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Helper class to store the command line variables
|
|
32 |
* with name / value pair.
|
|
33 |
* @ant.type name="arg" category="Core"
|
|
34 |
* @ant.type name="makeOption" category="Core"
|
|
35 |
*/
|
|
36 |
public class VariableImpl extends DataType implements MappedVariable
|
|
37 |
{
|
|
38 |
private String name;
|
|
39 |
private String value;
|
|
40 |
private String cmdLine;
|
|
41 |
|
|
42 |
/**
|
|
43 |
* Set the name of the variable.
|
|
44 |
* @param name
|
|
45 |
*/
|
|
46 |
public void setName(String name) {
|
|
47 |
this.name = name;
|
|
48 |
}
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Get the name of the variable. Will return name is name attribute is set
|
|
52 |
* or first command line parameter if line is used.
|
|
53 |
* @return name.
|
|
54 |
*/
|
|
55 |
public String getName() {
|
|
56 |
if ( cmdLine == null) {
|
|
57 |
if (name == null ) {
|
|
58 |
throw new BuildException( "'name' attribute must be defined");
|
|
59 |
}
|
|
60 |
if (value == null) {
|
|
61 |
throw new BuildException( "'value' attribute must be defined");
|
|
62 |
}
|
|
63 |
return name;
|
|
64 |
} else {
|
|
65 |
if (name != null && value != null) {
|
|
66 |
throw new BuildException( "You can define either name, value or line attribute and not both");
|
|
67 |
}
|
|
68 |
String cmdPart = cmdLine.trim();
|
|
69 |
String[] cmdArgs = cmdPart.split(" ");
|
|
70 |
return cmdArgs[0];
|
|
71 |
}
|
|
72 |
}
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Set the value of the variable.
|
|
76 |
* @param value
|
|
77 |
*/
|
|
78 |
public void setValue(String value) {
|
|
79 |
this.value = value;
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
|
83 |
* Helper function to set the command line string
|
|
84 |
* @param line, string as input to command line.
|
|
85 |
*/
|
|
86 |
public void setLine(String line) {
|
|
87 |
cmdLine = line;
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Get the value of the variable. Returns value if name/value are used, or the line attribute minus
|
|
92 |
* the first command line parameter.
|
|
93 |
* @return value.
|
|
94 |
*/
|
|
95 |
public String getValue() {
|
|
96 |
if ( cmdLine == null) {
|
|
97 |
if (name == null ) {
|
|
98 |
throw new BuildException( "'name' attribute must be defined");
|
|
99 |
}
|
|
100 |
if (value == null) {
|
|
101 |
throw new BuildException( "'value' attribute must be defined");
|
|
102 |
}
|
|
103 |
return value;
|
|
104 |
} else {
|
|
105 |
if (name != null && value != null) {
|
|
106 |
throw new BuildException( "You can define either name, value or line attribute but not both");
|
|
107 |
}
|
|
108 |
|
|
109 |
String cmdPart = cmdLine.trim();
|
|
110 |
List<String> cmdList = new ArrayList<String>(Arrays.asList(cmdPart.split(" ")));
|
|
111 |
if (cmdList.size() > 0) {
|
|
112 |
cmdList.remove(0);
|
|
113 |
}
|
|
114 |
return StringUtils.join(cmdList.toArray(), " "); // "-c armv5 -c foobar" : " armv5 -c foobar"
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* {@inheritDoc}
|
|
120 |
*/
|
|
121 |
public String getParameter() {
|
|
122 |
return getParameter("=");
|
|
123 |
}
|
|
124 |
|
|
125 |
/**
|
|
126 |
* {@inheritDoc}
|
|
127 |
*/
|
|
128 |
public String getParameter(String separator) {
|
|
129 |
if ( cmdLine == null) {
|
|
130 |
if (name == null ) {
|
|
131 |
throw new BuildException( "'name' attribute must be defined");
|
|
132 |
}
|
|
133 |
if (value == null) {
|
|
134 |
throw new BuildException( "'value' attribute must be defined");
|
|
135 |
}
|
|
136 |
return name + separator + value;
|
|
137 |
} else {
|
|
138 |
if (name != null && value != null) {
|
|
139 |
throw new BuildException( "You can define either name, value or line attribute but not both");
|
|
140 |
}
|
|
141 |
return cmdLine;
|
|
142 |
}
|
|
143 |
}
|
|
144 |
|
|
145 |
} |