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:org.b3log.latke.service.LangPropsServiceImpl.java

@Override
public Map<String, String> getAll(final Locale locale) {
    Map<String, String> ret = LANGS.get(locale);

    if (null == ret) {
        ret = new HashMap<String, String>();
        ResourceBundle langBundle;

        try {//from w w  w . j  av a 2  s . c o m
            langBundle = ResourceBundle.getBundle(Keys.LANGUAGE, locale);
        } catch (final MissingResourceException e) {
            LOGGER.log(Level.WARN, "{0}, using default locale[{1}] instead",
                    new Object[] { e.getMessage(), Latkes.getLocale() });

            try {
                langBundle = ResourceBundle.getBundle(Keys.LANGUAGE, Latkes.getLocale());
            } catch (final MissingResourceException ex) {
                LOGGER.log(Level.WARN, "{0}, using default lang.properties instead",
                        new Object[] { e.getMessage() });
                langBundle = ResourceBundle.getBundle(Keys.LANGUAGE);
            }
        }

        final Enumeration<String> keys = langBundle.getKeys();

        while (keys.hasMoreElements()) {
            final String key = keys.nextElement();
            final String value = langBundle.getString(key);

            ret.put(key, value);
        }

        LANGS.put(locale, ret);
    }

    return ret;
}

From source file:org.jahia.utils.maven.plugin.resources.GWTDictionaryMojo.java

/**
 * Performs conversion of the property file into JavaScript file.
 * /*from  w ww  .  j  a v a  2  s .c  o m*/
 * @param locale
 *            locale to be used
 * @throws IOException
 *             in case of an error
 */
private void convert(String locale) throws IOException {
    ResourceBundle defBundle = lookupBundle("", "en");
    if (defBundle == null) {
        throw new FileNotFoundException("ERROR : Couldn't find bundle with name " + resourceBundle
                + ".properties nor " + resourceBundle + "_en.properties in " + src + " folder, skipping...");
    }

    ResourceBundle bundle = locale != null ? lookupBundle(locale) : null;

    if (!dest.exists() && !dest.mkdirs()) {
        throw new IOException("Unable to create folder " + dest);
    }
    File target = new File(dest, targetFileName + (locale != null ? "_" + locale : "") + ".js");
    getLog().info("Creating " + target + " ...");
    PrintWriter out = new PrintWriter(target);
    Enumeration<String> keyEnum = defBundle.getKeys();
    List<String> keys = new LinkedList<String>();
    while (keyEnum.hasMoreElements()) {
        keys.add(keyEnum.nextElement());
    }
    Collections.sort(keys);
    out.print("var ");
    out.print(dictionaryName);
    out.print("={");
    if (prettyPrint) {
        out.println();
    }
    for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();) {
        String key = iterator.next();
        String value = bundle != null ? JavaScriptDictionaryMojo.getValue(bundle, key) : null;
        if (value == null) {
            value = JavaScriptDictionaryMojo.getValue(defBundle, key);
        }

        if (value != null) {
            out.append(normalizeKey(key)).append(":\"").append(JavaScriptDictionaryMojo.escape(value))
                    .append("\"");
            if (iterator.hasNext()) {
                out.append(",");
            }
            if (prettyPrint) {
                out.println();
            }
        }
    }
    out.print("};");
    if (prettyPrint) {
        out.println();
    }

    out.flush();
    out.close();
    getLog().info("done");
}

From source file:org.schemaspy.Config.java

/**
 * Returns a {@link Properties} populated with the contents of <code>bundle</code>
 *
 * @param bundle ResourceBundle/*  w w w  .  jav  a 2 s. com*/
 * @return Properties
 */
public static Properties asProperties(ResourceBundle bundle) {
    Properties props = new Properties();
    Enumeration<String> iter = bundle.getKeys();
    while (iter.hasMoreElements()) {
        String key = iter.nextElement();
        props.put(key, bundle.getObject(key));
    }

    return props;
}

From source file:org.b3log.latke.service.LangPropsService.java

/**
 * Gets all language properties as a map by the specified locale.
 *
 * @param locale the specified locale//from   w w w  .  j  a  v a2 s.  c o m
 * @return a map of language configurations
 */
