|
|
|
Re: Mapping arrays [message #491471 is a reply to message #488352] |
Wed, 14 October 2009 17:57 |
|
Hi Matti,
You could solve this by writing a custom AttributeAccessor. Below is an example of what it could look like.
XMLCompositeDirectCollectionMapping valueMapping = new XMLCompositeDirectCollectionMapping();
ArrayAttributeAccessor<String> arrayAttributeAccessor = new ArrayAttributeAccessor<String>(String.class);
arrayAttributeAccessor.setAttributeName("value");
valueMapping.setAttributeAccessor(arrayAttributeAccessor);
valueMapping.setXPath("valuePartB/text()");
xmlDescriptor.addMapping(valueMapping);
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Vector;
import org.eclipse.persistence.exceptions.DescriptorException;
import org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor;
public class ArrayAttributeAccessor<T> extends InstanceVariableAttributeAccessor {
private Class<T> itemType;
public ArrayAttributeAccessor(Class<T> itemType) {
this.itemType = itemType;
}
@Override
public Object getAttributeValueFromObject(Object object) throws DescriptorException {
T[] array = (T[]) super.getAttributeValueFromObject(object);
Vector<T> vector = new Vector<T>(array.length);
for(T t : array) {
vector.add(t);
}
return vector;
}
@Override
public void setAttributeValueInObject(Object object, Object value) throws DescriptorException {
Vector<T> vector = (Vector<T>) value;
T[] array = (T[]) Array.newInstance(itemType, vector.size());
vector.toArray(array);
super.setAttributeValueInObject(object, array);
}
}
-Blaise
|
|
|
|
Powered by
FUDForum. Page generated in 0.02343 seconds