01: /**
02: * MVEL (The MVFLEX Expression Language)
03: *
04: * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: *
18: */package org.mvel.conversion;
19:
20: import org.mvel.ConversionException;
21: import org.mvel.ConversionHandler;
22:
23: import static java.lang.Integer.parseInt;
24: import static java.lang.String.valueOf;
25: import java.util.HashMap;
26: import java.util.Map;
27:
28: public class ArrayCH implements ConversionHandler {
29: private static final Map<Class, Converter> CNV = new HashMap<Class, Converter>();
30:
31: public Object convertFrom(Object in) {
32: if (!CNV.containsKey(in.getClass()))
33: throw new ConversionException("cannot convert type: "
34: + in.getClass().getName() + " to: "
35: + Boolean.class.getName());
36: return CNV.get(in.getClass()).convert(in);
37: }
38:
39: public boolean canConvertFrom(Class cls) {
40: return CNV.containsKey(cls);
41: }
42:
43: static {
44: CNV.put(String[].class, new Converter() {
45: public Object convert(Object o) {
46: Object[] old = (Object[]) o;
47: String[] n = new String[old.length];
48: for (int i = 0; i < old.length; i++) {
49: n[i] = valueOf(old[i]);
50: }
51:
52: return n;
53: }
54: });
55:
56: CNV.put(Integer[].class, new Converter() {
57: public Object convert(Object o) {
58: Object[] old = (Object[]) o;
59: Integer[] n = new Integer[old.length];
60: for (int i = 0; i < old.length; i++) {
61: n[i] = parseInt(valueOf(old[i]));
62: }
63:
64: return n;
65: }
66:
67: });
68:
69: CNV.put(int[].class, new Converter() {
70: public Object convert(Object o) {
71: Object[] old = (Object[]) o;
72: int[] n = new int[old.length];
73: for (int i = 0; i < old.length; i++) {
74: n[i] = parseInt(valueOf(old[i]));
75: }
76:
77: return n;
78: }
79:
80: });
81:
82: }
83: }
|