Search in sources :

Example 6 with MessageFormat

use of java.text.MessageFormat in project qi4j-sdk by Qi4j.

the class ConstraintViolationException method localizedMessagesFrom.

/**
     * Creates localized messages of all the constraint violations that has occured.
     * <p>
     * The key "<code>Qi4j_ConstraintViolation_<i><strong>CompositeType</strong></i></code>" 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 &nbsp;"<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;
     * </p>
     * <table summary="Localization of constraint vioations.">
     * <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 summary="List of arguments available."><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.
     * </p>
     *
     * @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;
}
Also used : Locale(java.util.Locale) MessageFormat(java.text.MessageFormat) MissingResourceException(java.util.MissingResourceException) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) Function(org.qi4j.functional.Function)

Example 7 with MessageFormat

use of java.text.MessageFormat in project pinpoint by naver.

the class StdoutCommonLogger method format.

private String format(String logLevel, String msg, String exceptionMessage) {
    exceptionMessage = defaultString(exceptionMessage, "");
    MessageFormat messageFormat = new MessageFormat(messagePattern);
    final long date = System.currentTimeMillis();
    Object[] parameter = { date, logLevel, msg, exceptionMessage };
    return messageFormat.format(parameter);
}
Also used : MessageFormat(java.text.MessageFormat)

Example 8 with MessageFormat

use of java.text.MessageFormat in project ninja by ninjaframework.

the class MessagesImpl method get.

@Override
public Optional<String> get(String key, Optional<String> language, Object... params) {
    Configuration configuration = getLanguageConfigurationForLocale(language);
    String value = configuration.getString(key);
    if (value != null) {
        MessageFormat messageFormat = getMessageFormatForLocale(value, language);
        return Optional.of(messageFormat.format(params));
    } else {
        return Optional.empty();
    }
}
Also used : Configuration(org.apache.commons.configuration.Configuration) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) MessageFormat(java.text.MessageFormat)

Example 9 with MessageFormat

use of java.text.MessageFormat in project ninja by ninjaframework.

the class MessagesImpl method getMessageFormatForLocale.

MessageFormat getMessageFormatForLocale(String value, Optional<String> language) {
    Locale locale = lang.getLocaleFromStringOrDefault(language);
    MessageFormat messageFormat = new MessageFormat(value, locale);
    return messageFormat;
}
Also used : Locale(java.util.Locale) MessageFormat(java.text.MessageFormat)

Example 10 with MessageFormat

use of java.text.MessageFormat in project robovm by robovm.

the class OldMessageFormatTest method test_formatToCharacterIteratorLjava_lang_Object.

public void test_formatToCharacterIteratorLjava_lang_Object() {
    // Test for method formatToCharacterIterator(java.lang.Object)
    new Support_MessageFormat("test_formatToCharacterIteratorLjava_lang_Object").t_formatToCharacterIterator();
    try {
        new MessageFormat("{1, number}").formatToCharacterIterator(null);
        fail("NullPointerException was not thrown.");
    } catch (NullPointerException npe) {
    //expected
    }
    try {
        new MessageFormat("{0, time}").formatToCharacterIterator(new Object[] { "" });
        fail("IllegalArgumentException was not thrown.");
    } catch (IllegalArgumentException iae) {
    //expected
    }
}
Also used : Support_MessageFormat(tests.support.Support_MessageFormat) MessageFormat(java.text.MessageFormat) Support_MessageFormat(tests.support.Support_MessageFormat)

Aggregations

MessageFormat (java.text.MessageFormat)237 CertificateException (java.security.cert.CertificateException)27 KeyStoreException (java.security.KeyStoreException)23 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)22 UnrecoverableKeyException (java.security.UnrecoverableKeyException)22 Date (java.util.Date)22 UnrecoverableEntryException (java.security.UnrecoverableEntryException)21 CertStoreException (java.security.cert.CertStoreException)21 X509Certificate (java.security.cert.X509Certificate)17 File (java.io.File)14 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 Format (java.text.Format)12 Support_MessageFormat (tests.support.Support_MessageFormat)12 Certificate (java.security.cert.Certificate)10 ChoiceFormat (java.text.ChoiceFormat)10 DateFormat (java.text.DateFormat)10 MissingResourceException (java.util.MissingResourceException)10 SimpleDateFormat (java.text.SimpleDateFormat)9 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)9