以编程方式将csv映射到smooks中的Java无法处理空Double [英] Mapping csv to java in smooks programatically can't handle empty Double

查看:149
本文介绍了以编程方式将csv映射到smooks中的Java无法处理空Double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用smooks库快速轻松地解析csv文件,并以编程方式将其映射到POJO. 但是我在处理POJO的Double属性上的空字段时遇到麻烦.例如,java对象具有:

I'm using smooks library to parse csv files fast and easy, mapping them programmatically to POJOs. But I have troubles handling empty fields on POJO's Double attributes. For example, the java object has:

public class MulticashHeader {

        // ...
    private Double c06;
    private static Double c07;
    private Double c08;
    private Double c09;
    private String c10;
    // ...
    public Double getC06_SaldoInicial() {
        return c06_SaldoInicial;
    }
    /**
     * @param c06_SaldoInicial the c06_SaldoInicial to set
     */
    public void setC06_SaldoInicial(Double c06_SaldoInicial) {
        if (c06_SaldoInicial != null)
            this.c06_SaldoInicial = c06_SaldoInicial;
    }
    public void setC06_SaldoInicial(String c06_SaldoInicial) {
        if (c06_SaldoInicial != null && !"".equals(c06_SaldoInicial.trim()) ) {
            this.c06_SaldoInicial = new Double(c06_SaldoInicial.trim());
        } else {
            this.c06_SaldoInicial = Double.valueOf("0");
        }
    }
    /**
     * @return the c07_TotalDebitos
     */
    public Double getC07_TotalDebitos() {
        return c07_TotalDebitos;
    }
    /**
     * @param c07_TotalDebitos the c07_TotalDebitos to set
     */
    public void setC07_TotalDebitos(Double c07_TotalDebitos) {
        MulticashEncabezado.c07_TotalDebitos = c07_TotalDebitos;
    }
    public void setC07_TotalDebitos(String c07_TotalDebitos) {
        if (c07_TotalDebitos != null && !"".equals(c07_TotalDebitos.trim()) ) {
            MulticashEncabezado.c07_TotalDebitos = new Double(c07_TotalDebitos.trim());
        } else {
            MulticashEncabezado.c07_TotalDebitos = Double.valueOf("0");
        }
    }
    /**
     * @return the c08_TotalCreditos
     */
    public Double getC08_TotalCreditos() {
        return c08_TotalCreditos;
    }
    /**
     * @param c08_TotalCreditos the c08_TotalCreditos to set
     */
    public void setC08_TotalCreditos(Double c08_TotalCreditos) {
        if (c08_TotalCreditos != null)
            this.c08_TotalCreditos = c08_TotalCreditos;
    }
    public void setC08_TotalCreditos(String c08_TotalCreditos) {
        if (c08_TotalCreditos != null && !"".equals(c08_TotalCreditos.trim()) ) {
            this.c08_TotalCreditos = new Double( c08_TotalCreditos.trim() );
        } else {
            this.c08_TotalCreditos = Double.valueOf("0");
        }
    }
        // ...
}

smooks映射如下:

And the smooks mapping goes like this:

private static final char CchrSeparator = ';';
// File mapping
private static final String CMulticashHeader = "c01_ClaveBanco,c02_Cuenta,c03_Consecutivo,c04_FechaMovimientos,c05_ClaveMoneda,c06_SaldoInicial,c07_TotalDebitos,"+
        "c08_TotalCreditos,c09_SaldoFinal,c10_TipoCuenta,c11,c12,c13,c14,c15,c16,c17,c18_NumeroMovs";
smooks.setReaderConfig( new CSVReaderConfigurator( CMulticashHeader ).setBinding( new CSVBinding(
                            "balanceList", MulticashHeader.class, CSVBindingType.LIST )).setSeparatorChar(CchrSeparator) );

但是,当处理具有空值的文件c_06,c_07,c_08和c_09(不仅是零而是空)时,它们碰巧是Double值,即使在POJO的属性设置器上设置函数重载也不起作用并抛出

But when processing a file with empty values for c_06, c_07, c_08 and c_09 (not just zeros but empty), which happen to be Double values, even setting function overloading on POJO's attribute setter does not work and throws

