git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271327 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -0,0 +1,83 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.aut.converter; | |||||
| import java.util.Map; | |||||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||||
| import org.apache.avalon.excalibur.i18n.Resources; | |||||
| /** | |||||
| * Instances of this interface are used to convert between different types. | |||||
| * | |||||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||||
| * @version $Revision$ $Date$ | |||||
| */ | |||||
| public abstract class AbstractConverter | |||||
| implements Converter | |||||
| { | |||||
| private final static Resources REZ = | |||||
| ResourceManager.getPackageResources( AbstractConverter.class ); | |||||
| private final Class m_source; | |||||
| private final Class m_destination; | |||||
| /** | |||||
| * Constructor for a converter between types source and destination | |||||
| * | |||||
| * @param source the source type | |||||
| * @param destination the destination type | |||||
| */ | |||||
| public AbstractConverter( final Class source, final Class destination ) | |||||
| { | |||||
| m_source = source; | |||||
| m_destination = destination; | |||||
| } | |||||
| /** | |||||
| * Convert an object from original to destination types | |||||
| * | |||||
| * @param destination the destination type | |||||
| * @param original the original Object | |||||
| * @param context the context in which to convert | |||||
| * @return the converted object | |||||
| * @exception ConverterException if an error occurs | |||||
| */ | |||||
| public Object convert( final Class destination, | |||||
| final Object original, | |||||
| final Map context ) | |||||
| throws ConverterException | |||||
| { | |||||
| if( m_destination != destination ) | |||||
| { | |||||
| final String message = | |||||
| REZ.getString( "bad-destination.error", destination.getName(), m_destination ); | |||||
| throw new IllegalArgumentException( message ); | |||||
| } | |||||
| if( !m_source.isInstance( original ) ) | |||||
| { | |||||
| final String message = | |||||
| REZ.getString( "bad-instance.error", original, m_source.getName() ); | |||||
| throw new IllegalArgumentException( message ); | |||||
| } | |||||
| return convert( original, context ); | |||||
| } | |||||
| /** | |||||
| * Overide this in a particular converter to do the conversion. | |||||
| * | |||||
| * @param original the original Object | |||||
| * @param context the context in which to convert | |||||
| * @return the converted object | |||||
| * @exception ConverterException if an error occurs | |||||
| */ | |||||
| protected abstract Object convert( Object original, Map context ) | |||||
| throws ConverterException; | |||||
| } | |||||
| @@ -0,0 +1,37 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.aut.converter; | |||||
| import org.apache.avalon.framework.context.Context; | |||||
| import java.util.Map; | |||||
| /** | |||||
| * Instances of this interface are used to convert between different types. | |||||
| * | |||||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||||
| * @version $Revision$ $Date$ | |||||
| * @ant:role shorthand="converter" | |||||
| */ | |||||
| public interface Converter | |||||
| { | |||||
| String ROLE = Converter.class.getName(); | |||||
| /** | |||||
| * Convert original to destination type. | |||||
| * Destination is passed so that one converter can potentiall | |||||
| * convert to multiple different types. | |||||
| * | |||||
| * @param destination the destinaiton type | |||||
| * @param original the original type | |||||
| * @param context the context in which to convert | |||||
| * @return the converted object | |||||
| * @exception ConverterException if an error occurs | |||||
| */ | |||||
| Object convert( Class destination, Object original, Map context ) | |||||
| throws ConverterException; | |||||
| } | |||||
| @@ -0,0 +1,42 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.aut.converter; | |||||
| import org.apache.avalon.framework.CascadingException; | |||||
| /** | |||||
| * ConverterException thrown when a problem occurs during convertion etc. | |||||
| * | |||||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||||
| * @version $Revision$ $Date$ | |||||
| */ | |||||
| public class ConverterException | |||||
| extends CascadingException | |||||
| { | |||||
| /** | |||||
| * Basic constructor with a message | |||||
| * | |||||
| * @param message the message | |||||
| */ | |||||
| public ConverterException( final String message ) | |||||
| { | |||||
| this( message, null ); | |||||
| } | |||||
| /** | |||||
| * Constructor that builds cascade so that other exception information can be retained. | |||||
| * | |||||
| * @param message the message | |||||
| * @param throwable the throwable | |||||
| */ | |||||
| public ConverterException( final String message, final Throwable throwable ) | |||||
| { | |||||
| super( message, throwable ); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,2 @@ | |||||
| bad-destination.error=Destination type ({0}) is not equal to {1}. | |||||
| bad-instance.error=Object {0} is not an instance of {1}. | |||||