KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > messages > Messages


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jul 19, 2005
14  * @author James Dixon
15  *
16  */

17 package org.pentaho.messages;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.MissingResourceException JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25
26 import org.pentaho.messages.util.LocaleHelper;
27
28 public class Messages {
29     private static final String JavaDoc BUNDLE_NAME = "org.pentaho.locale.messages";//$NON-NLS-1$
30

31     private static final Map JavaDoc locales = Collections.synchronizedMap(new HashMap JavaDoc());
32
33     protected static Map JavaDoc getLocales() {
34         return locales;
35     }
36
37     private static ResourceBundle JavaDoc getBundle() {
38         Locale JavaDoc locale = LocaleHelper.getLocale();
39         ResourceBundle JavaDoc bundle = (ResourceBundle JavaDoc) locales.get(locale);
40         if (bundle == null) {
41             bundle = ResourceBundle.getBundle(BUNDLE_NAME, locale);
42             locales.put(locale, bundle);
43         }
44         return bundle;
45     }
46
47     public static String JavaDoc getEncodedString(String JavaDoc rawValue) {
48         if ( rawValue == null ) {
49             return( "" ); //$NON-NLS-1$
50
}
51
52         StringBuffer JavaDoc value = new StringBuffer JavaDoc();
53         for (int n = 0; n < rawValue.length(); n++) {
54             int charValue = rawValue.charAt(n);
55             if (charValue >= 0x80) {
56                 value.append("&#x"); //$NON-NLS-1$
57
value.append(Integer.toString(charValue, 0x10));
58                 value.append(";"); //$NON-NLS-1$
59
} else {
60                 value.append((char) charValue);
61             }
62         }
63         return value.toString();
64
65     }
66
67     public static String JavaDoc getXslString(String JavaDoc key) {
68         String JavaDoc rawValue = getString(key);
69         return getEncodedString(rawValue);
70     }
71
72     public static String JavaDoc getString(String JavaDoc key) {
73         try {
74             return getBundle().getString(key);
75         } catch (MissingResourceException JavaDoc e) {
76             return '!' + key + '!';
77         }
78     }
79
80     public static String JavaDoc getString(String JavaDoc key, String JavaDoc param1) {
81         return MessageUtil.getString(getBundle(), key, param1);
82     }
83
84     public static String JavaDoc getString(String JavaDoc key, String JavaDoc param1, String JavaDoc param2) {
85         return MessageUtil.getString(getBundle(), key, param1, param2);
86     }
87
88     public static String JavaDoc getString(String JavaDoc key, String JavaDoc param1, String JavaDoc param2, String JavaDoc param3) {
89         return MessageUtil.getString(getBundle(), key, param1, param2, param3);
90     }
91
92     public static String JavaDoc getString(String JavaDoc key, String JavaDoc param1, String JavaDoc param2, String JavaDoc param3, String JavaDoc param4) {
93         return MessageUtil.getString(getBundle(), key, param1, param2, param3, param4);
94     }
95
96     public static String JavaDoc getErrorString(String JavaDoc key) {
97         return MessageUtil.formatErrorMessage(key, getString(key));
98     }
99
100     public static String JavaDoc getErrorString(String JavaDoc key, String JavaDoc param1) {
101         return MessageUtil.getErrorString(getBundle(), key, param1);
102     }
103
104     public static String JavaDoc getErrorString(String JavaDoc key, String JavaDoc param1, String JavaDoc param2) {
105         return MessageUtil.getErrorString(getBundle(), key, param1, param2);
106     }
107
108     public static String JavaDoc getErrorString(String JavaDoc key, String JavaDoc param1, String JavaDoc param2, String JavaDoc param3) {
109         return MessageUtil.getErrorString(getBundle(), key, param1, param2, param3);
110     }
111
112 }
Popular Tags