| 1 | /** | |
| 2 | * SpamSchutz - simple way to protect your mail addresses from naïve spammers | |
| 3 | * Copyright (C) 2011, Aiki IT | |
| 4 | * <p/> | |
| 5 | * This program is free software: you can redistribute it and/or modify | |
| 6 | * it under the terms of the GNU General Public License as published by | |
| 7 | * the Free Software Foundation, either version 3 of the License, or | |
| 8 | * (at your option) any later version. | |
| 9 | * <p/> | |
| 10 | * This program is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | * GNU General Public License for more details. | |
| 14 | * <p/> | |
| 15 | * You should have received a copy of the GNU General Public License | |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | */ | |
| 18 | package de.aikiit.spamprotector.util; | |
| 19 | ||
| 20 | import com.google.common.base.Strings; | |
| 21 | import lombok.AccessLevel; | |
| 22 | import lombok.NoArgsConstructor; | |
| 23 | import org.apache.logging.log4j.LogManager; | |
| 24 | import org.apache.logging.log4j.Logger; | |
| 25 | ||
| 26 | import java.text.MessageFormat; | |
| 27 | import java.util.Arrays; | |
| 28 | import java.util.Locale; | |
| 29 | import java.util.MissingResourceException; | |
| 30 | import java.util.ResourceBundle; | |
| 31 | ||
| 32 | /** | |
| 33 | * Helper to deal with localizations and ease the usage of | |
| 34 | * resource bundles within spamschutz. | |
| 35 | */ | |
| 36 | @NoArgsConstructor(access = AccessLevel.PRIVATE) | |
| 37 | public final class LocalizationHelper { | |
| 38 | ||
| 39 | /** | |
| 40 | * Logger. | |
| 41 | */ | |
| 42 | private static final Logger LOG = | |
| 43 | LogManager.getLogger(LocalizationHelper.class); | |
| 44 | ||
| 45 | /** | |
| 46 | * ResourceBundle used for this application. | |
| 47 | */ | |
| 48 | private static final String BASE_NAME = "spamprotector"; | |
| 49 | private static final Locale FALLBACK_LOCALE = Locale.GERMANY; | |
| 50 | ||
| 51 | private static ResourceBundle BUNDLE; | |
| 52 | private static Locale LOCALE; | |
| 53 | private static MessageFormat FORMAT; | |
| 54 | ||
| 55 | static { | |
| 56 | setLocale(); | |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Set locale depending on system properties, in case of errors fallback is Locale.GERMANY. | |
| 61 | */ | |
| 62 | public static void setLocale() { | |
| 63 | String userLanguage = System.getProperty("user.language"); | |
| 64 | String userCountry = System.getProperty("user.country"); | |
| 65 | ||
| 66 | LOG.info("Your system emits the following l10n-properties: language={}, country={}", userLanguage, userCountry); | |
| 67 | ||
| 68 |
2
1. setLocale : negated conditional → KILLED 2. setLocale : negated conditional → KILLED |
if (Strings.isNullOrEmpty(userLanguage) || Strings.isNullOrEmpty(userCountry)) { |
| 69 | LOCALE = FALLBACK_LOCALE; | |
| 70 | LOG.info("Falling back to locale {}", LOCALE); | |
| 71 | } else { | |
| 72 | LOCALE = new Locale(userLanguage, userCountry); | |
| 73 | LOG.info("Setting locale to {}", LOCALE); | |
| 74 | } | |
| 75 | ||
| 76 | BUNDLE = ResourceBundle.getBundle(BASE_NAME, LOCALE); | |
| 77 | FORMAT = new MessageFormat(""); | |
| 78 |
1
1. setLocale : removed call to java/text/MessageFormat::setLocale → SURVIVED |
FORMAT.setLocale(LOCALE); |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * Return current application locale. | |
| 83 | * @return the currently set Locale of this application. Fallback is Locale.GERMANY. | |
| 84 | */ | |
| 85 | public static Locale getLocale() { | |
| 86 |
1
1. getLocale : negated conditional → KILLED |
if (LOCALE == null) { |
| 87 | LOG.warn("Returning fallback Locale for Germany - please make sure you've configured your environment correctly via system properties 'user.country'/'user.language'."); | |
| 88 |
1
1. getLocale : replaced return value with null for de/aikiit/spamprotector/util/LocalizationHelper::getLocale → NO_COVERAGE |
return FALLBACK_LOCALE; |
| 89 | } | |
| 90 |
1
1. getLocale : replaced return value with null for de/aikiit/spamprotector/util/LocalizationHelper::getLocale → KILLED |
return LOCALE; |
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * Return currently set language. | |
| 95 | * @return the currently set language | |
| 96 | */ | |
| 97 | public static String getLanguage() { | |
| 98 |
1
1. getLanguage : replaced return value with "" for de/aikiit/spamprotector/util/LocalizationHelper::getLanguage → KILLED |
return getLocale().getLanguage(); |
| 99 | } | |
| 100 | ||
| 101 | /** | |
| 102 | * Helper function to retrieve a given key | |
| 103 | * from the underlying resource bundle. | |
| 104 | * | |
| 105 | * @param key Key to retrieve from the bundle, | |
| 106 | * e.g. <i>spamprotector.foo.title</i> | |
| 107 | * @return Returns the value from the bundle. | |
| 108 | */ | |
| 109 | public static String getBundleString(final String key) { | |
| 110 | LOG.debug("Retrieving key {}", key); | |
| 111 | try { | |
| 112 |
1
1. getBundleString : replaced return value with "" for de/aikiit/spamprotector/util/LocalizationHelper::getBundleString → KILLED |
return BUNDLE.getString(key); |
| 113 | } catch (MissingResourceException mre) { | |
| 114 | LOG.error("Retrieving unknown key {}. Please fix your property files.", key); | |
| 115 |
1
1. getBundleString : replaced return value with "" for de/aikiit/spamprotector/util/LocalizationHelper::getBundleString → KILLED |
return key; |
| 116 | } | |
| 117 | } | |
| 118 | ||
| 119 | /** | |
| 120 | * Helper function to retrieve a given key | |
| 121 | * from the underlying resource bundle while | |
| 122 | * applying localization parameters. | |
| 123 | * | |
| 124 | * @param key Key to retrieve from the bundle, | |
| 125 | * e.g. <i>spamprotector.foo.title</i> | |
| 126 | * @param parameters Object array with all parameters. | |
| 127 | * @return Returns the value from the bundle | |
| 128 | * with the given parameters applied. | |
| 129 | * @see <a href="https://docs.oracle.com/javase/tutorial/i18n/format/messageFormat.html"> | |
| 130 | * I18N-tutorial</a> | |
| 131 | */ | |
| 132 | public static String getParameterizedBundleString(final String key, final Object... parameters) { | |
| 133 | LOG.debug("Applying {} parameters to {}", (parameters == null) ? null : Arrays.toString(parameters), key); | |
| 134 |
1
1. getParameterizedBundleString : removed call to java/text/MessageFormat::applyPattern → KILLED |
FORMAT.applyPattern(getBundleString(key)); |
| 135 |
1
1. getParameterizedBundleString : replaced return value with "" for de/aikiit/spamprotector/util/LocalizationHelper::getParameterizedBundleString → KILLED |
return FORMAT.format(parameters); |
| 136 | } | |
| 137 | ||
| 138 | } | |
Mutations | ||
| 68 |
1.1 2.2 |
|
| 78 |
1.1 |
|
| 86 |
1.1 |
|
| 88 |
1.1 |
|
| 90 |
1.1 |
|
| 98 |
1.1 |
|
| 112 |
1.1 |
|
| 115 |
1.1 |
|
| 134 |
1.1 |
|
| 135 |
1.1 |