1

I'm facing some issues when trying to set an enum decoder in Smooks, in order to decode values from a CSV file.

I need an EnumDecoder and although I saw that you can instantiate it and set the configuration, I could not find where to set it in order to make it active.

So far I managed to write something like this:

EnumDecoder decoder = (EnumDecoder)DataDecoder.Factory.create(SomeEnumTypeClass.class);
Properties properties = new Properties();
properties.put("enumType", SomeEnumTypeClass.class.getName());
properties.put("John", SomeEnumTypeClass.JOHN);
decoder.setConfiguration(properties);

One other issue with setting the decoder I had from the configuration file.

smooks-config.xml:

<?xml version="1.0" encoding="UTF-8"?>

<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                  xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.2.xsd"
                  xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.2.xsd">

<csv:reader fields="field1,field2EnumType" separator=";">
    <csv:listBinding beanId="beanList" class="com.myApp.TestingBean"/>
</csv:reader>

<jb:bean beanId="beanMockUp" 
         class="com.myApp.TestingBean">        
    <jb:value property="field1" data="header/field1" />
    <jb:value property="field2EnumType" 
              data="header/field2EnumType" 
              decoder="Enum">
        <jb:decodeParam name="enumType">com.myApp.SomeEnumTypeClass</jb:decodeParam>
        <jb:decodeParam name="John">JOHN</jb:decodeParam>
        <jb:decodeParam name="Jack">JACK</jb:decodeParam>
    </jb:value>
</jb:bean>
</smooks-resource-list>

Main.java:

Smooks smooks = null;

    try {
        smooks = new Smooks("smooks-config.xml");
    } catch (IOException | SAXException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        ExecutionContext executionContext = smooks.createExecutionContext();
        JavaResult result = new JavaResult();

        smooks.filterSource(executionContext, new StringSource(messageIn), result);

        return (List) result.getBean("beanList");
    } finally {
        smooks.close();
    }

TestingBean.java:

public class TestingBean {

private String field1;

private SomeEnumTypeClass field2EnumType;

/**
 * Get the value of field2EnumType
 *
 * @return the value of field2EnumType
 */
public SomeEnumTypeClass getField2EnumType() {
    return field2EnumType;
}

/**
 * Set the value of field2EnumType
 *
 * @param field2EnumType new value of field2EnumType
 */
public void setField2EnumType(SomeEnumTypeClass field2EnumType) {
    this.field2EnumType = field2EnumType;
}


/**
 * Get the value of field1
 *
 * @return the value of field1
 */
public String getField1() {
    return field1;
}

/**
 * Set the value of field1
 *
 * @param field1 new value of field1
 */
public void setField1(String field1) {
    this.field1= field1;
}

}

CSV file:

abc;john

I always end up with the error:

Exception in thread "main" org.milyn.SmooksException: Failed to filter source.
at org.milyn.delivery.sax.SmooksSAXFilter.doFilter(SmooksSAXFilter.java:97)
at org.milyn.delivery.sax.SmooksSAXFilter.doFilter(SmooksSAXFilter.java:64)
at org.milyn.Smooks._filter(Smooks.java:526)
at org.milyn.Smooks.filterSource(Smooks.java:482)
..........
Caused by: org.milyn.javabean.DataDecodeException: Failed to decode binding value 'John' for property 'field2EnumType' on bean 'fewijf-3243-fijewoi'
at org.milyn.javabean.BeanInstancePopulator.decodeDataString(BeanInstancePopulator.java:624)
at org.milyn.javabean.BeanInstancePopulator.decodeAndSetPropertyValue(BeanInstancePopulator.java:513)
at org.milyn.javabean.BeanInstancePopulator.bindSaxDataValue(BeanInstancePopulator.java:449)
at org.milyn.javabean.BeanInstancePopulator.visitAfter(BeanInstancePopulator.java:379)
at org.milyn.delivery.sax.SAXHandler.visitAfter(SAXHandler.java:389)
at org.milyn.delivery.sax.SAXHandler.endElement(SAXHandler.java:204)
at org.milyn.delivery.SmooksContentHandler.endElement(SmooksContentHandler.java:96)
at org.milyn.flatfile.FlatFileReader.parse(FlatFileReader.java:165)
at org.milyn.delivery.sax.SAXParser.parse(SAXParser.java:76)
at org.milyn.delivery.sax.SmooksSAXFilter.doFilter(SmooksSAXFilter.java:86)
... 5 more

Thanks!

1 Answers1

0

In your csv data "john" is lowercase, but your smooks config decodeParam for "JOHN" is tied to "John" capitalized.

Your smocks-config for the Enum should be something like this:

<jb:value property="field2EnumType" 
          data="header/field2EnumType" 
          decoder="Enum">
    <jb:decodeParam name="enumType">com.myApp.SomeEnumTypeClass</jb:decodeParam>
    <jb:decodeParam name="john">JOHN</jb:decodeParam>
    <jb:decodeParam name="jack">JACK</jb:decodeParam>
</jb:value>

Notice the name attribute of the decodeParam. It needs to exactly match the data you are decoding (case sensitive).