@@ -21,7 +21,7 @@ import org.jsoup.nodes.Document
21
21
22
22
/**
23
23
* Parse licenses from remote urls*/
24
- class RemoteLicenseFetcher implements Serializable {
24
+ abstract class RemoteLicenseFetcher implements Serializable {
25
25
private static final HtmlToPlainText TEXT_FORMATTER = new HtmlToPlainText ()
26
26
27
27
private final String remoteUrl
@@ -51,7 +51,7 @@ class RemoteLicenseFetcher implements Serializable {
51
51
Thread . sleep(i * 1000 )
52
52
}
53
53
54
- return processDocument( Jsoup . connect(remoteUrl) . get() )
54
+ return getTextAttempt( )
55
55
} catch (IOException ex) {
56
56
if (storedEx == null ) {
57
57
storedEx = ex
@@ -64,10 +64,8 @@ class RemoteLicenseFetcher implements Serializable {
64
64
throw storedEx
65
65
}
66
66
67
- /* * Extracts the license text from the rest of the document. */
68
- String processDocument (Document doc ) {
69
- return TEXT_FORMATTER . getPlainText(doc)
70
- }
67
+ /* * Attempts to download and extract the license exactly once. */
68
+ abstract String getTextAttempt ()
71
69
72
70
static final class AndroidSdkTermsFetcher extends RemoteLicenseFetcher {
73
71
@@ -76,10 +74,11 @@ class RemoteLicenseFetcher implements Serializable {
76
74
}
77
75
78
76
@Override
79
- String processDocument ( Document doc ) {
77
+ String getTextAttempt ( ) {
80
78
// TODO(vkryachko, allisonbm92): Fix this silent failure.
81
79
// This evaluates to an empty string. The HTML for this page must have changed since this
82
80
// filter was original written. Interestingly, this is a hard-failure if run from Java.
81
+ def doc = Jsoup . connect(getRemoteUrl()). get()
83
82
return TEXT_FORMATTER . getPlainText(doc. select(" #body-content > div.jd-descr > div" )[0 ])
84
83
}
85
84
}
@@ -89,6 +88,11 @@ class RemoteLicenseFetcher implements Serializable {
89
88
Apache2LicenseFetcher () {
90
89
super (" http://www.apache.org/licenses/LICENSE-2.0.txt" )
91
90
}
91
+
92
+ @Override
93
+ String getTextAttempt () {
94
+ return getRemoteUrl(). toURL(). getText()
95
+ }
92
96
}
93
97
94
98
static final class AnotherApache2LicenseFetcher extends RemoteLicenseFetcher {
@@ -98,7 +102,8 @@ class RemoteLicenseFetcher implements Serializable {
98
102
}
99
103
100
104
@Override
101
- String processDocument (Document doc ) {
105
+ String getTextAttempt () {
106
+ def doc = Jsoup . connect(getRemoteUrl()). get()
102
107
return TEXT_FORMATTER . getPlainText(doc. select(" #content-wrapper" ). get(0 ))
103
108
}
104
109
}
@@ -108,6 +113,11 @@ class RemoteLicenseFetcher implements Serializable {
108
113
YetAnotherApache2LicenseFetcher () {
109
114
super (" http://www.apache.org/licenses/LICENSE-2.0" )
110
115
}
116
+
117
+ @Override
118
+ String getTextAttempt () {
119
+ return getRemoteUrl(). toURL(). getText()
120
+ }
111
121
}
112
122
113
123
static final class BSDLicenseFetcher extends RemoteLicenseFetcher {
@@ -117,7 +127,8 @@ class RemoteLicenseFetcher implements Serializable {
117
127
}
118
128
119
129
@Override
120
- String processDocument (Document doc ) {
130
+ String getTextAttempt () {
131
+ def doc = Jsoup . connect(getRemoteUrl()). get()
121
132
return TEXT_FORMATTER . getPlainText(doc. select(" #content-wrapper" ). get(0 ))
122
133
}
123
134
}
@@ -129,7 +140,8 @@ class RemoteLicenseFetcher implements Serializable {
129
140
}
130
141
131
142
@Override
132
- String processDocument (Document doc ) {
143
+ String getTextAttempt () {
144
+ def doc = Jsoup . connect(getRemoteUrl()). get()
133
145
return TEXT_FORMATTER . getPlainText(doc. select(" #deed" ). get(0 ))
134
146
}
135
147
}
@@ -141,7 +153,8 @@ class RemoteLicenseFetcher implements Serializable {
141
153
}
142
154
143
155
@Override
144
- String processDocument (Document doc ) {
156
+ String getTextAttempt () {
157
+ def doc = Jsoup . connect(getRemoteUrl()). get()
145
158
return TEXT_FORMATTER . getPlainText(doc. select(" #content-wrapper" ). get(0 ))
146
159
}
147
160
}
@@ -153,7 +166,8 @@ class RemoteLicenseFetcher implements Serializable {
153
166
}
154
167
155
168
@Override
156
- String processDocument (Document doc ) {
169
+ String getTextAttempt () {
170
+ def doc = Jsoup . connect(getRemoteUrl()). get()
157
171
return TEXT_FORMATTER . getPlainText(doc. select(" #content-wrapper" ). get(0 ))
158
172
}
159
173
}
@@ -166,7 +180,8 @@ class RemoteLicenseFetcher implements Serializable {
166
180
}
167
181
168
182
@Override
169
- String processDocument (Document doc ) {
183
+ String getTextAttempt () {
184
+ def doc = Jsoup . connect(getRemoteUrl()). get()
170
185
return TEXT_FORMATTER . getPlainText(doc. select(" body > table > tbody > tr:nth-child(2) > td:nth-child(2) > table > tbody > tr:nth-child(3) > td > en > blockquote" ). get(0 ))
171
186
}
172
187
}
0 commit comments