Example usage for java.util ResourceBundle getKeys

List of usage examples for java.util ResourceBundle getKeys

Introduction

In this page you can find the example usage for java.util ResourceBundle getKeys.

Prototype

public abstract Enumeration<String> getKeys();

Source Link

Document

Returns an enumeration of the keys.

Usage

From source file:edu.umich.its.lti.google.GoogleLtiServlet.java

private Map<String, String> convertResourceBundleToMap(ResourceBundle resource) {
    Map<String, String> map = new HashMap<String, String>();

    Enumeration<String> keys = resource.getKeys();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        map.put(key, resource.getString(key));
    }/*from w ww. jav a 2  s . co  m*/

    return map;
}

From source file:org.netbeans.modules.trintejs.json.JSONObject.java

/**
 * Construct a JSONObject from a ResourceBundle.
 * @param baseName The ResourceBundle base name.
 * @param locale The Locale to load the ResourceBundle for.
 * @throws JSONException If any JSONExceptions are detected.
 *//*w  w w  .j a  v  a2  s  . c  o m*/
public JSONObject(String baseName, Locale locale) throws JSONException {
    this();
    ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
            Thread.currentThread().getContextClassLoader());

    // Iterate through the keys in the bundle.

    Enumeration keys = bundle.getKeys();
    while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        if (key instanceof String) {

            // Go through the path, ensuring that there is a nested JSONObject for each
            // segment except the last. Add the value using the last segment's name into
            // the deepest nested JSONObject.

            String[] path = ((String) key).split("\\.");
            int last = path.length - 1;
            JSONObject target = this;
            for (int i = 0; i < last; i += 1) {
                String segment = path[i];
                JSONObject nextTarget = target.optJSONObject(segment);
                if (nextTarget == null) {
                    nextTarget = new JSONObject();
                    target.put(segment, nextTarget);
                }
                target = nextTarget;
            }
            target.put(path[last], bundle.getString((String) key));
        }
    }
}

From source file:org.openx.data.jsonserde.json.JSONObject.java

/**
 * Construct a JSONObject from a ResourceBundle.
 * @param baseName The ResourceBundle base name.
 * @param locale The Locale to load the ResourceBundle for.
 * @throws JSONException If any JSONExceptions are detected.
 */// w  w  w  .  j av a2  s  .c  o  m
public JSONObject(String baseName, Locale locale) throws JSONException {
    this();
    ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
            Thread.currentThread().getContextClassLoader());

    // Iterate through the keys in the bundle.

    Enumeration keys = bundle.getKeys();
    while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        if (key instanceof String) {

            // Go through the path, ensuring that there is a nested JSONObject for each 
            // segment except the last. Add the value using the last segment's name into
            // the deepest nested JSONObject.

            String[] path = ((String) key).split("\\.");
            int last = path.length - 1;
            JSONObject target = this;
            for (int i = 0; i < last; i += 1) {
                String segment = path[i];
                JSONObject nextTarget = target.optJSONObject(segment);
                if (nextTarget == null) {
                    nextTarget = new JSONObject();
                    target.put(segment.toLowerCase(), nextTarget);
                }
                target = nextTarget;
            }
            target.put(path[last].toLowerCase(), bundle.getString((String) key));
        }
    }
}

From source file:com.jskaleel.xml.JSONObject.java

/**
 * Construct a JSONObject from a ResourceBundle.
 *
 * @param baseName//from w w  w  . ja  v  a 2  s .c  o m
 *            The ResourceBundle base name.
 * @param locale
 *            The Locale to load the ResourceBundle for.
 * @throws JSONException
 *             If any JSONExceptions are detected.
 */
