1 | package org.springframework.batch.item.xml.stax; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.List; |
5 | |
6 | import javax.xml.stream.events.XMLEvent; |
7 | |
8 | /** |
9 | * Holds a list of XML events, typically corresponding to a single record. |
10 | * |
11 | * @author tomas.slanina |
12 | */ |
13 | class EventSequence { |
14 | |
15 | private static final int BEFORE_BEGINNING = -1; |
16 | |
17 | private List events; |
18 | |
19 | private int currentIndex; |
20 | |
21 | /** |
22 | * Creates instance of this class. |
23 | * |
24 | */ |
25 | public EventSequence() { |
26 | init(); |
27 | } |
28 | |
29 | /** |
30 | * Adds event to the list of stored events. |
31 | * |
32 | * @param event |
33 | */ |
34 | public void addEvent(XMLEvent event) { |
35 | events.add(event); |
36 | } |
37 | |
38 | /** |
39 | * Gets next XMLEvent from cache and moves cursor to next event. |
40 | * If cache contains no more events, null is returned. |
41 | */ |
42 | public XMLEvent nextEvent() { |
43 | return (hasNext()) ? (XMLEvent)events.get(++currentIndex) :null; |
44 | } |
45 | |
46 | /** |
47 | * Gets next XMLEvent from cache but cursor remains on the same position. |
48 | * If cache contains no more events, null is returned. |
49 | */ |
50 | public XMLEvent peek() { |
51 | return (hasNext()) ? (XMLEvent)events.get(currentIndex+1) :null; |
52 | } |
53 | |
54 | /** |
55 | * Removes events from the internal cache. |
56 | * |
57 | */ |
58 | public void clear() { |
59 | init(); |
60 | } |
61 | |
62 | /** |
63 | * Resets cursor to the cache start. |
64 | * |
65 | */ |
66 | public void reset() { |
67 | currentIndex = BEFORE_BEGINNING; |
68 | } |
69 | |
70 | /** |
71 | * Check if there are more events. Returns true if there are more events and |
72 | * false otherwise. |
73 | * |
74 | * @return true if the event reader has more events, false otherwise |
75 | */ |
76 | public boolean hasNext() { |
77 | return currentIndex + 1 < events.size(); |
78 | } |
79 | |
80 | private void init() { |
81 | events = (events != null) ? new ArrayList(events.size()) |
82 | : new ArrayList(1000); |
83 | |
84 | reset(); |
85 | } |
86 | |
87 | |
88 | } |