KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > webapp > WebappResources


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.webapp;
12
13 import java.text.*;
14 import java.util.*;
15
16 import org.eclipse.core.runtime.*;
17
18 /**
19  * Uses a resource bundle to load images and strings from a property file in a
20  * documentation plugin
21  */

22 public class WebappResources {
23
24     // resource bundles indexed by locale
25
private static HashMap resourceBundleTable = new HashMap();
26
27     /**
28      * Returns a string from a property file. It uses 'name' as a the key to
29      * retrieve from the webapp.properties file.
30      */

31     public static String JavaDoc getString(String JavaDoc name, Locale locale) {
32
33         // get bundle
34
ResourceBundle bundle = getBundle(locale);
35         if (bundle == null) {
36             return name;
37         }
38
39         // get value
40
try {
41             return bundle.getString(name);
42         } catch (MissingResourceException mre) {
43             return name;
44         }
45     }
46
47     /**
48      * Returns a string from a property file
49      */

50     public static String JavaDoc getString(String JavaDoc name, Locale locale, String JavaDoc replace0) {
51
52         // get bundle
53
ResourceBundle bundle = getBundle(locale);
54         if (bundle == null) {
55             return name;
56         }
57
58         // get value
59
try {
60             String JavaDoc stringFromPropertiesFile = bundle.getString(name);
61             stringFromPropertiesFile = MessageFormat.format(
62                     stringFromPropertiesFile, new Object JavaDoc[]{replace0});
63             return stringFromPropertiesFile;
64         } catch (Exception JavaDoc e) {
65             return name;
66         }
67
68     }
69     /**
70      * Obtains resource bundle for specified locale. Loads bundle if necessary
71      *
72      * @param locale
73      * Locale or null to use default locale
74      * @return ResourceBundle or null if not found
75      */

76     private static ResourceBundle getBundle(Locale locale) {
77         if (locale == null)
78             locale = getDefaultLocale();
79
80         // check cache
81
ResourceBundle bundle = (ResourceBundle) resourceBundleTable
82                 .get(locale);
83
84         // load bundle
85
if (bundle == null) {
86             bundle = ResourceBundle.getBundle(WebappResources.class.getName(),
87                     locale);
88             if (bundle != null) {
89                 resourceBundleTable.put(locale, bundle);
90             }
91         }
92         return bundle;
93     }
94     private static Locale getDefaultLocale() {
95         String JavaDoc nl = Platform.getNL();
96         // sanity test
97
if (nl == null)
98             return Locale.getDefault();
99
100         // break the string into tokens to get the Locale object
101
StringTokenizer locales = new StringTokenizer(nl, "_"); //$NON-NLS-1$
102
if (locales.countTokens() == 1)
103             return new Locale(locales.nextToken(), ""); //$NON-NLS-1$
104
else if (locales.countTokens() == 2)
105             return new Locale(locales.nextToken(), locales.nextToken());
106         else if (locales.countTokens() == 3)
107             return new Locale(locales.nextToken(), locales.nextToken(), locales
108                     .nextToken());
109         else
110             return Locale.getDefault();
111     }
112 }
113
Popular Tags