public Map<String, String> getAll(final Locale locale) {
    Map<String, String> ret = LANGS.get(locale);

    if (null == ret) {
        ret = new HashMap<String, String>();
        ResourceBundle langBundle;

        try {
            langBundle = ResourceBundle.getBundle(Keys.LANGUAGE, locale);
        } catch (final MissingResourceException e) {
            LOGGER.log(Level.WARNING, "{0}, using default locale[{1}] instead",
                    new Object[] { e.getMessage(), Latkes.getLocale() });

            try {
                langBundle = ResourceBundle.getBundle(Keys.LANGUAGE, Latkes.getLocale());
            } catch (final MissingResourceException ex) {
                LOGGER.log(Level.WARNING, "{0}, using default lang.properties instead",
                        new Object[] { e.getMessage() });
                langBundle = ResourceBundle.getBundle(Keys.LANGUAGE);
            }
        }

        final Enumeration<String> keys = langBundle.getKeys();

        while (keys.hasMoreElements()) {
            final String key = keys.nextElement();
            final String value = langBundle.getString(key);

            ret.put(key, value);
        }

        LANGS.put(locale, ret);
    }

    return ret;
}

From source file:org.sakaiproject.component.section.SectionManagerHibernateImpl.java

/**
 * {@inheritDoc}/*from   ww  w . java  2 s  .  c  om*/
 */
public List<String> getSectionCategories(String siteContext) {
    ResourceBundle bundle = ResourceBundle.getBundle(CATEGORY_BUNDLE);

    Enumeration keys = bundle.getKeys();
    List<String> categoryIds = new ArrayList<String>();
    while (keys.hasMoreElements()) {
        categoryIds.add(keys.nextElement().toString());
    }
    Collections.sort(categoryIds);
    return categoryIds;
}

From source file:org.displaytag.properties.TableProperties.java

/**
 * Try to load the properties from the local properties file, displaytag.properties, and merge them into the
 * existing properties./*  w  w  w  . jav a 2s  .  c o  m*/
 * @param userLocale the locale from which the properties are to be loaded
 */
private void addProperties(Locale userLocale) {
    ResourceBundle bundle = loadUserProperties(userLocale);

    if (bundle != null) {
        Enumeration keys = bundle.getKeys();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            properties.setProperty(key, bundle.getString(key));
        }
    }
}

From source file:org.jahia.utils.maven.plugin.resources.JavaScriptDictionaryMojo.java

/**
 * Performs conversion of the property file into JavaScript file.
 * /* w  ww  .jav a2s  . c o m*/
 * @param locale
 *            locale to be used
 * @throws IOException
 *             in case of an error
 */
protected void convert(String locale) throws IOException {
    ResourceBundle defBundle = lookupBundle("", "en");
    if (defBundle == null) {
        throw new FileNotFoundException("ERROR : Couldn't find bundle with name " + resourceBundle
                + ".properties nor " + resourceBundle + "_en.properties in " + src + " folder, skipping...");
    }

    ResourceBundle bundle = locale != null ? lookupBundle(locale) : null;

    if (!dest.exists() && !dest.mkdirs()) {
        throw new IOException("Unable to create folder " + dest);
    }
    File target = new File(dest, targetFileName + (locale != null ? "_" + locale : "") + ".js");
    getLog().info("Creating " + target + " ...");
    PrintWriter out = new PrintWriter(target);
    Enumeration<String> keyEnum = defBundle.getKeys();
    List<String> keys = new LinkedList<String>();
    while (keyEnum.hasMoreElements()) {
        keys.add(keyEnum.nextElement());
    }
    Collections.sort(keys);
    out.print("var ");
    out.print(dictionaryName);
    out.print("={");
    if (prettyPrint) {
        out.println();
    }
    for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();) {
        String key = iterator.next();
        String value = bundle != null ? getValue(bundle, key) : null;
        if (value == null) {
            value = getValue(defBundle, key);
        }

        if (value != null) {
            if (quoteKeys) {
                out.append("\"");
            }
            out.append(processKey(key));
            if (quoteKeys) {
                out.append("\"");
            }
            out.append(":\"").append(processValue(value)).append("\"");
            if (iterator.hasNext()) {
                out.append(",");
            }
            if (prettyPrint) {
                out.println();
            }
        }
    }
    out.print("};");
    if (prettyPrint) {
        out.println();
    }

    out.flush();
    out.close();
    getLog().info("done");
}