public JSONObject(String baseName, Locale locale) throws JSONException {
    this();
    ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
            Thread.currentThread().getContextClassLoader());

    // Iterate through the keys in the bundle.

    Enumeration<String> keys = bundle.getKeys();
    while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        if (key != null) {

            // Go through the path, ensuring that there is a nested JSONObject for each
            // segment except the last. Add the value using the last segment's name into
            // the deepest nested JSONObject.

            String[] path = ((String) key).split("\\.");
            int last = path.length - 1;
            JSONObject target = this;
            for (int i = 0; i < last; i += 1) {
                String segment = path[i];
                JSONObject nextTarget = target.optJSONObject(segment);
                if (nextTarget == null) {
                    nextTarget = new JSONObject();
                    target.put(segment, nextTarget);
                }
                target = nextTarget;
            }
            target.put(path[last], bundle.getString((String) key));
        }
    }
}

From source file:com.ng.mats.psa.mt.paga.data.JSONObject.java

/**
 * Construct a JSONObject from a ResourceBundle.
 * /* w  w w . j  av  a2s.  co  m*/
 * @param baseName
 *            The ResourceBundle base name.
 * @param locale
 *            The Locale to load the ResourceBundle for.
 * @throws JSONException
 *             If any JSONExceptions are detected.
 */
public JSONObject(String baseName, Locale locale) throws JSONException {
    this();
    ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
            Thread.currentThread().getContextClassLoader());

    // Iterate through the keys in the bundle.

    Enumeration<String> keys = bundle.getKeys();
    while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        if (key != null) {

            // Go through the path, ensuring that there is a nested
            // JSONObject for each
            // segment except the last. Add the value using the last
            // segment's name into
            // the deepest nested JSONObject.

            String[] path = ((String) key).split("\\.");
            int last = path.length - 1;
            JSONObject target = this;
            for (int i = 0; i < last; i += 1) {
                String segment = path[i];
                JSONObject nextTarget = target.optJSONObject(segment);
                if (nextTarget == null) {
                    nextTarget = new JSONObject();
                    target.put(segment, nextTarget);
                }
                target = nextTarget;
            }
            target.put(path[last], bundle.getString((String) key));
        }
    }
}

From source file:org.alfresco.repo.i18n.MessageServiceImpl.java

/**
 * Get the messages for a locale./*from   w w  w .j a va 2  s .c  o m*/
 * <p>
 * Will use cache where available otherwise will load into cache from bundles.
 *
 * @param locale    the locale
 * @return          message map
 */
