Java Map Merge mergeResourceBundle(final Map map, final String path)

Here you can find the source of mergeResourceBundle(final Map map, final String path)

Description

Merge the resource bundle at the path into the specified map.

License

Open Source License

Parameter

Parameter Description
map the map into which the resources should be merged
path the path to the resource bundle

Declaration

static public void mergeResourceBundle(final Map map, final String path) throws MissingResourceException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**/*from  ww w  .  j  a va  2s . co m*/
     * Merge the resource bundle at the path into the specified map.
     * @param map the map into which the resources should be merged
     * @param path the path to the resource bundle
     */
    static public void mergeResourceBundle(final Map map, final String path) throws MissingResourceException {
        final ResourceBundle bundle = ResourceBundle.getBundle(path);

        final Enumeration<String> keyEnum = bundle.getKeys();
        while (keyEnum.hasMoreElements()) {
            final String key = keyEnum.nextElement();
            final String assignment = bundle.getString(key);

            // if the key begins with "+" then prepend the new assignment onto the existing assignment
            if (key.startsWith("+")) {
                final String baseKey = key.substring(1);
                final String initialAssignment = (String) map.get(baseKey);
                if (initialAssignment != null) {
                    map.put(baseKey, assignment + " - " + initialAssignment);
                } else {
                    map.put(baseKey, assignment);
                }
            }
            // if the key ends with "+" then append the new assignment onto the existing assignment
            else if (key.endsWith("+")) {
                final String baseKey = key.substring(0, key.length() - 1);
                final String initialAssignment = (String) map.get(baseKey);
                if (initialAssignment != null) {
                    map.put(baseKey, initialAssignment + " - " + assignment);
                } else {
                    map.put(baseKey, assignment);
                }
            }
            // if no modifier is found, then simply overwrite the assignment if existing or add a new one
            else {
                map.put(key, assignment);
            }
        }
    }
}

Related

  1. mergeNestableMap(Map original, Map additional)
  2. mergeNsPrefixes(final Map prioritaryPrefixes, final Map additionalPrefixes)
  3. mergeOptions(Map> ssio, Map> allOptions)
  4. mergePermMap(Map> permMap, Map> subPermMap)
  5. mergePropertiesToMap(Properties properties, Map map, boolean overwrite)
  6. mergeTemplate(String template, Map segments)
  7. mergeValue(Map obj, String key, Object value)
  8. mergeValueInMap(Map map, String name, String value)