Search in sources :

Example 1 with MissingResourceException

use of java.util.MissingResourceException in project camel by apache.

the class BraintreeLogHandler method formatMessage.

private String formatMessage(LogRecord record) {
    String message = record.getMessage();
    if (message != null) {
        ResourceBundle bundle = record.getResourceBundle();
        if (bundle != null) {
            try {
                message = bundle.getString(message);
            } catch (MissingResourceException e) {
            }
        }
        Object[] params = record.getParameters();
        // http://jira.qos.ch/browse/SLF4J-203
        if (params != null && params.length > 0) {
            try {
                message = MessageFormat.format(message, params);
            } catch (IllegalArgumentException e) {
                // see also http://jira.qos.ch/browse/SLF4J-337
                return message;
            }
        }
    } else {
        message = "";
    }
    return message;
}
Also used : MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle)

Example 2 with MissingResourceException

use of java.util.MissingResourceException in project tomcat by apache.

the class Util method message.

static String message(ELContext context, String name, Object... props) {
    Locale locale = null;
    if (context != null) {
        locale = context.getLocale();
    }
    if (locale == null) {
        locale = Locale.getDefault();
        if (locale == null) {
            return "";
        }
    }
    ResourceBundle bundle = ResourceBundle.getBundle("javax.el.LocalStrings", locale);
    try {
        String template = bundle.getString(name);
        if (props != null) {
            template = MessageFormat.format(template, props);
        }
        return template;
    } catch (MissingResourceException e) {
        return "Missing Resource: '" + name + "' for Locale " + locale.getDisplayName();
    }
}
Also used : Locale(java.util.Locale) MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle)

Example 3 with MissingResourceException

use of java.util.MissingResourceException in project tomcat by apache.

the class Localizer method getMessage.

/*
     * Returns the localized error message corresponding to the given error
     * code.
     *
     * If the given error code is not defined in the resource bundle for
     * localized error messages, it is used as the error message.
     *
     * @param errCode Error code to localize
     * @param args Arguments for parametric replacement
     *
     * @return Localized error message
     */
public static String getMessage(String errCode, Object[] args) {
    String errMsg = errCode;
    try {
        errMsg = bundle.getString(errCode);
        if (args != null && args.length > 0) {
            MessageFormat formatter = new MessageFormat(errMsg);
            errMsg = formatter.format(args);
        }
    } catch (MissingResourceException e) {
    }
    return errMsg;
}
Also used : MessageFormat(java.text.MessageFormat) MissingResourceException(java.util.MissingResourceException)

Example 4 with MissingResourceException

use of java.util.MissingResourceException 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 5 with MissingResourceException

use of java.util.MissingResourceException in project jersey by jersey.

the class Localizer method localize.

public String localize(Localizable l) {
    String key = l.getKey();
    if (Localizable.NOT_LOCALIZABLE.equals(key)) {
        // this message is not localizable
        return (String) l.getArguments()[0];
    }
    String bundlename = l.getResourceBundleName();
    try {
        ResourceBundle bundle = _resourceBundles.get(bundlename);
        if (bundle == null) {
            try {
                bundle = ResourceBundle.getBundle(bundlename, _locale);
            } catch (MissingResourceException e) {
                // work around a bug in the com.sun.enterprise.deployment.WebBundleArchivist:
                //   all files with an extension different from .class (hence all the .properties files)
                //   get copied to the top level directory instead of being in the package where they
                //   are defined
                // so, since we can't find the bundle under its proper name, we look for it under
                //   the top-level package
                int i = bundlename.lastIndexOf('.');
                if (i != -1) {
                    String alternateBundleName = bundlename.substring(i + 1);
                    try {
                        bundle = ResourceBundle.getBundle(alternateBundleName, _locale);
                    } catch (MissingResourceException e2) {
                        // try OSGi
                        OsgiRegistry osgiRegistry = ReflectionHelper.getOsgiRegistryInstance();
                        if (osgiRegistry != null) {
                            bundle = osgiRegistry.getResourceBundle(bundlename);
                        } else {
                            final String path = bundlename.replace('.', '/') + ".properties";
                            final URL bundleUrl = ResourceFinder.findEntry(path);
                            if (bundleUrl != null) {
                                try {
                                    bundle = new PropertyResourceBundle(bundleUrl.openStream());
                                } catch (IOException ex) {
                                // ignore
                                }
                            }
                        }
                    }
                }
            }
            if (bundle == null) {
                return getDefaultMessage(l);
            } else {
                _resourceBundles.put(bundlename, bundle);
            }
        }
        if (key == null) {
            key = "undefined";
        }
        String msg;
        try {
            msg = bundle.getString(key);
        } catch (MissingResourceException e) {
            // notice that this may throw a MissingResourceException of its own (caught below)
            msg = bundle.getString("undefined");
        }
        // localize all arguments to the given localizable object
        Object[] args = l.getArguments();
        for (int i = 0; i < args.length; ++i) {
            if (args[i] instanceof Localizable) {
                args[i] = localize((Localizable) args[i]);
            }
        }
        String message = MessageFormat.format(msg, args);
        return message;
    } catch (MissingResourceException e) {
        return getDefaultMessage(l);
    }
}
Also used : OsgiRegistry(org.glassfish.jersey.internal.OsgiRegistry) MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle) PropertyResourceBundle(java.util.PropertyResourceBundle) IOException(java.io.IOException) URL(java.net.URL) PropertyResourceBundle(java.util.PropertyResourceBundle)

Aggregations

MissingResourceException (java.util.MissingResourceException)151 ResourceBundle (java.util.ResourceBundle)77 Locale (java.util.Locale)66 ArrayList (java.util.ArrayList)10 IOException (java.io.IOException)8 MessageFormat (java.text.MessageFormat)8 File (java.io.File)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 SMSException (com.sun.identity.sm.SMSException)6 PropertyResourceBundle (java.util.PropertyResourceBundle)6 Secure.getString (android.provider.Settings.Secure.getString)5 SSOException (com.iplanet.sso.SSOException)5 InputStream (java.io.InputStream)4 ISResourceBundle (com.sun.identity.common.ISResourceBundle)3 Enumeration (java.util.Enumeration)3 Iterator (java.util.Iterator)3 List (java.util.List)3 Set (java.util.Set)3 AttributeSchema (com.sun.identity.sm.AttributeSchema)2