Castor使用之:映射Clob字段到xml文件

在使用Castor将java对象转换为xml文件时,会遇到Clob类型的属性的转换。Castor中没有Clob类型的处理器,因此需要我们自己实现一个ClobFieldHandler。

例如:我们要将Root.java转换成一个xml文件。

1.Root.java
[code]
package lab.castor.lab01;

import java.sql.Clob;

public class Root {
private Clob comment;

public Clob getComment() {
return comment;
}

public void setComment(Clob comment) {
this.comment = comment;
}
}

[/code]

2.mapping.xml

[code]

<?xml version="1.0"?>
<mapping>
<class name="lab.castor.lab01.Root">
<field name="comment" type="string" handler="lab.castor.lab01.ClobFieldHandler">
<bind-xml node="comment"/>
</field>
</class>
</mapping>
[/code]


3.ClobFieldHandler.java

[code]
package lab.castor.lab01;

import java.io.Reader;
import java.sql.Clob;

import org.exolab.castor.mapping.GeneralizedFieldHandler;

public class ClobFieldHandler extends GeneralizedFieldHandler {

/**
* Creates a new ClobFieldHandler instance
*/
public ClobFieldHandler() {
super();
}

/**
* This method is used to convert the value when the getValue method is called. The getValue method will obtain the
* actual field value from given 'parent' object. This convert method is then invoked with the field's value. The
* value returned from this method will be the actual value returned by getValue method.
*
* @param value the object value to convert after performing a get operation
* @return the converted value.
*/
public Object convertUponGet(Object value) {
if (value == null) {
return null;
}

StringBuffer sb = new StringBuffer();
try {
Clob clob = (Clob) value;
Reader reader = clob.getCharacterStream();
if (reader == null) {
return null;
}

char[] charbuf = new char[4096];
// 解决4096字节大小的限制
for (int i = reader.read(charbuf); i > 0; i = reader.read(charbuf)) {
sb.append(charbuf, 0, i);
}
} catch (Exception e) {
//
}
return sb.toString();
}

/**
* This method is used to convert the value when the setValue method is called. The setValue method will call this
* method to obtain the converted value. The converted value will then be used as the value to set for the field.
*
* @param value the object value to convert before performing a set operation
* @return the converted value.
*/
public Object convertUponSet(Object value) {
return null;
}

/**
* Returns the class type for the field that this GeneralizedFieldHandler converts to and from. This should be the
* type that is used in the object model.
*
* @return the class type of of the field
*/
public Class getFieldType() {
return Clob.class;
}

/**
* Creates a new instance of the object described by this field.
*
* @param parent The object for which the field is created
* @return A new instance of the field's value
* @throws IllegalStateException This field is a simple type and cannot be instantiated
*/
public Object newInstance(Object parent) throws IllegalStateException {
// -- Since it's marked as a string...just return null,
// -- it's not needed.
return null;
}

}
[/code]

没有实现String到Clob的转换。
[url=http://www.51.la/?1613417][img]http://img.users.51.la/1613417.asp[/img][/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值