e220b52576ead679de393daf4f67b13bb30bd8c3
[simdecs_seam.git] / Diagrama / src / java / org / ufcspa / simdecs / diagram / util / MessageResourceUtils.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package org.ufcspa.simdecs.diagram.util;
6
7 import java.text.MessageFormat;
8 import java.util.Locale;
9 import java.util.MissingResourceException;
10 import java.util.ResourceBundle;
11
12 /**
13  *
14  * @author Maroni
15  */
16 public class MessageResourceUtils {
17
18     protected static ClassLoader getCurrentClassLoader(Object defaultObject) {
19
20         ClassLoader loader = Thread.currentThread().getContextClassLoader();
21
22         if (loader == null) {
23             loader = defaultObject.getClass().getClassLoader();
24         }
25
26         return loader;
27     }
28
29     public static String getMessageResourceString(
30             String bundleName,
31             String key,
32             Object params[],
33             Locale locale) {
34
35         String text = null;
36
37         ResourceBundle bundle =
38                 ResourceBundle.getBundle(bundleName, locale,
39                 getCurrentClassLoader(params));
40
41         try {
42             text = bundle.getString(key);
43         } catch (MissingResourceException e) {
44             text = "?? key " + key + " not found ??";
45         }
46
47         if (params != null) {
48             MessageFormat mf = new MessageFormat(text, locale);
49             text = mf.format(params, new StringBuffer(), null).toString();
50         }
51
52         return text;
53     }
54 }