donaldp 2002/06/08 22:12:38
Modified: converter/src/java/org/apache/excalibur/converter
AbstractMasterConverter.java
converter/src/java/org/apache/excalibur/converter/lib
SimpleMasterConverter.java
Added: converter/src/java/org/apache/excalibur/converter
ConverterFactory.java
converter/src/java/org/apache/excalibur/converter/lib
SimpleConverterFactory.java
Log:
Update to use the latest Converter changes from myrmidon.
Submitted By: Adam Murdoch
Revision Changes Path
1.2 +57 -66
jakarta-avalon-excalibur/converter/src/java/org/apache/excalibur/converter/AbstractMasterConverter.java
Index: AbstractMasterConverter.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/converter/src/java/org/apache/excalibur/converter/AbstractMasterConverter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AbstractMasterConverter.java 27 Apr 2002 05:23:00 -0000 1.1
+++ AbstractMasterConverter.java 9 Jun 2002 05:12:38 -0000 1.2
@@ -14,23 +14,15 @@
import org.apache.avalon.excalibur.i18n.Resources;
/**
- * This is an abstract implementation of a <code>MasterConverter</code>.
- * A MasterConverter is capable of converting between many different
- * source and destination types. The <code>MasterConverter</code>
- * delegates to other converters that do the actual work.
+ * This is a Converter implementation that is capable of converting between
+ * many different source and destination types, by delegating delegates to
+ * other converters that do the actual work.
*
- * <p>To use this class you must subclass it, overide the
- * (@link #createConverter(String)) method and register some
- * converters using the (@link #registerConverter(String,String,String))
- * method.</p>
- *
- * <p>The reason this class deals with strings rather than Class objects
- * is because dealing with strings allows us to implement alternative
- * mechanisms for defining Converters in the future, only defining converter
- * when it is first used.</p>
+ * <p>To use this class you must subclass it, and register some converters
+ * using the (@link #registerConverter} method.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
- * @version $Revision: 1.1 $ $Date: 2002/04/27 05:23:00 $
+ * @version $Revision: 1.2 $ $Date: 2002/06/09 05:12:38 $
*/
public abstract class AbstractMasterConverter
implements Converter
@@ -39,13 +31,13 @@
ResourceManager.getPackageResources( AbstractMasterConverter.class );
/**
- * Map from converter classname to instance of converter.
+ * Cache of converter instances. This is a map from ConverterFactory to
+ * a Converter instance created by that factory.
*/
private final Map m_converters = new HashMap();
/**
- * This holds the mapping between source/destination
- * and converter name.
+ * This holds the mapping between source/destination and
ConverterFactory.
*/
private final HashMap m_mapping = new HashMap();
@@ -56,7 +48,7 @@
* @param original the original object
* @param context the context in which to convert
* @return the converted object
- * @exception ConverterException if an error occurs
+ * @throws ConverterException if an error occurs
*/
public Object convert( final Class destination,
final Object original,
@@ -72,16 +64,8 @@
try
{
- // Search inheritance hierarchy for converter
- final String name = findConverter( originalClass, destination );
-
- // Create the converter
- Converter converter = (Converter)m_converters.get( name );
- if( converter == null )
- {
- converter = createConverter( name );
- m_converters.put( name, converter );
- }
+ // Determine which converter to use
+ final Converter converter = findConverter( originalClass,
destination );
// Convert
final Object object = converter.convert( destination, original,
context );
@@ -106,13 +90,34 @@
}
/**
+ * Returns the Converter instance to use to convert between a particular
+ * pair of classes.
+ */
+ private Converter findConverter( final Class originalClass,
+ final Class destination )
+ throws Exception
+ {
+ // Locate the factory to use
+ final ConverterFactory factory = findConverterFactory(
originalClass, destination );
+
+ // Create the converter
+ Converter converter = (Converter)m_converters.get( factory );
+ if( converter == null )
+ {
+ converter = factory.createConverter();
+ m_converters.put( factory, converter );
+ }
+ return converter;
+ }
+
+ /**
* Register a converter
*
- * @param classname the className of converter
+ * @param factory the factory to use to create converter instances.
* @param source the source classname
* @param destination the destination classname
*/
- protected void registerConverter( final String classname,
+ protected void registerConverter( final ConverterFactory factory,
final String source,
final String destination )
{
@@ -123,28 +128,18 @@
m_mapping.put( source, map );
}
- map.put( destination, classname );
+ map.put( destination, factory );
//Remove instance of converter if it has already been created
- m_converters.remove( classname );
+ m_converters.remove( factory );
}
/**
- * Create an instance of converter with specified name.
- *
- * @param name the name of converter
- * @return the created converter instance
- * @throws Exception if converter can not be created.
+ * Determine the type of converter (represented as a ConverterFactory) to
+ * use to convert between original and destination classes.
*/
- protected abstract Converter createConverter( final String name )
- throws Exception;
-
- /**
- * Determine the name of the converter to use to convert between
- * original and destination classes.
- */
- private String findConverter( final Class originalClass,
- final Class destination )
+ private ConverterFactory findConverterFactory( final Class originalClass,
+ final Class destination )
throws ConverterException
{
//TODO: Maybe we should search the destination classes hierarchy as
well
@@ -153,14 +148,14 @@
// looking for a converter from source type -> destination type.
// If more than one is found, choose the most specialised.
- Class match = null;
- String converterName = null;
+ Class bestSrcMatch = null;
+ ConverterFactory matchFactory = null;
ArrayList queue = new ArrayList();
queue.add( originalClass );
while( !queue.isEmpty() )
{
- Class clazz = (Class)queue.remove( 0 );
+ final Class clazz = (Class)queue.remove( 0 );
// Add superclass and all interfaces
if( clazz.getSuperclass() != null )
@@ -174,20 +169,20 @@
}
// Check if we can convert from current class to destination
- final String name = getConverterClassname( clazz.getName(),
- destination.getName()
);
- if( name == null )
+ final ConverterFactory factory =
+ getConverterFactory( clazz.getName(), destination.getName()
);
+ if( factory == null )
{
continue;
}
// Choose the more specialised source class
- if( match == null || match.isAssignableFrom( clazz ) )
+ if( bestSrcMatch == null || bestSrcMatch.isAssignableFrom( clazz
) )
{
- match = clazz;
- converterName = name;
+ bestSrcMatch = clazz;
+ matchFactory = factory;
}
- else if( clazz.isAssignableFrom( clazz ) )
+ else if( clazz.isAssignableFrom( bestSrcMatch ) )
{
continue;
}
@@ -200,9 +195,9 @@
}
// TODO - should cache the (src, dest) -> converter mapping
- if( match != null )
+ if( bestSrcMatch != null )
{
- return converterName;
+ return matchFactory;
}
// Could not find a converter
@@ -211,20 +206,16 @@
}
/**
- * Retrieve name of ConverterInfo that describes converter that converts
- * from source to destination.
- *
- * @param source the source classname
- * @param destination the destination classname
- * @return the className of converter or null if none available
+ * Retrieve factory for the converter that converts from source to
destination.
*/
- private String getConverterClassname( final String source, final String
destination )
+ private ConverterFactory getConverterFactory( final String source,
+ final String destination )
{
final HashMap map = (HashMap)m_mapping.get( source );
if( null == map )
{
return null;
}
- return (String)map.get( destination );
+ return (ConverterFactory)map.get( destination );
}
}
1.1
jakarta-avalon-excalibur/converter/src/java/org/apache/excalibur/converter/ConverterFactory.java
Index: ConverterFactory.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.excalibur.converter;
/**
* A factory used to create converter instances.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/06/09 05:12:38 $
*/
public interface ConverterFactory
{
/**
* Creates an instance of a converter.
*/
Converter createConverter() throws Exception;
}
1.2 +12 -28
jakarta-avalon-excalibur/converter/src/java/org/apache/excalibur/converter/lib/SimpleMasterConverter.java
Index: SimpleMasterConverter.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/converter/src/java/org/apache/excalibur/converter/lib/SimpleMasterConverter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SimpleMasterConverter.java 27 Apr 2002 05:23:01 -0000 1.1
+++ SimpleMasterConverter.java 9 Jun 2002 05:12:38 -0000 1.2
@@ -8,14 +8,13 @@
package org.apache.excalibur.converter.lib;
import org.apache.excalibur.converter.AbstractMasterConverter;
-import org.apache.excalibur.converter.Converter;
/**
* A very simple master converter that is capable of using
* any of the converters in this package.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
- * @version $Revision: 1.1 $ $Date: 2002/04/27 05:23:01 $
+ * @version $Revision: 1.2 $ $Date: 2002/06/09 05:12:38 $
*/
public class SimpleMasterConverter
extends AbstractMasterConverter
@@ -26,53 +25,38 @@
*/
public SimpleMasterConverter()
{
- registerConverter( ObjectToStringConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory(
ObjectToStringConverter.class ),
"java.lang.Object",
"java.lang.String" );
- registerConverter( StringToBooleanConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory(
StringToBooleanConverter.class ),
"java.lang.String",
"java.lang.Boolean" );
- registerConverter( StringToByteConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory(
StringToByteConverter.class ),
"java.lang.String",
"java.lang.Byte" );
- registerConverter( StringToClassConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory(
StringToClassConverter.class ),
"java.lang.String",
"java.lang.Class" );
- registerConverter( StringToDoubleConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory(
StringToDoubleConverter.class ),
"java.lang.String",
"java.lang.Double" );
- registerConverter( StringToFloatConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory(
StringToFloatConverter.class ),
"java.lang.String",
"java.lang.Float" );
- registerConverter( StringToIntegerConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory(
StringToIntegerConverter.class ),
"java.lang.String",
"java.lang.Integer" );
- registerConverter( StringToLongConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory(
StringToLongConverter.class ),
"java.lang.String",
"java.lang.Long" );
- registerConverter( StringToShortConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory(
StringToShortConverter.class ),
"java.lang.String",
"java.lang.Short" );
- registerConverter( StringToURLConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory(
StringToURLConverter.class ),
"java.lang.String",
"java.net.URL" );
- registerConverter( StringToDateConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory(
StringToDateConverter.class ),
"java.lang.String",
"java.util.Date" );
- }
-
- /**
- * Create an instance of converter with specified name.
- *
- * @param name the name of converter
- * @return the created converter instance
- * @throws Exception if converter can not be created.
- */
- protected Converter createConverter( final String name )
- throws Exception
- {
- final ClassLoader classLoader = getClass().getClassLoader();
- final Class clazz = classLoader.loadClass( name );
- return (Converter)clazz.newInstance();
}
}
1.1
jakarta-avalon-excalibur/converter/src/java/org/apache/excalibur/converter/lib/SimpleConverterFactory.java
Index: SimpleConverterFactory.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.excalibur.converter.lib;
import org.apache.excalibur.converter.Converter;
import org.apache.excalibur.converter.ConverterFactory;
/**
* A ConverterFactory that creates converter instances using reflection.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/06/09 05:12:38 $
*/
public class SimpleConverterFactory
implements ConverterFactory
{
private Class m_converterClass;
public SimpleConverterFactory( final Class converterClass )
{
m_converterClass = converterClass;
}
/**
* Creates an instance of a converter.
*/
public Converter createConverter() throws Exception
{
return (Converter)m_converterClass.newInstance();
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>