Skip to content

Commit 984e914

Browse files
committed
SiteParser: introduce an interface.
Addressed to #801 No functional changes.
1 parent 8f04801 commit 984e914

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

src/main/java/ru/mystamps/web/config/EventsConfig.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
// CheckStyle: ignore AvoidStarImportCheck for next 1 line
4343
import ru.mystamps.web.controller.event.*; // NOPMD: UnusedImports (false positive)
4444
import ru.mystamps.web.util.extractor.JsoupSiteParser;
45+
import ru.mystamps.web.util.extractor.SiteParser;
4546

4647
@Configuration
4748
@RequiredArgsConstructor
@@ -56,10 +57,10 @@ public class EventsConfig {
5657

5758
@PostConstruct
5859
public void init() {
59-
Map<Integer, JsoupSiteParser> parsers = createSiteParsers();
60-
for (Map.Entry<Integer, JsoupSiteParser> entry : parsers.entrySet()) {
60+
Map<Integer, SiteParser> parsers = createSiteParsers();
61+
for (Map.Entry<Integer, SiteParser> entry : parsers.entrySet()) {
6162
Integer num = entry.getKey();
62-
JsoupSiteParser parser = entry.getValue();
63+
SiteParser parser = entry.getValue();
6364
if (!parser.isFullyInitialized()) {
6465
LOG.warn("Ignored non-fully initialized site parser (app.site-parser[{}])", num);
6566
continue;
@@ -89,7 +90,7 @@ public ApplicationListener<DownloadingFailed> getDownloadingFailedEventListener(
8990

9091
@Bean
9192
public ApplicationListener<DownloadingSucceeded> getDownloadingSucceededEventListener(
92-
List<JsoupSiteParser> siteParsers) {
93+
List<SiteParser> siteParsers) {
9394

9495
return new DownloadingSucceededEventListener(
9596
LoggerFactory.getLogger(DownloadingSucceededEventListener.class),
@@ -108,9 +109,9 @@ public ApplicationListener<ParsingFailed> getParsingFailedEventListener() {
108109
}
109110

110111
@SuppressWarnings("PMD.ModifiedCyclomaticComplexity") // TODO: deal with it someday
111-
private Map<Integer, JsoupSiteParser> createSiteParsers() {
112+
private Map<Integer, SiteParser> createSiteParsers() {
112113
boolean foundSiteParserProps = false;
113-
Map<Integer, JsoupSiteParser> parsers = new HashMap<>();
114+
Map<Integer, SiteParser> parsers = new HashMap<>();
114115

115116
for (PropertySource<?> source : env.getPropertySources()) {
116117
// while we expect that properties will be in PropertiesPropertySource, we use
@@ -140,7 +141,7 @@ private Map<Integer, JsoupSiteParser> createSiteParsers() {
140141

141142
Integer num = Integer.valueOf(strNum);
142143
if (!parsers.containsKey(num)) {
143-
JsoupSiteParser parser =
144+
SiteParser parser =
144145
new JsoupSiteParser(); // NOPMD: AvoidInstantiatingObjectsInLoops
145146
parsers.put(num, parser);
146147
}
@@ -152,7 +153,7 @@ private Map<Integer, JsoupSiteParser> createSiteParsers() {
152153
continue;
153154
}
154155

155-
JsoupSiteParser parser = parsers.get(num);
156+
SiteParser parser = parsers.get(num);
156157
boolean validProperty = parser.setField(fieldName, propertyValue);
157158
if (!validProperty) {
158159
LOG.warn("Ignored property '{}': unknown or unsupported", name);

src/main/java/ru/mystamps/web/controller/event/DownloadingSucceededEventListener.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import ru.mystamps.web.service.SeriesImportService;
3232
import ru.mystamps.web.service.dto.RawParsedDataDto;
3333
import ru.mystamps.web.util.extractor.SeriesInfo;
34-
import ru.mystamps.web.util.extractor.JsoupSiteParser;
34+
import ru.mystamps.web.util.extractor.SiteParser;
3535

3636
/**
3737
* Listener of the @{link DownloadingSucceeded} event.
@@ -44,7 +44,7 @@ public class DownloadingSucceededEventListener
4444

4545
private final Logger log;
4646
private final SeriesImportService importService;
47-
private final List<JsoupSiteParser> siteParsers;
47+
private final List<SiteParser> siteParsers;
4848
private final ApplicationEventPublisher eventPublisher;
4949

5050
@PostConstruct
@@ -66,8 +66,8 @@ public void onApplicationEvent(DownloadingSucceeded event) {
6666
}
6767

6868
String url = event.getUrl();
69-
JsoupSiteParser parser = null;
70-
for (JsoupSiteParser candidate : siteParsers) {
69+
SiteParser parser = null;
70+
for (SiteParser candidate : siteParsers) {
7171
if (candidate.canParse(url)) {
7272
parser = candidate;
7373
break;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
// Getters/setters are being used in unit tests
3636
@Getter(AccessLevel.PROTECTED)
3737
@Setter(AccessLevel.PROTECTED)
38-
public class JsoupSiteParser {
38+
public class JsoupSiteParser implements SiteParser {
3939
private static final Logger LOG = LoggerFactory.getLogger(JsoupSiteParser.class);
4040

4141
// When you're adding a new field don't forget to also update:
@@ -51,6 +51,7 @@ public class JsoupSiteParser {
5151
private String imageUrlAttribute;
5252
private String issueDateLocator;
5353

54+
@Override
5455
public boolean setField(String name, String value) {
5556
Validate.validState(StringUtils.isNotBlank(name), "Field name must be non-blank");
5657
Validate.validState(StringUtils.isNotBlank(value), "Field value must be non-blank");
@@ -99,6 +100,7 @@ public boolean setField(String name, String value) {
99100
return valid;
100101
}
101102

103+
@Override
102104
public boolean isFullyInitialized() {
103105
return name != null
104106
&& matchedUrl != null
@@ -111,6 +113,7 @@ public boolean isFullyInitialized() {
111113
);
112114
}
113115

116+
@Override
114117
public boolean canParse(String url) {
115118
Validate.validState(url != null, "Site URL must be non-null");
116119
Validate.validState(matchedUrl != null, "Matched URL must be set");
@@ -123,6 +126,7 @@ public boolean canParse(String url) {
123126
*
124127
* @return info about a series from the document
125128
*/
129+
@Override
126130
public SeriesInfo parse(String htmlPage) {
127131
Validate.isTrue(StringUtils.isNotBlank(htmlPage), "Page content must be non-blank");
128132

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (C) 2009-2018 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.util.extractor;
19+
20+
public interface SiteParser {
21+
boolean setField(String name, String value);
22+
boolean isFullyInitialized();
23+
boolean canParse(String url);
24+
SeriesInfo parse(String htmlPage);
25+
}

0 commit comments

Comments
 (0)