ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/converter Resources.properties ConverterException.java Converter.java AbstractConverter.java
Date Thu, 14 Feb 2002 10:03:51 GMT
donaldp     02/02/14 02:03:51

  Added:       proposal/myrmidon/src/java/org/apache/aut/converter
                        Resources.properties ConverterException.java
                        Converter.java AbstractConverter.java
  Log:
  Start to play with a converter architecture decoupled from ant completely
  
  Revision  Changes    Path
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/converter/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  bad-destination.error=Destination type ({0}) is not equal to {1}.
  bad-instance.error=Object {0} is not an instance of {1}.
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/converter/ConverterException.java
  
  Index: ConverterException.java
  ===================================================================
  /*
   * 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: 1.1 $ $Date: 2002/02/14 10:03:51 $
   */
  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 );
      }
  }
  
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/converter/Converter.java
  
  Index: Converter.java
  ===================================================================
  /*
   * 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: 1.1 $ $Date: 2002/02/14 10:03:51 $
   * @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;
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/converter/AbstractConverter.java
  
  Index: AbstractConverter.java
  ===================================================================
  /*
   * 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: 1.1 $ $Date: 2002/02/14 10:03:51 $
   */
  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;
  }
  
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message