Unable to filter InputStream for target profile [org.milyn.profile.Profile#default_profile].
org.milyn.SmooksException: Unable to filter InputStream for target profile [org.milyn.profile.Profile#default_profile].
        at org.milyn.delivery.dom.SmooksDOMFilter.filter(SmooksDOMFilter.java:294)
        at org.milyn.delivery.dom.SmooksDOMFilter.doFilter(SmooksDOMFilter.java:243)
        at org.milyn.delivery.dom.SmooksDOMFilter.doFilter(SmooksDOMFilter.java:216)
        at org.milyn.Smooks._filter(Smooks.java:516)
        at org.milyn.Smooks.filterSource(Smooks.java:475)
        at com.example.CSVParserServicio.runSmooksTransform(CSVParserServicio.java:98)
        ...
Caused by: org.milyn.javabean.DataDecodeException: Failed to decode binding value '' for property 'c07_TotalDebitos' on bean '9b06d0eb-9231-
4c49-a612-75ace2b5d44c'.
        at org.milyn.javabean.BeanInstancePopulator.decodeDataString(BeanInstancePopulator.java:581)
        at org.milyn.javabean.BeanInstancePopulator.decodeAndSetPropertyValue(BeanInstancePopulator.java:482)
        at org.milyn.javabean.BeanInstancePopulator.bindDomDataValue(BeanInstancePopulator.java:384)
        at org.milyn.javabean.BeanInstancePopulator.visitAfter(BeanInstancePopulator.java:319)
        at org.milyn.delivery.dom.SmooksDOMFilter$ElementProcessor.processMapping(SmooksDOMFilter.java:778)
        at org.milyn.delivery.dom.SmooksDOMFilter$ElementProcessor.process(SmooksDOMFilter.java:717)
        at org.milyn.delivery.dom.SmooksDOMFilter$ElementProcessor.access$000(SmooksDOMFilter.java:666)
        at org.milyn.delivery.dom.SmooksDOMFilter.filter(SmooksDOMFilter.java:385)
        at org.milyn.delivery.dom.SmooksDOMFilter.filter(SmooksDOMFilter.java:317)
        at org.milyn.delivery.dom.SmooksDOMFilter.filter(SmooksDOMFilter.java:292)
        ... 27 more
Caused by: org.milyn.javabean.DataDecodeException: Failed to decode Double value ''.
        at org.milyn.javabean.decoders.DoubleDecoder.decode(DoubleDecoder.java:34)
        at org.milyn.javabean.BeanInstancePopulator.decodeDataString(BeanInstancePopulator.java:579)
        ... 36 more
Caused by: java.lang.NumberFormatException: empty String
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:992)
        at java.lang.Double.parseDouble(Double.java:510)
        at org.milyn.javabean.decoders.DoubleDecoder.decode(DoubleDecoder.java:32)
        ... 37 more

aoyone怎样避免这种陷阱以编程方式进行?

Does anoyone how to avoid this trap keeping the programmatic approach?

---------------- EDIT ----------------: 我正在csv-to-java的smooks示例中对此进行测试,使用Tom所说的config xml.我有这个错误会重复出现:

----------------EDIT----------------: I'm testing this in the smooks example fro csv-to-java, using a config xml as Tom says. I have this and the error replicates:

<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                      xmlns:csv1="http://www.milyn.org/xsd/smooks/csv-1.3.xsd">

    <csv1:reader fields="firstName,lastName,gender,age,country" separator=";">
        <csv1:listBinding beanId="customerList" class="example.Customer"/>
    </csv1:reader>

</smooks-resource-list>

文件是

charles;moulliard;Male;43;belgium
maxence;dewil;Male;;belgium
eleonor;moulliard;Female;12;belgium

(请注意空白年龄). Java主类是

(note the empty age). And the java main class is

private static String messageIn = readInputMessage();

Smooks smooks = new Smooks("/route/to/smooks-config.xml");

        try {
            ExecutionContext executionContext = smooks.createExecutionContext();
            JavaResult result = new JavaResult();
            smooks.filterSource(executionContext, new StringSource(messageIn), result);
            return (List) result.getBean("customerList");
        } finally {
            smooks.close();
        }
        ...

我发现这是汤姆的例子(!!!),所以我问:如何使用<jb:bean>将csv字段映射到POJO属性?

I found out that it's Tom's example (!!!), so I ask: how to map csv fields to POJO attributes using <jb:bean>?

---------------- EDIT2 ----------------: 我尝试了在codehaus的JIRA线程中找到的内容,并获得了以下配置文件:

---------------- EDIT2 ----------------: I tried something I found in a codehaus' JIRA thread and I got the following config file:

<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                      xmlns:csv1="http://www.milyn.org/xsd/smooks/csv-1.3.xsd"
                      xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.1.xsd">

    <csv1:reader fields="firstName,lastName,gender,age,country" separator=";" />

    <jb:bindings beanId="customer" class="example.Customer" createOnElement="csv-record">
        <jb:value property="firstName" data="csv-record/firstName" />
        <jb:value property="lastName" data="csv-record/lastName" />
        <jb:value property="gender" data="csv-record/gender" />
        <jb:value property="age" data="csv-record/age" decoder="Integer" default="0"  />
        <jb:value property="country" data="csv-record/country" />
    </jb:bindings>

    <jb:bindings beanId="customerList" class="java.util.ArrayList" createOnElement="csv-set">
        <jb:wiring beanIdRef="customer" />
    </jb:bindings>

</smooks-resource-list>

使用CSV时可以使用带有空Integer(年龄)的CSV,但是在我将JavaBean xsd升级到1.1以上时,它会失败.引发错误

It works using the CSV with the empty Integer (age) shown before BUT it fails when I update javabean xsd above 1.1. Throws the error

Exception in thread "main" org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'jb:bindings'. One of '{"http://www.milyn.org/xsd/smooks-1.1.xsd":abstract-reader, "http://www.milyn.org/xsd/smooks-1.1.xsd":abstract-resource-config}' is expected

¿我想念什么吗?

推荐答案

您在此处使用CSV简写绑定机制,该机制不允许您指定默认值.我认为您的选择将是...

You are using the CSV shorthand binding mechanism there, which doesn't allow you to specify defaults. I think your options will be...

  1. 修改CSVBinding代码以满足您的需求.
  2. 使用更详细的配置机制之一,即xml <jb:bean> 配置
  1. Modify the CSVBinding code to meet your needs.
  2. Use one of the more verbose config mechansism i.e. the xml <jb:bean> configs or the programmatic config. Either of these will let you specify a default value.

这篇关于以编程方式将csv映射到smooks中的Java无法处理空Double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