/** * Look up the welcome section of the top branding theme. The message keys are translated in the * language of the passed in {@code Locale} * * @param locale The {@code Locale} to use to look up the appropriate messages. * @return An HTML string to be placed in the welcome page. */ public String getWelcomeSections(final Locale locale) { Map<String, String> messageMap = getMessageMap(WELCOME, locale); List<BrandingTheme> brandingThemes = getBrandingThemes(); StringBuilder templateBuilder = new StringBuilder(); for (BrandingTheme theme : brandingThemes) { String template = theme.getWelcomePageSectionTemplate(); String replacedTemplate = template; Matcher keyMatcher = TEMPLATE_PATTERN.matcher(template); while (keyMatcher.find()) { String key = keyMatcher.group(1); // Don't replace {userLocale} here. if (!USER_LOCALE_HOLDER.substring(2, USER_LOCALE_HOLDER.length() - 2).equals(key) && messageMap.get(key) != null) { replacedTemplate = replacedTemplate.replaceAll( "\\{" + key + "\\}", // $NON-NLS-1 //$NON-NLS-2$ messageMap.get(key)); } } replacedTemplate = replacedTemplate.replaceAll(USER_LOCALE_HOLDER, locale.toString()); if (theme.shouldReplaceWelcomePageSectionTemplate()) { // Clear the template builder as the theme wants to replace instead of append to the // template. templateBuilder = new StringBuilder(); } templateBuilder.append(replacedTemplate); } return templateBuilder.toString(); }
/** * Get a list of available {@code BrandingTheme}s. * * @return A {@code List} of {@code BrandingTheme}s. */ public synchronized List<BrandingTheme> getBrandingThemes() { if (themes == null && brandingRootPath.exists() && brandingRootPath.isDirectory() && brandingRootPath.canRead()) { themes = new ArrayList<BrandingTheme>(); List<File> directories = Arrays.asList( brandingRootPath.listFiles( new FileFilter() { @Override public boolean accept(final File file) { return file.isDirectory() && DIRECTORY_PATTERN.matcher(file.getName()).matches(); } })); Collections.sort(directories); for (File directory : directories) { BrandingTheme theme = new BrandingTheme( directory.getAbsolutePath(), brandingRootPath, CURRENT_BRANDING_VERSION); if (theme.load()) { themes.add(theme); } } } return themes != null ? themes : new ArrayList<BrandingTheme>(); }
/** * Returns a Map of String keys and values. * * @param prefix The prefix to use for getting the keys. * @param locale The locale to get the messages for. * @return A {@code Map} of keys and values. */ Map<String, String> getMessageMap(final String prefix, final Locale locale) { List<BrandingTheme> messageThemes = getBrandingThemes(); // We need this map to remove potential duplicate strings from the resource bundles. Map<String, String> keyValues = new HashMap<String, String>(); if (messageThemes != null) { for (BrandingTheme theme : messageThemes) { List<ResourceBundle> bundles = theme.getMessagesBundle(locale); for (ResourceBundle bundle : bundles) { getKeyValuesFromResourceBundle(prefix, keyValues, bundle); } } } return keyValues; }