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 package org.apache.hc.client5.http.psl;
28
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.Reader;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import org.apache.hc.core5.annotation.Contract;
36 import org.apache.hc.core5.annotation.ThreadingBehavior;
37
38
39
40
41
42
43
44 @Contract(threading = ThreadingBehavior.STATELESS)
45 public final class PublicSuffixListParser {
46
47 public PublicSuffixListParser() {
48 }
49
50
51
52
53
54
55
56
57
58
59 public PublicSuffixList parse(final Reader reader) throws IOException {
60 final List<String> rules = new ArrayList<>();
61 final List<String> exceptions = new ArrayList<>();
62 final BufferedReader r = new BufferedReader(reader);
63
64 String line;
65 while ((line = r.readLine()) != null) {
66 if (line.isEmpty()) {
67 continue;
68 }
69 if (line.startsWith("//")) {
70 continue;
71 }
72 if (line.startsWith(".")) {
73 line = line.substring(1);
74 }
75
76 final boolean isException = line.startsWith("!");
77 if (isException) {
78 line = line.substring(1);
79 }
80
81 if (isException) {
82 exceptions.add(line);
83 } else {
84 rules.add(line);
85 }
86 }
87 return new PublicSuffixList(DomainType.UNKNOWN, rules, exceptions);
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101 public List<PublicSuffixList> parseByType(final Reader reader) throws IOException {
102 final List<PublicSuffixList> result = new ArrayList<>(2);
103
104 final BufferedReader r = new BufferedReader(reader);
105
106 DomainType domainType = null;
107 List<String> rules = null;
108 List<String> exceptions = null;
109 String line;
110 while ((line = r.readLine()) != null) {
111 if (line.isEmpty()) {
112 continue;
113 }
114 if (line.startsWith("//")) {
115
116 if (domainType == null) {
117 if (line.contains("===BEGIN ICANN DOMAINS===")) {
118 domainType = DomainType.ICANN;
119 } else if (line.contains("===BEGIN PRIVATE DOMAINS===")) {
120 domainType = DomainType.PRIVATE;
121 }
122 } else {
123 if (line.contains("===END ICANN DOMAINS===") || line.contains("===END PRIVATE DOMAINS===")) {
124 if (rules != null) {
125 result.add(new PublicSuffixList(domainType, rules, exceptions));
126 }
127 domainType = null;
128 rules = null;
129 exceptions = null;
130 }
131 }
132
133 continue;
134 }
135 if (domainType == null) {
136 continue;
137 }
138
139 if (line.startsWith(".")) {
140 line = line.substring(1);
141 }
142
143 final boolean isException = line.startsWith("!");
144 if (isException) {
145 line = line.substring(1);
146 }
147
148 if (isException) {
149 if (exceptions == null) {
150 exceptions = new ArrayList<>();
151 }
152 exceptions.add(line);
153 } else {
154 if (rules == null) {
155 rules = new ArrayList<>();
156 }
157 rules.add(line);
158 }
159 }
160 return result;
161 }
162
163 }