From 8393e9258899fb34ccc1401076e3ca99d183c1a0 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Sat, 29 Dec 2001 21:18:55 +0000 Subject: [PATCH] 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 --- .../antlib/core/StringToEnumConverter.java | 102 ++++++++++++++++++ .../src/manifest/core-ant-descriptor.xml | 3 + 2 files changed, 105 insertions(+) create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/core/StringToEnumConverter.java diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/StringToEnumConverter.java b/proposal/myrmidon/src/java/org/apache/antlib/core/StringToEnumConverter.java new file mode 100644 index 000000000..59c07ec61 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/StringToEnumConverter.java @@ -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 Peter Donald + */ +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 ); + } + } +} + diff --git a/proposal/myrmidon/src/manifest/core-ant-descriptor.xml b/proposal/myrmidon/src/manifest/core-ant-descriptor.xml index 56da9eaa6..e30fd6595 100644 --- a/proposal/myrmidon/src/manifest/core-ant-descriptor.xml +++ b/proposal/myrmidon/src/manifest/core-ant-descriptor.xml @@ -11,6 +11,9 @@ +