[LIB-13] Functional for working with strings moved to StringUtils
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / ChessGamesScanner.java
index 28493cf..56fc032 100644 (file)
@@ -20,7 +20,9 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
-import org.hedgecode.chess.scanner.ChessHogScannerException;
+import org.hedgecode.chess.scanner.Scanner;
+import org.hedgecode.chess.scanner.ScannerException;
+import org.hedgecode.chess.scanner.StringUtils;
 import org.hedgecode.chess.scanner.entity.PGNGame;
 import org.hedgecode.chess.scanner.entity.PGNTournament;
 
@@ -29,7 +31,7 @@ import org.hedgecode.chess.scanner.entity.PGNTournament;
  *
  * @author Dmitry Samoshin aka gotty
  */
-public class ChessGamesScanner extends AbstractSettingsScanner {
+public class ChessGamesScanner extends AbstractSettingsScanner implements Scanner {
 
     private static final String SETTINGS_FILENAME = "chessgames.settings";
 
@@ -39,14 +41,14 @@ public class ChessGamesScanner extends AbstractSettingsScanner {
     }
 
     @Override
-    public PGNTournament scanTournament(String tournamentId) throws ChessHogScannerException {
+    public PGNTournament scanTournament(String tournamentId) throws ScannerException {
         PGNTournament tournament = new PGNTournament(tournamentId);
         assignTournamentGames(tournament);
         return tournament;
     }
 
     @Override
-    public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException {
+    public PGNTournament findTournament(String tournamentName) throws ScannerException {
         PGNTournament tournament = null;
         Map<String, String> result = matchMap(
                 assignUrl(
@@ -70,12 +72,12 @@ public class ChessGamesScanner extends AbstractSettingsScanner {
     }
 
     @Override
-    public PGNGame scanGame(String gameId) throws ChessHogScannerException {
+    public PGNGame scanGame(String gameId) throws ScannerException {
         String pgn = request(
                 assignUrl(gameId)
         );
-        if (!isPgnFormat(pgn)) {
-            throw new ChessHogScannerException(
+        if (!StringUtils.isPgn(pgn)) {
+            throw new ScannerException(
                     String.format("Failed to get PGN for requesting game ID: %s", gameId)
             );
         }
@@ -85,20 +87,19 @@ public class ChessGamesScanner extends AbstractSettingsScanner {
     }
 
     @Override
-    public PGNGame scanGame(String gameId, String tournamentId) throws ChessHogScannerException {
+    public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
         return scanGame(gameId);
     }
 
     @Override
-    public PGNGame scanUrl(String gameUrl) throws ChessHogScannerException {
-        String pgn = regex(
-                request(
-                        gameUrl
-                ),
+    public PGNGame scanUrl(String gameUrl) throws ScannerException {
+        String gamePage = request(gameUrl);
+        String pgn = StringUtils.match(
+                gamePage,
                 getSettings().getGameUrlRegex()
         );
-        if (pgn == null || !isPgnFormat(pgn)) {
-            throw new ChessHogScannerException(
+        if (pgn == null || !StringUtils.isPgn(pgn)) {
+            throw new ScannerException(
                     String.format("Failed to get PGN for requesting URL: %s", gameUrl)
             );
         }
@@ -108,7 +109,7 @@ public class ChessGamesScanner extends AbstractSettingsScanner {
         );
     }
 
-    private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {
+    private void assignTournamentGames(PGNTournament tournament) throws ScannerException {
         tournament.clearGames();
         List<String> gamesId = new ArrayList<>();