View Javadoc

1   /*
2    * $Id: RemoveAccentTranslator.java,v 1.1 2007/11/29 12:58:13 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.validator.impl;
5   
6   import java.util.BitSet;
7   import java.util.regex.Pattern;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.rcfaces.core.validator.IClientValidatorContext;
12  import org.rcfaces.core.validator.ITranslatorTask;
13  
14  /**
15   * 
16   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
17   * @version $Revision: 1.1 $ $Date: 2007/11/29 12:58:13 $
18   */
19  public class RemoveAccentTranslator extends AbstractClientValidatorTask
20          implements ITranslatorTask {
21      private static final String REVISION = "$Revision: 1.1 $";
22  
23      private static final Log LOG = LogFactory
24              .getLog(RemoveAccentTranslator.class);
25  
26      private static final BitSet accentsHash = new BitSet(512);
27  
28      private static final AccentTranslator ACCENTS_MAPPERS[] = new AccentTranslator[] {
29              new AccentTranslator("áãàâäå???", 'a'),
30              new AccentTranslator("????ç", 'c'),
31              new AccentTranslator("??", 'd'),
32              new AccentTranslator("éèêë?????", 'e'),
33              new AccentTranslator("????", 'g'), new AccentTranslator("??", 'h'),
34              new AccentTranslator("íìîï?????", 'i'),
35              new AccentTranslator("ñ?????", 'n'),
36              new AccentTranslator("óõòôö???", 'o'),
37              new AccentTranslator("úùûüµ??????", 'u'),
38              new AccentTranslator("ýÿ?", 'y'),
39              new AccentTranslator("ÀÁÂÃÄÅ???", 'A'),
40              new AccentTranslator("Ç????", 'C'),
41              new AccentTranslator("??", 'D'),
42              new AccentTranslator("ÈÉÊË?????", 'E'),
43              new AccentTranslator("????", 'G'), new AccentTranslator("??", 'H'),
44              new AccentTranslator("ÌÍÎÏ?????", 'I'),
45              new AccentTranslator("Ñ????", 'N'),
46              new AccentTranslator("ÓÔÕÖÒ???", 'O'),
47              new AccentTranslator("ÙÚÛÜ??????", 'U'),
48              new AccentTranslator("Ý??", 'Y') };
49  
50      public char applyTranslator(IClientValidatorContext context, char keyChar) {
51  
52          if (accentsHash.get(computeHashCode(keyChar)) == false) {
53              return keyChar;
54          }
55  
56          String ch = String.valueOf(keyChar);
57  
58          for (int i = 0; i < ACCENTS_MAPPERS.length; i++) {
59              AccentTranslator at = ACCENTS_MAPPERS[i];
60  
61              if (at.pattern.matcher(ch).find()) {
62                  return at.ch;
63              }
64          }
65  
66          return keyChar;
67      }
68  
69      private static int computeHashCode(char keyChar) {
70          return ((keyChar * 2777) >> 3) & 0x03ff;
71      }
72  
73      /**
74       * 
75       * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
76       * @version $Revision: 1.1 $ $Date: 2007/11/29 12:58:13 $
77       */
78      private static class AccentTranslator {
79          private static final String REVISION = "$Revision: 1.1 $";
80  
81          private final Pattern pattern;
82  
83          private final char ch;
84  
85          public AccentTranslator(String regEx, char ch) {
86              this.pattern = Pattern.compile("[" + regEx + "]");
87              this.ch = ch;
88  
89              for (int i = 0; i < regEx.length(); i++) {
90                  accentsHash.set(computeHashCode(regEx.charAt(i)));
91              }
92          }
93      }
94  }