From source file:org.pentaho.platform.engine.services.runtime.RuntimeContext.java

private static Map createComponentClassMap() {
    Properties knownComponents = new Properties();
    // First, get known plugin names...
    try {/*from   w  ww.  j av  a  2s  . co m*/
        ResourceBundle pluginBundle = ResourceBundle.getBundle(RuntimeContext.PLUGIN_BUNDLE_NAME);
        if (pluginBundle != null) { // Copy the bundle here...
            Enumeration keyEnum = pluginBundle.getKeys();
            String bundleKey = null;
            while (keyEnum.hasMoreElements()) {
                bundleKey = (String) keyEnum.nextElement();
                knownComponents.put(bundleKey, pluginBundle.getString(bundleKey));
            }
        }
    } catch (Exception ex) {
        RuntimeContext.logger
                .warn(Messages.getInstance().getString("RuntimeContext.WARN_NO_PLUGIN_PROPERTIES_BUNDLE")); //$NON-NLS-1$
    }
    // Get overrides...
    //
    // Note - If the override wants to remove an existing "known" plugin,
    // simply adding an empty value will cause the "known" plugin to be removed.
    //
    InputStream is = null;
    try {
        File f = new File(PentahoSystem.getApplicationContext().getSolutionPath("system/plugin.properties"));
        if (f.exists()) {
            is = new FileInputStream(f);
            Properties overrideComponents = new Properties();
            overrideComponents.load(is);
            knownComponents.putAll(overrideComponents); // load over the top of the known properties
        }
    } catch (FileNotFoundException ignored) {
        RuntimeContext.logger
                .warn(Messages.getInstance().getString("RuntimeContext.WARN_NO_PLUGIN_PROPERTIES")); //$NON-NLS-1$
    } catch (IOException ignored) {
        RuntimeContext.logger
                .warn(Messages.getInstance().getString("RuntimeContext.WARN_BAD_PLUGIN_PROPERTIES"), ignored); //$NON-NLS-1$
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            //ignored
        }
    }
    return knownComponents;
}

From source file:org.codice.ddf.catalog.ui.config.ConfigurationApplication.java

public void setI18n(ResourceBundleLocator resourceBundleLocator) {
    try {/*w  ww  .  j a va2 s. c o  m*/
        ResourceBundle resourceBundle = resourceBundleLocator.getBundle(INTRIGUE_BASE_NAME);

        if (resourceBundle != null) {
            Enumeration bundleKeys = resourceBundle.getKeys();

            Map<String, String> keywords = new HashMap<>();

            while (bundleKeys.hasMoreElements()) {
                String key = (String) bundleKeys.nextElement();
                String value = resourceBundle.getString(key);

                keywords.put(key, value);
            }

            i18n = keywords;
        }
    } catch (IOException e) {
        LOGGER.debug("An error occurred while creating class loader to URL for ResourceBundle: {}, {}",
                INTRIGUE_BASE_NAME, Locale.getDefault(), e);
    }
}

From source file:org.sakaiproject.tool.app.scheduler.SchedulerTool.java

/**
 * Sets the ConfigurableJobBeanWrapper to establish the job whose properties are being editted
 * during a job or trigger creation process.
 *
 * @param job//from  w  ww  . ja  v a  2 s. com
 */
public void setConfigurableJobBeanWrapper(ConfigurableJobBeanWrapper job) {
    configurableJobBeanWrapper = job;

    if (job != null) {
        final String rbBase = job.getResourceBundleBase();

        //process the ResourceBundle into a map, b/c JSF won't allow method calls like rb.getString()
        final ResourceBundle rb = ResourceBundle.getBundle(rbBase);

        if (rb != null) {
            configurableJobResources = new HashMap<String, String>();

            final Enumeration keyIt = rb.getKeys();

            while (keyIt.hasMoreElements()) {
                final String key = (String) keyIt.nextElement();

                configurableJobResources.put(key, rb.getString(key));
            }
        } else {
            configurableJobResources = null;
        }
    } else {
        configurableJobResources = null;
    }
    refreshProperties();
}