X-Git-Url: https://git.hedgecode.org/?p=chesshog-scanner.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fscanner%2FStringUtils.java;fp=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fscanner%2FStringUtils.java;h=6a0f5c52826c7b8a125398ea205e485f06ceca4a;hp=0000000000000000000000000000000000000000;hb=6e3a8590a26312b6cea579777db885107cae88df;hpb=df574e5ec8ae08c1c50adbe30b597cd86e6ab3fa diff --git a/src/main/java/org/hedgecode/chess/scanner/StringUtils.java b/src/main/java/org/hedgecode/chess/scanner/StringUtils.java new file mode 100644 index 0000000..6a0f5c5 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/StringUtils.java @@ -0,0 +1,106 @@ +/* + * 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()) + ); + } + +}