Skip to content

Commit 0dac42b

Browse files
committed
Implement TimedSiteParser.
Fix #801 No functional changes.
1 parent 984e914 commit 0dac42b

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import ru.mystamps.web.controller.event.*; // NOPMD: UnusedImports (false positive)
4444
import ru.mystamps.web.util.extractor.JsoupSiteParser;
4545
import ru.mystamps.web.util.extractor.SiteParser;
46+
import ru.mystamps.web.util.extractor.TimedSiteParser;
4647

4748
@Configuration
4849
@RequiredArgsConstructor
@@ -108,7 +109,10 @@ public ApplicationListener<ParsingFailed> getParsingFailedEventListener() {
108109
);
109110
}
110111

111-
@SuppressWarnings("PMD.ModifiedCyclomaticComplexity") // TODO: deal with it someday
112+
@SuppressWarnings({
113+
"PMD.AvoidInstantiatingObjectsInLoops",
114+
"PMD.ModifiedCyclomaticComplexity" // TODO: deal with it someday
115+
})
112116
private Map<Integer, SiteParser> createSiteParsers() {
113117
boolean foundSiteParserProps = false;
114118
Map<Integer, SiteParser> parsers = new HashMap<>();
@@ -141,8 +145,13 @@ private Map<Integer, SiteParser> createSiteParsers() {
141145

142146
Integer num = Integer.valueOf(strNum);
143147
if (!parsers.containsKey(num)) {
148+
// @todo #801 EventsConfig.createSiteParsers(): split the logic for properties
149+
// parsing and the object instantiation
144150
SiteParser parser =
145-
new JsoupSiteParser(); // NOPMD: AvoidInstantiatingObjectsInLoops
151+
new TimedSiteParser(
152+
LoggerFactory.getLogger(TimedSiteParser.class),
153+
new JsoupSiteParser()
154+
);
146155
parsers.put(num, parser);
147156
}
148157

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (C) 2009-2018 Slava Semushin <[email protected]>
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+
import org.apache.commons.lang3.StringUtils;
21+
import org.apache.commons.lang3.time.StopWatch;
22+
23+
import org.slf4j.Logger;
24+
25+
import lombok.RequiredArgsConstructor;
26+
27+
// @todo #801 TimedSiteParser: add unit tests
28+
@RequiredArgsConstructor
29+
public class TimedSiteParser implements SiteParser {
30+
31+
private final Logger log;
32+
private final SiteParser parser;
33+
34+
@Override
35+
public boolean setField(String name, String value) {
36+
return parser.setField(name, value);
37+
}
38+
39+
@Override
40+
public boolean isFullyInitialized() {
41+
return parser.isFullyInitialized();
42+
}
43+
44+
@Override
45+
public boolean canParse(String url) {
46+
return parser.canParse(url);
47+
}
48+
49+
@Override
50+
public SeriesInfo parse(String htmlPage) {
51+
// Why we don't use Spring's StopWatch?
52+
// 1) because its javadoc says that it's not intended for production
53+
// 2) because we don't want to have strong dependencies on the Spring Framework
54+
StopWatch timer = new StopWatch();
55+
56+
// start() and stop() may throw IllegalStateException and in this case it's possible
57+
// that we won't generate anything or lose already generated result. I don't want to
58+
// make method body too complicated by adding many try/catches and I believe that such
59+
// exception will never happen because it would mean that we're using API in a wrong way.
60+
timer.start();
61+
SeriesInfo result = parser.parse(htmlPage);
62+
timer.stop();
63+
64+
if (result != null) {
65+
log.debug(
66+
"HTML page with {} characters has been parsed in {} msecs",
67+
StringUtils.length(htmlPage),
68+
timer.getTime()
69+
);
70+
}
71+
72+
return result;
73+
}
74+
75+
}

0 commit comments

Comments
 (0)