1 package org.andromda.cartridges.jsf.validator;
2
3 import java.text.MessageFormat;
4 import java.util.Locale;
5 import java.util.MissingResourceException;
6 import javax.faces.context.FacesContext;
7 import org.andromda.cartridges.jsf.Messages;
8 import org.apache.commons.validator.Arg;
9 import org.apache.commons.validator.Field;
10 import org.apache.commons.validator.ValidatorAction;
11
12
13
14
15
16
17 public class ValidatorMessages
18 {
19
20
21
22
23
24
25
26
27
28 public static String getMessage(
29 final ValidatorAction action,
30 final String[] args,
31 final FacesContext context)
32 {
33 final Locale locale = context.getViewRoot().getLocale();
34 String message = null;
35 final String messageKey = action.getMsg();
36 if (messageKey != null)
37 {
38 try
39 {
40 message = Messages.get(
41 messageKey,
42 args);
43 }
44 catch (final MissingResourceException exception)
45 {
46 message = messageKey;
47 }
48 }
49 message = new MessageFormat(
50 message,
51 locale).format(args);
52 return message;
53 }
54
55
56
57
58
59
60
61
62
63 public static String getMessage(
64 final ValidatorAction action,
65 final Field field,
66 final FacesContext context)
67 {
68 return getMessage(
69 action,
70 getArgs(
71 action.getName(),
72 field),
73 context);
74 }
75
76
77
78
79
80
81
82
83 public static String[] getArgs(
84 final String action,
85 final Field field)
86 {
87 final Arg[] args = field.getArgs(action);
88 final String[] argMessages = new String[args.length];
89 for (int ctr = 0; ctr < args.length; ctr++)
90 {
91 final Arg arg = args[ctr];
92 if (arg != null)
93 {
94 if (arg.isResource())
95 {
96 argMessages[ctr] = Messages.get(
97 arg.getKey(),
98 null);
99 }
100 else
101 {
102 argMessages[ctr] = arg.getKey();
103 }
104 }
105 }
106 return argMessages;
107 }
108 }