private Map<String, String> getLocaleProperties(Locale locale) {
    Set<String> loadedBundles = null;
    Map<String, String> props = null;

    int loadedBundleCount = 0;

    String tenantDomain = getTenantDomain();
    boolean init = false;

    Map<Locale, Set<String>> tenantLoadedResourceBundles = null;
    Map<Locale, Map<String, String>> tenantCachedMessages = null;
    Set<String> tenantResourceBundleBaseNames = null;

    LockHelper.tryLock(readLock, tryLockTimeout,
            "getting loaded resource bundles, messages and base names in 'MessageServiceImpl.getLocaleProperties()'");
    try {
        tenantLoadedResourceBundles = getLoadedResourceBundles(tenantDomain, true);
        loadedBundles = tenantLoadedResourceBundles.get(locale);

        tenantCachedMessages = getMessages(tenantDomain, true);
        props = tenantCachedMessages.get(locale);

        tenantResourceBundleBaseNames = getResourceBundleBaseNames(tenantDomain, false, false);
        loadedBundleCount = tenantResourceBundleBaseNames.size();
    } finally {
        readLock.unlock();
    }

    if (loadedBundles == null) {
        LockHelper.tryLock(writeLock, tryLockTimeout,
                "adding resource bundle for locale in 'MessageServiceImpl.getLocaleProperties()'");
        try {
            loadedBundles = new HashSet<String>();
            tenantLoadedResourceBundles.put(locale, loadedBundles);
            putLoadedResourceBundles(tenantDomain, tenantLoadedResourceBundles);
            init = true;
        } finally {
            writeLock.unlock();
        }
    }

    if (props == null) {
        LockHelper.tryLock(writeLock, tryLockTimeout,
                "adding resource bundle properties into the cache (because properties are not cached) in 'MessageServiceImpl.getLocaleProperties()'");
        try {
            props = new HashMap<String, String>();
            tenantCachedMessages.put(locale, props);
            putMessages(tenantDomain, tenantCachedMessages);
            init = true;
        } finally {
            writeLock.unlock();
        }
    }

    if ((loadedBundles.size() != loadedBundleCount) || (init == true)) {
        LockHelper.tryLock(writeLock, tryLockTimeout,
                "searching resource bundle and adding new resource bundle for locale if the bundle is not found in 'MessageServiceImpl.getLocaleProperties()'");
        try {
            // get registered resource bundles               
            Set<String> resBundleBaseNames = getResourceBundleBaseNames(tenantDomain, true, false);

            int count = 0;

            // load resource bundles for given locale (by tenant, if applicable)
            for (String resBundlePath : resBundleBaseNames) {
                if (loadedBundles.contains(resBundlePath) == false) {
                    ResourceBundle resourcebundle = null;

                    int idx1 = resBundlePath.indexOf(StoreRef.URI_FILLER);

                    if (idx1 != -1) {
                        // load from repository
                        int idx2 = resBundlePath.indexOf("/", idx1 + 3);

                        String store = resBundlePath.substring(0, idx2);
                        String path = resBundlePath.substring(idx2);

                        StoreRef storeRef = tenantService.getName(new StoreRef(store));

                        try {
                            resourcebundle = getRepoResourceBundle(storeRef, path, locale);
                        } catch (IOException ioe) {
                            throw new AlfrescoRuntimeException(
                                    "Failed to read message resource bundle from repository " + resBundlePath
                                            + " : " + ioe);
                        }
                    } else {
                        // load from classpath
                        resourcebundle = ResourceBundle.getBundle(resBundlePath, locale);
                    }

                    if (resourcebundle != null) {
                        Enumeration<String> enumKeys = resourcebundle.getKeys();
                        while (enumKeys.hasMoreElements() == true) {
                            String key = enumKeys.nextElement();
                            props.put(key, resourcebundle.getString(key));
                        }

                        loadedBundles.add(resBundlePath);
                        count++;
                    }
                }
            }

            logger.info("Message bundles (x " + count + ") loaded for locale " + locale);
        } finally {
            writeLock.unlock();
        }
    }

    return props;
}

From source file:org.alfresco.repo.i18n.MessageServiceImpl.java

public void unregisterResourceBundle(String resBundlePath) {
    Map<Locale, Set<String>> loadedResourceBundlesForAllLocales;
    Map<Locale, Map<String, String>> cachedMessagesForAllLocales;
    Set<String> resourceBundleBaseNamesForAllLocales;

    String tenantDomain = getTenantDomain();
    LockHelper.tryLock(readLock, tryLockTimeout,
            "getting loaded resource bundles, messages and base names in 'MessageServiceImpl.unregisterResourceBundle()'");
    try {/*from w  w w.j a va2s  . c  o  m*/
        // all locales
        loadedResourceBundlesForAllLocales = getLoadedResourceBundles(tenantDomain, false);
        cachedMessagesForAllLocales = getMessages(tenantDomain, false);
        resourceBundleBaseNamesForAllLocales = getResourceBundleBaseNames(tenantDomain, false, true);
    } finally {
        readLock.unlock();
    }

    LockHelper.tryLock(writeLock, tryLockTimeout,
            "removing resource bundle by path in 'MessageServiceImpl.unregisterResourceBundle()'");
    try {
        // unload resource bundles for each locale (by tenant, if applicable)        
        if ((loadedResourceBundlesForAllLocales != null) && (cachedMessagesForAllLocales != null)) {
            Iterator<Locale> itr = loadedResourceBundlesForAllLocales.keySet().iterator();

            while (itr.hasNext()) {
                Locale locale = itr.next();

                Set<String> loadedBundles = loadedResourceBundlesForAllLocales.get(locale);
                Map<String, String> props = cachedMessagesForAllLocales.get(locale);

                if ((loadedBundles != null) && (props != null)) {
                    if (loadedBundles.contains(resBundlePath)) {
                        ResourceBundle resourcebundle = null;

                        int idx1 = resBundlePath.indexOf(StoreRef.URI_FILLER);

                        if (idx1 != -1) {
                            // load from repository
                            int idx2 = resBundlePath.indexOf("/", idx1 + 3);

                            String store = resBundlePath.substring(0, idx2);
                            String path = resBundlePath.substring(idx2);

                            StoreRef storeRef = tenantService.getName(new StoreRef(store));

                            try {
                                resourcebundle = getRepoResourceBundle(storeRef, path, locale);
                            } catch (IOException ioe) {
                                throw new AlfrescoRuntimeException(
                                        "Failed to read message resource bundle from repository "
                                                + resBundlePath + " : " + ioe);
                            }
                        } else {
                            // load from classpath
                            resourcebundle = ResourceBundle.getBundle(resBundlePath, locale);
                        }

                        if (resourcebundle != null) {
                            // unload from the cached messages
                            Enumeration<String> enumKeys = resourcebundle.getKeys();
                            while (enumKeys.hasMoreElements() == true) {
                                String key = enumKeys.nextElement();
                                props.remove(key);
                            }
                        }

                        loadedBundles.remove(resBundlePath);
                    }
                }
            }
        }

        // unregister resource bundle
        if (resourceBundleBaseNamesForAllLocales != null) {
            resourceBundleBaseNamesForAllLocales.remove(resBundlePath);
            logger.info("Unregistered message bundle '" + resBundlePath + "'");
        }

        clearLoadedResourceBundles(tenantDomain); // force re-load of message cache
    } finally {
        writeLock.unlock();
    }
}

