LICHESS ( TYPE_LICHESS, DOMAIN_LICHESS ),
CHESSBOMB ( TYPE_CHESSBOMB, DOMAIN_CHESSBOMB ),
CHESS24 ( TYPE_CHESS24, DOMAIN_CHESS24 ),
- CHESSCOM ( TYPE_CHESSCOM, DOMAIN_CHESSCOM );
+ CHESSCOM ( TYPE_CHESSCOM, DOMAIN_CHESSCOM ),
+ CHESS2700 ( TYPE_2700CHESS, DOMAIN_2700CHESS );
private String type;
private String domain;
public static ScannerType byHost(String host) {
if (host != null) {
for (ScannerType scannerType : ScannerType.values()) {
- if (host.contains(scannerType.domain))
+ if (StringUtils.belongDomain(scannerType.domain, host)) {
return scannerType;
+ }
}
}
return null;
--- /dev/null
+/*
+ * 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.URL;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.hedgecode.chess.scanner.format.PGNConstants;
+
+/**
+ * StringUtils
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public final class StringUtils {
+
+ private static final String EMPTY = "";
+ private static final char BACKSLASH = '\\';
+
+ private static final String CRLF_REGEX = "(\\\\r)?\\\\n";
+ private static final String SHIELD_REGEX = "\\\\";
+
+ private static final String DOMAIN_REGEX_FORMAT = "(\\w+\\.)*\\b%s";
+
+ public static String match(String source, String regex) {
+ Matcher matcher = Pattern.compile(
+ regex,
+ Pattern.MULTILINE
+ ).matcher(source);
+ if (matcher.find()) {
+ return matcher.groupCount() > 0
+ ? matcher.group(1)
+ : matcher.group();
+ }
+ return null;
+ }
+
+ public static boolean isPgn(String source) {
+ return match(
+ source,
+ PGNConstants.PGN_DETECT_REGEX
+ ) != null;
+ }
+
+ public static String shield(String source, char[] shields) {
+ for (char shield : shields) {
+ if (source.indexOf(shield) >= 0) {
+ String regexShield =
+ shield == BACKSLASH
+ ? SHIELD_REGEX
+ : String.valueOf(shield);
+ source = source.replaceAll(
+ String.format("([%s])", regexShield),
+ SHIELD_REGEX.concat("$1")
+ );
+ }
+ }
+ return source;
+ }
+
+ public static String unshield(String source) {
+ return source.replaceAll(SHIELD_REGEX, EMPTY);
+ }
+
+ public static String formatCrlf(String source) {
+ return unshield(
+ source.replaceAll(
+ CRLF_REGEX, PGNConstants.PGN_CRLF
+ )
+ );
+ }
+
+ public static boolean belongDomain(String domain, URL url) {
+ return belongDomain(
+ domain, url.getHost()
+ );
+ }
+
+ public static boolean belongDomain(String domain, String host) {
+ return host.matches(
+ String.format(DOMAIN_REGEX_FORMAT, domain)
+ );
+ }
+
+ private StringUtils() {
+ throw new AssertionError(
+ String.format("No %s instances!", getClass().getName())
+ );
+ }
+
+}
import java.util.Arrays;
import java.util.Comparator;
+import org.hedgecode.chess.scanner.StringUtils;
+
/**
* PGNTag
*
MODE ( "Mode", false, PGNTag.EMPTY ),
PLY_COUNT ( "PlyCount", false, PGNTag.EMPTY );
+ // todo: -> PGNConstants
public static final String EMPTY = "";
public static final String HYPHEN = "-";
public static final String ZERO = "0";
public static final String TAG_FORMAT = "[%s \"%s\"]";
+ public static final char[] TAG_SHIELD_CHARS = { '\\', '"' };
+
private String tagName;
private boolean isRequired;
private String defaultValue;
return defaultValue;
}
+ public static String formatTagValue(String tagValue) {
+ return StringUtils.shield(
+ tagValue, TAG_SHIELD_CHARS
+ );
+ }
+
public static PGNTag[] tags() {
PGNTag[] tags = values();
Arrays.sort(
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;
String pgn = request(
assignUrl(gameId)
);
- if (!isPgnFormat(pgn)) {
+ if (!StringUtils.isPgn(pgn)) {
throw new ScannerException(
String.format("Failed to get PGN for requesting game ID: %s", gameId)
);
@Override
public PGNGame scanUrl(String gameUrl) throws ScannerException {
- String pgn = regex(
- request(
- gameUrl
- ),
+ String gamePage = request(gameUrl);
+ String pgn = StringUtils.match(
+ gamePage,
getSettings().getGameUrlRegex()
);
- if (pgn == null || !isPgnFormat(pgn)) {
+ if (pgn == null || !StringUtils.isPgn(pgn)) {
throw new ScannerException(
String.format("Failed to get PGN for requesting URL: %s", gameUrl)
);
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.ScannerConstants.*;
+import static org.hedgecode.chess.scanner.format.PGNConstants.*;
/**
* LiChessScanner
String pgn = request(
assignUrl(gameId)
);
- if (!isPgnFormat(pgn)) {
+ if (!StringUtils.isPgn(pgn)) {
throw new ScannerException(
String.format("Failed to get PGN for requesting game ID: %s", gameId)
);
@Override
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()
);
);
} else {
pgn = StringEscapeUtils.unescapeHtml4(pgn);
- if (!isPgnFormat(pgn)) {
+ 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
);
}
if (!pgnGames.isEmpty()) {
tournament.setName(
- regex(
+ StringUtils.match(
pgnGames.get(0),
getSettings().getTournamentNameRegex()
)
}
for (String pgn : pgnGames) {
- String gameId = regex(
+ String gameId = StringUtils.match(
pgn,
getSettings().getGameIdRegex()
);