01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.converter;
16:
17: import org.strecks.exceptions.ConversionException;
18: import org.strecks.util.Assert;
19: import org.strecks.util.StringUtils;
20:
21: /**
22: * Operates in a very similar way to <code>BeanUtilsConverter</code> except that in <code>convertToObject()</code>,
23: * an empty String is treated as null
24: * @author Phil Zoio
25: */
26: public class SafeBeanUtilsConverter implements
27: Converter<String, Object> {
28:
29: private Class clazz;
30:
31: public void setTargetClass(Class clazz) {
32: this .clazz = clazz;
33: }
34:
35: public Object toTargetType(String toConvert)
36: throws ConversionException {
37: Assert.notNull(clazz);
38:
39: if (!StringUtils.notBlankOrNull(toConvert))
40: return null;
41:
42: Object convert = null;
43: try {
44: convert = BeanUtilsConverter.getInstance().convert(
45: toConvert, clazz);
46: } catch (Exception e) {
47: throw new ConversionException(e);
48: }
49: return convert;
50: }
51:
52: public String toSourceType(Object toConvert)
53: throws ConversionException {
54: Assert.notNull(clazz);
55:
56: if (toConvert == null)
57: return null;
58:
59: String convert = null;
60: try {
61: convert = BeanUtilsConverter.getInstance().convert(
62: toConvert);
63: } catch (Exception e) {
64: throw new ConversionException(e);
65: }
66: return convert;
67: }
68:
69: }
|