From source file:org.springframework.extensions.webscripts.AbstractWebScript.java

/**
 * Helper to render a bundle of webscript I18N resources as a JSON object
 * //w  w  w .  ja va  2  s .co  m
 * @param resources     To render - can be null if no resources present,
 *                      in which case an empty JSON object will be output.
 * 
 * @return JSON object string
 */
private String renderJSONResources(ResourceBundle resources) {
    String result = "{}";
    if (resources != null) {
        final Locale locale = I18NUtil.getLocale();

        String cacheKey = locale.toString();
        if (resources instanceof WebScriptPropertyResourceBundle) {
            // Add a String of all the additional paths merged into the bundle to ensure that we do not 
            // retrieve stale cached data. It is important that we always return bundle data that is 
            // specific to the request as each request might result in different modules being applied
            // and therefore different bundles being merged together...
            cacheKey = cacheKey + "_" + ((WebScriptPropertyResourceBundle) resources).getMergedBundlePaths();
        }

        this.jsonResourcesLock.readLock().lock();
        try {
            result = jsonResources.get(cacheKey);
        } finally {
            this.jsonResourcesLock.readLock().unlock();
        }
        if (result == null) {
            StringBuilderWriter buf = new StringBuilderWriter(256);
            JSONWriter out = new JSONWriter(buf);
            try {
                out.startObject();
                Enumeration<String> keys = resources.getKeys();
                while (keys.hasMoreElements()) {
                    String key = keys.nextElement();
                    out.writeValue(key, resources.getString(key));
                }
                out.endObject();
            } catch (IOException jsonErr) {
                throw new WebScriptException("Error rendering I18N resources.", jsonErr);
            }
            result = buf.toString();

            this.jsonResourcesLock.writeLock().lock();
            try {
                this.jsonResources.put(cacheKey, result);
            } finally {
                this.jsonResourcesLock.writeLock().unlock();
            }
        }
    }

    return result;
}

From source file:de.dmarcini.submatix.pclogger.gui.MainCommGUI.java

/**
 * verfgbare Sprachen in Men eintragen Project: SubmatixBTConfigPC Package: de.dmarcini.submatix.pclogger.gui
 * //from   w ww .j a  v a  2 s  .co  m
 * @author Dirk Marciniak (dirk_marciniak@arcor.de) Stand: 18.12.2011
 * @param programLocale
 */
