fengguowusheng 2011-08-02 10:35
浏览 272
已采纳

JAVA Castor 使用GeneralizedFieldHandler 在unmarshall时报错

我在使用GeneralizedFieldHandler自定义Field的handler时,marshall成功,unmarshall却报错,不求甚解啊。
下面是代码以及输出结果,希望高手能帮忙解答,不甚感激。
代码由JAVABEAN : Root.java , GeneralizedFieldHandler : DivDateHandler ,
Mapping File : Root.cst.xml 以及 Test File : DivCastorTest
结尾为DivCastorTest输出结果
[code="java"]

  • Root.java package com.siyuan.castor;

import java.util.Date;

/**

  • This class
  • @author Carro.Zhu
  • @version $Revision$, $Date$, $Author$, $Name$
    */
    public class Root {

    private Date date;

    public Root() {

    }

    /**

    • @return the date */ public Date getDate() { return date; }

    /**

    • @param date
    • the date to set */ public void setDate(Date date) { this.date = date; }

    @Override
    public String toString() {
    return "ROOT[Date = " + date + "]";
    }

}
[/code]
[code="java"]

  • DivDateHandler.java package com.siyuan.castor.handler;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.exolab.castor.mapping.GeneralizedFieldHandler;

/**

  • This class
  • @author Carro.Zhu
  • @version $Revision$, $Date$, $Author$, $Name$
    */
    public class DivDateHandler extends GeneralizedFieldHandler {

    private static final String LOCAL_DATE_FORMAT = "yyyy-MM-dd";

    @Override
    public Object convertUponGet(Object value) {
    if (value == null)
    return null;
    DateFormat dateFmt = new SimpleDateFormat(LOCAL_DATE_FORMAT);
    return dateFmt.format((Date) value);
    }

    @Override
    public Object convertUponSet(Object value) {
    if (value == null)
    return null;
    DateFormat dateFmt = new SimpleDateFormat(LOCAL_DATE_FORMAT);
    Date date = null;
    try {
    date = dateFmt.parse((String) value);
    } catch (ParseException e) {
    throw new IllegalArgumentException(e);
    }
    return date;
    }

    @Override
    public Class getFieldType() {
    return Date.class;
    }

}
[/code]
[code="xml"]
<?xml version="1.0"?>


handler="com.siyuan.castor.handler.DivDateHandler">




[/code]
[code="java"]

  • DivCastorTest.java package com.siyuan.castor.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Date;

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.xml.sax.InputSource;

import com.siyuan.castor.Root;

/**

  • This class
  • @author Carro.Zhu
  • @version $Revision$, $Date$, $Author$, $Name$
    */
    public class DivCastorTest {

    /**

    • @param args
      */
      public static void main(String[] args) {
      Root root = new Root();
      root.setDate(new Date());

      Mapping mapping = new Mapping();
      InputStream mappingFileIn = DivCastorTest.class
      .getResourceAsStream("/com/siyuan/castor/Root.cst.xml");
      mapping.loadMapping(new InputSource(mappingFileIn));

      Marshaller marshaller = new Marshaller();
      StringWriter result = new StringWriter();
      Unmarshaller unmarshaller = new Unmarshaller();
      try {
      marshaller.setMapping(mapping);
      marshaller.setWriter(result);
      marshaller.marshal(root);
      System.out.println(result);

      unmarshaller.setMapping(mapping);
      Root rootParsed = (Root) unmarshaller.unmarshal(new StringReader(
              result.toString()));
      System.out.println(rootParsed);
      

      } catch (MappingException e) {
      e.printStackTrace();
      } catch (MarshalException e) {
      e.printStackTrace();
      } catch (ValidationException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }

    }

}
[/code]
输出结果:
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>

