View Javadoc
1   /**
2    * SpamSchutz - simple way to protect your mail adresses from naiive 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   * @author hirsch
29   * @version 2015-12-05, 21:52
30   */
31  @NoArgsConstructor(access = AccessLevel.PRIVATE)
32  public final class SpamProtector {
33  
34      private static final Map<String, CharacterConverter> ENCODED = Maps.newHashMap();
35      private static final Map<Character, CharacterConverter> PLAIN = Maps.newHashMap();
36  
37      static {
38          // split up characters
39          for (CharacterConverter character : CharacterConverter.values()) {
40              ENCODED.put(character.getReplacement().toLowerCase(), character);
41              PLAIN.put(character.getPlain().charAt(0), character);
42          }
43      }
44  
45      /**
46       * Converts an encoded value into a plain one.
47       *
48       * @param input encoded value as set in an enumeration instance of this class.
49       * @return encoded value or input if no mapping is found.
50       */
51      public static String toPlain(String input) {
52          if (!Strings.isNullOrEmpty(input)) {
53              String toTransform = input;
54              StringBuilder result = new StringBuilder();
55  
56              while (!Strings.isNullOrEmpty(toTransform)) {
57                  int lengthBeforeConversion = toTransform.length();
58  
59                  for(Map.Entry<String, CharacterConverter> entry : ENCODED.entrySet()) {
60                      // cut out any found items
61                      String prefix = entry.getKey();
62                      if (toTransform.startsWith(prefix)) {
63                          result.append(entry.getValue().getPlain());
64                          toTransform = toTransform.substring(prefix.length(), toTransform.length());
65                      }
66                  }
67  
68                  // in case we did not replace any character trim one and restart
69                  if (toTransform.length() == lengthBeforeConversion) {
70                      result.append(toTransform.charAt(0));
71                      toTransform = toTransform.substring(1);
72                  }
73              }
74  
75              return result.toString();
76  
77          }
78          return input;
79      }
80  
81      /**
82       * Converts a plain value into an encoded one.
83       *
84       * @param input plain value that is transformed by an enumeration instance of this class.
85       * @return encoded value or input if no mapping is found.
86       */
87      public static String toEncoded(String input) {
88          if (!Strings.isNullOrEmpty(input)) {
89              StringBuilder result = new StringBuilder();
90  
91              for (Character character : input.toCharArray()) {
92                  result.append(PLAIN.containsKey(character) ? PLAIN.get(character).getReplacement() : character);
93              }
94  
95              return result.toString();
96          }
97          return input;
98      }
99  }