org.openide.util
Class NbBundle

java.lang.Object
  extended by org.openide.util.NbBundle

public class NbBundle
extends java.lang.Object

Convenience class permitting easy loading of localized resources of various sorts. Extends the functionality of the default Java resource support, and interacts better with class loaders in a multiple-loader system.

Example usage:

 package com.mycom;
 public class Foo {
   // Search for tag Foo_theMessage in /com/mycom/Bundle.properties:
   private static String theMessage = NbBundle.getMessage (Foo.class, "Foo_theMessage");
   // Might also look in /com/mycom/Bundle_de.properties, etc.
 }
 


Nested Class Summary
static interface NbBundle.ClassLoaderFinder
          Deprecated. Useless.
(package private) static class NbBundle.DebugLoader
          Classloader whose special trick is inserting debug information into any *.properties files it loads.
 
Field Summary
(package private) static java.util.Map<java.lang.ClassLoader,java.util.Map<java.lang.String,java.lang.ref.Reference<java.util.ResourceBundle>>> bundleCache
          Cache of resource bundles.
(package private) static java.util.Map<java.lang.ClassLoader,java.util.Map<java.lang.String,java.net.URL>> localizedFileCache
          Cache of URLs for localized files.
 
Constructor Summary
NbBundle()
          Deprecated. There is no reason to instantiate or subclass this class. All methods in it are static.
 
Method Summary
static java.lang.String getBranding()
          Get the current branding token.
static java.util.ResourceBundle getBundle(java.lang.Class clazz)
          Get a resource bundle in the same package as the provided class, with the default locale/branding and the class' own classloader.
static java.util.ResourceBundle getBundle(java.lang.String baseName)
          Get a resource bundle with the default class loader and locale/branding.
static java.util.ResourceBundle getBundle(java.lang.String baseName, java.util.Locale locale)
          Get a resource bundle with the default class loader and branding.
static java.util.ResourceBundle getBundle(java.lang.String baseName, java.util.Locale locale, java.lang.ClassLoader loader)
          Get a resource bundle the hard way.
static java.net.URL getLocalizedFile(java.lang.String baseName, java.lang.String ext)
          Deprecated. Use the nbresloc URL protocol instead. This method does a poor job of handling resources such as /some.dir/res.txt or /some/res.txt.sample.
static java.net.URL getLocalizedFile(java.lang.String baseName, java.lang.String ext, java.util.Locale locale)
          Deprecated. Use the nbresloc URL protocol instead. This method does a poor job of handling resources such as /some.dir/res.txt or /some/res.txt.sample.
static java.net.URL getLocalizedFile(java.lang.String baseName, java.lang.String ext, java.util.Locale locale, java.lang.ClassLoader loader)
          Deprecated. Use the nbresloc URL protocol instead. This method does a poor job of handling resources such as /some.dir/res.txt or /some/res.txt.sample.
static java.lang.String getLocalizedValue(java.util.jar.Attributes attr, java.util.jar.Attributes.Name key)
          Find a localized and/or branded value in a JAR manifest in the default system locale.
static java.lang.String getLocalizedValue(java.util.jar.Attributes attr, java.util.jar.Attributes.Name key, java.util.Locale locale)
          Find a localized and/or branded value in a JAR manifest.
static
<T> T
getLocalizedValue(java.util.Map<java.lang.String,T> table, java.lang.String key)
          Find a localized and/or branded value for a given key in the default system locale.
static
<T> T
getLocalizedValue(java.util.Map<java.lang.String,T> table, java.lang.String key, java.util.Locale locale)
          Find a localized and/or branded value for a given key and locale.
static java.util.Iterator<java.lang.String> getLocalizingSuffixes()
          Get a list of all suffixes used to search for localized/branded resources.
static java.lang.String getMessage(java.lang.Class clazz, java.lang.String resName)
          Finds a localized and/or branded string in a bundle.
static java.lang.String getMessage(java.lang.Class clazz, java.lang.String resName, java.lang.Object param1)
          Finds a localized and/or branded string in a bundle and formats the message by passing requested parameters.
static java.lang.String getMessage(java.lang.Class clazz, java.lang.String resName, java.lang.Object[] arr)
          Finds a localized and/or branded string in a bundle and formats the message by passing requested parameters.
static java.lang.String getMessage(java.lang.Class clazz, java.lang.String resName, java.lang.Object param1, java.lang.Object param2)
          Finds a localized and/or branded string in a bundle and formats the message by passing requested parameters.
static java.lang.String getMessage(java.lang.Class clazz, java.lang.String resName, java.lang.Object param1, java.lang.Object param2, java.lang.Object param3)
          Finds a localized and/or branded string in a bundle and formats the message by passing requested parameters.
static void setBranding(java.lang.String bt)
          Set the current branding token.
static void setClassLoaderFinder(NbBundle.ClassLoaderFinder loaderFinder)
          Deprecated. Useless.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

localizedFileCache

static final java.util.Map<java.lang.ClassLoader,java.util.Map<java.lang.String,java.net.URL>> localizedFileCache
Cache of URLs for localized files. Keeps only weak references to the class loaders.

