- Revision
- 400
- Author
- mauro
- Date
- 2007-11-17 04:34:16 -0600 (Sat, 17 Nov 2007)
Log Message
Renamed OgnlTypeConverter to DelegatingTypeConverter
Modified Paths
- trunk/waffle-core/src/main/java/org/codehaus/waffle/context/pico/PicoComponentRegistry.java
- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/OgnlDataBinderTest.java
- trunk/waffle-core/src/test/java/org/codehaus/waffle/context/pico/PicoComponentRegistryTest.java
- trunk/waffle-distribution/src/site/content/pluggability.html
Added Paths
- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DelegatingTypeConverter.java
- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/DelegatingTypeConverterTest.java
Removed Paths
Diff
Copied: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DelegatingTypeConverter.java (from rev 399, trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/OgnlTypeConverter.java) (0 => 400)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DelegatingTypeConverter.java (rev 0) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DelegatingTypeConverter.java 2007-11-17 10:34:16 UTC (rev 400) @@ -0,0 +1,112 @@ +/***************************************************************************** + * Copyright (C) 2005,2006 Michael Ward * + * All rights reserved. * + * ------------------------------------------------------------------------- * + * The software in this package is published under the terms of the BSD * + * style license a copy of which has been included with this distribution in * + * the LICENSE.txt file. * + * * + * Original code by: Michael Ward * + *****************************************************************************/ +package org.codehaus.waffle.bind; + +import ognl.OgnlOps; +import ognl.TypeConverter; + +import java.lang.reflect.Member; +import java.util.HashMap; +import java.util.Map; + +/** + * An implementation of Ognl's <code>TypeConverter</code> which handles Java 5 enums and will delegate + * custom <code>ValueConverter</code>'s registered per application. + * + * @author Michael Ward + * @author Mauro Talevi + */ +public class DelegatingTypeConverter implements TypeConverter { + private static final String EMPTY = ""; + private final ValueConverter[] valueConverters; + private final Map<Class<?>, ValueConverter> cache = new HashMap<Class<?>, ValueConverter>(); + + public DelegatingTypeConverter() { + this.valueConverters = new ValueConverter[0]; + } + + public DelegatingTypeConverter(ValueConverter... valueConverters) { + if (valueConverters == null) { + this.valueConverters = new ValueConverter[0]; + } else { + this.valueConverters = valueConverters; + } + } + + /** + * <b>Comments copied from Ognl</b> + * <p/> + * Converts the given value to a given type. The OGNL context, target, member and + * name of property being set are given. This method should be able to handle + * conversion in general without any context, target, member or property name specified. + * + * @param context OGNL context under which the conversion is being done + * @param target target object in which the property is being set + * @param member member (Constructor, Method or Field) being set + * @param propertyName property name being set + * @param value value to be converted + * @param toType type to which value is converted + * @return Converted value Object of type toType or TypeConverter.NoConversionPossible to indicate that the + * conversion was not possible. + */ + public Object convertValue(Map context, + Object target, + Member member, + String propertyName, + Object value, + Class toType) { + return convertValue(propertyName, (String) value, toType); + } + + /** + * Simplified entry point for Ognl use in Waffle + * + * @param propertyName property name being set + * @param value value to be converted + * @param toType type to which value is converted + * @return Converted value Object of type toType or TypeConverter.NoConversionPossible to indicate that the + * conversion was not possible. + */ + @SuppressWarnings({"unchecked"}) + public Object convertValue(String propertyName, String value, Class toType) { + if (toType.isEnum()) { + if (EMPTY.equals(value)) { + return null; + } + return Enum.valueOf(toType, value); + } + + ValueConverter converter = findConverter(toType); + + if (converter != null) { + return converter.convertValue(propertyName, value, toType); + } + + return OgnlOps.convertValue(value, toType); + } + + private ValueConverter findConverter(Class<?> toType) { + if (cache.containsKey(toType)) { // cache hit + return cache.get(toType); + } + + for (ValueConverter converter : valueConverters) { + if (converter.accept(toType)) { + cache.put(toType, converter); + return converter; + } + } + + cache.put(toType, null); // cache the null + return null; + } + +}
Deleted: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/OgnlTypeConverter.java (399 => 400)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/OgnlTypeConverter.java 2007-11-17 10:21:57 UTC (rev 399) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/OgnlTypeConverter.java 2007-11-17 10:34:16 UTC (rev 400) @@ -1,112 +0,0 @@ -/***************************************************************************** - * Copyright (C) 2005,2006 Michael Ward * - * All rights reserved. * - * ------------------------------------------------------------------------- * - * The software in this package is published under the terms of the BSD * - * style license a copy of which has been included with this distribution in * - * the LICENSE.txt file. * - * * - * Original code by: Michael Ward * - *****************************************************************************/ -package org.codehaus.waffle.bind; - -import ognl.OgnlOps; -import ognl.TypeConverter; - -import java.lang.reflect.Member; -import java.util.HashMap; -import java.util.Map; - -/** - * An implementation of Ognl's <code>TypeConverter</code> which handles Java 5 enums and will delegate - * custom <code>ValueConverter</code>'s registered per application. - * - * @author Michael Ward - * @author Mauro Talevi - */ -public class OgnlTypeConverter implements TypeConverter { - private static final String EMPTY = ""; - private final ValueConverter[] valueConverters; - private final Map<Class<?>, ValueConverter> cache = new HashMap<Class<?>, ValueConverter>(); - - public OgnlTypeConverter() { - this.valueConverters = new ValueConverter[0]; - } - - public OgnlTypeConverter(ValueConverter... valueConverters) { - if (valueConverters == null) { - this.valueConverters = new ValueConverter[0]; - } else { - this.valueConverters = valueConverters; - } - } - - /** - * <b>Comments copied from Ognl</b> - * <p/> - * Converts the given value to a given type. The OGNL context, target, member and - * name of property being set are given. This method should be able to handle - * conversion in general without any context, target, member or property name specified. - * - * @param context OGNL context under which the conversion is being done - * @param target target object in which the property is being set - * @param member member (Constructor, Method or Field) being set - * @param propertyName property name being set - * @param value value to be converted - * @param toType type to which value is converted - * @return Converted value Object of type toType or TypeConverter.NoConversionPossible to indicate that the - * conversion was not possible. - */ - public Object convertValue(Map context, - Object target, - Member member, - String propertyName, - Object value, - Class toType) { - return convertValue(propertyName, (String) value, toType); - } - - /** - * Simplified entry point for Ognl use in Waffle - * - * @param propertyName property name being set - * @param value value to be converted - * @param toType type to which value is converted - * @return Converted value Object of type toType or TypeConverter.NoConversionPossible to indicate that the - * conversion was not possible. - */ - @SuppressWarnings({"unchecked"}) - public Object convertValue(String propertyName, String value, Class toType) { - if (toType.isEnum()) { - if (EMPTY.equals(value)) { - return null; - } - return Enum.valueOf(toType, value); - } - - ValueConverter waffleTypeConverter = findConverter(toType); - - if (waffleTypeConverter != null) { - return waffleTypeConverter.convertValue(propertyName, value, toType); - } - - return OgnlOps.convertValue(value, toType); - } - - private ValueConverter findConverter(Class<?> toType) { - if (cache.containsKey(toType)) { // cache hit - return cache.get(toType); - } - - for (ValueConverter converter : valueConverters) { - if (converter.accept(toType)) { - cache.put(toType, converter); - return converter; - } - } - - cache.put(toType, null); // cache the null - return null; - } - -}
Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/context/pico/PicoComponentRegistry.java (399 => 400)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/context/pico/PicoComponentRegistry.java 2007-11-17 10:21:57 UTC (rev 399) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/context/pico/PicoComponentRegistry.java 2007-11-17 10:34:16 UTC (rev 400) @@ -33,7 +33,7 @@ import org.codehaus.waffle.bind.DefaultBindErrorMessageResolver; import org.codehaus.waffle.bind.IntrospectingRequestAttributeBinder; import org.codehaus.waffle.bind.OgnlDataBinder; -import org.codehaus.waffle.bind.OgnlTypeConverter; +import org.codehaus.waffle.bind.DelegatingTypeConverter; import org.codehaus.waffle.bind.RequestAttributeBinder; import org.codehaus.waffle.context.ContextContainerFactory; import org.codehaus.waffle.controller.ContextControllerDefinitionFactory; @@ -88,7 +88,7 @@ register(BindErrorMessageResolver.class, DefaultBindErrorMessageResolver.class, servletContext); register(DataBinder.class, OgnlDataBinder.class, servletContext); register(RequestAttributeBinder.class, IntrospectingRequestAttributeBinder.class, servletContext); - register(TypeConverter.class, OgnlTypeConverter.class, servletContext); + register(TypeConverter.class, DelegatingTypeConverter.class, servletContext); register(ContextContainerFactory.class, PicoContextContainerFactory.class, servletContext); register(ControllerDefinitionFactory.class, ContextControllerDefinitionFactory.class, servletContext); register(ControllerNameResolver.class, ContextPathControllerNameResolver.class, servletContext);
Copied: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/DelegatingTypeConverterTest.java (from rev 399, trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/OgnlTypeConverterTest.java) (0 => 400)
--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/DelegatingTypeConverterTest.java (rev 0) +++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/DelegatingTypeConverterTest.java 2007-11-17 10:34:16 UTC (rev 400) @@ -0,0 +1,56 @@ +package org.codehaus.waffle.bind; + +import static org.junit.Assert.assertEquals; + +import java.util.Vector; + +import org.codehaus.waffle.context.ContextLevel; +import org.jmock.Expectations; +import org.jmock.Mockery; +import org.jmock.integration.junit4.JMock; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * + * @author Michael Ward + * @author Mauro Talevi + */ [EMAIL PROTECTED](JMock.class) +public class DelegatingTypeConverterTest { + + private Mockery mockery = new Mockery(); + + @Test + public void canConvertValueForEnum() { + DelegatingTypeConverter converter = new DelegatingTypeConverter(); + Object result = converter.convertValue("foobar", "APPLICATION", ContextLevel.class); + + assertEquals(ContextLevel.APPLICATION, result); + } + + @Test + public void canConvertValueForIntegers() { + DelegatingTypeConverter converter = new DelegatingTypeConverter(); + int value = (Integer) converter.convertValue("foobar", "15", Integer.class); + + assertEquals(15, value); + } + + @Test + public void canDelegateToValueConverter() { + // Mock ValueConverter + final ValueConverter valueConverter = mockery.mock(ValueConverter.class); + mockery.checking(new Expectations() { + { + one(valueConverter).accept(Vector.class); + will(returnValue(true)); + one(valueConverter).convertValue(with(same("propertyName")), with(same("foobar")), with(same(Vector.class))); + will(returnValue(new Vector<Object>())); + } + }); + DelegatingTypeConverter converter = new DelegatingTypeConverter(new ValueConverter[] {valueConverter}); + + converter.convertValue("propertyName", "foobar", Vector.class); + } +}
Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/OgnlDataBinderTest.java (399 => 400)
--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/OgnlDataBinderTest.java 2007-11-17 10:21:57 UTC (rev 399) +++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/OgnlDataBinderTest.java 2007-11-17 10:34:16 UTC (rev 400) @@ -58,7 +58,7 @@ }); FakeController fakeController = new FakeController(); - DataBinder binder = new OgnlDataBinder(new OgnlTypeConverter(), null, new SilentMonitor()); + DataBinder binder = new OgnlDataBinder(new DelegatingTypeConverter(), null, new SilentMonitor()); ErrorsContext errorsContext = new DefaultErrorsContext(); binder.bind(request, null, errorsContext, fakeController);
Deleted: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/OgnlTypeConverterTest.java (399 => 400)
--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/OgnlTypeConverterTest.java 2007-11-17 10:21:57 UTC (rev 399) +++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/OgnlTypeConverterTest.java 2007-11-17 10:34:16 UTC (rev 400) @@ -1,57 +0,0 @@ -package org.codehaus.waffle.bind; - -import static org.junit.Assert.assertEquals; - -import java.util.Vector; - -import org.codehaus.waffle.context.ContextLevel; -import org.jmock.Expectations; -import org.jmock.Mockery; -import org.jmock.integration.junit4.JMock; -import org.junit.Test; -import org.junit.runner.RunWith; - -/** - * - * @author Michael Ward - * @author Mauro Talevi - */ [EMAIL PROTECTED](JMock.class) -public class OgnlTypeConverterTest { - - private Mockery mockery = new Mockery(); - - @Test - public void canConvertValueForEnum() { - OgnlTypeConverter converter = new OgnlTypeConverter(); - Object result = converter.convertValue("foobar", "APPLICATION", ContextLevel.class); - - assertEquals(ContextLevel.APPLICATION, result); - } - - @Test - public void canHandleNulls() { - OgnlTypeConverter converter = new OgnlTypeConverter(); - int value = (Integer) converter.convertValue("foobar", "15", Integer.class); - - assertEquals(15, value); - } - - @Test - public void canConvertDelegatesToWaffleTypeConverter() { - // Mock TypeConverter - final ValueConverter waffleTypeConverter = mockery.mock(ValueConverter.class); - mockery.checking(new Expectations() { - { - one(waffleTypeConverter).accept(Vector.class); - will(returnValue(true)); - one(waffleTypeConverter).convertValue(with(same("propertyName")), with(same("foobar")), with(same(Vector.class))); - will(returnValue(new Vector<Object>())); - } - }); - ValueConverter[] waffleTypeConverters = {waffleTypeConverter}; - OgnlTypeConverter converter = new OgnlTypeConverter(waffleTypeConverters); - - converter.convertValue("propertyName", "foobar", Vector.class); - } -}
Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/context/pico/PicoComponentRegistryTest.java (399 => 400)
--- trunk/waffle-core/src/test/java/org/codehaus/waffle/context/pico/PicoComponentRegistryTest.java 2007-11-17 10:21:57 UTC (rev 399) +++ trunk/waffle-core/src/test/java/org/codehaus/waffle/context/pico/PicoComponentRegistryTest.java 2007-11-17 10:34:16 UTC (rev 400) @@ -32,7 +32,7 @@ import org.codehaus.waffle.bind.DataBinder; import org.codehaus.waffle.bind.DefaultBindErrorMessageResolver; import org.codehaus.waffle.bind.OgnlDataBinder; -import org.codehaus.waffle.bind.OgnlTypeConverter; +import org.codehaus.waffle.bind.DelegatingTypeConverter; import org.codehaus.waffle.bind.RequestAttributeBinder; import org.codehaus.waffle.context.AbstractContextContainerFactory; import org.codehaus.waffle.context.ContextContainerFactory; @@ -154,7 +154,7 @@ assertTrue(componentRegistry.getMessageResources() instanceof DefaultMessageResources); assertTrue(componentRegistry.getRegistrarMonitor() instanceof AbstractWritingMonitor); assertTrue(componentRegistry.getServletMonitor() instanceof AbstractWritingMonitor); - assertTrue(componentRegistry.getTypeConverter() instanceof OgnlTypeConverter); + assertTrue(componentRegistry.getTypeConverter() instanceof DelegatingTypeConverter); assertTrue(componentRegistry.getValidator() instanceof DefaultValidator); assertTrue(componentRegistry.getValidationMonitor() instanceof AbstractWritingMonitor); assertTrue(componentRegistry.getViewDispatcher() instanceof DefaultViewDispatcher);
Modified: trunk/waffle-distribution/src/site/content/pluggability.html (399 => 400)
--- trunk/waffle-distribution/src/site/content/pluggability.html 2007-11-17 10:21:57 UTC (rev 399) +++ trunk/waffle-distribution/src/site/content/pluggability.html 2007-11-17 10:34:16 UTC (rev 400) @@ -87,8 +87,8 @@ <tr class="b"> <td align="left"><a href="" org.codehaus.waffle.bind.TypeConverter</a></td> - <td align="left"><a href="" - org.codehaus.waffle.bind.OgnlTypeConverter</a></td> + <td align="left"><a href="" + org.codehaus.waffle.bind.DelegatingTypeConverter</a></td> </tr> <tr class="b"> <td align="left"><a href="" @@ -162,12 +162,12 @@ <p> For a more concrete example Waffle defines the interface <i> - <a href="" - org.codehaus.waffle.bind.WaffleTypeConverter</a></i>. + <a href="" + org.codehaus.waffle.bind.ValueConverter</a></i>. Implementations of this interface allows you to write custom conversions for a specific class type. The following is an example of how you can - register your converters with Waffle. Waffle's <i>OgnlTypeConverter</i> - is dependent on all <i>org.codehaus.waffle.bind.WaffleTypeConverter</i> + register your converters with Waffle. Waffle's <i>DelegatingTypeConverter</i> + is dependent on all <i>org.codehaus.waffle.bind.ValueConverter</i> that are registered in the <b>web.xml</b>. So the following example will provide 2 custom converters for use in your application. </p>
To unsubscribe from this list please visit:
