Index: magnolia-core/src/test/java/info/magnolia/content2bean/Content2BeanTest.java =================================================================== --- magnolia-core/src/test/java/info/magnolia/content2bean/Content2BeanTest.java (revision 48500) +++ magnolia-core/src/test/java/info/magnolia/content2bean/Content2BeanTest.java (working copy) @@ -39,9 +39,11 @@ import info.magnolia.test.mock.MockUtil; import java.io.IOException; +import java.text.MessageFormat; import java.util.AbstractMap; import java.util.HashMap; import java.util.Map; +import java.util.regex.Pattern; import javax.jcr.RepositoryException; @@ -311,8 +313,32 @@ assertTrue(result.getSample().getClass().isEnum()); assertTrue(result.getSample() instanceof SampleEnum); assertEquals(SampleEnum.two, result.getSample()); + } + public void testMessageFormatIsConvertedAutomagically() throws Exception { + Content node = MockUtil.createContent("myNode", new String[][]{ + {"class", "info.magnolia.content2bean.Content2BeanTest$BeanWithMessageFormat"}, + {"myFormat", "plop {0} plop {1} plop"} + }, new Content[0]); + final BeanWithMessageFormat res = (BeanWithMessageFormat) Content2BeanUtil.toBean(node, true); + assertNotNull(res); + assertNotNull(res.getMyFormat()); + assertTrue(res.getMyFormat() instanceof MessageFormat); + assertEquals("plop hey plop ho plop", res.formatIt("hey", "ho")); } + + public void testRegexPatternIsConvertedAutomagically() throws Exception { + Content node = MockUtil.createContent("myNode", new String[][]{ + {"class", "info.magnolia.content2bean.Content2BeanTest$BeanWithRegexPattern"}, + {"myPattern", "^My (\\w+) is (\\w+)$"} + }, new Content[0]); + final BeanWithRegexPattern res = (BeanWithRegexPattern) Content2BeanUtil.toBean(node, true); + assertNotNull(res); + assertNotNull(res.getMyPattern()); + assertTrue(res.getMyPattern() instanceof Pattern); + assertTrue(res.matches("My rich is tailor")); + assertFalse(res.matches("My tailor is not rich")); + } public void testCanSpecifySpecificMapImplementation() throws Exception { final Content node = MockUtil.createNode("/foo/bar", @@ -389,6 +415,38 @@ } } + public static final class BeanWithMessageFormat { + private MessageFormat myFormat; + + public void setMyFormat(MessageFormat myFormat) { + this.myFormat = myFormat; + } + + public MessageFormat getMyFormat() { + return myFormat; + } + + public String formatIt(String in1, String in2) { + return myFormat.format(new Object[]{in1,in2}); + } + } + + public static final class BeanWithRegexPattern { + private Pattern myPattern; + + public Pattern getMyPattern() { + return myPattern; + } + + public void setMyPattern(Pattern myPattern) { + this.myPattern = myPattern; + } + + public boolean matches(String in) { + return myPattern.matcher(in).matches(); + } + } + public static class MyMap extends HashMap { } Index: magnolia-core/src/main/java/info/magnolia/content2bean/impl/Content2BeanTransformerImpl.java =================================================================== --- magnolia-core/src/main/java/info/magnolia/content2bean/impl/Content2BeanTransformerImpl.java (revision 48500) +++ magnolia-core/src/main/java/info/magnolia/content2bean/impl/Content2BeanTransformerImpl.java (working copy) @@ -45,6 +45,7 @@ import info.magnolia.objectfactory.Classes; import info.magnolia.objectfactory.Components; import org.apache.commons.beanutils.BeanUtilsBean; +import org.apache.commons.beanutils.Converter; import org.apache.commons.beanutils.MethodUtils; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.beanutils.PropertyUtilsBean; @@ -55,10 +56,12 @@ import javax.jcr.RepositoryException; import java.lang.reflect.Method; +import java.text.MessageFormat; import java.util.Collection; import java.util.Iterator; import java.util.Locale; import java.util.Map; +import java.util.regex.Pattern; /** * Concrete implementation using reflection and adder methods. @@ -83,6 +86,18 @@ // de-register the converter for Class, we do our own conversion in convertPropertyValue() convertUtilsBean.deregister(Class.class); + convertUtilsBean.register(new Converter() { + public Object convert(Class type, Object value) { + return new MessageFormat((String) value); + } + }, MessageFormat.class); + + convertUtilsBean.register(new Converter() { + public Object convert(Class type, Object value) { + return Pattern.compile((String) value); + } + }, Pattern.class); + this.beanUtilsBean = new BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean()); } @@ -308,7 +323,7 @@ try { return Classes.getClassFactory().forName(value.toString()); } catch (ClassNotFoundException e) { - log.error(e.getMessage()); + log.error("Can convert {} to class: {}", value, e.toString()); throw new Content2BeanException(e); } }