See Also:
"#9275"

bundleCache

static final java.util.Map<java.lang.ClassLoader,java.util.Map<java.lang.String,java.lang.ref.Reference<java.util.ResourceBundle>>> bundleCache
Cache of resource bundles.

Constructor Detail

NbBundle

@Deprecated
public NbBundle()
Deprecated. There is no reason to instantiate or subclass this class. All methods in it are static.

Do not call.

Method Detail

getBranding

public static java.lang.String getBranding()
Get the current branding token.

Returns:
the branding, or null for none

setBranding

public static void setBranding(java.lang.String bt)
                        throws java.lang.IllegalArgumentException
Set the current branding token. The permitted format, as a regular expression:
[a-z][a-z0-9]*(_[a-z][a-z0-9]*)*

This is normally only called by NetBeans startup code and unit tests. Currently the branding may be specified by passing the --branding command-line option to the launcher.

Parameters:
bt - the new branding, or null to clear
Throws:
java.lang.IllegalArgumentException - if in an incorrect format

getLocalizedFile

@Deprecated
public static java.net.URL getLocalizedFile(java.lang.String baseName,
                                                       java.lang.String ext)
                                     throws java.util.MissingResourceException
Deprecated. Use the nbresloc URL protocol instead. This method does a poor job of handling resources such as /some.dir/res.txt or /some/res.txt.sample.

Get a localized and/or branded file in the default locale with the default class loader.

Note that use of this call is similar to using the URL protocol nbresloc (which may in fact be implemented using the fuller form of the method).

The extension may be null, in which case no final dot will be appended. If it is the empty string, the resource will end in a dot.

Parameters:
baseName - base name of file, as dot-separated path (e.g. some.dir.File)
ext - extension of file (or null)
Returns:
URL of matching localized file
Throws:
java.util.MissingResourceException - if not found

getLocalizedFile

@Deprecated
public static java.net.URL getLocalizedFile(java.lang.String baseName,
                                                       java.lang.String ext,
                                                       java.util.Locale locale)
                                     throws java.util.MissingResourceException
Deprecated. Use the nbresloc URL protocol instead. This method does a poor job of handling resources such as /some.dir/res.txt or /some/res.txt.sample.

Get a localized and/or branded file with the default class loader.

Parameters:
baseName - base name of file, as dot-separated path (e.g. some.dir.File)
ext - extension of file (or null)
locale - locale of file
Returns:
URL of matching localized file
Throws:
java.util.MissingResourceException - if not found

getLocalizedFile

@Deprecated
public static java.net.URL getLocalizedFile(java.lang.String baseName,
                                                       java.lang.String ext,
                                                       java.util.Locale locale,
                                                       java.lang.ClassLoader loader)
                                     throws java.util.MissingResourceException
Deprecated. Use the nbresloc URL protocol instead. This method does a poor job of handling resources such as /some.dir/res.txt or /some/res.txt.sample.

Get a localized and/or branded file.

Parameters:
baseName - base name of file, as dot-separated path (e.g. some.dir.File)
ext - extension of file (or null)
locale - locale of file
loader - class loader to use
Returns:
URL of matching localized file
Throws:
java.util.MissingResourceException - if not found

getLocalizedValue

public static <T> T getLocalizedValue(java.util.Map<java.lang.String,T> table,
                                      java.lang.String key,
                                      java.util.Locale locale)
Find a localized and/or branded value for a given key and locale. Scans through a map to find the most localized match possible. For example:

   findLocalizedValue (hashTable, "keyName", new Locale ("cs_CZ"))
 

This would return the first non-null value obtained from the following tests:

Parameters:
table - mapping from localized strings to objects
key - the key to look for
locale - the locale to use
Returns:
the localized object or null if no key matches

getLocalizedValue

public static <T> T getLocalizedValue(java.util.Map<java.lang.String,T> table,
                                      java.lang.String key)
Find a localized and/or branded value for a given key in the default system locale.

Parameters:
table - mapping from localized strings to objects
key - the key to look for
Returns:
the localized object or null if no key matches
See Also:
getLocalizedValue(Map,String,Locale)

getLocalizedValue

public static java.lang.String getLocalizedValue(java.util.jar.Attributes attr,
                                                 java.util.jar.Attributes.Name key,
                                                 java.util.Locale locale)
Find a localized and/or branded value in a JAR manifest.

Parameters:
attr - the manifest attributes
key - the key to look for (case-insensitive)
locale - the locale to use
Returns:
the value if found, else null

getLocalizedValue

public static java.lang.String getLocalizedValue(java.util.jar.Attributes attr,
                                                 java.util.jar.Attributes.Name key)
Find a localized and/or branded value in a JAR manifest in the default system locale.

Parameters:
attr - the manifest attributes
key - the key to look for (case-insensitive)
Returns:
the value if found, else null

getBundle

public static final java.util.ResourceBundle getBundle(java.lang.String baseName)
                                                throws java.util.MissingResourceException
Get a resource bundle with the default class loader and locale/branding. Caution: getBundle(Class) is generally safer when used from a module as this method relies on the module's classloader to currently be part of the system classloader. NetBeans does add enabled modules to this classloader, however calls to this variant of the method made in ModuleInstall.validate, or made soon after a module is uninstalled (due to background threads) could fail unexpectedly.

