1 | package org.springframework.batch.item.xml.oxm; |
2 | |
3 | import java.io.IOException; |
4 | |
5 | import javax.xml.stream.XMLEventReader; |
6 | |
7 | import org.springframework.batch.item.xml.EventReaderDeserializer; |
8 | import org.springframework.dao.DataAccessResourceFailureException; |
9 | import org.springframework.oxm.Unmarshaller; |
10 | import org.springframework.util.Assert; |
11 | import org.springframework.xml.transform.StaxSource; |
12 | |
13 | /** |
14 | * Delegates deserializing to Spring OXM {@link Unmarshaller}. |
15 | * |
16 | * @author Robert Kasanicky |
17 | * @author Lucas Ward |
18 | */ |
19 | public class UnmarshallingEventReaderDeserializer implements EventReaderDeserializer { |
20 | |
21 | private Unmarshaller unmarshaller; |
22 | |
23 | public UnmarshallingEventReaderDeserializer(Unmarshaller unmarshaller){ |
24 | Assert.notNull(unmarshaller); |
25 | this.unmarshaller = unmarshaller; |
26 | } |
27 | |
28 | public Object deserializeFragment(XMLEventReader eventReader) { |
29 | Object item = null; |
30 | try { |
31 | item = unmarshaller.unmarshal(new StaxSource(eventReader)); |
32 | } |
33 | catch (IOException e) { |
34 | throw new DataAccessResourceFailureException("IO error during unmarshalling", e); |
35 | } |
36 | return item; |
37 | } |
38 | } |