[LIB-13] Add function to Scanner API
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / regex / RegexBuilder.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.regex;
18
19 import java.io.UnsupportedEncodingException;
20 import java.net.URLEncoder;
21
22 import org.hedgecode.chess.scanner.ChessHogScannerConstants;
23
24 /**
25  * RegexBuilder
26  *
27  * @author Dmitry Samoshin aka gotty
28  */
29 public class RegexBuilder {
30
31     public static String build(Type type, String target, RegexParams params) {
32         return type.format(
33                 target,
34                 params
35         );
36     }
37
38     public enum Type {
39
40         TOURNAMENT {
41             @Override
42             public String format(String target, RegexParams params) {
43                 if (params.getPageId() != null) {
44                     target = target.replace(
45                             RegexParams.PAGE_ID,
46                             params.getPageId()
47                     );
48                 }
49                 return target.replace(
50                         RegexParams.TOURNAMENT_ID,
51                         params.getTournamentId()
52                 );
53             }
54         },
55
56         GAME {
57             @Override
58             public String format(String target, RegexParams params) {
59                 if (params.getTournamentId() != null) {
60                     target = target.replace(
61                             RegexParams.TOURNAMENT_ID,
62                             params.getTournamentId()
63                     );
64                 }
65                 return target.replace(
66                         RegexParams.GAME_ID,
67                         params.getGameId()
68                 );
69             }
70         },
71
72         QUERY {
73             @Override
74             public String format(String target, RegexParams params) {
75                 return target.replace(
76                         RegexParams.QUERY,
77                         params.isUrlEncode()
78                                 ? urlEncode(params.getQuery())
79                                 : params.getQuery()
80                 );
81             }
82         };
83
84         public abstract String format(String target, RegexParams params);
85
86         private static String urlEncode(String query) throws RuntimeException {
87             String encodeQuery;
88             try {
89                 encodeQuery = URLEncoder.encode(
90                         query, ChessHogScannerConstants.CHARSET.name()
91                 );
92             } catch (UnsupportedEncodingException cause) {
93                 throw new RuntimeException(
94                         String.format("Unsupported encoding: %s", ChessHogScannerConstants.CHARSET.name()),
95                         cause
96                 );
97             }
98             return encodeQuery;
99         }
100
101     }
102
103 }