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.Chess2700Scanner;
29 import org.hedgecode.chess.scanner.portal.ChessBombScanner;
30 import org.hedgecode.chess.scanner.portal.ChessComScanner;
31 import org.hedgecode.chess.scanner.portal.ChessGamesScanner;
32 import org.hedgecode.chess.scanner.portal.LiChessScanner;
37 * @author Dmitry Samoshin aka gotty
39 public final class ChessHogScanner {
41 private static ChessHogScanner singleton;
43 private final Map<ScannerType, Scanner> scanners = new HashMap<ScannerType, Scanner>() {
45 put( ScannerType.CHESSGAMES, new ChessGamesScanner() );
46 put( ScannerType.LICHESS, new LiChessScanner() );
47 put( ScannerType.CHESSBOMB, new ChessBombScanner() );
48 put( ScannerType.CHESS24, new Chess24Scanner() );
49 put( ScannerType.CHESSCOM, new ChessComScanner() );
50 put( ScannerType.CHESS2700, new Chess2700Scanner() );
54 private final Set<ScannerType> initScanners = new HashSet<>();
57 * Get Scanner by {@code ScannerType}.
59 * @param type Scanner type (binding to specific chess portal).
60 * @return Implementation of {@code Scanner} for specific portal.
62 public static Scanner scanner(ScannerType type) {
63 return getStore().getScanner(type);
67 * Scan PGN game by URL (for all scanner types).
69 * @param url URL string for scan.
71 * @throws ScannerException Incorrect URL or unknown chess portal.
73 public static PGNGame scan(String url) throws ScannerException {
76 hostName = new URL(url).getHost();
77 } catch (MalformedURLException cause) {
78 throw new ScannerException(
79 String.format("Incorrect URL: %s", url), cause
82 ScannerType type = ScannerType.byHost(hostName);
84 throw new ScannerException(
85 String.format("Host %s is not among the known for the scanner", hostName)
88 return scanner(type).scanUrl(url);
92 for (ScannerType type : scanners.keySet()) {
97 private void init(ScannerType type) {
98 Scanner scanner = scanners.get(type);
100 if (scanner instanceof Initiable) {
101 Initiable initiableScanner = (Initiable) scanner;
102 initiableScanner.init();
104 initScanners.add(type);
105 } catch (ScannerException e) {
106 throw new RuntimeException(e);
111 private Scanner getScanner(ScannerType type) {
112 if (!initScanners.contains(type)) {
115 return scanners.get(type);
118 private ChessHogScanner() {
119 if (!ScannerProperties.is("scanner.lazy.init")) {
124 private static ChessHogScanner getStore() {
125 if (singleton == null) {
126 singleton = new ChessHogScanner();