1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.transform.pull;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Objects;
25
26 import org.codehaus.plexus.util.xml.pull.XmlPullParser;
27 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
28
29
30
31
32
33
34
35 public abstract class NodeBufferingParser extends BufferingParser {
36
37 private final List<Event> buffer = new ArrayList<>();
38
39 private final String nodeName;
40
41 private boolean buffering;
42
43 public NodeBufferingParser(XmlPullParser xmlPullParser, String nodeName) {
44 super(xmlPullParser);
45 this.nodeName = Objects.requireNonNull(nodeName);
46 }
47
48 @Override
49 protected boolean accept() throws XmlPullParserException, IOException {
50 if (nodeName.equals(xmlPullParser.getName())) {
51 if (xmlPullParser.getEventType() == START_TAG && !buffering) {
52 buffer.add(bufferEvent());
53 buffering = true;
54 return false;
55 }
56 if (xmlPullParser.getEventType() == END_TAG && buffering) {
57 buffer.add(bufferEvent());
58 process(buffer);
59 buffering = false;
60 buffer.clear();
61 return false;
62 }
63 } else if (buffering) {
64 buffer.add(bufferEvent());
65 return false;
66 }
67 return true;
68 }
69
70 protected abstract void process(List<Event> buffer);
71 }