MentaTutorials

Rev

Rev 4 | Rev 8 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 soliveira 1
package org.hellomenta.action;
2 soliveira 2
 
7 soliveira 3
import java.util.regex.Matcher;
4
import java.util.regex.Pattern;
5
 
2 soliveira 6
import org.mentawai.core.BaseAction;
7
 
8
public class HelloAction extends BaseAction {
9
 
10
        public String hi() {
11
                String msg = input.getString("msg");
12
                if (isEmpty(msg)) {
13
                        msg = "Why you did not type anything?";
14
                }
7 soliveira 15
                output.setValue("msg", removeAccents(msg));
2 soliveira 16
                return SUCCESS;
17
        }
7 soliveira 18
 
19
        public static String[] REPLACES = { "a", "e", "i", "o", "u", "c", "A", "E", "I", "O", "U", "C" };
20
 
21
        public static Pattern[] PATTERNS = null;
22
 
23
        static {
24
                PATTERNS = new Pattern[REPLACES.length];
25
                PATTERNS[0] = Pattern.compile("[âãáàä]");
26
                PATTERNS[1] = Pattern.compile("[éèêë]");
27
                PATTERNS[2] = Pattern.compile("[íìîï]");
28
                PATTERNS[3] = Pattern.compile("[óòôõö]");
29
                PATTERNS[4] = Pattern.compile("[úùûü]");
30
                PATTERNS[5] = Pattern.compile("[ç]");
31
                PATTERNS[6] = Pattern.compile("[ÂÃÁÀÄ]");
32
                PATTERNS[7] = Pattern.compile("[ÉÈÊË]");
33
                PATTERNS[8] = Pattern.compile("[ÍÌÎÏ]");
34
                PATTERNS[9] = Pattern.compile("[ÓÒÔÕÖ]");
35
                PATTERNS[10] = Pattern.compile("[ÚÙÛÜ]");
36
                PATTERNS[11] = Pattern.compile("[Ç]");
37
        }
38
 
39
        public static String removeAccents(String text) {
40
                String result = text;
41
                for (int i = 0; i < PATTERNS.length; i++) {
42
                        Matcher matcher = PATTERNS[i].matcher(result);
43
                        result = matcher.replaceAll(REPLACES[i]);
44
                }
45
                return result;
46
        }
2 soliveira 47
}