views:

505

answers:

2

Sample XML.

<person>
  <name>Joe Dirt</name>
  <ssn>123-45-6789</ssn>
  <dob>07/04/1981</dob>
</person>

Sample Java Class

public class Person(){
  private String name;
  private String ssn;
  private java.util.Date dob;
.....
}

Sample Digester rules

<?xml version="1.0"?>
<digester-rules>
  <pattern value="message">
      <object-create-rule classname="Person" />
      <bean-property-setter-rule pattern="name" propertyname="name" />
      <bean-property-setter-rule pattern="ssn" propertyname="ssn" />
      <bean-property-setter-rule pattern="dob" propertyname="dob" />
 </pattern>
</digester-rules>
A: 

I've once faced this question myself. I didn't find any standard way to do this. The best I came up with was this:

digester.addRule(ROOT + "/lastBuildDate", new Rule() {
  public void body(final String namespace, final String name, final String text) throws Exception {
    Message message = (Message) getDigester().peek();
    if (text == null || text.trim().equals("")) {
      message.setDate(new Date());
    } else {
      try {
        message.setDate(parseDate(text));
      } catch (final ParseException e) {
        Log.warn("failed to parse date: " + text, e);
        message.setDate(new Date());
      }
    }
  }
});

Date parseDate(String text) throws ParseException {
  // left as an exercise to the reader
}

I can't help with using custom rules from your digester XML as I've always preferred to build digesters programmatically.

As a workaround, you could create an additional setter in your Person class:

public void setDobAsString(String dob) throws ParseException {
    setDob(parseDate(dob));
}
sfussenegger
+2  A: 

Should be able to register a DateConverter, and all should work:

import org.apache.commons.beanutils.Converter;

class MyDateConverter implements Converter {
    @Override
    public Object convert(Class clazz, Object value) {
        if (clazz.equals(Date.class)) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            try {
                return sdf.parse((String) value);
            } catch (ParseException pe) {
                throw new IllegalArgumentException(pe);
            } catch (ClassCastException cce) {
                throw new IllegalArgumentException(cce);
            }
        } else {
            throw new IllegalArgumentException("Expected Date class");
        }
    }
}

......

import org.apache.commons.beanutils.ConvertUtils;

@Test
public void testXmlRules() throws Exception {
    ConvertUtils.register(new MyDateConverter(), Date.class);
    Digester digester = DigesterLoader.createDigester(new InputSource(this
            .getClass().getResourceAsStream("rules.xml")));
    Person person = (Person) digester.parse(this.getClass()
            .getResourceAsStream("person.xml"));
    Assert.assertEquals("Joe Dirt", person.getName());
    Assert.assertEquals("123-45-6789", person.getSsn());
    Assert.assertEquals(new SimpleDateFormat("dd/MM/yyyy")
            .parse("07/04/1981"), person.getDob());
}
toolkit
That's exactly what I needed. Thanks!
ScArcher2