forked from subshell/simplexml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPullReader.java
More file actions
164 lines (125 loc) · 3.46 KB
/
Copy pathPullReader.java
File metadata and controls
164 lines (125 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package org.simpleframework.xml.stream;
import static org.xmlpull.v1.XmlPullParser.END_TAG;
import static org.xmlpull.v1.XmlPullParser.START_TAG;
import static org.xmlpull.v1.XmlPullParser.TEXT;
import javax.xml.stream.events.Attribute;
import org.xmlpull.v1.XmlPullParser;
class PullReader implements EventReader {
private XmlPullParser parser;
private NodeEvent peek;
public PullReader(XmlPullParser parser) {
this.parser = parser;
}
public NodeEvent peek() throws Exception {
if(peek == null) {
peek = next();
}
return peek;
}
public NodeEvent next() throws Exception {
NodeEvent next = peek;
if(next == null) {
next = read();
} else {
peek = null;
}
return next;
}
private NodeEvent read() throws Exception {
int event = parser.next();
if(event == START_TAG){
return start();
}
if(event == TEXT) {
return text();
}
if(event == END_TAG) {
return end();
}
return read();
}
private Text text() throws Exception {
return new Text(parser);
}
private Start start() throws Exception {
Start event = new Start(parser);
if(event.isEmpty()) {
return populate(event);
}
return event;
}
private Start populate(Start event) throws Exception {
int count = parser.getAttributeCount();
for(int i = 0; i < count; i++) {
Entry entry = attribute(i);
if(!entry.isReserved()) {
event.add(entry);
}
}
return event;
}
private Entry attribute(int index) throws Exception {
return new Entry(parser, index);
}
private End end() throws Exception {
return new End();
}
private class Entry extends NodeAttribute {
private final String name;
private final String value;
public Entry(XmlPullParser node, int index) {
this.value = node.getAttributeValue(index);
this.name = node.getAttributeName(index);
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
public boolean isReserved() {
return false;
}
}
private static class Start extends NodeStart {
private final String reference;
private final String prefix;
private final String name;
private final int line;
public Start(XmlPullParser parser) {
this.reference = parser.getNamespace();
this.line = parser.getLineNumber();
this.prefix = parser.getPrefix();
this.name = parser.getName();
}
public int getLine() {
return line;
}
public String getName() {
return name;
}
public String getReference() {
return reference;
}
public String getPrefix() {
return prefix;
}
}
private static class Text extends NodeValue {
private final String text;
public Text(XmlPullParser parser){
this.text = parser.getText();
}
public String getValue(){
return text;
}
public boolean isText() {
return true;
}
}
private static class End extends NodeValue {
public boolean isEnd() {
return true;
}
}
}