(Posted on behalf of a colleague, Stan Tyree) I just started using Betwixt and was amazed how easy it was to use. I am working on a project where I am trying to use it as an "import/export" tool for my Java beans. Betwixt has saved me a lot of time and energy. In the process doing my task, I ran into a small problem when using the BeanReader to re-create my bean. The bean contained BigDecimal and Timestamp properties. When the bean was exported, it worked fine. The problem arose when it tried to create either of these object types inside "protected Object createBean(Attributes attributes)" method in org.apache.commons.betwixt.io.BeanCreateRule. This function calls the "newInstance" function on the bean class for this rule. Neither Timestamp and BigDecimal have default constructors, so this fails. I made some small changes to BeanReader and BeanCreateRule to fix this issue. I was wondering if this change or something similar could be added to the project, or is there already a way to handle this situation using Betwixt that we just missed. Below are the steps I took to fix my problem. If you want to see the files, I can send those as well. 1. I created an interface for creating objects. package org.apache.commons.betwixt.io; import org.xml.sax.Attributes; /** * This interface is used to create beans that do not * have a default constructor. */ public interface BeanCreator { public Object createBean(BeanCreateRule rule, Attributes attributes) throws Exception; }; 2. On the BeanReader class, I added a Hashtable and methods to support it: private Hashtable creatorMap = new Hashtable(); public void addBeanCreator(String sPath, BeanCreator creator) { creatorMap.put(sPath, creator); } public BeanCreator getBeanCreator(String sPath) { return (BeanCreator)creatorMap.get(sPath); } public void removeBeanCreators(String sPath) { creatorMap.remove(sPath); } public void clearBeanCreators() { creatorMap.clear(); }
3. On the BeanCreateRule, I changed the function "createBean" to: protected Object createBean(Attributes attributes) throws Exception : try { BeanCreator beanCreator = getBeanReader().getBeanCreator(beanClass.getName()); if (beanCreator != null) return beanCreator.createBean(this, attributes); else return beanClass.newInstance(); } catch (Exception e) { log.warn( "Could not create instance of type: " + beanClass.getName() ); return null; } After these changes, you can create a bean with BigDecimal and Timestamp functions by doing the following: BeanCreator tsCreator = new BeanCreator() { public Object createBean(BeanCreateRule rule, Attributes attributes) throws Exception { Timestamp ts = new Timestamp(new java.util.GregorianCalendar().getTime().getTime()); return ts; } }; BeanCreator bigdCreator = new BeanCreator() { public Object createBean(BeanCreateRule rule, Attributes attributes) throws Exception { BigDecimal bd = new BigDecimal(0.0d); return bd; } }; BeanReader reader = new BeanReader(); reader.addBeanCreator("java.math.BigDecimal", bigdCreator); reader.addBeanCreator("java.sql.Timestamp", tsCreator); Object bean = reader.parse(new File("testBean.xml")); Thank you for your time, Stan Tyree -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>