| 1 | package org.springframework.batch.item.xml.stax; |
| 2 | |
| 3 | import java.util.NoSuchElementException; |
| 4 | |
| 5 | import javax.xml.stream.XMLEventReader; |
| 6 | import javax.xml.stream.XMLStreamException; |
| 7 | import javax.xml.stream.events.XMLEvent; |
| 8 | |
| 9 | import org.springframework.beans.factory.InitializingBean; |
| 10 | import org.springframework.util.Assert; |
| 11 | |
| 12 | /** |
| 13 | * Class used to wrap XMLEventReader. Events from wrapped reader are stored in |
| 14 | * {@link EventSequence} to support transactions. |
| 15 | * |
| 16 | * @deprecated no longer used, to be removed in 2.0 |
| 17 | * |
| 18 | * @author Tomas Slanina |
| 19 | * @author Robert Kasanicky |
| 20 | */ |
| 21 | public class DefaultTransactionalEventReader extends AbstractEventReaderWrapper implements TransactionalEventReader, InitializingBean { |
| 22 | |
| 23 | private EventSequence recorder = new EventSequence(); |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * Creates instance of this class and wraps XMLEventReader. |
| 28 | * |
| 29 | * @param wrappedReader event reader to be wrapped. |
| 30 | */ |
| 31 | public DefaultTransactionalEventReader(XMLEventReader wrappedReader) { |
| 32 | super(wrappedReader); |
| 33 | } |
| 34 | |
| 35 | public void afterPropertiesSet() throws Exception { |
| 36 | Assert.notNull(wrappedEventReader); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Callback on transaction rollback. |
| 41 | */ |
| 42 | public void onRollback() { |
| 43 | recorder.reset(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Callback on transacion commit. |
| 48 | * |
| 49 | */ |
| 50 | public void onCommit() { |
| 51 | recorder.clear(); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Check if there are more events. Returns true if there are more events and |
| 57 | * false otherwise. |
| 58 | * |
| 59 | * @return true if the event reader has more events, false otherwise |
| 60 | */ |
| 61 | public boolean hasNext() { |
| 62 | return recorder.hasNext() || wrappedEventReader.hasNext(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get the next XMLEvent |
| 67 | * |
| 68 | * @see XMLEvent |
| 69 | * @throws XMLStreamException if there is an error with the underlying XML. |
| 70 | * @throws NoSuchElementException iteration has no more elements. |
| 71 | */ |
| 72 | public XMLEvent nextEvent() throws XMLStreamException { |
| 73 | if (!recorder.hasNext()) { |
| 74 | recorder.addEvent(wrappedEventReader.nextEvent()); |
| 75 | } |
| 76 | return recorder.nextEvent(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Check the next XMLEvent without reading it from the stream. Returns null |
| 81 | * if the stream is at EOF or has no more XMLEvents. A call to peek() will |
| 82 | * be equal to the next return of next(). |
| 83 | * |
| 84 | * @see XMLEvent |
| 85 | * @throws XMLStreamException |
| 86 | */ |
| 87 | public XMLEvent peek() throws XMLStreamException { |
| 88 | return (recorder.hasNext()) ? recorder.peek() : wrappedEventReader.peek(); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * In this implementation throws UnsupportedOperationException. |
| 94 | */ |
| 95 | public void remove() { |
| 96 | throw new UnsupportedOperationException(); |
| 97 | } |
| 98 | } |