[LIB-13] Functional for working with strings moved to StringUtils
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / LiChessScanner.java
index 18497a1..820e513 100644 (file)
@@ -20,20 +20,22 @@ import java.util.List;
 
 import org.apache.commons.text.StringEscapeUtils;
 
-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;
 import org.hedgecode.chess.scanner.format.lichess.Format;
 import org.hedgecode.chess.scanner.format.lichess.GameFormat;
 
-import static org.hedgecode.chess.scanner.ChessHogScannerConstants.*;
+import static org.hedgecode.chess.scanner.format.PGNConstants.*;
 
 /**
  * LiChessScanner
  *
  * @author Dmitry Samoshin aka gotty
  */
-public class LiChessScanner extends AbstractSettingsScanner {
+public class LiChessScanner extends AbstractSettingsScanner implements Scanner {
 
     private static final String SETTINGS_FILENAME = "lichess.settings";
 
@@ -43,26 +45,26 @@ public class LiChessScanner 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 {
-        throw new ChessHogScannerException(
+    public PGNTournament findTournament(String tournamentName) throws ScannerException {
+        throw new ScannerException(
                 "Lichess does not support searching for a tournament by name!"
         );
     }
 
     @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)
             );
         }
@@ -72,24 +74,24 @@ public class LiChessScanner 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 {
+    public PGNGame scanUrl(String gameUrl) throws ScannerException {
         String gamePage = request(gameUrl);
-        String pgn = regex(
+        String pgn = StringUtils.match(
                 gamePage,
                 getSettings().getGameUrlRegex()
         );
         if (pgn == null) {
-            pgn = regex(
+            pgn = StringUtils.match(
                     gamePage,
                     getSettings().getGameJsonUrlRegex()
             );
             if (pgn == null) {
-                throw new ChessHogScannerException(
+                throw new ScannerException(
                         String.format("Failed to get source data for requesting URL: %s", gameUrl)
                 );
             }
@@ -100,19 +102,22 @@ public class LiChessScanner extends AbstractSettingsScanner {
             );
         } else {
             pgn = StringEscapeUtils.unescapeHtml4(pgn);
-            if (!isPgnFormat(pgn)) {
-                throw new ChessHogScannerException(
+            if (!StringUtils.isPgn(pgn)) {
+                throw new ScannerException(
                         String.format("Failed to get PGN for requesting URL: %s", gameUrl)
                 );
             }
             return new PGNGame(
-                    regex(pgn, getSettings().getGameIdRegex()),
+                    StringUtils.match(
+                            pgn,
+                            getSettings().getGameIdRegex()
+                    ),
                     pgn
             );
         }
     }
 
-    private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {
+    private void assignTournamentGames(PGNTournament tournament) throws ScannerException {
         tournament.clearGames();
         List<String> pgnGames = split(
                 assignUrl(
@@ -124,7 +129,7 @@ public class LiChessScanner extends AbstractSettingsScanner {
 
         if (!pgnGames.isEmpty()) {
             tournament.setName(
-                    regex(
+                    StringUtils.match(
                             pgnGames.get(0),
                             getSettings().getTournamentNameRegex()
                     )
@@ -132,7 +137,7 @@ public class LiChessScanner extends AbstractSettingsScanner {
         }
 
         for (String pgn : pgnGames) {
-            String gameId = regex(
+            String gameId = StringUtils.match(
                     pgn,
                     getSettings().getGameIdRegex()
             );