1 | /** | |
2 | * SpamSchutz - simple way to protect your mail addresses from naïve spammers | |
3 | * Copyright (C) 2011, Aiki IT | |
4 | * <p/> | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * <p/> | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * <p/> | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | package de.aikiit.spamprotector.converter; | |
19 | ||
20 | import com.google.common.base.Strings; | |
21 | import com.google.common.collect.Maps; | |
22 | import lombok.AccessLevel; | |
23 | import lombok.NoArgsConstructor; | |
24 | ||
25 | import java.util.Map; | |
26 | ||
27 | /** | |
28 | * Class encapsulates the actual character conversion logics. | |
29 | */ | |
30 | @NoArgsConstructor(access = AccessLevel.PRIVATE) | |
31 | public final class SpamProtector { | |
32 | ||
33 | private static final Map<String, CharacterConverter> ENCODED = Maps.newHashMap(); | |
34 | private static final Map<Character, CharacterConverter> PLAIN = Maps.newHashMap(); | |
35 | ||
36 | static { | |
37 | // split up characters | |
38 | for (CharacterConverter character : CharacterConverter.values()) { | |
39 | ENCODED.put(character.getReplacement().toLowerCase(), character); | |
40 | PLAIN.put(character.getPlain().charAt(0), character); | |
41 | } | |
42 | } | |
43 | ||
44 | /** | |
45 | * Converts an encoded value into a plain one. | |
46 | * | |
47 | * @param input encoded value as set in an enumeration instance of this class. | |
48 | * @return encoded value or input if no mapping is found. | |
49 | */ | |
50 | public static String toPlain(String input) { | |
51 |
1
1. toPlain : negated conditional → KILLED |
if (!Strings.isNullOrEmpty(input)) { |
52 | String toTransform = input; | |
53 | StringBuilder result = new StringBuilder(); | |
54 | ||
55 |
1
1. toPlain : negated conditional → KILLED |
while (!Strings.isNullOrEmpty(toTransform)) { |
56 | int lengthBeforeConversion = toTransform.length(); | |
57 | ||
58 | for(Map.Entry<String, CharacterConverter> entry : ENCODED.entrySet()) { | |
59 | // cut out any found items | |
60 | String prefix = entry.getKey(); | |
61 |
1
1. toPlain : negated conditional → KILLED |
if (toTransform.startsWith(prefix)) { |
62 | result.append(entry.getValue().getPlain()); | |
63 | toTransform = toTransform.substring(prefix.length(), toTransform.length()); | |
64 | } | |
65 | } | |
66 | ||
67 | // in case we did not replace any character trim one and restart | |
68 |
1
1. toPlain : negated conditional → TIMED_OUT |
if (toTransform.length() == lengthBeforeConversion) { |
69 | result.append(toTransform.charAt(0)); | |
70 | toTransform = toTransform.substring(1); | |
71 | } | |
72 | } | |
73 | ||
74 |
1
1. toPlain : replaced return value with "" for de/aikiit/spamprotector/converter/SpamProtector::toPlain → KILLED |
return result.toString(); |
75 | ||
76 | } | |
77 |
1
1. toPlain : replaced return value with "" for de/aikiit/spamprotector/converter/SpamProtector::toPlain → KILLED |
return input; |
78 | } | |
79 | ||
80 | /** | |
81 | * Converts a plain value into an encoded one. | |
82 | * | |
83 | * @param input plain value that is transformed by an enumeration instance of this class. | |
84 | * @return encoded value or input if no mapping is found. | |
85 | */ | |
86 | public static String toEncoded(String input) { | |
87 |
1
1. toEncoded : negated conditional → KILLED |
if (!Strings.isNullOrEmpty(input)) { |
88 | StringBuilder result = new StringBuilder(); | |
89 | ||
90 | for (Character character : input.toCharArray()) { | |
91 |
1
1. toEncoded : negated conditional → KILLED |
result.append(PLAIN.containsKey(character) ? PLAIN.get(character).getReplacement() : character); |
92 | } | |
93 | ||
94 |
1
1. toEncoded : replaced return value with "" for de/aikiit/spamprotector/converter/SpamProtector::toEncoded → KILLED |
return result.toString(); |
95 | } | |
96 |
1
1. toEncoded : replaced return value with "" for de/aikiit/spamprotector/converter/SpamProtector::toEncoded → KILLED |
return input; |
97 | } | |
98 | } | |
Mutations | ||
51 |
1.1 |
|
55 |
1.1 |
|
61 |
1.1 |
|
68 |
1.1 |
|
74 |
1.1 |
|
77 |
1.1 |
|
87 |
1.1 |
|
91 |
1.1 |
|
94 |
1.1 |
|
96 |
1.1 |