1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.apache.commons.jelly.tags.core; |
17 |
|
|
18 |
|
import java.util.HashMap; |
19 |
|
import java.util.Map; |
20 |
|
|
21 |
|
import org.apache.commons.beanutils.ConversionException; |
22 |
|
import org.apache.commons.beanutils.Converter; |
23 |
|
import org.apache.commons.beanutils.converters.BooleanConverter; |
24 |
|
import org.apache.commons.beanutils.converters.ByteConverter; |
25 |
|
import org.apache.commons.beanutils.converters.CharacterConverter; |
26 |
|
import org.apache.commons.beanutils.converters.DoubleConverter; |
27 |
|
import org.apache.commons.beanutils.converters.FloatConverter; |
28 |
|
import org.apache.commons.beanutils.converters.IntegerConverter; |
29 |
|
import org.apache.commons.beanutils.converters.LongConverter; |
30 |
|
import org.apache.commons.beanutils.converters.ShortConverter; |
31 |
|
import org.apache.commons.jelly.JellyTagException; |
32 |
|
import org.apache.commons.jelly.XMLOutput; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
332 |
public class ArgTag extends BaseClassLoaderTag { |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
114 |
public ArgTag() { |
48 |
114 |
} |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
public void setType(String type) { |
60 |
76 |
this.typeString = type; |
61 |
76 |
} |
62 |
|
|
63 |
|
|
64 |
|
public void setValue(Object value) { |
65 |
144 |
this.value= value; |
66 |
144 |
} |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
public void doTag(XMLOutput output) throws JellyTagException { |
72 |
146 |
invokeBody(output); |
73 |
|
|
74 |
146 |
Class klass = null; |
75 |
146 |
if("boolean".equals(typeString)) { |
76 |
4 |
klass = Boolean.TYPE; |
77 |
4 |
assertNotNull(value); |
78 |
142 |
} else if("byte".equals(typeString)) { |
79 |
6 |
klass = Byte.TYPE; |
80 |
6 |
assertNotNull(value); |
81 |
136 |
} else if("short".equals(typeString)) { |
82 |
6 |
klass = Short.TYPE; |
83 |
6 |
assertNotNull(value); |
84 |
130 |
} else if("int".equals(typeString)) { |
85 |
14 |
klass = Integer.TYPE; |
86 |
14 |
assertNotNull(value); |
87 |
116 |
} else if("char".equals(typeString)) { |
88 |
4 |
klass = Character.TYPE; |
89 |
4 |
assertNotNull(value); |
90 |
112 |
} else if("float".equals(typeString)) { |
91 |
6 |
klass = Float.TYPE; |
92 |
6 |
assertNotNull(value); |
93 |
106 |
} else if("long".equals(typeString)) { |
94 |
6 |
klass = Long.TYPE; |
95 |
6 |
assertNotNull(value); |
96 |
100 |
} else if("double".equals(typeString)) { |
97 |
6 |
klass = Double.TYPE; |
98 |
6 |
assertNotNull(value); |
99 |
94 |
} else if(null != typeString) { |
100 |
|
try { |
101 |
24 |
klass = getClassLoader().loadClass(typeString); |
102 |
24 |
} catch (ClassNotFoundException e) { |
103 |
0 |
throw new JellyTagException(e); |
104 |
24 |
} |
105 |
70 |
} else if(null == value) { |
106 |
0 |
klass = Object.class; |
107 |
|
} else { |
108 |
70 |
klass = value.getClass(); |
109 |
|
} |
110 |
|
|
111 |
130 |
if(!isInstanceOf(klass,value)) { |
112 |
36 |
if (klass.equals(Class.class)) |
113 |
|
{ |
114 |
|
try { |
115 |
0 |
value = getClassLoader().loadClass((String) value); |
116 |
0 |
} catch (ClassNotFoundException e) { |
117 |
0 |
throw new JellyTagException(e); |
118 |
0 |
} |
119 |
|
} |
120 |
|
else |
121 |
|
{ |
122 |
36 |
value = convert(klass,value); |
123 |
|
} |
124 |
|
} |
125 |
|
|
126 |
130 |
ArgTagParent parent = (ArgTagParent)findAncestorWithClass(ArgTagParent.class); |
127 |
130 |
if(null == parent) { |
128 |
0 |
throw new JellyTagException("This tag must be enclosed inside an ArgTagParent implementation (for example, <new> or <invoke>)" ); |
129 |
|
} else { |
130 |
130 |
parent.addArgument(klass,value); |
131 |
|
} |
132 |
130 |
} |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
private void assertNotNull(Object value) throws JellyTagException { |
138 |
52 |
if(null == value) { |
139 |
16 |
throw new JellyTagException("A " + typeString + " instance cannot be null."); |
140 |
|
} |
141 |
36 |
} |
142 |
|
|
143 |
|
private boolean isInstanceOf(Class klass, Object value) { |
144 |
130 |
return (null == value || (klass.isInstance(value))); |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
private String typeString; |
152 |
|
|
153 |
|
|
154 |
|
private Object value; |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
private static Object convert(Class klass, Object value) throws JellyTagException { |
160 |
36 |
if(null == value) { |
161 |
0 |
return null; |
162 |
36 |
} else if(!klass.isInstance(value)) { |
163 |
36 |
Converter converter = (Converter)(converterMap.get(klass)); |
164 |
36 |
if(null == converter) { |
165 |
0 |
throw new JellyTagException("Can't convert " + value + " to " + klass); |
166 |
|
} else { |
167 |
|
try { |
168 |
36 |
return converter.convert(klass,value); |
169 |
|
} catch(ConversionException e) { |
170 |
0 |
throw new JellyTagException("Can't convert " + value + " to " + klass + " (" + e.toString() + ")",e); |
171 |
|
} |
172 |
|
} |
173 |
|
} else { |
174 |
0 |
return value; |
175 |
|
} |
176 |
|
|
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
80 |
private static Map converterMap = new HashMap(); |
181 |
|
|
182 |
|
static { |
183 |
|
{ |
184 |
40 |
Converter c = new BooleanConverter(); |
185 |
40 |
converterMap.put(Boolean.TYPE,c); |
186 |
40 |
converterMap.put(Boolean.class,c); |
187 |
|
} |
188 |
|
{ |
189 |
40 |
Converter c = new CharacterConverter(); |
190 |
40 |
converterMap.put(Character.TYPE,c); |
191 |
40 |
converterMap.put(Character.class,c); |
192 |
|
} |
193 |
|
{ |
194 |
40 |
Converter c = new Converter() { |
195 |
|
public Object convert(Class klass, Object value) { |
196 |
|
if(value instanceof Number) { |
197 |
|
return new Byte(((Number)value).byteValue()); |
198 |
|
} else { |
199 |
|
return inner.convert(klass,value); |
200 |
|
} |
201 |
|
} |
202 |
|
private Converter inner = new ByteConverter(); |
203 |
|
}; |
204 |
40 |
converterMap.put(Byte.TYPE,c); |
205 |
40 |
converterMap.put(Byte.class,c); |
206 |
|
} |
207 |
|
{ |
208 |
40 |
Converter c = new Converter() { |
209 |
|
public Object convert(Class klass, Object value) { |
210 |
|
if(value instanceof Number) { |
211 |
|
return new Short(((Number)value).shortValue()); |
212 |
|
} else { |
213 |
|
return inner.convert(klass,value); |
214 |
|
} |
215 |
|
} |
216 |
|
private Converter inner = new ShortConverter(); |
217 |
|
}; |
218 |
40 |
converterMap.put(Short.TYPE,c); |
219 |
40 |
converterMap.put(Short.class,c); |
220 |
|
} |
221 |
|
{ |
222 |
40 |
Converter c = new Converter() { |
223 |
|
public Object convert(Class klass, Object value) { |
224 |
|
if(value instanceof Number) { |
225 |
|
return new Integer(((Number)value).intValue()); |
226 |
|
} else { |
227 |
|
return inner.convert(klass,value); |
228 |
|
} |
229 |
|
} |
230 |
|
private Converter inner = new IntegerConverter(); |
231 |
|
}; |
232 |
40 |
converterMap.put(Integer.TYPE,c); |
233 |
40 |
converterMap.put(Integer.class,c); |
234 |
|
} |
235 |
|
{ |
236 |
40 |
Converter c = new Converter() { |
237 |
|
public Object convert(Class klass, Object value) { |
238 |
|
if(value instanceof Number) { |
239 |
|
return new Long(((Number)value).longValue()); |
240 |
|
} else { |
241 |
|
return inner.convert(klass,value); |
242 |
|
} |
243 |
|
} |
244 |
|
private Converter inner = new LongConverter(); |
245 |
|
}; |
246 |
40 |
converterMap.put(Long.TYPE,c); |
247 |
40 |
converterMap.put(Long.class,c); |
248 |
|
} |
249 |
|
{ |
250 |
40 |
Converter c = new Converter() { |
251 |
|
public Object convert(Class klass, Object value) { |
252 |
|
if(value instanceof Number) { |
253 |
|
return new Float(((Number)value).floatValue()); |
254 |
|
} else { |
255 |
|
return inner.convert(klass,value); |
256 |
|
} |
257 |
|
} |
258 |
|
private Converter inner = new FloatConverter(); |
259 |
|
}; |
260 |
40 |
converterMap.put(Float.TYPE,c); |
261 |
40 |
converterMap.put(Float.class,c); |
262 |
|
} |
263 |
|
{ |
264 |
40 |
Converter c = new Converter() { |
265 |
|
public Object convert(Class klass, Object value) { |
266 |
|
if(value instanceof Number) { |
267 |
|
return new Double(((Number)value).doubleValue()); |
268 |
|
} else { |
269 |
|
return inner.convert(klass,value); |
270 |
|
} |
271 |
|
} |
272 |
|
private Converter inner = new DoubleConverter(); |
273 |
|
}; |
274 |
40 |
converterMap.put(Double.TYPE,c); |
275 |
40 |
converterMap.put(Double.class,c); |
276 |
|
} |
277 |
|
} |
278 |
|
} |