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 java.util.HashMap;
24: import java.util.Map;
25:
26: public class IntArrayCH implements ConversionHandler {
27: private static final Map<Class, Converter> CNV = new HashMap<Class, Converter>();
28:
29: public Object convertFrom(Object in) {
30:
31: if (!CNV.containsKey(in.getClass()))
32: throw new ConversionException("cannot convert type: "
33: + in.getClass().getName() + " to: "
34: + Boolean.class.getName());
35: return CNV.get(in.getClass()).convert(in);
36: }
37:
38: public boolean canConvertFrom(Class cls) {
39: return CNV.containsKey(cls);
40: }
41:
42: static {
43: CNV.put(String[].class, new Converter() {
44: public Object convert(Object o) {
45: String[] old = (String[]) o;
46: Integer[] n = new Integer[old.length];
47: for (int i = 0; i < old.length; i++) {
48: n[i] = Integer.parseInt(old[i]);
49: }
50:
51: return n;
52: }
53: });
54:
55: CNV.put(Object[].class, new Converter() {
56: public Object convert(Object o) {
57: Object[] old = (Object[]) o;
58: Integer[] n = new Integer[old.length];
59: for (int i = 0; i < old.length; i++) {
60: n[i] = Integer.parseInt(String.valueOf(old[i]));
61: }
62:
63: return n;
64: }
65: });
66:
67: }
68: }
|