01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.commons.betwixt.strategy;
18:
19: import org.apache.commons.beanutils.ConvertUtils;
20: import org.apache.commons.betwixt.expression.Context;
21:
22: /**
23: * String <-> object conversion strategy that delegates to ConvertUtils.
24: *
25: * @author Robert Burrell Donkin
26: * @since 0.5
27: */
28: public class ConvertUtilsObjectStringConverter extends
29: ObjectStringConverter {
30:
31: /**
32: * Converts an object to a string representation using ConvertUtils.
33: *
34: * @param object the object to be converted, possibly null
35: * @param type the property class of the object, not null
36: * @param flavour a string allow symantic differences in formatting
37: * to be communicated (ignored)
38: * @param context not null
39: * @return a String representation, not null
40: */
41: public String objectToString(Object object, Class type,
42: String flavour, Context context) {
43: if (object != null) {
44: String text = ConvertUtils.convert(object);
45: if (text != null) {
46: return text;
47: }
48: }
49: return "";
50: }
51:
52: /**
53: * Converts an object to a string representation using ConvertUtils.
54: * This implementation ignores null and empty string values (rather than converting them).
55: *
56: * @param value the String to be converted, not null
57: * @param type the property class to be returned (if possible), not null
58: * @param flavour a string allow symantic differences in formatting
59: * to be communicated (ignored)
60: * @param context not null
61: * @return an Object converted from the String, not null
62: */
63: public Object stringToObject(String value, Class type,
64: String flavour, Context context) {
65: if (value == null || "".equals(value)) {
66: return null;
67: }
68:
69: return ConvertUtils.convert(value, type);
70: }
71: }
|