[LIB-13] Rename some common classes
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / AbstractSettingsScanner.java
1 /*
2  * Copyright (c) 2019-2020. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.scanner.portal;
18
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
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;
29
30 import static org.hedgecode.chess.scanner.ScannerConstants.*;
31 import static org.hedgecode.chess.scanner.regex.RegexBuilder.Type;
32
33 /**
34  * AbstractSettingsScanner
35  *
36  * @author Dmitry Samoshin aka gotty
37  */
38 public abstract class AbstractSettingsScanner extends AbstractRequestScanner implements Initiable {
39
40     private Settings settings;
41
42     protected abstract String getResourceName();
43
44     @Override
45     public void init() throws ScannerException {
46         SettingsBuilder settingsBuilder = ServiceRegistry.singleProvider(
47                 SettingsBuilder.class
48         );
49         settings = settingsBuilder.build(
50                 getResourceName()
51         );
52     }
53
54     protected Settings getSettings() {
55         return settings;
56     }
57
58     protected String assignUrl(String tournamentId, String pageId) {
59         String tournamentUrl = assignUrlWithParams(
60                 settings.getTournamentUrl(), settings.getTournamentQueryParams()
61         );
62         return RegexBuilder.build(
63                 Type.TOURNAMENT,
64                 tournamentUrl,
65                 new RegexParams(
66                         tournamentId,
67                         settings.isTournamentMultiPage() ? pageId : null
68                 )
69         );
70     }
71
72     protected String assignUrl(String gameId) {
73         String gameUrl = settings.getGamePgnUrl() != null
74                 ? settings.getGamePgnUrl()
75                 : settings.getGameUrl();
76         return RegexBuilder.build(
77                 Type.GAME,
78                 assignUrlWithParams(gameUrl, settings.getGameQueryParams()),
79                 new RegexParams(gameId)
80         );
81     }
82
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(
88                 Type.GAME,
89                 assignUrlWithParams(gameUrl, settings.getGameQueryParams()),
90                 new RegexParams(
91                         gameId,
92                         tournamentId,
93                         withTournament
94                 )
95         );
96     }
97
98     protected String assignUrl(String query, boolean isUrlEncode) {
99         return RegexBuilder.build(
100                 Type.QUERY,
101                 settings.getTournamentQuery(),
102                 new RegexParams(
103                         query,
104                         isUrlEncode
105                 )
106         );
107     }
108
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();
113         }
114         return null;
115     }
116
117     protected boolean isPgnFormat(String source) {
118         return regex(source, PGN_DETECT_REGEX) != null;
119     }
120
121     private String assignUrlWithParams(String url, String params) {
122         return params != null
123                 ? url.concat(params)
124                 : url;
125     }
126
127 }