/** * Creates localized messages of all the constraint violations that has occured. * * <p>The key "<code>Qi4j_ConstraintViolation_<i><strong>CompositeType</strong></code></i>" * will be used to lookup the text formatting pattern from the ResourceBundle, where <strong> * <code><i>CompositeType</i></code></strong> is the class name of the Composite where the * constraint was violated. If such key does not exist, then the key "<code> * Qi4j_ConstraintViolation</code>" will be used, and if that one also doesn't exist, or the * resourceBundle argument is null, then the default patterns will be used; * * <table><tr><th>Type of Composite</th><th>Pattern used</th></tr> * <tr><td>Composite</td> * <td><code>Constraint Violation in {2}.{3} with constraint {4}, in composite \n{0} of type {1}</code></td> * </tr> * <tr><td>EntityComposite</td> * <td><code>Constraint Violation in {2}.{3} with constraint {4}, in entity {1}[id={0}]</code></td> * </tr> * <tr><td>ServiceComposite</td> * <td><code>Constraint Violation in {2}.{3} with constraint {4}, in service {0}</code></td> * </tr> * </table> * * Then format each ConstraintViolation according to such pattern, where the following argument * are passed; * * <table><tr><th>Arg</th><th>Value</th></tr> * <tr> * <td>{0}</td> * <td>Composite instance toString()</td> * </tr> * <tr> * <td>{1}</td> * <td>CompositeType class name</td> * </tr> * <tr> * <td>{2}</td> * <td>MixinType class name</td> * </tr> * <tr> * <td>{3}</td> * <td>MixinType method name</td> * </tr> * <tr> * <td>{4}</td> * <td>Annotation toString()</td> * </tr> * <tr> * <td>{5}</td> * <td>toString() of value passed as the argument, or "null" text if argument was null.</td> * </tr> * </table> * * <p><b>NOTE!!!</b> This class is still under construction and will be modified further. * * @param bundle The ResourceBundle for Localization, or null if default formatting and locale to * be used. * @return An array of localized messages of the violations incurred. */ public String[] localizedMessagesFrom(ResourceBundle bundle) { String pattern = "Constraint violation in {0}.{1} for method ''{3}'' with constraint \"{4}({6})\", for value ''{5}''"; ArrayList<String> list = new ArrayList<String>(); for (ConstraintViolation violation : constraintViolations) { Locale locale; if (bundle != null) { try { pattern = bundle.getString("qi4j.constraint." + mixinTypeName + "." + methodName); } catch (MissingResourceException e1) { try { pattern = bundle.getString("qi4j.constraint"); } catch (MissingResourceException e2) { // ignore. The default pattern will be used. } } locale = bundle.getLocale(); } else { locale = Locale.getDefault(); } MessageFormat format = new MessageFormat(pattern, locale); Annotation annotation = violation.constraint(); String name = violation.name(); Object value = violation.value(); String classes; if (Iterables.count(instanceTypes) == 1) { classes = Iterables.first(instanceTypes).getSimpleName(); } else { classes = "[" + Iterables.<Class<?>>toString( instanceTypes, new Function<Class<?>, String>() { @Override public String map(Class<?> from) { return from.getSimpleName(); } }, ",") + "]"; } Object[] args = new Object[] { instanceToString, classes, mixinTypeName, methodName, annotation.toString(), "" + value, name }; StringBuffer text = new StringBuffer(); format.format(args, text, null); list.add(text.toString()); } String[] result = new String[list.size()]; list.toArray(result); return result; }