Thanks for the tip. I was able to write my version of XmlUtils.deepCopy that works for objects without the XmlRootElement anotation.
Not sure if it is the best way to do that, but the following works for me:
- Code: Select all
private <T> T deepCopy(T value) {
try {
JAXBElement<?> elem;
Class<?> valueClass;
if (value instanceof JAXBElement<?>) {
elem = (JAXBElement<?>) value;
valueClass = elem.getDeclaredType();
} else {
@SuppressWarnings("unchecked")
Class<T> classT = (Class<T>) value.getClass();
elem = new JAXBElement<T>(
new QName("temp"), classT, value);
valueClass = classT;
}
Marshaller mar = Context.jc.createMarshaller();
ByteArrayOutputStream bout = new ByteArrayOutputStream(256);
mar.marshal(elem, bout);
Unmarshaller unmar = Context.jc.createUnmarshaller();
elem = unmar.unmarshal(
new StreamSource(
new ByteArrayInputStream(bout.toByteArray())),
valueClass);
T res;
if (value instanceof JAXBElement<?>) {
@SuppressWarnings("unchecked")
T resT = (T) elem;
res = resT;
} else {
@SuppressWarnings("unchecked")
T resT = (T) elem.getValue();
res = resT;
}
return res;
} catch (JAXBException ex) {
throw new IllegalArgumentException(ex);
}
}
I suitable, feel free to take it over into the XmlUtils class of docx4j. But I've not tested it very much, just with the classes I need in my current context (TblPr childs).
Regards
Holger