Parameters:
baseName - bundle basename
Returns:
the resource bundle
Throws:
java.util.MissingResourceException - if the bundle does not exist

getBundle

public static java.util.ResourceBundle getBundle(java.lang.Class clazz)
                                          throws java.util.MissingResourceException
Get a resource bundle in the same package as the provided class, with the default locale/branding and the class' own classloader. This is the usual style of invocation.

Parameters:
clazz - the class to take the package name from
Returns:
the resource bundle
Throws:
java.util.MissingResourceException - if the bundle does not exist

getBundle

public static final java.util.ResourceBundle getBundle(java.lang.String baseName,
                                                       java.util.Locale locale)
                                                throws java.util.MissingResourceException
Get a resource bundle with the default class loader and branding.

Parameters:
baseName - bundle basename
locale - the locale to use (but still uses default branding)
Returns:
the resource bundle
Throws:
java.util.MissingResourceException - if the bundle does not exist

getBundle

public static final java.util.ResourceBundle getBundle(java.lang.String baseName,
                                                       java.util.Locale locale,
                                                       java.lang.ClassLoader loader)
                                                throws java.util.MissingResourceException
Get a resource bundle the hard way.

Parameters:
baseName - bundle basename
locale - the locale to use (but still uses default branding)
loader - the class loader to use
Returns:
the resource bundle
Throws:
java.util.MissingResourceException - if the bundle does not exist

getMessage

public static java.lang.String getMessage(java.lang.Class clazz,
                                          java.lang.String resName)
                                   throws java.util.MissingResourceException
Finds a localized and/or branded string in a bundle.

Parameters:
clazz - the class to use to locate the bundle
resName - name of the resource to look for
Returns:
the string associated with the resource
Throws:
java.util.MissingResourceException - if either the bundle or the string cannot be found

getMessage

public static java.lang.String getMessage(java.lang.Class clazz,
                                          java.lang.String resName,
                                          java.lang.Object param1)
                                   throws java.util.MissingResourceException
Finds a localized and/or branded string in a bundle and formats the message by passing requested parameters.

Parameters:
clazz - the class to use to locate the bundle
resName - name of the resource to look for
param1 - the argument to use when formatting the message
Returns:
the string associated with the resource
Throws:
java.util.MissingResourceException - if either the bundle or the string cannot be found
See Also:
MessageFormat.format(String,Object[])

getMessage

public static java.lang.String getMessage(java.lang.Class clazz,
                                          java.lang.String resName,
                                          java.lang.Object param1,
                                          java.lang.Object param2)
                                   throws java.util.MissingResourceException
Finds a localized and/or branded string in a bundle and formats the message by passing requested parameters.

Parameters:
clazz - the class to use to locate the bundle
resName - name of the resource to look for
param1 - the argument to use when formatting the message
param2 - the second argument to use for formatting
Returns:
the string associated with the resource
Throws:
java.util.MissingResourceException - if either the bundle or the string cannot be found
See Also:
MessageFormat.format(String,Object[])

getMessage

public static java.lang.String getMessage(java.lang.Class clazz,
                                          java.lang.String resName,
                                          java.lang.Object param1,
                                          java.lang.Object param2,
                                          java.lang.Object param3)
                                   throws java.util.MissingResourceException
Finds a localized and/or branded string in a bundle and formats the message by passing requested parameters.

Parameters:
clazz - the class to use to locate the bundle
resName - name of the resource to look for
param1 - the argument to use when formatting the message
param2 - the second argument to use for formatting
param3 - the third argument to use for formatting
Returns:
the string associated with the resource
Throws:
java.util.MissingResourceException - if either the bundle or the string cannot be found
See Also:
MessageFormat.format(String,Object[])

getMessage

public static java.lang.String getMessage(java.lang.Class clazz,
                                          java.lang.String resName,
                                          java.lang.Object[] arr)
                                   throws java.util.MissingResourceException
Finds a localized and/or branded string in a bundle and formats the message by passing requested parameters.

Parameters:
clazz - the class to use to locate the bundle
resName - name of the resource to look for
arr - array of parameters to use for formatting the message
Returns:
the string associated with the resource
Throws:
java.util.MissingResourceException - if either the bundle or the string cannot be found
See Also:
MessageFormat.format(String,Object[])

getLocalizingSuffixes

public static java.util.Iterator<java.lang.String> getLocalizingSuffixes()
Get a list of all suffixes used to search for localized/branded resources. Based on the default locale and branding, returns the list of suffixes which various NbBundle methods use as the search order. For example, when getBranding() returns branding and the default locale is German, you might get a sequence such as:
  1. "_branding_de"
  2. "_branding"
  3. "_de"
  4. ""

Returns:
a read-only iterator of type String
Since:
1.1.5

setClassLoaderFinder

@Deprecated
public static void setClassLoaderFinder(NbBundle.ClassLoaderFinder loaderFinder)
Deprecated. Useless.

Do not use.

Parameters:
loaderFinder - ignored