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;
19 import java.net.MalformedURLException;
21 import java.util.HashMap;
22 import java.util.HashSet;
26 import org.hedgecode.chess.scanner.entity.PGNGame;
27 import org.hedgecode.chess.scanner.portal.Chess24Scanner;
28 import org.hedgecode.chess.scanner.portal.ChessBombScanner;
29 import org.hedgecode.chess.scanner.portal.ChessComScanner;
30 import org.hedgecode.chess.scanner.portal.ChessGamesScanner;
31 import org.hedgecode.chess.scanner.portal.LiChessScanner;
36 * @author Dmitry Samoshin aka gotty
38 public final class ChessHogScanner {
40 private static ChessHogScanner singleton;
42 private final Map<ScannerType, Scanner> scanners = new HashMap<ScannerType, Scanner>() {
44 put( ScannerType.CHESSGAMES, new ChessGamesScanner() );
45 put( ScannerType.LICHESS, new LiChessScanner() );
46 put( ScannerType.CHESSBOMB, new ChessBombScanner() );
47 put( ScannerType.CHESS24, new Chess24Scanner() );
48 put( ScannerType.CHESSCOM, new ChessComScanner() );
52 private final Set<ScannerType> initScanners = new HashSet<>();
55 * Get Scanner by {@code ScannerType}.
57 * @param type Scanner type (binding to specific chess portal).
58 * @return Implementation of {@code Scanner} for specific portal.
60 public static Scanner scanner(ScannerType type) {
61 return getStore().getScanner(type);
65 * Scan PGN game by URL (for all scanner types).
67 * @param url URL string for scan.
69 * @throws ChessHogScannerException Incorrect URL or unknown chess portal.
71 public static PGNGame scan(String url) throws ChessHogScannerException {
74 hostName = new URL(url).getHost();
75 } catch (MalformedURLException cause) {
76 throw new ChessHogScannerException(
77 String.format("Incorrect URL: %s", url), cause
80 ScannerType type = ScannerType.byHost(hostName);
82 throw new ChessHogScannerException(
83 String.format("Host %s is not among the known for the scanner", hostName)
86 return scanner(type).scanUrl(url);
90 for (ScannerType type : scanners.keySet()) {
95 private void init(ScannerType type) {
96 Scanner scanner = scanners.get(type);
98 if (scanner instanceof Initiable) {
99 Initiable initiableScanner = (Initiable) scanner;
100 initiableScanner.init();
102 initScanners.add(type);
103 } catch (ChessHogScannerException e) {
104 throw new RuntimeException(e);
109 private Scanner getScanner(ScannerType type) {
110 if (!initScanners.contains(type)) {
113 return scanners.get(type);
116 private ChessHogScanner() {
117 if (!ChessHogScannerProperties.is("scanner.lazy.init")) {
122 private static ChessHogScanner getStore() {
123 if (singleton == null) {
124 singleton = new ChessHogScanner();