private void initLanuageMenu(Locale programLocale) {
    ResourceBundle rb;
    Enumeration<String> enu;
    String key = null;
    String cmd = null;
    try {
        lg.debug("try init language menu...");
        ignoreAction = true;
        // Lies die Resource aus
        rb = ResourceBundle.getBundle("de.dmarcini.submatix.pclogger.lang.languages");
        // Alle KEYS lesen
        enu = rb.getKeys();
        try {
            lg.debug("try init language menuitems...");
            while (enu.hasMoreElements()) {
                JMenuItem menuItem = new JMenuItem();
                key = enu.nextElement();
                cmd = rb.getString(key);
                menuItem.setText(key);
                menuItem.addActionListener(this);
                menuItem.setActionCommand(cmd);
                menuItem.addMouseMotionListener(this);
                mnLanguages.add(menuItem);
            }
            lg.debug("try init language menuitems...done");
        } catch (NullPointerException ex) {
            lg.error("NULL POINTER EXCEPTION <" + ex.getMessage() + ">");
            statusTextField.setText("ERROR set language strings");
            System.out.println("ERROR set language strings <" + ex.getMessage() + "> ABORT!");
            System.exit(-1);
        } catch (MissingResourceException ex) {
            lg.error("MISSING RESOURCE EXCEPTION <" + ex.getMessage() + ">");
            statusTextField.setText("ERROR set language strings");
            System.out.println("ERROR set language strings <" + ex.getMessage() + "> ABORT!");
            System.exit(-1);
        } catch (ClassCastException ex) {
            lg.error("CLASS CAST EXCEPTION <" + ex.getMessage() + ">");
            statusTextField.setText("ERROR set language strings");
            System.out.println("ERROR set language strings <" + ex.getMessage() + "> ABORT!");
            System.exit(-1);
        } finally {
            ignoreAction = false;
        }
    } catch (NullPointerException ex) {
        lg.error("NULL POINTER EXCEPTION <" + ex.getMessage() + ">");
        statusTextField.setText("ERROR set language strings");
        System.out.println("ERROR set language strings <" + ex.getMessage() + "> ABORT!");
        System.exit(-1);
    } catch (MissingResourceException ex) {
        lg.error("MISSING RESOURCE EXCEPTION <" + ex.getMessage() + ">");
        statusTextField.setText("ERROR set language strings - the given key can be found");
        System.out.println(
                "ERROR set language strings - the given key can be found <" + ex.getMessage() + "> ABORT!");
        System.exit(-1);
    } catch (ClassCastException ex) {
        lg.error("CLASS CAST EXCEPTION <" + ex.getMessage() + ">");
        statusTextField.setText("ERROR set language strings");
        System.out.println("ERROR set language strings <" + ex.getMessage() + "> ABORT!");
        System.exit(-1);
    } finally {
        ignoreAction = false;
    }
    lg.debug("try init language menu...done");
}

From source file:davmail.exchange.ExchangeSession.java

/**
 * Convert keyword value to IMAP flag.//from  w w  w.ja  v a 2s .  c  o m
 *
 * @param value keyword value
 * @return IMAP flag
 */
public String convertKeywordToFlag(String value) {
    // first test for keyword in settings
    Properties flagSettings = Settings.getSubProperties("davmail.imapFlags");
    Enumeration flagSettingsEnum = flagSettings.propertyNames();
    while (flagSettingsEnum.hasMoreElements()) {
        String key = (String) flagSettingsEnum.nextElement();
        if (value.equalsIgnoreCase(flagSettings.getProperty(key))) {
            return key;
        }
    }

    ResourceBundle flagBundle = ResourceBundle.getBundle("imapflags");
    Enumeration<String> flagBundleEnum = flagBundle.getKeys();
    while (flagBundleEnum.hasMoreElements()) {
        String key = flagBundleEnum.nextElement();
        if (value.equalsIgnoreCase(flagBundle.getString(key))) {
            return key;
        }
    }

    // fall back to raw value
    return value;
}