blob: e465eb74e30adbf096a942af7ba13b5606350433 (
plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
package org.jsoup.parser;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
/**
* Parse tokens for the Tokeniser.
*/
abstract class Token {
TokenType type;
private Token() {
}
String tokenType() {
return this.getClass().getSimpleName();
}
static class Doctype extends Token {
final StringBuilder name = new StringBuilder();
final StringBuilder publicIdentifier = new StringBuilder();
final StringBuilder systemIdentifier = new StringBuilder();
boolean forceQuirks = false;
Doctype() {
type = TokenType.Doctype;
}
String getName() {
return name.toString();
}
String getPublicIdentifier() {
return publicIdentifier.toString();
}
public String getSystemIdentifier() {
return systemIdentifier.toString();
}
public boolean isForceQuirks() {
return forceQuirks;
}
}
static abstract class Tag extends Token {
protected String tagName;
private String pendingAttributeName;
private String pendingAttributeValue;
boolean selfClosing = false;
Attributes attributes = new Attributes(); // todo: allow nodes to not
// have attributes
void newAttribute() {
if (pendingAttributeName != null) {
if (pendingAttributeValue == null) {
pendingAttributeValue = "";
}
Attribute attribute = new Attribute(pendingAttributeName,
pendingAttributeValue);
attributes.put(attribute);
}
pendingAttributeName = null;
pendingAttributeValue = null;
}
void finaliseTag() {
// finalises for emit
if (pendingAttributeName != null) {
// todo: check if attribute name exists; if so, drop and error
newAttribute();
}
}
String name() {
Validate.isFalse(tagName.length() == 0);
return tagName;
}
Tag name(String name) {
tagName = name;
return this;
}
boolean isSelfClosing() {
return selfClosing;
}
@SuppressWarnings({ "TypeMayBeWeakened" })
Attributes getAttributes() {
return attributes;
}
// these appenders are rarely hit in not null state-- caused by null
// chars.
void appendTagName(String append) {
tagName = tagName == null ? append : tagName.concat(append);
}
void appendTagName(char append) {
appendTagName(String.valueOf(append));
}
void appendAttributeName(String append) {
pendingAttributeName = pendingAttributeName == null ? append
: pendingAttributeName.concat(append);
}
void appendAttributeName(char append) {
appendAttributeName(String.valueOf(append));
}
void appendAttributeValue(String append) {
pendingAttributeValue = pendingAttributeValue == null ? append
: pendingAttributeValue.concat(append);
}
void appendAttributeValue(char append) {
appendAttributeValue(String.valueOf(append));
}
}
static class StartTag extends Tag {
StartTag() {
super();
type = TokenType.StartTag;
}
StartTag(String name) {
this();
tagName = name;
}
StartTag(String name, Attributes attributes) {
this();
tagName = name;
this.attributes = attributes;
}
@Override
public String toString() {
return "<" + name() + " " + attributes.toString() + ">";
}
}
static class EndTag extends Tag {
EndTag() {
super();
type = TokenType.EndTag;
}
EndTag(String name) {
this();
tagName = name;
}
@Override
public String toString() {
return "</" + name() + " " + attributes.toString() + ">";
}
}
static class Comment extends Token {
final StringBuilder data = new StringBuilder();
Comment() {
type = TokenType.Comment;
}
String getData() {
return data.toString();
}
@Override
public String toString() {
return "<!--" + getData() + "-->";
}
}
static class Character extends Token {
private final String data;
Character(String data) {
type = TokenType.Character;
this.data = data;
}
String getData() {
return data;
}
@Override
public String toString() {
return getData();
}
}
static class EOF extends Token {
EOF() {
type = Token.TokenType.EOF;
}
}
boolean isDoctype() {
return type == TokenType.Doctype;
}
Doctype asDoctype() {
return (Doctype) this;
}
boolean isStartTag() {
return type == TokenType.StartTag;
}
StartTag asStartTag() {
return (StartTag) this;
}
boolean isEndTag() {
return type == TokenType.EndTag;
}
EndTag asEndTag() {
return (EndTag) this;
}
boolean isComment() {
return type == TokenType.Comment;
}
Comment asComment() {
return (Comment) this;
}
boolean isCharacter() {
return type == TokenType.Character;
}
Character asCharacter() {
return (Character) this;
}
boolean isEOF() {
return type == TokenType.EOF;
}
enum TokenType {
Doctype, StartTag, EndTag, Comment, Character, EOF
}
}
|