/
TemplateParser.java
569 lines (483 loc) · 15.9 KB
/
TemplateParser.java
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package chan.text;
import android.util.Pair;
import chan.annotation.Extendable;
import chan.annotation.Public;
import chan.util.CommonUtils;
import java.util.ArrayList;
import java.util.HashMap;
// TODO CHAN
// Remove this class and ChanManger.CompatPathClassLoader after updating
// alphachan alterchan brchan chaosach chiochan dangeru exach fiftyfive fourplebs haibane kropyvach lainchan lolifox
// nulltirech onechanca ponyach randomarchive sevenchan synch taima tiretirech twentyseven uboachan valkyria wizardchan
// Added: 24.09.20 08:23
@SuppressWarnings("ALL")
@Public
public final class TemplateParser<H> {
private final HashMap<String, ArrayList<AttributeMatcher<H>>> openMatchers = new HashMap<>();
private final HashMap<String, ArrayList<AttributeMatcher<H>>> closeMatchers = new HashMap<>();
private final ArrayList<TextCallback<H>> textCallbacks = new ArrayList<>();
private boolean ready;
private final ArrayList<Pair<String, AttributeMatcher<H>>> buildingMatchers = new ArrayList<>();
private OpenCallback<H> openCallback;
private ContentCallback<H> contentCallback;
private CloseCallback<H> closeCallback;
public TemplateParser() {}
public static <H> InitialBuilder<H> builder() {
return new TemplateParser<H>().contentBuilder;
}
private static class AttributeMatcher<H> {
public enum Method {EQUALS, STARTS, CONTAINS, ENDS}
private final String attribute;
private final String value;
private final Method method;
public OpenCallback<H> openCallback;
public ContentCallback<H> contentCallback;
public CloseCallback<H> closeCallback;
public AttributeMatcher(String attribute, String value, Method method) {
this.attribute = attribute;
this.value = value;
this.method = method;
}
public boolean match(Attributes attributes) {
if (method == null) {
return true;
}
String value = attributes.get(attribute);
switch (method) {
case EQUALS: {
return CommonUtils.equals(value, this.value);
}
case STARTS: {
return value != null && value.startsWith(this.value);
}
case CONTAINS: {
return value != null && value.contains(this.value);
}
case ENDS: {
return value != null && value.endsWith(this.value);
}
}
throw new RuntimeException();
}
}
private void copyCallbacks() {
if (openCallback != null || contentCallback != null || closeCallback != null) {
if ((openCallback != null || contentCallback != null) && closeCallback != null) {
throw new IllegalStateException("OpenCallback and ContentCallback can not be defined "
+ "with CloseCallback at once");
}
for (Pair<String, AttributeMatcher<H>> pair : buildingMatchers) {
if (closeCallback != null && pair.second.attribute != null) {
throw new IllegalStateException("Attributed tag definition is not supported for closing tags");
}
HashMap<String, ArrayList<AttributeMatcher<H>>> map = closeCallback != null
? closeMatchers : openMatchers;
ArrayList<AttributeMatcher<H>> matchers = map.get(pair.first);
if (matchers == null) {
matchers = new ArrayList<>();
map.put(pair.first, matchers);
}
pair.second.openCallback = openCallback;
pair.second.contentCallback = contentCallback;
pair.second.closeCallback = closeCallback;
matchers.add(pair.second);
}
buildingMatchers.clear();
openCallback = null;
contentCallback = null;
closeCallback = null;
}
}
private void normalize() {
for (ArrayList<AttributeMatcher<H>> matchers : openMatchers.values()) {
for (int i = 0, j = matchers.size(); i < j; i++) {
AttributeMatcher<H> matcher = matchers.get(i);
if (matcher.attribute == null) {
// Move to end
matchers.remove(i);
matchers.add(matcher);
j--;
}
}
}
}
private void checkReady() {
if (ready) {
throw new IllegalStateException("You can not call this method after prepare() call");
}
}
public TemplateParser<H> name(String tagName) {
return tag(tagName, null, null, null);
}
public TemplateParser<H> equals(String tagName, String attribute, String value) {
return tag(tagName, attribute, value, AttributeMatcher.Method.EQUALS);
}
public TemplateParser<H> starts(String tagName, String attribute, String value) {
return tag(tagName, attribute, value, AttributeMatcher.Method.STARTS);
}
public TemplateParser<H> contains(String tagName, String attribute, String value) {
return tag(tagName, attribute, value, AttributeMatcher.Method.CONTAINS);
}
public TemplateParser<H> ends(String tagName, String attribute, String value) {
return tag(tagName, attribute, value, AttributeMatcher.Method.ENDS);
}
private TemplateParser<H> tag(String tagName, String attribute, String value, AttributeMatcher.Method method) {
checkReady();
copyCallbacks();
if (attribute == null) {
value = null;
}
buildingMatchers.add(new Pair<>(tagName, new AttributeMatcher<>(attribute, value, method)));
return this;
}
public TemplateParser<H> open(OpenCallback<H> openCallback) {
checkReady();
checkHasMatchers();
this.openCallback = openCallback;
return this;
}
public TemplateParser<H> content(ContentCallback<H> contentCallback) {
checkReady();
checkHasMatchers();
this.contentCallback = contentCallback;
return this;
}
public TemplateParser<H> close(CloseCallback<H> closeCallback) {
checkReady();
checkHasMatchers();
this.closeCallback = closeCallback;
return this;
}
private void checkHasMatchers() {
if (buildingMatchers.isEmpty()) {
throw new IllegalStateException("You must define at least one parsing rule before adding this callback");
}
}
public TemplateParser<H> text(TextCallback<H> textCallback) {
checkReady();
copyCallbacks();
if (!buildingMatchers.isEmpty()) {
throw new IllegalStateException("This callback can not be used with any parsing rules");
}
textCallbacks.add(textCallback);
return this;
}
public TemplateParser<H> prepare() {
checkReady();
copyCallbacks();
normalize();
ready = true;
return this;
}
@Public
public void parse(String source, H holder) throws ParseException {
if (!ready) {
throw new IllegalStateException("prepare() was not called");
}
try {
GroupParser.parse(source, new Implementation<>(this, holder));
} catch (FinishException e) {
// finish() was called
}
}
@Public
public static final class Attributes {
private static final String NULL = "null";
private GroupParser.Attributes attributes;
private final HashMap<String, String> lastValues = new HashMap<>();
@Public
@SuppressWarnings("StringEquality")
public String get(String attribute) {
String value = lastValues.get(attribute);
if (value == null) {
value = attributes.get(attribute);
lastValues.put(attribute, value != null ? value : NULL);
}
return value == NULL ? null : value;
}
public void set(GroupParser.Attributes attributes) {
this.attributes = attributes;
lastValues.clear();
}
}
@Public
public static final class Instance {
private final Implementation implementation;
public Instance(Implementation implementation) {
this.implementation = implementation;
}
@Public
public void finish() {
implementation.finish = true;
}
}
@Extendable
public interface OpenCallback<H> {
@Extendable
public boolean onOpen(Instance instance, H holder, String tagName, Attributes attributes) throws ParseException;
}
@Extendable
public interface ContentCallback<H> {
@Extendable
public void onContent(Instance instance, H holder, String text) throws ParseException;
}
@Extendable
public interface CloseCallback<H> {
@Extendable
public void onClose(Instance instance, H holder, String tagName) throws ParseException;
}
@Extendable
public interface TextCallback<H> {
@Extendable
public void onText(Instance instance, H holder, String source, int start, int end) throws ParseException;
}
private static class FinishException extends ParseException {}
private static class Implementation<H> implements GroupParser.Callback {
private final TemplateParser<H> parser;
private final H holder;
private final Attributes attributes = new Attributes();
private final Instance instance = new Instance(this);
private AttributeMatcher<H> workMatcher;
private boolean finish = false;
public Implementation(TemplateParser<H> parser, H holder) {
this.parser = parser;
this.holder = holder;
}
private void checkFinish() throws FinishException {
if (finish) {
throw new FinishException();
}
}
@Override
public boolean onStartElement(GroupParser parser, String tagName,
GroupParser.Attributes attributes) throws ParseException {
ArrayList<AttributeMatcher<H>> matchers = this.parser.openMatchers.get(tagName);
if (matchers != null) {
this.attributes.set(attributes);
for (AttributeMatcher<H> matcher : matchers) {
if (matcher.match(this.attributes)) {
boolean readContent;
if (matcher.openCallback != null) {
readContent = matcher.openCallback.onOpen(instance, holder, tagName, this.attributes);
checkFinish();
} else {
readContent = true;
}
if (readContent) {
workMatcher = matcher;
return true;
}
}
}
}
return false;
}
@Deprecated
@Override
public boolean onStartElement(GroupParser parser, String tagName, String attrs) {
throw new IllegalStateException();
}
@Override
public void onEndElement(GroupParser parser, String tagName) throws ParseException {
ArrayList<AttributeMatcher<H>> matchers = this.parser.closeMatchers.get(tagName);
if (matchers != null) {
for (AttributeMatcher<H> matcher : matchers) {
matcher.closeCallback.onClose(instance, holder, tagName);
checkFinish();
}
}
}
@Override
public void onGroupComplete(GroupParser parser, String text) throws ParseException {
if (workMatcher.contentCallback != null) {
workMatcher.contentCallback.onContent(instance, holder, text);
checkFinish();
}
}
@Override
public void onText(GroupParser parser, CharSequence text) throws ParseException {
String textString = null;
ArrayList<TextCallback<H>> textCallbacks = this.parser.textCallbacks;
for (int i = 0, size = textCallbacks.size(); i < size; i++) {
if (textString == null) {
textString = text.toString();
}
textCallbacks.get(i).onText(instance, holder, textString, 0, textString.length());
checkFinish();
}
}
@Deprecated
@Override
public void onText(GroupParser parser, String source, int start, int end) {
throw new IllegalStateException();
}
}
@Public
public interface SimpleRuleBuilder<H> {
@Public
public SimpleBuilder<H> name(String tagName);
}
@Public
public interface ComplexSimpleRuleBuilder<H> {
@Public
public ComplexBuilder<H> name(String tagName);
}
@Public
public interface ComplexRuleBuilder<H> {
@Public
public ComplexBuilder<H> equals(String tagName, String attribute, String value);
@Public
public ComplexBuilder<H> starts(String tagName, String attribute, String value);
@Public
public ComplexBuilder<H> contains(String tagName, String attribute, String value);
@Public
public ComplexBuilder<H> ends(String tagName, String attribute, String value);
}
@Public
public interface OpenBuilder<H> {
@Public
public ContentBuilder<H> open(OpenCallback<H> openCallback);
@Public
public InitialBuilder<H> content(ContentCallback<H> contentCallback);
}
@Public
public interface InitialBuilder<H> extends SimpleRuleBuilder<H>, ComplexRuleBuilder<H> {
@Public
public InitialBuilder<H> text(TextCallback<H> textCallback);
@Public
public TemplateParser<H> prepare();
}
@Public
public interface SimpleBuilder<H> extends SimpleRuleBuilder<H>, ComplexRuleBuilder<H>, OpenBuilder<H> {
@Public
public InitialBuilder<H> close(CloseCallback<H> closeCallback);
}
@Public
public interface ComplexBuilder<H> extends ComplexSimpleRuleBuilder<H>, ComplexRuleBuilder<H>, OpenBuilder<H> {}
@Public
public interface ContentBuilder<H> extends InitialBuilder<H> {
@Public
public InitialBuilder<H> content(ContentCallback<H> contentCallback);
}
private ContentBuilder<H> contentBuilder = new ContentBuilder<H>() {
@Override
public SimpleBuilder<H> name(String tagName) {
TemplateParser.this.name(tagName);
return simpleBuilder;
}
@Override
public ComplexBuilder<H> equals(String tagName, String attribute, String value) {
TemplateParser.this.equals(tagName, attribute, value);
return complexBuilder;
}
@Override
public ComplexBuilder<H> starts(String tagName, String attribute, String value) {
TemplateParser.this.starts(tagName, attribute, value);
return complexBuilder;
}
@Override
public ComplexBuilder<H> contains(String tagName, String attribute, String value) {
TemplateParser.this.contains(tagName, attribute, value);
return complexBuilder;
}
@Override
public ComplexBuilder<H> ends(String tagName, String attribute, String value) {
TemplateParser.this.ends(tagName, attribute, value);
return complexBuilder;
}
@Override
public InitialBuilder<H> text(TextCallback<H> textCallback) {
TemplateParser.this.text(textCallback);
return contentBuilder;
}
@Override
public InitialBuilder<H> content(ContentCallback<H> contentCallback) {
TemplateParser.this.content(contentCallback);
return contentBuilder;
}
@Override
public TemplateParser<H> prepare() {
TemplateParser.this.prepare();
return TemplateParser.this;
}
};
private SimpleBuilder<H> simpleBuilder = new SimpleBuilder<H>() {
@Override
public SimpleBuilder<H> name(String tagName) {
TemplateParser.this.name(tagName);
return simpleBuilder;
}
@Override
public ComplexBuilder<H> equals(String tagName, String attribute, String value) {
TemplateParser.this.equals(tagName, attribute, value);
return complexBuilder;
}
@Override
public ComplexBuilder<H> starts(String tagName, String attribute, String value) {
TemplateParser.this.starts(tagName, attribute, value);
return complexBuilder;
}
@Override
public ComplexBuilder<H> contains(String tagName, String attribute, String value) {
TemplateParser.this.contains(tagName, attribute, value);
return complexBuilder;
}
@Override
public ComplexBuilder<H> ends(String tagName, String attribute, String value) {
TemplateParser.this.ends(tagName, attribute, value);
return complexBuilder;
}
@Override
public ContentBuilder<H> open(OpenCallback<H> openCallback) {
TemplateParser.this.open(openCallback);
return contentBuilder;
}
@Override
public InitialBuilder<H> content(ContentCallback<H> contentCallback) {
TemplateParser.this.content(contentCallback);
return contentBuilder;
}
@Override
public InitialBuilder<H> close(CloseCallback<H> closeCallback) {
TemplateParser.this.close(closeCallback);
return contentBuilder;
}
};
private ComplexBuilder<H> complexBuilder = new ComplexBuilder<H>() {
@Override
public ComplexBuilder<H> name(String tagName) {
TemplateParser.this.name(tagName);
return complexBuilder;
}
@Override
public ComplexBuilder<H> equals(String tagName, String attribute, String value) {
TemplateParser.this.equals(tagName, attribute, value);
return complexBuilder;
}
@Override
public ComplexBuilder<H> starts(String tagName, String attribute, String value) {
TemplateParser.this.starts(tagName, attribute, value);
return complexBuilder;
}
@Override
public ComplexBuilder<H> contains(String tagName, String attribute, String value) {
TemplateParser.this.contains(tagName, attribute, value);
return complexBuilder;
}
@Override
public ComplexBuilder<H> ends(String tagName, String attribute, String value) {
TemplateParser.this.ends(tagName, attribute, value);
return complexBuilder;
}
@Override
public ContentBuilder<H> open(OpenCallback<H> openCallback) {
TemplateParser.this.open(openCallback);
return contentBuilder;
}
@Override
public InitialBuilder<H> content(ContentCallback<H> contentCallback) {
TemplateParser.this.content(contentCallback);
return contentBuilder;
}
};
}