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.sonar.core.i18n.I18nManager.java

@VisibleForTesting
void doStart(I18nClassloader classloader) {
    this.i18nClassloader = classloader;
    propertyToBundles = Maps.newHashMap();
    for (PluginMetadata plugin : pluginRepository.getMetadata()) {
        try {//from w w  w  .  j a va 2s.  co m
            String bundleKey = BUNDLE_PACKAGE + plugin.getKey();
            ResourceBundle bundle = ResourceBundle.getBundle(bundleKey, Locale.ENGLISH, i18nClassloader);
            Enumeration<String> keys = bundle.getKeys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement();
                propertyToBundles.put(key, bundleKey);
            }
        } catch (MissingResourceException e) {
            // ignore
        }
    }
    LOG.debug(String.format("Loaded %d properties from l10n bundles", propertyToBundles.size()));
}

From source file:org.bizbundles.reverse.ResourcesToExcel.java

/**
 * This method will reverse resource bundle files into an excel spreadsheeet.
 * the spreadsheet will be created with the baseName parameter and the curnet date and time appended to it in 24 hour format.
 *  There will be n number of tabs, one for each resource(property) file found.
 *
 * @param pathToResources Directory where all the resources files are located
 * @param baseName        The basename for the resource bundles
 *//*from   w ww . ja v  a 2s  .  c o  m*/
public void generate(final File pathToResources, final String baseName) {
    assert pathToResources != null : "null file path entered";
    assert pathToResources.exists() : "file does not exists.";
    assert pathToResources.isDirectory() : "File is not a directory";
    assert pathToResources.canRead();
    assert baseName != null || baseName.length() > 0 : "The base name cannot be empty.";
    File files[] = pathToResources.listFiles(new FilenameFilter() {
        public boolean accept(File file, String string) {
            return string.endsWith("properties") && string.startsWith(baseName);
        }
    });
    assert null != files && files.length > 0 : "There were no resources found with the given baseName "
            + baseName;
    WritableWorkbook workbook = null;
    try {
        WritableCellFormat wrapFormat = new WritableCellFormat();
        wrapFormat.setWrap(true);
        CellView cellView = new CellView();
        cellView.setFormat(wrapFormat);
        WorkbookSettings settings = new WorkbookSettings();
        settings.setGCDisabled(true);
        workbook = Workbook.createWorkbook(new File(
                pathToResources + File.separator + baseName + FileNameHelper.getDateTimeString() + ".xls"),
                settings);
        int col1MaxSize = 10;
        int col2MaxSize = 10;
        for (File file : files) {
            if (file.isFile()) {
                String filename = file.getName();
                if (logger.isDebugEnabled()) {
                    logger.debug("Procesing File " + file.getAbsoluteFile());
                }
                int i = 0;
                WritableFont timesboldfont = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD, true);
                WritableCellFormat timesBoldFormat = new WritableCellFormat(timesboldfont);
                WritableSheet sheet = workbook.createSheet(filename, i);
                sheet.getColumnView(1).setSize(60);
                sheet.addCell(new Label(0, 0, KEY, timesBoldFormat));
                sheet.addCell(new Label(1, 0, VALUE, timesBoldFormat));
                sheet.addCell(new Label(2, 0, ACTIVE, timesBoldFormat));
                int row = 1;
                ResourceBundle bundle = new PropertyResourceBundle(new FileInputStream(file));
                Enumeration enums = bundle.getKeys();
                while (enums.hasMoreElements()) {
                    String key = (String) enums.nextElement();
                    String val = bundle.getString(key);
                    col1MaxSize = key.length() > col1MaxSize ? key.length() : col1MaxSize;
                    col2MaxSize = val.length() > col1MaxSize ? val.length() : col1MaxSize;
                    sheet.addCell(new Label(0, row, key));
                    sheet.addCell(new Label(1, row, val, wrapFormat));
                    sheet.addCell(new Label(2, row++, ACTIVE_INDCATOR));
                }
                sheet.setColumnView(1, col2MaxSize + 4);
                sheet.setColumnView(0, col1MaxSize + 4);
            }

        }
        workbook.write();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != workbook) {
            try {
                workbook.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

From source file:org.talend.dataprep.transformation.actions.date.AbstractDate.java

/**
 * @return the Parameters to display for the date related action.
 *//*from  w ww . j  a va2  s.c  o m*/
protected List<Parameter> getParametersForDatePattern() {

    ResourceBundle patterns = ResourceBundle
            .getBundle("org.talend.dataprep.transformation.actions.date.date_patterns", Locale.ENGLISH);
    Enumeration<String> keys = patterns.getKeys();

    List<Item> items = new ArrayList<>();
    Item defaultItem = null;
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        String value = patterns.getString(key);
        Item item = Item.Builder.builder().value(value).label(key).build();
        items.add(item);

        if ("ISO".equals(key)) {
            defaultItem = item;
        }
    }
    if (defaultItem == null) {
        defaultItem = items.get(0);
    }

    items.sort((item, t1) -> item.getLabel().compareTo(t1.getLabel()));

    List<Parameter> parameters = new ArrayList<>();
    parameters.add(SelectParameter.Builder.builder() //
            .name(NEW_PATTERN) //
            .items(items) //
            .item("custom", CUSTOM_PATTERN_PARAMETER) //
            .defaultValue(defaultItem.getValue()) //
            .build());

    return parameters;
}

From source file:org.alfresco.web.app.ResourceBundleWrapper.java

/**
 * @see java.util.ResourceBundle#getKeys()
 *//*from w  ww . j a  va  2  s . c  o  m*/
public Enumeration<String> getKeys() {
    if (getDelegates().size() == 1) {
        return getDelegates().get(0).getKeys();
    } else {
        Vector<String> allKeys = new Vector<String>(100, 2);
        for (ResourceBundle delegate : getDelegates()) {
            Enumeration<String> keys = delegate.getKeys();
            while (keys.hasMoreElements() == true) {
                allKeys.add(keys.nextElement());
            }
        }

        return allKeys.elements();
    }
}

From source file:org.ambiance.azureus.notifier.DownloadNotifier.java

@SuppressWarnings("unchecked")
public DownloadNotifier(AmbianceChain ac, GlobalManager globalManager) throws AmbianceAzureusException {
    this.ac = ac;
    this.globalManager = globalManager;

    ResourceBundle props = ResourceBundle.getBundle("org.ambiance.azureus.notifier.notifier");
    refreshTime = Long.parseLong(props.getString("refresh.time"));

    // Context initialization
    this.ctx = new ContextBase();
    Enumeration keys = props.getKeys();
    while (keys.hasMoreElements()) {
        String key = (String) keys.nextElement();
        ctx.put(key, props.getString(key));
    }/*from  ww w  . j  a va  2 s .  c om*/
}

From source file:org.sonar.core.i18n.DefaultI18n.java

private void initPlugin(String pluginKey) {
    try {/*from   ww w.j  a v a 2 s.  co m*/
        String bundleKey = BUNDLE_PACKAGE + pluginKey;
        ResourceBundle bundle = ResourceBundle.getBundle(bundleKey, Locale.ENGLISH, this.classloader, control);
        Enumeration<String> keys = bundle.getKeys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            propertyToBundles.put(key, bundleKey);
        }
    } catch (MissingResourceException e) {
        // ignore
    }
}

