1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.function;
20
21 import net.sf.jameleon.util.Configurator;
22
23 import org.apache.commons.jelly.JellyContext;
24
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.ArrayList;
28
29 /***
30 * The <code>ContextHelper</code> class is used as a helper class to remove the context-specific
31 * calls from Jameleon Tags.
32 */
33 public final class ContextHelper {
34
35 /***
36 * This class was not meant to be instantiated
37 */
38 private ContextHelper(){
39 }
40
41
42
43 public static void setVariable(JellyContext context, String name, Object value) {
44 context.setVariable(name,value);
45 }
46
47 public static Object getVariable(JellyContext context, String name) {
48 Object value = null;
49 if (context != null && name != null && name.length() > 0) {
50 value = context.getVariable(name);
51 }
52 return value;
53 }
54
55 public static void removeVariable(JellyContext context, String name) {
56 context.removeVariable(name);
57 }
58
59
60
61 public static String getVariableAsString(JellyContext context, String name) {
62 String value = null;
63 if (context != null && name != null && name.length() > 0) {
64 Object obj = context.getVariable(name);
65 if (obj != null) {
66 value = (String) obj;
67 }
68 }
69 return value;
70 }
71
72 public static boolean getValueAsBooleanWithConfig(JellyContext context, String contextName, String configName, boolean defaultValue){
73 boolean value = Boolean.valueOf(Configurator.getInstance().getValue(configName, defaultValue+"")).booleanValue();
74 if (ContextHelper.getVariable(context, contextName) != null) {
75 value = ContextHelper.getVariableAsBoolean(context, contextName);
76 }
77 return value;
78 }
79
80 public static int getValueAsIntWithConfig(JellyContext context, String contextName, String configName, int defaultValue){
81 int value;
82 try{
83 value = Integer.parseInt(Configurator.getInstance().getValue(configName, defaultValue+""));
84 }catch(NumberFormatException nfe){
85 value = -1;
86 }
87 if (ContextHelper.getVariable(context, contextName) != null) {
88 value = ContextHelper.getVariableAsInt(context, contextName);
89 }
90 return value;
91 }
92
93 public static String getValueAsStringWithConfig(JellyContext context, String contextName, String configName, String defaultValue){
94 String value = Configurator.getInstance().getValue(configName, defaultValue);
95 String contextValue = ContextHelper.getVariableAsString(context, contextName);
96 if (contextValue != null) {
97 value = contextValue;
98 }
99 return value;
100 }
101
102 public static boolean getVariableAsBoolean(JellyContext context, String name) {
103 Object o = ContextHelper.getVariable(context, name);
104 boolean returnValue = false;
105 if (o != null && o instanceof Boolean) {
106 returnValue = ((Boolean)o).booleanValue();
107 } else if (o != null && o instanceof String) {
108 returnValue = Boolean.valueOf((String)o).booleanValue();
109 }
110 return returnValue;
111 }
112
113 public static int getVariableAsInt(JellyContext context, String name) {
114 int returnValue = -1;
115 Object o = ContextHelper.getVariable(context, name);
116 if (o != null && o instanceof Integer) {
117 returnValue = ((Integer)o).intValue();
118 } else if (o != null && o instanceof String) {
119 try{
120 returnValue = Integer.parseInt((String)o);
121 }catch(NumberFormatException nfe){
122 returnValue = -1;
123 }
124 }
125 return returnValue;
126 }
127
128 public static List getVariableAsList(JellyContext context, String key){
129 Object obj = context.getVariable(key);
130 return makeList(obj);
131 }
132
133 public static boolean isVariableNull(JellyContext context, String name) {
134 Object obj = context.getVariable(name);
135 boolean isNull = false;
136 if (obj != null) {
137 if (obj instanceof String) {
138 String str = (String)obj;
139 if (str.equals("")) {
140 isNull = true;
141 }
142 }else if (obj instanceof List) {
143 List l = (List)obj;
144 if ( l.size() < 1 ) {
145 isNull = true;
146 }
147 }
148 }else{
149 isNull = true;
150 }
151 return isNull;
152 }
153
154 public static List makeList(Object obj) {
155 List l;
156 if ( obj != null && obj instanceof List ) {
157 l = (List)obj;
158 }else{
159 l = new ArrayList();
160 if ( obj != null ) {
161 l.add(obj);
162 }
163 }
164 return l;
165 }
166
167 public static void removeVariables(JellyContext context, List vars){
168 if (vars != null) {
169 Iterator it = vars.iterator();
170 while (it.hasNext()) {
171 context.removeVariable((String)it.next());
172 }
173 }
174 }
175 }
176