|
| 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