001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Jul 19, 2005
014: * @author James Dixon
015: *
016: */
017: package org.pentaho.messages;
018:
019: import java.util.Collections;
020: import java.util.HashMap;
021: import java.util.Locale;
022: import java.util.Map;
023: import java.util.MissingResourceException;
024: import java.util.ResourceBundle;
025:
026: import org.pentaho.messages.util.LocaleHelper;
027:
028: public class Messages {
029: private static final String BUNDLE_NAME = "org.pentaho.locale.messages";//$NON-NLS-1$
030:
031: private static final Map locales = Collections
032: .synchronizedMap(new HashMap());
033:
034: protected static Map getLocales() {
035: return locales;
036: }
037:
038: private static ResourceBundle getBundle() {
039: Locale locale = LocaleHelper.getLocale();
040: ResourceBundle bundle = (ResourceBundle) locales.get(locale);
041: if (bundle == null) {
042: bundle = ResourceBundle.getBundle(BUNDLE_NAME, locale);
043: locales.put(locale, bundle);
044: }
045: return bundle;
046: }
047:
048: public static String getEncodedString(String rawValue) {
049: if (rawValue == null) {
050: return (""); //$NON-NLS-1$
051: }
052:
053: StringBuffer value = new StringBuffer();
054: for (int n = 0; n < rawValue.length(); n++) {
055: int charValue = rawValue.charAt(n);
056: if (charValue >= 0x80) {
057: value.append("&#x"); //$NON-NLS-1$
058: value.append(Integer.toString(charValue, 0x10));
059: value.append(";"); //$NON-NLS-1$
060: } else {
061: value.append((char) charValue);
062: }
063: }
064: return value.toString();
065:
066: }
067:
068: public static String getXslString(String key) {
069: String rawValue = getString(key);
070: return getEncodedString(rawValue);
071: }
072:
073: public static String getString(String key) {
074: try {
075: return getBundle().getString(key);
076: } catch (MissingResourceException e) {
077: return '!' + key + '!';
078: }
079: }
080:
081: public static String getString(String key, String param1) {
082: return MessageUtil.getString(getBundle(), key, param1);
083: }
084:
085: public static String getString(String key, String param1,
086: String param2) {
087: return MessageUtil.getString(getBundle(), key, param1, param2);
088: }
089:
090: public static String getString(String key, String param1,
091: String param2, String param3) {
092: return MessageUtil.getString(getBundle(), key, param1, param2,
093: param3);
094: }
095:
096: public static String getString(String key, String param1,
097: String param2, String param3, String param4) {
098: return MessageUtil.getString(getBundle(), key, param1, param2,
099: param3, param4);
100: }
101:
102: public static String getErrorString(String key) {
103: return MessageUtil.formatErrorMessage(key, getString(key));
104: }
105:
106: public static String getErrorString(String key, String param1) {
107: return MessageUtil.getErrorString(getBundle(), key, param1);
108: }
109:
110: public static String getErrorString(String key, String param1,
111: String param2) {
112: return MessageUtil.getErrorString(getBundle(), key, param1,
113: param2);
114: }
115:
116: public static String getErrorString(String key, String param1,
117: String param2, String param3) {
118: return MessageUtil.getErrorString(getBundle(), key, param1,
119: param2, param3);
120: }
121:
122: }
|