1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Iterator;
24
25 import org.apache.commons.jelly.MissingAttributeException;
26 import org.apache.commons.jelly.JellyTagException;
27 import org.apache.commons.jelly.TagSupport;
28 import org.apache.commons.jelly.XMLOutput;
29
30 /***
31 * Used for mapping a List to another variable.
32 * The implementation of this class will be to decide if the List is a list of values or a List of variabl names.
33 * NOTE:<br>
34 * No setters or getters defined for the instances variables defined below. If these variables are to be defined
35 * in the tag as attributes, then in the subclass of this class, the setters and getters must be defined for the
36 * appropriate attributes.
37 */
38 public abstract class VariableTag extends TagSupport {
39
40 protected static final String LIST = "list";
41 protected static final String STRING = "string";
42
43 protected TestCaseTag tct;
44 /***
45 * The variable name to map from. This is the variable name is normally <b>not</b> supported by the function tag.
46 */
47 protected String fromVariable;
48 /***
49 * The value of the fromVariable.
50 */
51 protected String value;
52 /***
53 * The variable name to map the <code>fromVariable</code> to. This is the variable name <b>is</b> supported by the function tag.
54 */
55 protected String toVariable;
56 /***
57 * The type of the variable being stored. Currently, this only supports List and String (the default).
58 */
59 protected String variableType = STRING;
60
61 /***
62 * An implementation of the <code>doTag</code> method provided by the <code>TagSupport</code> class.
63 * Maps the value in the <code>fromVariable</code> over to the original variable name, <code>toVariable</code>.
64 */
65 public void doTag(XMLOutput out) throws MissingAttributeException, JellyTagException{
66
67 init();
68 validate();
69 setVariableValue(toVariable, value);
70 }
71
72 public void setVariableValue(String toVariable, Object value){
73 if ( variableType.equalsIgnoreCase(STRING) ) {
74 context.setVariable(toVariable, value);
75 }else if ( variableType.equalsIgnoreCase(LIST) ) {
76 List l = null;
77 Object to = context.getVariable(toVariable);
78 if ( to != null && to instanceof List ) {
79 l = (List)to;
80 }else if ( to == null || to instanceof String ) {
81 l = new ArrayList();
82 if (to instanceof String ) {
83
84 l.add(to);
85 }
86 }
87 if (value instanceof List) {
88 Iterator it = ((List)value).iterator();
89 while (it.hasNext()) {
90 l.add(it.next());
91 }
92 }else{
93 l.add(value);
94 }
95 setVariable(toVariable,l);
96 }
97 }
98
99 /***
100 * Used to validate everything is set up correctly.
101 * @throws MissingAttributeException - When a required attribute isn't set.
102 * @throws JellyTagException - When this tag is used out of context.
103 */
104 protected void validate()throws MissingAttributeException, JellyTagException{
105 if (toVariable == null){
106 throw new MissingAttributeException("toVariable is a required attribute!");
107 }else if ( !(STRING.equalsIgnoreCase(variableType)) && !(LIST.equalsIgnoreCase(variableType)) ) {
108 throw new MissingAttributeException("variableType must be set to either string or list!");
109 }
110 }
111
112 protected void setVariable(String key, Object value){
113 init();
114 context.setVariable(key, value);
115 if (tct != null){
116 tct.getKeySet().add(key);
117 }
118 }
119
120 protected void init(){
121 if (tct == null) {
122 Object obj = findAncestorWithClass(TestCaseTag.class);
123 if (obj != null){
124 tct = (TestCaseTag)obj;
125 }
126 }
127 }
128 }