/* * Copyright (c) 2019-2020. Developed by Hedgecode. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.hedgecode.chess.scanner; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.hedgecode.chess.scanner.entity.PGNGame; import org.hedgecode.chess.scanner.portal.Chess24Scanner; import org.hedgecode.chess.scanner.portal.ChessBombScanner; import org.hedgecode.chess.scanner.portal.ChessComScanner; import org.hedgecode.chess.scanner.portal.ChessGamesScanner; import org.hedgecode.chess.scanner.portal.LiChessScanner; /** * ChessHogScanner * * @author Dmitry Samoshin aka gotty */ public final class ChessHogScanner { private static ChessHogScanner singleton; private final Map scanners = new HashMap() { { put( ScannerType.CHESSGAMES, new ChessGamesScanner() ); put( ScannerType.LICHESS, new LiChessScanner() ); put( ScannerType.CHESSBOMB, new ChessBombScanner() ); put( ScannerType.CHESS24, new Chess24Scanner() ); put( ScannerType.CHESSCOM, new ChessComScanner() ); } }; private final Set initScanners = new HashSet<>(); /** * Get Scanner by {@code ScannerType}. * * @param type Scanner type (binding to specific chess portal). * @return Implementation of {@code Scanner} for specific portal. */ public static Scanner scanner(ScannerType type) { return getStore().getScanner(type); } /** * Scan PGN game by URL (for all scanner types). * * @param url URL string for scan. * @return PGN game. * @throws ChessHogScannerException Incorrect URL or unknown chess portal. */ public static PGNGame scan(String url) throws ChessHogScannerException { String hostName; try { hostName = new URL(url).getHost(); } catch (MalformedURLException cause) { throw new ChessHogScannerException( String.format("Incorrect URL: %s", url), cause ); } ScannerType type = ScannerType.byHost(hostName); if (type == null) { throw new ChessHogScannerException( String.format("Host %s is not among the known for the scanner", hostName) ); } return scanner(type).scanUrl(url); } private void init() { for (ScannerType type : scanners.keySet()) { init(type); } } private void init(ScannerType type) { Scanner scanner = scanners.get(type); try { if (scanner instanceof Initiable) { Initiable initiableScanner = (Initiable) scanner; initiableScanner.init(); } initScanners.add(type); } catch (ChessHogScannerException e) { throw new RuntimeException(e); } } private Scanner getScanner(ScannerType type) { if (!initScanners.contains(type)) { init(type); } return scanners.get(type); } private ChessHogScanner() { if (!ChessHogScannerProperties.is("scanner.lazy.init")) { init(); } } private static ChessHogScanner getStore() { if (singleton == null) { singleton = new ChessHogScanner(); } return singleton; } }