|
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 * <hlm:argSet id="test.variableSet"> |
|
36 * <variable name="v1" value="the_value_1"/> |
|
37 * <variable name="v2" value="the_value_2"/> |
|
38 * <variable name="v3" value="the_value_3"/> |
|
39 * </hlm:argSet> |
|
40 * |
|
41 * <hlm:getVariableValue name="v3" property="v1.value"> |
|
42 * <hlm:argSet refid="test.variableSet"/> |
|
43 * </hlm:getVariableValue> |
|
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 } |