2 * Copyright (c) 2019-2020. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.chess.scanner.portal;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
22 import org.hedgecode.chess.scanner.Initiable;
23 import org.hedgecode.chess.scanner.ScannerException;
24 import org.hedgecode.chess.scanner.Settings;
25 import org.hedgecode.chess.scanner.SettingsBuilder;
26 import org.hedgecode.chess.scanner.regex.RegexBuilder;
27 import org.hedgecode.chess.scanner.regex.RegexParams;
28 import org.hedgecode.chess.scanner.spi.ServiceRegistry;
30 import static org.hedgecode.chess.scanner.ScannerConstants.*;
31 import static org.hedgecode.chess.scanner.regex.RegexBuilder.Type;
34 * AbstractSettingsScanner
36 * @author Dmitry Samoshin aka gotty
38 public abstract class AbstractSettingsScanner extends AbstractRequestScanner implements Initiable {
40 private Settings settings;
42 protected abstract String getResourceName();
45 public void init() throws ScannerException {
46 SettingsBuilder settingsBuilder = ServiceRegistry.singleProvider(
49 settings = settingsBuilder.build(
54 protected Settings getSettings() {
58 protected String assignUrl(String tournamentId, String pageId) {
59 String tournamentUrl = assignUrlWithParams(
60 settings.getTournamentUrl(), settings.getTournamentQueryParams()
62 return RegexBuilder.build(
67 settings.isTournamentMultiPage() ? pageId : null
72 protected String assignUrl(String gameId) {
73 String gameUrl = settings.getGamePgnUrl() != null
74 ? settings.getGamePgnUrl()
75 : settings.getGameUrl();
76 return RegexBuilder.build(
78 assignUrlWithParams(gameUrl, settings.getGameQueryParams()),
79 new RegexParams(gameId)
83 protected String assignUrl(String gameId, String tournamentId, boolean withTournament) {
84 String gameUrl = settings.getGamePgnUrl() != null
85 ? settings.getGamePgnUrl()
86 : settings.getGameUrl();
87 return RegexBuilder.build(
89 assignUrlWithParams(gameUrl, settings.getGameQueryParams()),
98 protected String assignUrl(String query, boolean isUrlEncode) {
99 return RegexBuilder.build(
101 settings.getTournamentQuery(),
109 protected String regex(String source, String regex) {
110 Matcher matcher = Pattern.compile(regex, Pattern.MULTILINE).matcher(source);
111 if (matcher.find()) {
112 return matcher.groupCount() > 0 ? matcher.group(1) : matcher.group();
117 protected boolean isPgnFormat(String source) {
118 return regex(source, PGN_DETECT_REGEX) != null;
121 private String assignUrlWithParams(String url, String params) {
122 return params != null