View Javadoc
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   * Retrieves and formats the validation messages.
14   *
15   * @author Chad Brandon
16   */
17  public class ValidatorMessages
18  {
19      /**
20       * Gets the <code>message</code> based on the <code>action</code>
21       * message and the <code>field</code>'s arg objects.
22       *
23       * @param action Validator action
24       * @param args any message arguments to be substituted.
25       * @param context the faces context
26       * @return action message
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       * Gets the message given the action, field and faces context.
57       *
58       * @param action the validator action instance.
59       * @param field the field.
60       * @param context the faces context.
61       * @return the message
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       * Gets the message arguments based on the given
78       * validator <code>action</code> and <code>field</code>.
79       * @param action action name
80       * @param field the validator field
81       * @return message arguments
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 }