Browse Source

Add a converter that converts between strings and org.apache.avalon.framework.Enums. It is expected that the Enum class have two public static methods with the following signatures

BlahEnum getByName(String name)
(may return null or throw an IllegalArgumentException if bad name)

String[] getNames()

This is a replacement for EnumeratedAttribute that is more friendly to people who want to use enums programatically.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270398 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Donald 23 years ago
parent
commit
8393e92588
2 changed files with 105 additions and 0 deletions
  1. +102
    -0
      proposal/myrmidon/src/java/org/apache/antlib/core/StringToEnumConverter.java
  2. +3
    -0
      proposal/myrmidon/src/manifest/core-ant-descriptor.xml

+ 102
- 0
proposal/myrmidon/src/java/org/apache/antlib/core/StringToEnumConverter.java View File

@@ -0,0 +1,102 @@
/*
* 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.antlib.core;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.avalon.framework.context.Context;
import org.apache.myrmidon.converter.Converter;
import org.apache.myrmidon.converter.ConverterException;

/**
* String to Enum converter
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
*/
public class StringToEnumConverter
implements Converter
{
private static final Resources REZ =
ResourceManager.getPackageResources( StringToEnumConverter.class );

public Object convert( final Class destination,
final Object original,
final Context context )
throws ConverterException
{
final Object object = getEnum( destination, original );

if( null == object )
{
final String[] names = getValidNames( destination );
final String message =
REZ.getString( "invalid.enum.error", object, Arrays.asList( names ) );
throw new ConverterException( message );
}
else
{
return object;
}
}

private Object getEnum( final Class destination, final Object original )
throws ConverterException
{
try
{
final Class[] types = new Class[]{String.class};
final Object[] args = new Object[]{original.toString()};

final Method method = destination.getMethod( "getByName", types );
return method.invoke( null, args );
}
catch( final InvocationTargetException ite )
{
final Throwable target = ite.getTargetException();
if( target instanceof IllegalArgumentException )
{
return null;
}
else
{
final String message =
REZ.getString( "getByName.error", destination.getName(), target );
throw new ConverterException( message, target );
}
}
catch( final Exception e )
{
final String message =
REZ.getString( "enum.missing.getByName.error", destination.getName() );
throw new ConverterException( message, e );
}
}

private String[] getValidNames( final Class clazz )
throws ConverterException
{
try
{
final Class[] types = new Class[ 0 ];
final Object[] args = new Object[ 0 ];

final Method method = clazz.getMethod( "getNames", types );
return (String[])method.invoke( null, args );
}
catch( final Exception e )
{
final String message =
REZ.getString( "enum.missing.getNames.error", clazz.getName() );
throw new ConverterException( message, e );
}
}
}


+ 3
- 0
proposal/myrmidon/src/manifest/core-ant-descriptor.xml View File

@@ -11,6 +11,9 @@

<converters>

<converter classname="org.apache.antlib.core.StringToEnumConverter"
source="java.lang.String"
destination="org.apache.avalon.framework.Enum" />
<converter classname="org.apache.antlib.core.StringToFileConverter"
source="java.lang.String"
destination="java.io.File" />


Loading…
Cancel
Save