From source file:org.wso2.carbon.ui.taglibs.JSi18n.java

public int doEndTag() throws JspException {
    if (request != null) {
        // Retrieve locale from either session or request headers
        Locale locale = getLocaleFromPageContext(pageContext);

        String jsString = "<script type=\"text/javascript\" src='../yui/build/utilities/utilities.js'></script>"
                + "<script type=\"text/javascript\" src='../yui/build/yahoo/yahoo-min.js'></script>"
                + "<script type=\"text/javascript\" src='../yui/build/json/json-min.js'></script>"
                + "<script type=\"text/javascript\"> var "
                + ((getNamespace() != null) ? getNamespace() + "_" : "") + "tmpPairs = '{";

        boolean firstPair = true;

        // Retrieve the default carbon JS resource bundle
        ResourceBundle defaultBunndle = ResourceBundle.getBundle(ORG_WSO2_CARBON_I18N_JSRESOURCES, locale);

        // Retrieve keys from the default bundle
        for (Enumeration e = defaultBunndle.getKeys(); e.hasMoreElements();) {
            String key = (String) e.nextElement();
            String value = defaultBunndle.getString(key);
            if (firstPair) {
                jsString = jsString + "\"" + key + "\":\"" + value + "\"";
                firstPair = false;/*from w  w w .ja v  a  2s.  c  om*/
            } else {
                jsString = jsString + ",\"" + key + "\":\"" + value + "\"";
            }
        }

        if (resourceBundle != null) {
            // Retrieve the resource bundle
            ResourceBundle bundle = ResourceBundle.getBundle(resourceBundle, locale);

            // Retrieve keys from the user defined bundle
            for (Enumeration e = bundle.getKeys(); e.hasMoreElements();) {
                String key = (String) e.nextElement();
                String value = bundle.getString(key);
                if (firstPair) {
                    jsString = jsString + "\"" + key + "\":\"" + value + "\"";
                    firstPair = false;
                } else {
                    jsString = jsString + ",\"" + key + "\":\"" + value + "\"";
                }
            }
        }

        jsString = jsString + "}'; var " + getI18nObjectName() + " = YAHOO.lang.JSON.parse("
                + ((getNamespace() != null) ? getNamespace() + "_" : "") + "tmpPairs);</script>";

        StringBuffer content = new StringBuffer();
        content.append(jsString);

        // Write to output
        JspWriter writer = pageContext.getOut();
        try {
            writer.write(content.toString());
        } catch (IOException e) {
            String msg = "Cannot write i18n tag content";
            log.error(msg, e);

            try {
                //exit gracefully
                writer.write("");
            } catch (IOException e1) {
                /*do nothing*/}
        }

    }

    return 0;
}