org.exolab.castor.xml.MarshalException: Bad DateTime format: 2011-08-02
DateTime is not long enough{File: ; line: 2; column: 26}
at org.exolab.castor.xml.Unmarshaller.convertSAXExceptionToMarshalException(Unmarshaller.java:755)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:721)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:610)
at com.siyuan.castor.test.DivCastorTest.main(DivCastorTest.java:55)
Caused by: java.lang.IllegalStateException: Bad DateTime format: 2011-08-02
DateTime is not long enough
at org.exolab.castor.xml.handlers.DateFieldHandler.setValue(DateFieldHandler.java:173)
at org.exolab.castor.xml.UnmarshalHandler.processAttribute(UnmarshalHandler.java:3026)
at org.exolab.castor.xml.UnmarshalHandler.processAttributes(UnmarshalHandler.java:2676)
at org.exolab.castor.xml.UnmarshalHandler.startElement(UnmarshalHandler.java:1638)
at org.exolab.castor.xml.UnmarshalHandler.startElement(UnmarshalHandler.java:1353)
at org.apache.xerces.parsers.SAXParser.startElement(SAXParser.java:1376)
at org.apache.xerces.validators.common.XMLValidator.callStartElement(XMLValidator.java:1284)
at org.apache.xerces.framework.XMLDocumentScanner.scanElement(XMLDocumentScanner.java:1806)
at org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:949)
at org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381)
at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1098)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:709)
... 2 more
Caused by: java.lang.IllegalStateException: Bad DateTime format: 2011-08-02
DateTime is not long enough
at org.exolab.castor.xml.handlers.DateFieldHandler.setValue(DateFieldHandler.java:173)
at org.exolab.castor.xml.UnmarshalHandler.processAttribute(UnmarshalHandler.java:3026)
at org.exolab.castor.xml.UnmarshalHandler.processAttributes(UnmarshalHandler.java:2676)
at org.exolab.castor.xml.UnmarshalHandler.startElement(UnmarshalHandler.java:1638)
at org.exolab.castor.xml.UnmarshalHandler.startElement(UnmarshalHandler.java:1353)
at org.apache.xerces.parsers.SAXParser.startElement(SAXParser.java:1376)
at org.apache.xerces.validators.common.XMLValidator.callStartElement(XMLValidator.java:1284)
at org.apache.xerces.framework.XMLDocumentScanner.scanElement(XMLDocumentScanner.java:1806)
at org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:949)
at org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381)
at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1098)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:709)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:610)
at com.siyuan.castor.test.DivCastorTest.main(DivCastorTest.java:55)
Caused by: java.lang.IllegalStateException: Bad DateTime format: 2011-08-02
DateTime is not long enough
at org.exolab.castor.xml.handlers.DateFieldHandler.setValue(DateFieldHandler.java:173)
at org.exolab.castor.xml.UnmarshalHandler.processAttribute(UnmarshalHandler.java:3026)
at org.exolab.castor.xml.UnmarshalHandler.processAttributes(UnmarshalHandler.java:2676)
at org.exolab.castor.xml.UnmarshalHandler.startElement(UnmarshalHandler.java:1638)
at org.exolab.castor.xml.UnmarshalHandler.startElement(UnmarshalHandler.java:1353)
at org.apache.xerces.parsers.SAXParser.startElement(SAXParser.java:1376)
at org.apache.xerces.validators.common.XMLValidator.callStartElement(XMLValidator.java:1284)
at org.apache.xerces.framework.XMLDocumentScanner.scanElement(XMLDocumentScanner.java:1806)
at org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:949)
at org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381)
at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1098)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:709)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:610)
at com.siyuan.castor.test.DivCastorTest.main(DivCastorTest.java:55)
[/code]

  • 写回答

3条回答 默认 最新

  • iteye_4873 2011-08-02 11:30
    关注

    或者将
    [code="xml"]
    <?xml version="1.0"?>





    handler="com.siyuan.castor.handler.DivDateHandler">









    [/code]
    改成
    [code="xml"]
    <?xml version="1.0"?>





    handler="com.siyuan.castor.handler.DivDateHandler">









    [/code]
    ,其他不改试试。
    可参考
    http://jira.codehaus.org/browse/CASTOR-1878

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)