001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.betwixt.strategy; 018 019 import java.lang.reflect.Array; 020 021 import org.apache.commons.beanutils.ConvertUtils; 022 import org.apache.commons.beanutils.Converter; 023 import org.apache.commons.betwixt.expression.Context; 024 025 /** 026 * String <-> object conversion strategy that delegates to ConvertUtils. 027 * 028 * @author Robert Burrell Donkin 029 * @since 0.5 030 */ 031 public class ConvertUtilsObjectStringConverter extends ObjectStringConverter { 032 033 /** 034 * Converts an object to a string representation using ConvertUtils. 035 * 036 * @param object the object to be converted, possibly null 037 * @param type the property class of the object, not null 038 * @param flavour a string allow symantic differences in formatting 039 * to be communicated (ignored) 040 * @param context not null 041 * @return a String representation, not null 042 */ 043 public String objectToString(Object object, Class type, String flavour, Context context) { 044 if ( object != null ) { 045 String text = ConvertUtils.convert( object ); 046 if ( text != null ) { 047 return text; 048 } 049 } 050 return ""; 051 } 052 053 /** 054 * Converts an object to a string representation using ConvertUtils. 055 * This implementation ignores null and empty string values (rather than converting them). 056 * 057 * @param value the String to be converted, not null 058 * @param type the property class to be returned (if possible), not null 059 * @param flavour a string allow symantic differences in formatting 060 * to be communicated (ignored) 061 * @param context not null 062 * @return an Object converted from the String, not null 063 */ 064 public Object stringToObject(String value, Class type, String flavour, Context context) { 065 if (value == null || "".equals(value)) 066 { 067 return ""; 068 } 069 070 return ConvertUtils.convert( value, type ); 071 } 072 }