14b4fbab97bf351d9709f290145ac73058b957fc
[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                 return target.replace(
60                         RegexParams.GAME_ID,
61                         params.getGameId()
62                 );
63             }
64         },
65
66         QUERY {
67             @Override
68             public String format(String target, RegexParams params) {
69                 return target.replace(
70                         RegexParams.QUERY,
71                         params.isUrlEncode()
72                                 ? urlEncode(params.getQuery())
73                                 : params.getQuery()
74                 );
75             }
76         };
77
78         public abstract String format(String target, RegexParams params);
79
80         private static String urlEncode(String query) throws RuntimeException {
81             String encodeQuery;
82             try {
83                 encodeQuery = URLEncoder.encode(
84                         query, ChessHogScannerConstants.CHARSET.name()
85                 );
86             } catch (UnsupportedEncodingException cause) {
87                 throw new RuntimeException(
88                         String.format("Unsupported encoding: %s", ChessHogScannerConstants.CHARSET.name()),
89                         cause
90                 );
91             }
92             return encodeQuery;
93         }
94
95     }
96
97 }