1

尝试在 Smooks 中设置枚举解码器以解码 CSV 文件中的值时,我遇到了一些问题。

我需要一个 EnumDecoder,虽然我看到您可以实例化它并设置配置,但我找不到设置它以使其处于活动状态的位置。

到目前为止,我设法写了这样的东西:

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);

我从配置文件中设置解码器的另一个问题。

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>

主.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();
    }

测试Bean.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 文件:

abc;john

我总是以错误告终:

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

谢谢!

4

1 回答 1

0

在您的 csv 数据中,“john”是小写的,但您的 smooks 配置 decodeParam 的“JOHN”与“John”大写。

Enum 的 smocks-config 应该是这样的:

<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>

注意decodeParam的name属性。它需要与您正在解码的数据完全匹配(区分大小写)。

于 2016-09-13T15:21:36.363 回答