Skip to content

Commit 44c3e73

Browse files
committed
JsoupSiteParser.getTextOfTheFirstElement(): introduce method to reduce duplication between extractQuantity() and extractPerforated().
Fix #782 No functional changes.
1 parent 0dac42b commit 44c3e73

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

src/main/java/ru/mystamps/web/util/extractor/JsoupSiteParser.java

+19-31
Original file line numberDiff line numberDiff line change
@@ -153,32 +153,24 @@ public String toString() {
153153

154154
protected String extractCategory(Element body) {
155155
String locator = ObjectUtils.firstNonNull(categoryLocator, shortDescriptionLocator);
156-
if (locator == null) {
157-
return null;
158-
}
159156

160-
Element elem = body.selectFirst(locator);
161-
if (elem == null) {
157+
String category = getTextOfTheFirstElement(body, locator);
158+
if (category == null) {
162159
return null;
163160
}
164161

165-
String category = elem.text();
166162
LOG.debug("Extracted category: '{}'", category);
167163
return category;
168164
}
169165

170166
protected String extractCountry(Element body) {
171167
String locator = ObjectUtils.firstNonNull(countryLocator, shortDescriptionLocator);
172-
if (locator == null) {
173-
return null;
174-
}
175168

176-
Element elem = body.selectFirst(locator);
177-
if (elem == null) {
169+
String country = getTextOfTheFirstElement(body, locator);
170+
if (country == null) {
178171
return null;
179172
}
180173

181-
String country = elem.text();
182174
LOG.debug("Extracted country: '{}'", country);
183175
return country;
184176
}
@@ -201,39 +193,37 @@ protected String extractImageUrl(Element body) {
201193

202194
protected String extractIssueDate(Element body) {
203195
String locator = ObjectUtils.firstNonNull(issueDateLocator, shortDescriptionLocator);
204-
if (locator == null) {
205-
return null;
206-
}
207196

208-
Element elem = body.selectFirst(locator);
209-
if (elem == null) {
197+
String date = getTextOfTheFirstElement(body, locator);
198+
if (date == null) {
210199
return null;
211200
}
212201

213-
String date = elem.text();
214202
LOG.debug("Extracted issue date: '{}'", date);
215203
return date;
216204
}
217205

218206
protected String extractQuantity(Element body) {
219-
String locator = shortDescriptionLocator;
220-
if (locator == null) {
221-
return null;
222-
}
223-
224-
Element elem = body.selectFirst(locator);
225-
if (elem == null) {
207+
String quantity = getTextOfTheFirstElement(body, shortDescriptionLocator);
208+
if (quantity == null) {
226209
return null;
227210
}
228211

229-
String quantity = elem.text();
230212
LOG.debug("Extracted quantity: '{}'", quantity);
231213
return quantity;
232214
}
233215

234-
// @todo #782 SiteParser: reduce duplication between extractQuantity() and extractPerforated()
235216
protected String extractPerforated(Element body) {
236-
String locator = shortDescriptionLocator;
217+
String perforated = getTextOfTheFirstElement(body, shortDescriptionLocator);
218+
if (perforated == null) {
219+
return null;
220+
}
221+
222+
LOG.debug("Extracted perforated flag: '{}'", perforated);
223+
return perforated;
224+
}
225+
226+
private static String getTextOfTheFirstElement(Element body, String locator) {
237227
if (locator == null) {
238228
return null;
239229
}
@@ -243,9 +233,7 @@ protected String extractPerforated(Element body) {
243233
return null;
244234
}
245235

246-
String perforated = elem.text();
247-
LOG.debug("Extracted perforated flag: '{}'", perforated);
248-
return perforated;
236+
return elem.text();
249237
}
250238

251239
}

0 commit comments

Comments
 (0)