From source file:org.tonguetied.web.servlet.ServletContextInitializer.java

/**
 * Create the list of supported languages used by the web front end and add
 * as a application scope variable./*from  w w w.  j  a  v  a  2s  .co  m*/
 * 
 * @param context the {@linkplain ServletContext} to add the list of 
 * languages
 */
private void loadSupportedLanguages(ServletContext context) {
    // read language.properties
    if (logger.isInfoEnabled())
        logger.info("loading resources from file: " + LANGUAGE_PROPERTIES);
    final ResourceBundle bundle = ResourceBundle.getBundle(LANGUAGE_PROPERTIES);
    Set<String> languageKeys = new TreeSet<String>(Collections.list(bundle.getKeys()));
    if (languageKeys.isEmpty()) {
        logger.warn("Resource file " + LANGUAGE_PROPERTIES + ".properties contains no entries");
        languageKeys.add(Locale.ENGLISH.getLanguage());
    }

    context.setAttribute(KEY_SUPPORTED_LANGUAGES, languageKeys);
}

From source file:org.cybercat.automation.core.ConfigurationManager.java

/**
 * Creates default users described in MetaData.properties file.
 * /*from ww w .  ja v a  2 s. co  m*/
 * @throws PageModelException
 */
public void initXmlRepository() throws PageModelException {
    PersistenceManager persistence = context.getBean(PersistenceManager.class);
    ResourceBundle resource = ResourceManager.getTestMetaData();
    List<Identity> users = persistence.load(Identity.class);
    Enumeration<String> keys = resource.getKeys();
    String key;
    while (keys.hasMoreElements()) {
        key = keys.nextElement();
        if (key.contains("registred.users")
                && (!users.contains(Identity.parseFromString(resource.getString(key))))) {
            persistence.save(Identity.parseFromString(resource.getString(key)));
        }
    }
}

From source file:org.kuali.rice.krad.messages.providers.ResourceMessageProvider.java

/**
 * Iterates through the resource bundles for the give namespace (or the application if namespace is not given)
 * and finds all messages that match the given namespace
 *
 * <p>//  w ww.ja v a2 s .  c  o m
 * If the same resource key is found in more than one bundle, the text from the bundle that is
 * loaded last will be used
 * </p>
 *
 * <p>
 * If the given component code is the default component, resource keys that do not have a component defined
 * and match the given key will also be considered matches
 * </p>
 *
 * @see org.kuali.rice.krad.messages.MessageProvider#getAllMessagesForComponent(java.lang.String, java.lang.String,
 *      java.lang.String)
 */
public Collection<Message> getAllMessagesForComponent(String namespace, String component, String locale) {
    List<ResourceBundle> bundles = getCachedResourceBundles(namespace, locale);

    Map<String, Message> messagesByKey = new HashMap<String, Message>();
    for (ResourceBundle bundle : bundles) {
        Enumeration<String> resourceKeys = bundle.getKeys();
        while (resourceKeys.hasMoreElements()) {
            String resourceKey = resourceKeys.nextElement();

            boolean match = false;
            if (StringUtils.contains(resourceKey,
                    COMPONENT_PLACEHOLDER_BEGIN + component + COMPONENT_PLACEHOLDER_END)) {
                match = true;
            } else if (MessageService.DEFAULT_COMPONENT_CODE.equals(component)
                    && !StringUtils.contains(resourceKey, COMPONENT_PLACEHOLDER_BEGIN)) {
                match = true;
            }

            if (match) {
                String messageText = bundle.getString(resourceKey);

                resourceKey = cleanResourceKey(resourceKey);
                Message message = buildMessage(namespace, component, resourceKey, messageText, locale);
                messagesByKey.put(resourceKey, message);
            }
        }
    }

    return messagesByKey.values();
}