1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.mask.algorithm.cover;
19
20 import com.google.common.base.Strings;
21 import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmPropertiesChecker;
22 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
23
24 import java.util.Properties;
25
26
27
28
29 public final class MaskBeforeSpecialCharsAlgorithm implements MaskAlgorithm<Object, String> {
30
31 private static final String SPECIAL_CHARS = "special-chars";
32
33 private static final String REPLACE_CHAR = "replace-char";
34
35 private String specialChars;
36
37 private Character replaceChar;
38
39 @Override
40 public void init(final Properties props) {
41 specialChars = createSpecialChars(props);
42 replaceChar = createReplaceChar(props);
43 }
44
45 private String createSpecialChars(final Properties props) {
46 MaskAlgorithmPropertiesChecker.checkAtLeastOneChar(props, SPECIAL_CHARS, this);
47 return props.getProperty(SPECIAL_CHARS);
48 }
49
50 private Character createReplaceChar(final Properties props) {
51 MaskAlgorithmPropertiesChecker.checkSingleChar(props, REPLACE_CHAR, this);
52 return props.getProperty(REPLACE_CHAR).charAt(0);
53 }
54
55 @Override
56 public String mask(final Object plainValue) {
57 String result = null == plainValue ? null : String.valueOf(plainValue);
58 if (Strings.isNullOrEmpty(result)) {
59 return result;
60 }
61 int index = result.indexOf(specialChars);
62 char[] chars = result.toCharArray();
63 for (int i = 0; i < index; i++) {
64 chars[i] = replaceChar;
65 }
66 return new String(chars);
67 }
68
69 @Override
70 public String getType() {
71 return "MASK_BEFORE_SPECIAL_CHARS";
72 }
73 }