[LIB-13] Functional for working with strings moved to StringUtils
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / StringUtils.java
1 /*
2  * Copyright (c) 2019-2020. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.scanner;
18
19 import java.net.URL;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22
23 import org.hedgecode.chess.scanner.format.PGNConstants;
24
25 /**
26  * StringUtils
27  *
28  * @author Dmitry Samoshin aka gotty
29  */
30 public final class StringUtils {
31
32     private static final String EMPTY = "";
33     private static final char BACKSLASH = '\\';
34
35     private static final String CRLF_REGEX = "(\\\\r)?\\\\n";
36     private static final String SHIELD_REGEX = "\\\\";
37
38     private static final String DOMAIN_REGEX_FORMAT = "(\\w+\\.)*\\b%s";
39
40     public static String match(String source, String regex) {
41         Matcher matcher = Pattern.compile(
42                 regex,
43                 Pattern.MULTILINE
44         ).matcher(source);
45         if (matcher.find()) {
46             return matcher.groupCount() > 0
47                     ? matcher.group(1)
48                     : matcher.group();
49         }
50         return null;
51     }
52
53     public static boolean isPgn(String source) {
54         return match(
55                 source,
56                 PGNConstants.PGN_DETECT_REGEX
57         ) != null;
58     }
59
60     public static String shield(String source, char[] shields) {
61         for (char shield : shields) {
62             if (source.indexOf(shield) >= 0) {
63                 String regexShield =
64                         shield == BACKSLASH
65                                 ? SHIELD_REGEX
66                                 : String.valueOf(shield);
67                 source = source.replaceAll(
68                         String.format("([%s])", regexShield),
69                         SHIELD_REGEX.concat("$1")
70                 );
71             }
72         }
73         return source;
74     }
75
76     public static String unshield(String source) {
77         return source.replaceAll(SHIELD_REGEX, EMPTY);
78     }
79
80     public static String formatCrlf(String source) {
81         return unshield(
82                 source.replaceAll(
83                         CRLF_REGEX, PGNConstants.PGN_CRLF
84                 )
85         );
86     }
87
88     public static boolean belongDomain(String domain, URL url) {
89         return belongDomain(
90                 domain, url.getHost()
91         );
92     }
93
94     public static boolean belongDomain(String domain, String host) {
95         return host.matches(
96                 String.format(DOMAIN_REGEX_FORMAT, domain)
97         );
98     }
99
100     private StringUtils() {
101         throw new AssertionError(
102                 String.format("No %s instances!", getClass().getName())
103         );
104     }
105
106 }