📄 abstractconverter.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils.converters;
import java.lang.reflect.Array;
import java.util.Collection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
/**
* Base {@link Converter} implementation that provides the structure
* for handling conversion <b>to</b> and <b>from</b> a specified type.
* <p>
* This implementation provides the basic structure for
* converting to/from a specified type optionally using a default
* value or throwing a {@link ConversionException} if a
* conversion error occurs.
* <p>
* Implementations should provide conversion to the specified
* type and from the specified type to a <code>String</code> value
* by implementing the following methods:
* <ul>
* <li><code>convertToString(value)</code> - convert to a String
* (default implementation uses the objects <code>toString()</code>
* method).</li>
* <li><code>convertToType(Class, value)</code> - convert
* to the specified type</li>
* </ul>
*
* @version $Revision: 555824 $ $Date: 2007-07-13 01:27:15 +0100 (Fri, 13 Jul 2007) $
* @since 1.8.0
*/
public abstract class AbstractConverter implements Converter {
/** Debug logging message to indicate default value configuration */
private static final String DEFAULT_CONFIG_MSG =
"(N.B. Converters can be configured to use default values to avoid throwing exceptions)";
/** Current package name */
// getPackage() below returns null on some platforms/jvm versions during the unit tests.
// private static final String PACKAGE = AbstractConverter.class.getPackage().getName() + ".";
private static final String PACKAGE = "org.apache.commons.beanutils.converters.";
/**
* Logging for this instance.
*/
private transient Log log;
/**
* The default type this <code>Converter</code> handles.
*/
private Class defaultType = null;
/**
* Should we return the default value on conversion errors?
*/
private boolean useDefault = false;
/**
* The default value specified to our Constructor, if any.
*/
private Object defaultValue = null;
// ----------------------------------------------------------- Constructors
/**
* Construct a <i>Converter</i> that throws a
* <code>ConversionException</code> if an error occurs.
*
* @param defaultType The default type this <code>Converter</code>
* handles
*/
public AbstractConverter(Class defaultType) {
this.defaultType = defaultType;
if (defaultType == null) {
throw new IllegalArgumentException("Default type is missing.");
}
}
/**
* Construct a <i>Converter</i> that returns a default
* value if an error occurs.
*
* @param defaultType The default type this <code>Converter</code>
* handles
* @param defaultValue The default value to be returned
* if the value to be converted is missing or an error
* occurs converting the value.
*/
public AbstractConverter(Class defaultType, Object defaultValue) {
this(defaultType);
setDefaultValue(defaultValue);
}
// --------------------------------------------------------- Public Methods
/**
* Indicates whether a default value will be returned or exception
* thrown in the event of a conversion error.
*
* @return <code>true</code> if a default value will be returned for
* conversion errors or <code>false</code> if a {@link ConversionException}
* will be thrown.
*/
public boolean isUseDefault() {
return useDefault;
}
/**
* Convert the input object into an output object of the
* specified type.
*
* @param type Data type to which this value should be converted
* @param value The input value to be converted
* @return The converted value.
* @throws ConversionException if conversion cannot be performed
* successfully and no default is specified.
*/
public Object convert(Class type, Object value) {
Class sourceType = value == null ? null : value.getClass();
Class targetType = primitive(type == null ? getDefaultType() : type);
if (log().isDebugEnabled()) {
log().debug("Converting"
+ (value == null ? "" : " '" + toString(sourceType) + "'")
+ " value '" + value + "' to type '" + toString(targetType) + "'");
}
value = convertArray(value);
// Missing Value
if (value == null) {
return handleMissing(targetType);
}
sourceType = value.getClass();
try {
// Convert --> String
if (targetType.equals(String.class)) {
return convertToString(value);
// No conversion necessary
} else if (targetType.equals(sourceType)) {
if (log().isDebugEnabled()) {
log().debug(" No conversion required, value is already a "
+ toString(targetType));
}
return value;
// Convert --> Type
} else {
Object result = convertToType(targetType, value);
if (log().isDebugEnabled()) {
log().debug(" Converted to " + toString(targetType) +
" value '" + result + "'");
}
return result;
}
} catch (Throwable t) {
return handleError(targetType, value, t);
}
}
/**
* Convert the input object into a String.
* <p>
* <b>N.B.</b>This implementation simply uses the value's
* <code>toString()</code> method and should be overriden if a
* more sophisticated mechanism for <i>conversion to a String</i>
* is required.
*
* @param value The input value to be converted.
* @return the converted String value.
* @throws Throwable if an error occurs converting to a String
*/
protected String convertToString(Object value) throws Throwable {
return value.toString();
}
/**
* Convert the input object into an output object of the
* specified type.
* <p>
* Typical implementations will provide a minimum of
* <code>String --> type</code> conversion.
*
* @param type Data type to which this value should be converted.
* @param value The input value to be converted.
* @return The converted value.
* @throws Throwable if an error occurs converting to the specified type
*/
protected abstract Object convertToType(Class type, Object value) throws Throwable;
/**
* Return the first element from an Array (or Collection)
* or the value unchanged if not an Array (or Collection).
*
* N.B. This needs to be overriden for array/Collection converters.
*
* @param value The value to convert
* @return The first element in an Array (or Collection)
* or the value unchanged if not an Array (or Collection)
*/
protected Object convertArray(Object value) {
if (value == null) {
return null;
}
if (value.getClass().isArray()) {
if (Array.getLength(value) > 0) {
return Array.get(value, 0);
} else {
return null;
}
}
if (value instanceof Collection) {
Collection collection = (Collection)value;
if (collection.size() > 0) {
return collection.iterator().next();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -