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 */ 017package org.apache.commons.beanutils2.converters; 018 019import java.lang.reflect.Array; 020import java.util.Collection; 021import java.util.Locale; 022import java.util.Objects; 023 024import org.apache.commons.beanutils2.ConversionException; 025import org.apache.commons.beanutils2.ConvertUtils; 026import org.apache.commons.beanutils2.Converter; 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029 030/** 031 * Base {@link Converter} implementation that provides the structure for handling conversion <strong>to</strong> and <strong>from</strong> a specified type. 032 * <p> 033 * This implementation provides the basic structure for converting to/from a specified type optionally using a default value or throwing a 034 * {@link ConversionException} if a conversion error occurs. 035 * </p> 036 * <p> 037 * Implementations should provide conversion to the specified type and from the specified type to a {@code String} value by implementing the following methods: 038 * </p> 039 * <ul> 040 * <li>{@code convertToString(value)} - convert to a String (default implementation uses the objects {@code toString()} method).</li> 041 * <li>{@code convertToType(Class, value)} - convert to the specified type</li> 042 * </ul> 043 * <p> 044 * The default value has to be compliant to the default type of this converter - which is enforced by the generic type parameter. If a conversion is not 045 * possible and a default value is set, the converter tries to transform the default value to the requested target type. If this fails, a 046 * {@code ConversionException} if thrown. 047 * </p> 048 * 049 * @param <D> The default value type. 050 * @since 1.8.0 051 */ 052public abstract class AbstractConverter<D> implements Converter<D> { 053 054 /** Debug logging message to indicate default value configuration */ 055 private static final String DEFAULT_CONFIG_MSG = "(N.B. Converters can be configured to use default values to avoid throwing exceptions)"; 056 057 /** Current package name */ 058 // getPackage() below returns null on some platforms/jvm versions during the unit tests. 059 // private static final String PACKAGE = AbstractConverter.class.getPackage().getName() + "."; 060 private static final String PACKAGE = "org.apache.commons.beanutils2.converters."; 061 062 /** 063 * Converts the given object to a lower-case string. 064 * 065 * @param value the input string. 066 * @return the given string trimmed and converter to lower-case. 067 */ 068 protected static String toLowerCase(final Object value) { 069 return toString(value).toLowerCase(Locale.ROOT); 070 } 071 072 /** 073 * Converts the given object to a lower-case string. 074 * 075 * @param value the input string. 076 * @return the given string trimmed and converter to lower-case. 077 */ 078 protected static String toString(final Object value) { 079 return Objects.requireNonNull(value, "value").toString(); 080 } 081 082 /** 083 * Converts the given object to a lower-case string. 084 * 085 * @param value the input string. 086 * @return the given string trimmed and converter to lower-case. 087 */ 088 protected static String toTrim(final Object value) { 089 return toString(value).trim(); 090 } 091 092 /** 093 * Logging for this instance. 094 */ 095 private transient Log log; 096 097 /** 098 * Should we return the default value on conversion errors? 099 */ 100 private boolean useDefault; 101 102 /** 103 * The default value specified to our Constructor, if any. 104 */ 105 private D defaultValue; 106 107 /** 108 * Constructs a <em>Converter</em> that throws a {@code ConversionException} if an error occurs. 109 */ 110 public AbstractConverter() { 111 } 112 113 /** 114 * Constructs a <em>Converter</em> that returns a default value if an error occurs. 115 * 116 * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value. 117 */ 118 public AbstractConverter(final D defaultValue) { 119 setDefaultValue(defaultValue); 120 } 121 122 /** 123 * Creates a standard conversion exception with a message indicating that the passed in value cannot be converted to the desired target type. 124 * 125 * @param type the target type 126 * @param value the value to be converted 127 * @return a {@code ConversionException} with a standard message 128 * @since 1.9 129 */ 130 protected ConversionException conversionException(final Class<?> type, final Object value) { 131 return ConversionException.format("Can't convert value '%s' to type %s", value, type); 132 } 133 134 /** 135 * Converts the input object into an output object of the specified type. 136 * 137 * @param type Data type to which this value should be converted 138 * @param value The input value to be converted 139 * @return The converted value. 140 * @throws ConversionException if conversion cannot be performed successfully and no default is specified. 141 */ 142 @Override 143 public <R> R convert(final Class<R> type, Object value) { 144 if (type == null) { 145 return convertToDefaultType(value); 146 } 147 148 Class<?> sourceType = value == null ? null : value.getClass(); 149 final Class<R> targetType = ConvertUtils.primitiveToWrapper(type); 150 151 if (log().isDebugEnabled()) { 152 log().debug( 153 "Converting" + (value == null ? "" : " '" + toString(sourceType) + "'") + " value '" + value + "' to type '" + toString(targetType) + "'"); 154 } 155 156 value = convertArray(value); 157 158 // Missing Value 159 if (value == null) { 160 return handleMissing(targetType); 161 } 162 163 sourceType = value.getClass(); 164 165 try { 166 // Convert --> String 167 if (targetType.equals(String.class)) { 168 return targetType.cast(convertToString(value)); 169 170 // No conversion necessary 171 } 172 if (targetType.equals(sourceType)) { 173 if (log().isDebugEnabled()) { 174 log().debug(" No conversion required, value is already a " + toString(targetType)); 175 } 176 return targetType.cast(value); 177 178 // Convert --> Type 179 } 180 final Object result = convertToType(targetType, value); 181 if (log().isDebugEnabled()) { 182 log().debug(" Converted to " + toString(targetType) + " value '" + result + "'"); 183 } 184 return targetType.cast(result); 185 } catch (final Throwable t) { 186 return handleError(targetType, value, t); 187 } 188 } 189 190 /** 191 * Returns the first element from an Array (or Collection) or the value unchanged if not an Array (or Collection). 192 * 193 * N.B. This needs to be overridden for array/Collection converters. 194 * 195 * @param value The value to convert 196 * @return The first element in an Array (or Collection) or the value unchanged if not an Array (or Collection) 197 */ 198 protected Object convertArray(final Object value) { 199 if (value == null) { 200 return null; 201 } 202 if (value.getClass().isArray()) { 203 if (Array.getLength(value) > 0) { 204 return Array.get(value, 0); 205 } 206 return null; 207 } 208 if (value instanceof Collection) { 209 final Collection<?> collection = (Collection<?>) value; 210 if (!collection.isEmpty()) { 211 return collection.iterator().next(); 212 } 213 return null; 214 } 215 return value; 216 } 217 218 /** 219 * Converts to the default type. This method is called if we do not have a target class. In this case, the T parameter is not set. Therefore, we can cast to 220 * it (which is required to fulfill the contract of the method signature). 221 * 222 * @param value the value to be converted 223 * @param <T> the type of the result object 224 * @return the converted value 225 */ 226 @SuppressWarnings("unchecked") 227 private <T> T convertToDefaultType(final Object value) { 228 return (T) convert(getDefaultType(), value); 229 } 230 231 /** 232 * Converts the input object into a String. 233 * <p> 234 * <strong>N.B.</strong>This implementation simply uses the value's {@code toString()} method and should be overridden if a more sophisticated mechanism for 235 * <em>conversion to a String</em> is required. 236 * </p> 237 * 238 * @param value The input value to be converted. 239 * @return the converted String value. 240 * @throws IllegalArgumentException if an error occurs converting to a String 241 */ 242 protected String convertToString(final Object value) { 243 return value.toString(); 244 } 245 246 /** 247 * Converts the input object into an output object of the specified type. 248 * <p> 249 * Typical implementations will provide a minimum of {@code String --> type} conversion. 250 * </p> 251 * 252 * @param <R> Target type of the conversion. 253 * @param type Data type to which this value should be converted. 254 * @param value The input value to be converted. 255 * @return The converted value. 256 * @throws Throwable if an error occurs converting to the specified type 257 */ 258 protected abstract <R> R convertToType(Class<R> type, Object value) throws Throwable; 259 260 /** 261 * Gets the default value for conversions to the specified type. 262 * 263 * @param type Data type to which this value should be converted. 264 * @return The default value for the specified type. 265 */ 266 protected Object getDefault(final Class<?> type) { 267 if (type.equals(String.class)) { 268 return null; 269 } 270 return defaultValue; 271 } 272 273 /** 274 * Gets the default type this {@code Converter} handles. 275 * 276 * @return The default type this {@code Converter} handles. 277 */ 278 protected abstract Class<D> getDefaultType(); 279 280 /** 281 * Handles Conversion Errors. 282 * <p> 283 * If a default value has been specified then it is returned otherwise a ConversionException is thrown. 284 * </p> 285 * 286 * @param <T> Target type of the conversion. 287 * @param type Data type to which this value should be converted. 288 * @param value The input value to be converted 289 * @param cause The exception thrown by the {@code convert} method 290 * @return The default value. 291 * @throws ConversionException if no default value has been specified for this {@link Converter}. 292 */ 293 protected <T> T handleError(final Class<T> type, final Object value, final Throwable cause) { 294 if (log().isDebugEnabled()) { 295 if (cause instanceof ConversionException) { 296 log().debug(" Conversion threw ConversionException: " + cause.getMessage()); 297 } else { 298 log().debug(" Conversion threw " + cause); 299 } 300 } 301 if (useDefault) { 302 return handleMissing(type); 303 } 304 ConversionException cex = null; 305 if (cause instanceof ConversionException) { 306 cex = (ConversionException) cause; 307 if (log().isDebugEnabled()) { 308 log().debug(" Re-throwing ConversionException: " + cex.getMessage()); 309 log().debug(" " + DEFAULT_CONFIG_MSG); 310 } 311 } else { 312 final String msg = "Error converting from '" + toString(value.getClass()) + "' to '" + toString(type) + "' " + cause.getMessage(); 313 cex = new ConversionException(msg, cause); 314 if (log().isDebugEnabled()) { 315 log().debug(" Throwing ConversionException: " + msg); 316 log().debug(" " + DEFAULT_CONFIG_MSG); 317 } 318 } 319 throw cex; 320 } 321 322 /** 323 * Handles missing values. 324 * <p> 325 * If a default value has been specified, then it is returned (after a cast to the desired target class); otherwise a ConversionException is thrown. 326 * </p> 327 * 328 * @param <T> the desired target type 329 * @param type Data type to which this value should be converted. 330 * @return The default value. 331 * @throws ConversionException if no default value has been specified for this {@link Converter}. 332 */ 333 protected <T> T handleMissing(final Class<T> type) { 334 if (useDefault || type.equals(String.class)) { 335 Object value = getDefault(type); 336 if (useDefault && value != null && !type.equals(value.getClass())) { 337 try { 338 value = convertToType(type, defaultValue); 339 } catch (final Throwable t) { 340 throw new ConversionException("Default conversion to " + toString(type) + " failed.", t); 341 } 342 } 343 if (log().isDebugEnabled()) { 344 log().debug(" Using default " + (value == null ? "" : toString(value.getClass()) + " ") + "value '" + defaultValue + "'"); 345 } 346 // value is now either null or of the desired target type 347 return type.cast(value); 348 } 349 350 final ConversionException cex = ConversionException.format("No value specified for '%s'", toString(type)); 351 if (log().isDebugEnabled()) { 352 log().debug(" Throwing ConversionException: " + cex.getMessage()); 353 log().debug(" " + DEFAULT_CONFIG_MSG); 354 } 355 throw cex; 356 } 357 358 /** 359 * Tests whether a default value will be returned or exception thrown in the event of a conversion error. 360 * 361 * @return {@code true} if a default value will be returned for conversion errors or {@code false} if a {@link ConversionException} will be thrown. 362 */ 363 public boolean isUseDefault() { 364 return useDefault; 365 } 366 367 /** 368 * Gets the Log instance. 369 * <p> 370 * The Log instance variable is transient and accessing it through this method ensures it is re-initialized when this instance is de-serialized. 371 * </p> 372 * 373 * @return The Log instance. 374 */ 375 Log log() { 376 if (log == null) { 377 log = LogFactory.getLog(getClass()); 378 } 379 return log; 380 } 381 382 /** 383 * Sets the default value, converting as required. 384 * <p> 385 * If the default value is different from the type the {@code Converter} handles, it will be converted to the handled type. 386 * </p> 387 * 388 * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value. 389 * @throws ConversionException if an error occurs converting the default value 390 */ 391 protected void setDefaultValue(final D defaultValue) { 392 useDefault = false; 393 if (log().isDebugEnabled()) { 394 log().debug("Setting default value: " + defaultValue); 395 } 396 if (defaultValue == null) { 397 this.defaultValue = null; 398 } else { 399 this.defaultValue = convert(getDefaultType(), defaultValue); 400 } 401 useDefault = true; 402 } 403 404 /** 405 * Converts this instance to a String. 406 * 407 * @return A String representation of this converter 408 */ 409 @Override 410 public String toString() { 411 return toString(getClass()) + "[UseDefault=" + useDefault + "]"; 412 } 413 414 /** 415 * Converts a {@link Class} to a String. 416 * 417 * @param type The {@link Class}. 418 * @return The String representation. 419 */ 420 String toString(final Class<?> type) { 421 String typeName = null; 422 if (type == null) { 423 typeName = "null"; 424 } else if (type.isArray()) { 425 Class<?> elementType = type.getComponentType(); 426 int count = 1; 427 while (elementType.isArray()) { 428 elementType = elementType.getComponentType(); 429 count++; 430 } 431 final StringBuilder typeNameBuilder = new StringBuilder(elementType.getName()); 432 for (int i = 0; i < count; i++) { 433 typeNameBuilder.append("[]"); 434 } 435 typeName = typeNameBuilder.toString(); 436 } else { 437 typeName = type.getName(); 438 } 439 if (typeName.startsWith("java.lang.") || typeName.startsWith("java.util.") || typeName.startsWith("java.math.")) { 440 typeName = typeName.substring("java.lang.".length()); 441 } else if (typeName.startsWith(PACKAGE)) { 442 typeName = typeName.substring(PACKAGE.length()); 443 } 444 return typeName; 445 } 446 447}