| 1 | package org.springframework.batch.item.xml.oxm; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import javax.xml.stream.XMLEventWriter; |
| 6 | import javax.xml.transform.Result; |
| 7 | |
| 8 | import org.springframework.batch.item.xml.EventWriterSerializer; |
| 9 | import org.springframework.dao.DataAccessResourceFailureException; |
| 10 | import org.springframework.oxm.Marshaller; |
| 11 | import org.springframework.xml.transform.StaxResult; |
| 12 | |
| 13 | /** |
| 14 | * Object to xml serializer that wraps a Spring OXM {@link Marshaller} object. |
| 15 | * |
| 16 | * @author Lucas Ward |
| 17 | * |
| 18 | */ |
| 19 | public class MarshallingEventWriterSerializer implements EventWriterSerializer { |
| 20 | |
| 21 | private Marshaller marshaller; |
| 22 | |
| 23 | public MarshallingEventWriterSerializer(Marshaller marshaller) { |
| 24 | this.marshaller = marshaller; |
| 25 | } |
| 26 | |
| 27 | public void serializeObject(XMLEventWriter writer, Object output) { |
| 28 | Result result = new StaxResult(writer); |
| 29 | try { |
| 30 | marshaller.marshal(output, result); |
| 31 | } |
| 32 | catch (IOException xse) { |
| 33 | throw new DataAccessResourceFailureException("Unable to write to file resource: [" + result.getSystemId() |
| 34 | + "]", xse); |
| 35 | } |
| 36 | } |
| 37 | } |