/
ParseSettings.java
88 lines (75 loc) · 2.33 KB
/
ParseSettings.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
package org.jsoup.parser;
import org.jsoup.nodes.Attributes;
import org.jspecify.annotations.Nullable;
import static org.jsoup.internal.Normalizer.lowerCase;
/**
* Controls parser case settings, to optionally preserve tag and/or attribute name case.
*/
public class ParseSettings {
/**
* HTML default settings: both tag and attribute names are lower-cased during parsing.
*/
public static final ParseSettings htmlDefault;
/**
* Preserve both tag and attribute case.
*/
public static final ParseSettings preserveCase;
static {
htmlDefault = new ParseSettings(false, false);
preserveCase = new ParseSettings(true, true);
}
private final boolean preserveTagCase;
private final boolean preserveAttributeCase;
/**
* Returns true if preserving tag name case.
*/
public boolean preserveTagCase() {
return preserveTagCase;
}
/**
* Returns true if preserving attribute case.
*/
public boolean preserveAttributeCase() {
return preserveAttributeCase;
}
/**
* Define parse settings.
* @param tag preserve tag case?
* @param attribute preserve attribute name case?
*/
public ParseSettings(boolean tag, boolean attribute) {
preserveTagCase = tag;
preserveAttributeCase = attribute;
}
ParseSettings(ParseSettings copy) {
this(copy.preserveTagCase, copy.preserveAttributeCase);
}
/**
* Normalizes a tag name according to the case preservation setting.
*/
public String normalizeTag(String name) {
name = name.trim();
if (!preserveTagCase)
name = lowerCase(name);
return name;
}
/**
* Normalizes an attribute according to the case preservation setting.
*/
public String normalizeAttribute(String name) {
name = name.trim();
if (!preserveAttributeCase)
name = lowerCase(name);
return name;
}
@Nullable Attributes normalizeAttributes(@Nullable Attributes attributes) {
if (attributes != null && !preserveAttributeCase) {
attributes.normalize();
}
return attributes;
}
/** Returns the normal name that a Tag will have (trimmed and lower-cased) */
static String normalName(String name) {
return lowerCase(name.trim());
}
}