private Map loadFully()

in src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java [173:243]


    private Map<String, Object> loadFully(
            final ResourceResolver resolver, Set<String> roots, Set<String> languageRoots) {

        final String[] searchPath = resolver.getSearchPath();

        // for each search path entry, have a list of maps (dictionaries)
        // plus other = "outside the search path" at the end

        //   [0] /apps2  -> [dict1, dict2, dict3 ...]
        //   [1] /apps   -> [dict4, dict5, ...]
        //   [2] /libs   -> [dict6, ...]
        //   [3] (other) -> [dict7, dict8 ...]

        List<List<Map<String, Object>>> dictionariesBySearchPath = new ArrayList<>(searchPath.length + 1);
        for (int i = 0; i < searchPath.length + 1; i++) {
            dictionariesBySearchPath.add(new ArrayList<Map<String, Object>>());
        }

        for (final String root : roots) {

            Resource dictionaryResource = resolver.getResource(root);
            if (dictionaryResource == null) {
                log.warn("Dictionary root found by search not accessible: {}", root);
                continue;
            }

            // linked hash map to keep order (not functionally important, but helpful for dictionary debugging)
            Map<String, Object> dictionary = new LinkedHashMap<>();

            // find where in the search path this dict belongs
            // otherwise put it in the outside-the-search-path bucket (last list)
            List<Map<String, Object>> targetList = dictionariesBySearchPath.get(searchPath.length);
            for (int i = 0; i < searchPath.length; i++) {
                if (root.startsWith(searchPath[i])) {
                    targetList = dictionariesBySearchPath.get(i);
                    break;
                }
            }
            targetList.add(dictionary);

            // check type of dictionary
            if (dictionaryResource.getName().endsWith(".json")) {
                loadJsonDictionary(dictionaryResource, dictionary);
            } else {
                loadSlingMessageDictionary(dictionaryResource, dictionary);
            }

            languageRoots.add(root);
        }

        // linked hash map to keep order (not functionally important, but helpful for dictionary debugging)
        final Map<String, Object> result = new LinkedHashMap<>();

        // first, add everything that's not under a search path (e.g. /content)
        // below, same strings inside a search path dictionary would overlay them since
        // they are added later to result = overwrite
        for (Map<String, Object> dict : dictionariesBySearchPath.get(searchPath.length)) {
            result.putAll(dict);
        }

        // then, in order of the search path, add all the individual dictionaries into
        // a single result, so that e.g. strings in /apps overlay the ones in /libs
        for (int i = searchPath.length - 1; i >= 0; i--) {

            for (Map<String, Object> dict : dictionariesBySearchPath.get(i)) {
                result.putAll(dict);
            }
        }

        return result;
    }