| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2017 the original author or authors. | |
| 4 | // | |
| 5 | // This library is free software; you can redistribute it and/or | |
| 6 | // modify it under the terms of the GNU Lesser General Public | |
| 7 | // License as published by the Free Software Foundation; either | |
| 8 | // version 2.1 of the License, or (at your option) any later version. | |
| 9 | // | |
| 10 | // This library 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 GNU | |
| 13 | // Lesser General Public License for more details. | |
| 14 | // | |
| 15 | // You should have received a copy of the GNU Lesser General Public | |
| 16 | // License along with this library; if not, write to the Free Software | |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 | //////////////////////////////////////////////////////////////////////////////// | |
| 19 | ||
| 20 | package com.puppycrawl.tools.checkstyle.checks; | |
| 21 | ||
| 22 | import java.io.File; | |
| 23 | import java.io.FileInputStream; | |
| 24 | import java.io.FileNotFoundException; | |
| 25 | import java.io.IOException; | |
| 26 | import java.io.InputStream; | |
| 27 | import java.util.Arrays; | |
| 28 | import java.util.Collections; | |
| 29 | import java.util.HashSet; | |
| 30 | import java.util.Locale; | |
| 31 | import java.util.Optional; | |
| 32 | import java.util.Properties; | |
| 33 | import java.util.Set; | |
| 34 | import java.util.SortedSet; | |
| 35 | import java.util.TreeSet; | |
| 36 | import java.util.regex.Matcher; | |
| 37 | import java.util.regex.Pattern; | |
| 38 | import java.util.stream.Collectors; | |
| 39 | ||
| 40 | import org.apache.commons.logging.Log; | |
| 41 | import org.apache.commons.logging.LogFactory; | |
| 42 | ||
| 43 | import com.google.common.collect.HashMultimap; | |
| 44 | import com.google.common.collect.SetMultimap; | |
| 45 | import com.google.common.io.Closeables; | |
| 46 | import com.puppycrawl.tools.checkstyle.Definitions; | |
| 47 | import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; | |
| 48 | import com.puppycrawl.tools.checkstyle.api.FileText; | |
| 49 | import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; | |
| 50 | import com.puppycrawl.tools.checkstyle.api.MessageDispatcher; | |
| 51 | import com.puppycrawl.tools.checkstyle.utils.CommonUtils; | |
| 52 | ||
| 53 | /** | |
| 54 | * <p> | |
| 55 | * The TranslationCheck class helps to ensure the correct translation of code by | |
| 56 | * checking locale-specific resource files for consistency regarding their keys. | |
| 57 | * Two locale-specific resource files describing one and the same context are consistent if they | |
| 58 | * contain the same keys. TranslationCheck also can check an existence of required translations | |
| 59 | * which must exist in project, if 'requiredTranslations' option is used. | |
| 60 | * </p> | |
| 61 | * <p> | |
| 62 | * An example of how to configure the check is: | |
| 63 | * </p> | |
| 64 | * <pre> | |
| 65 | * <module name="Translation"/> | |
| 66 | * </pre> | |
| 67 | * Check has the following options: | |
| 68 | * | |
| 69 | * <p><b>baseName</b> - a base name regexp for resource bundles which contain message resources. It | |
| 70 | * helps the check to distinguish config and localization resources. Default value is | |
| 71 | * <b>^messages.*$</b> | |
| 72 | * <p>An example of how to configure the check to validate only bundles which base names start with | |
| 73 | * "ButtonLabels": | |
| 74 | * </p> | |
| 75 | * <pre> | |
| 76 | * <module name="Translation"> | |
| 77 | * <property name="baseName" value="^ButtonLabels.*$"/> | |
| 78 | * </module> | |
| 79 | * </pre> | |
| 80 | * <p>To configure the check to check only files which have '.properties' and '.translations' | |
| 81 | * extensions: | |
| 82 | * </p> | |
| 83 | * <pre> | |
| 84 | * <module name="Translation"> | |
| 85 | * <property name="fileExtensions" value="properties, translations"/> | |
| 86 | * </module> | |
| 87 | * </pre> | |
| 88 | * | |
| 89 | * <p><b>requiredTranslations</b> which allows to specify language codes of required translations | |
| 90 | * which must exist in project. Language code is composed of the lowercase, two-letter codes as | |
| 91 | * defined by <a href="https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO 639-1</a>. | |
| 92 | * Default value is <b>empty String Set</b> which means that only the existence of | |
| 93 | * default translation is checked. Note, if you specify language codes (or just one language | |
| 94 | * code) of required translations the check will also check for existence of default translation | |
| 95 | * files in project. ATTENTION: the check will perform the validation of ISO codes if the option | |
| 96 | * is used. So, if you specify, for example, "mm" for language code, TranslationCheck will rise | |
| 97 | * violation that the language code is incorrect. | |
| 98 | * <br> | |
| 99 | * | |
| 100 | * @author Alexandra Bunge | |
| 101 | * @author lkuehne | |
| 102 | * @author Andrei Selkin | |
| 103 | */ | |
| 104 | public class TranslationCheck extends AbstractFileSetCheck { | |
| 105 | ||
| 106 | /** | |
| 107 | * A key is pointing to the warning message text for missing key | |
| 108 | * in "messages.properties" file. | |
| 109 | */ | |
| 110 | public static final String MSG_KEY = "translation.missingKey"; | |
| 111 | ||
| 112 | /** | |
| 113 | * A key is pointing to the warning message text for missing translation file | |
| 114 | * in "messages.properties" file. | |
| 115 | */ | |
| 116 | public static final String MSG_KEY_MISSING_TRANSLATION_FILE = | |
| 117 | "translation.missingTranslationFile"; | |
| 118 | ||
| 119 | /** Resource bundle which contains messages for TranslationCheck. */ | |
| 120 | private static final String TRANSLATION_BUNDLE = | |
| 121 | "com.puppycrawl.tools.checkstyle.checks.messages"; | |
| 122 | ||
| 123 | /** | |
| 124 | * A key is pointing to the warning message text for wrong language code | |
| 125 | * in "messages.properties" file. | |
| 126 | */ | |
| 127 | private static final String WRONG_LANGUAGE_CODE_KEY = "translation.wrongLanguageCode"; | |
| 128 | ||
| 129 | /** Logger for TranslationCheck. */ | |
| 130 | private static final Log LOG = LogFactory.getLog(TranslationCheck.class); | |
| 131 | ||
| 132 | /** | |
| 133 | * Regexp string for default translation files. | |
| 134 | * For example, messages.properties. | |
| 135 | */ | |
| 136 | private static final String DEFAULT_TRANSLATION_REGEXP = "^.+\\..+$"; | |
| 137 | ||
| 138 | /** | |
| 139 | * Regexp pattern for bundles names wich end with language code, followed by country code and | |
| 140 | * variant suffix. For example, messages_es_ES_UNIX.properties. | |
| 141 | */ | |
| 142 | private static final Pattern LANGUAGE_COUNTRY_VARIANT_PATTERN = | |
| 143 | CommonUtils.createPattern("^.+\\_[a-z]{2}\\_[A-Z]{2}\\_[A-Za-z]+\\..+$"); | |
| 144 | /** | |
| 145 | * Regexp pattern for bundles names wich end with language code, followed by country code | |
| 146 | * suffix. For example, messages_es_ES.properties. | |
| 147 | */ | |
| 148 | private static final Pattern LANGUAGE_COUNTRY_PATTERN = | |
| 149 | CommonUtils.createPattern("^.+\\_[a-z]{2}\\_[A-Z]{2}\\..+$"); | |
| 150 | /** | |
| 151 | * Regexp pattern for bundles names wich end with language code suffix. | |
| 152 | * For example, messages_es.properties. | |
| 153 | */ | |
| 154 | private static final Pattern LANGUAGE_PATTERN = | |
| 155 | CommonUtils.createPattern("^.+\\_[a-z]{2}\\..+$"); | |
| 156 | ||
| 157 | /** File name format for default translation. */ | |
| 158 | private static final String DEFAULT_TRANSLATION_FILE_NAME_FORMATTER = "%s.%s"; | |
| 159 | /** File name format with language code. */ | |
| 160 | private static final String FILE_NAME_WITH_LANGUAGE_CODE_FORMATTER = "%s_%s.%s"; | |
| 161 | ||
| 162 | /** Formatting string to form regexp to validate required translations file names. */ | |
| 163 | private static final String REGEXP_FORMAT_TO_CHECK_REQUIRED_TRANSLATIONS = | |
| 164 | "^%1$s\\_%2$s(\\_[A-Z]{2})?\\.%3$s$|^%1$s\\_%2$s\\_[A-Z]{2}\\_[A-Za-z]+\\.%3$s$"; | |
| 165 | /** Formatting string to form regexp to validate default translations file names. */ | |
| 166 | private static final String REGEXP_FORMAT_TO_CHECK_DEFAULT_TRANSLATIONS = "^%s\\.%s$"; | |
| 167 | ||
| 168 | /** The files to process. */ | |
| 169 | private final Set<File> filesToProcess = new HashSet<>(); | |
| 170 | ||
| 171 | /** The base name regexp pattern. */ | |
| 172 | private Pattern baseName; | |
| 173 | ||
| 174 | /** | |
| 175 | * Language codes of required translations for the check (de, pt, ja, etc). | |
| 176 | */ | |
| 177 | private Set<String> requiredTranslations = new HashSet<>(); | |
| 178 | ||
| 179 | /** | |
| 180 | * Creates a new {@code TranslationCheck} instance. | |
| 181 | */ | |
| 182 | public TranslationCheck() { | |
| 183 |
1
1. |
setFileExtensions("properties"); |
| 184 | baseName = CommonUtils.createPattern("^messages.*$"); | |
| 185 | } | |
| 186 | ||
| 187 | /** | |
| 188 | * Sets the base name regexp pattern. | |
| 189 | * @param baseName base name regexp. | |
| 190 | */ | |
| 191 | public void setBaseName(Pattern baseName) { | |
| 192 | this.baseName = baseName; | |
| 193 | } | |
| 194 | ||
| 195 | /** | |
| 196 | * Sets language codes of required translations for the check. | |
| 197 | * @param translationCodes a comma separated list of language codes. | |
| 198 | */ | |
| 199 | public void setRequiredTranslations(String... translationCodes) { | |
| 200 | requiredTranslations = Arrays.stream(translationCodes).collect(Collectors.toSet()); | |
| 201 |
1
1. setRequiredTranslations : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::validateUserSpecifiedLanguageCodes → KILLED |
validateUserSpecifiedLanguageCodes(requiredTranslations); |
| 202 | } | |
| 203 | ||
| 204 | /** | |
| 205 | * Validates the correctness of user specified language codes for the check. | |
| 206 | * @param languageCodes user specified language codes for the check. | |
| 207 | */ | |
| 208 | private void validateUserSpecifiedLanguageCodes(Set<String> languageCodes) { | |
| 209 |
1
1. validateUserSpecifiedLanguageCodes : negated conditional → KILLED |
for (String code : languageCodes) { |
| 210 |
1
1. validateUserSpecifiedLanguageCodes : negated conditional → KILLED |
if (!isValidLanguageCode(code)) { |
| 211 | final LocalizedMessage msg = new LocalizedMessage(0, TRANSLATION_BUNDLE, | |
| 212 | WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null); | |
| 213 | final String exceptionMessage = String.format(Locale.ROOT, | |
| 214 | "%s [%s]", msg.getMessage(), TranslationCheck.class.getSimpleName()); | |
| 215 | throw new IllegalArgumentException(exceptionMessage); | |
| 216 | } | |
| 217 | } | |
| 218 | } | |
| 219 | ||
| 220 | /** | |
| 221 | * Checks whether user specified language code is correct (is contained in available locales). | |
| 222 | * @param userSpecifiedLanguageCode user specified language code. | |
| 223 | * @return true if user specified language code is correct. | |
| 224 | */ | |
| 225 | private static boolean isValidLanguageCode(final String userSpecifiedLanguageCode) { | |
| 226 | boolean valid = false; | |
| 227 | final Locale[] locales = Locale.getAvailableLocales(); | |
| 228 |
3
1. isValidLanguageCode : changed conditional boundary → KILLED 2. isValidLanguageCode : Changed increment from 1 to -1 → KILLED 3. isValidLanguageCode : negated conditional → KILLED |
for (Locale locale : locales) { |
| 229 |
1
1. isValidLanguageCode : negated conditional → KILLED |
if (userSpecifiedLanguageCode.equals(locale.toString())) { |
| 230 | valid = true; | |
| 231 | break; | |
| 232 | } | |
| 233 | } | |
| 234 |
1
1. isValidLanguageCode : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return valid; |
| 235 | } | |
| 236 | ||
| 237 | @Override | |
| 238 | public void beginProcessing(String charset) { | |
| 239 |
1
1. beginProcessing : removed call to java/util/Set::clear → SURVIVED |
filesToProcess.clear(); |
| 240 | } | |
| 241 | ||
| 242 | @Override | |
| 243 | protected void processFiltered(File file, FileText fileText) { | |
| 244 | // We just collecting files for processing at finishProcessing() | |
| 245 | filesToProcess.add(file); | |
| 246 | } | |
| 247 | ||
| 248 | @Override | |
| 249 | public void finishProcessing() { | |
| 250 | final Set<ResourceBundle> bundles = groupFilesIntoBundles(filesToProcess, baseName); | |
| 251 |
1
1. finishProcessing : negated conditional → KILLED |
for (ResourceBundle currentBundle : bundles) { |
| 252 |
1
1. finishProcessing : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::checkExistenceOfDefaultTranslation → KILLED |
checkExistenceOfDefaultTranslation(currentBundle); |
| 253 |
1
1. finishProcessing : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::checkExistenceOfRequiredTranslations → KILLED |
checkExistenceOfRequiredTranslations(currentBundle); |
| 254 |
1
1. finishProcessing : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::checkTranslationKeys → KILLED |
checkTranslationKeys(currentBundle); |
| 255 | } | |
| 256 | } | |
| 257 | ||
| 258 | /** | |
| 259 | * Checks an existence of default translation file in the resource bundle. | |
| 260 | * @param bundle resource bundle. | |
| 261 | */ | |
| 262 | private void checkExistenceOfDefaultTranslation(ResourceBundle bundle) { | |
| 263 | final Optional<String> fileName = getMissingFileName(bundle, null); | |
| 264 |
1
1. checkExistenceOfDefaultTranslation : negated conditional → KILLED |
if (fileName.isPresent()) { |
| 265 |
1
1. checkExistenceOfDefaultTranslation : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::logMissingTranslation → KILLED |
logMissingTranslation(bundle.getPath(), fileName.get()); |
| 266 | } | |
| 267 | } | |
| 268 | ||
| 269 | /** | |
| 270 | * Checks an existence of translation files in the resource bundle. | |
| 271 | * The name of translation file begins with the base name of resource bundle which is followed | |
| 272 | * by '_' and a language code (country and variant are optional), it ends with the extension | |
| 273 | * suffix. | |
| 274 | * @param bundle resource bundle. | |
| 275 | */ | |
| 276 | private void checkExistenceOfRequiredTranslations(ResourceBundle bundle) { | |
| 277 |
1
1. checkExistenceOfRequiredTranslations : negated conditional → KILLED |
for (String languageCode : requiredTranslations) { |
| 278 | final Optional<String> fileName = getMissingFileName(bundle, languageCode); | |
| 279 |
1
1. checkExistenceOfRequiredTranslations : negated conditional → KILLED |
if (fileName.isPresent()) { |
| 280 |
1
1. checkExistenceOfRequiredTranslations : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::logMissingTranslation → KILLED |
logMissingTranslation(bundle.getPath(), fileName.get()); |
| 281 | } | |
| 282 | } | |
| 283 | } | |
| 284 | ||
| 285 | /** | |
| 286 | * Returns the name of translation file which is absent in resource bundle or Guava's Optional, | |
| 287 | * if there is not missing translation. | |
| 288 | * @param bundle resource bundle. | |
| 289 | * @param languageCode language code. | |
| 290 | * @return the name of translation file which is absent in resource bundle or Guava's Optional, | |
| 291 | * if there is not missing translation. | |
| 292 | */ | |
| 293 | private static Optional<String> getMissingFileName(ResourceBundle bundle, String languageCode) { | |
| 294 | final String fileNameRegexp; | |
| 295 | final boolean searchForDefaultTranslation; | |
| 296 | final String extension = bundle.getExtension(); | |
| 297 | final String baseName = bundle.getBaseName(); | |
| 298 |
1
1. getMissingFileName : negated conditional → KILLED |
if (languageCode == null) { |
| 299 | searchForDefaultTranslation = true; | |
| 300 | fileNameRegexp = String.format(Locale.ROOT, REGEXP_FORMAT_TO_CHECK_DEFAULT_TRANSLATIONS, | |
| 301 | baseName, extension); | |
| 302 | } | |
| 303 | else { | |
| 304 | searchForDefaultTranslation = false; | |
| 305 | fileNameRegexp = String.format(Locale.ROOT, | |
| 306 | REGEXP_FORMAT_TO_CHECK_REQUIRED_TRANSLATIONS, baseName, languageCode, extension); | |
| 307 | } | |
| 308 | Optional<String> missingFileName = Optional.empty(); | |
| 309 |
1
1. getMissingFileName : negated conditional → KILLED |
if (!bundle.containsFile(fileNameRegexp)) { |
| 310 |
1
1. getMissingFileName : negated conditional → KILLED |
if (searchForDefaultTranslation) { |
| 311 | missingFileName = Optional.of(String.format(Locale.ROOT, | |
| 312 | DEFAULT_TRANSLATION_FILE_NAME_FORMATTER, baseName, extension)); | |
| 313 | } | |
| 314 | else { | |
| 315 | missingFileName = Optional.of(String.format(Locale.ROOT, | |
| 316 | FILE_NAME_WITH_LANGUAGE_CODE_FORMATTER, baseName, languageCode, extension)); | |
| 317 | } | |
| 318 | } | |
| 319 |
1
1. getMissingFileName : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/TranslationCheck::getMissingFileName to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return missingFileName; |
| 320 | } | |
| 321 | ||
| 322 | /** | |
| 323 | * Logs that translation file is missing. | |
| 324 | * @param filePath file path. | |
| 325 | * @param fileName file name. | |
| 326 | */ | |
| 327 | private void logMissingTranslation(String filePath, String fileName) { | |
| 328 | final MessageDispatcher dispatcher = getMessageDispatcher(); | |
| 329 |
1
1. logMissingTranslation : removed call to com/puppycrawl/tools/checkstyle/api/MessageDispatcher::fireFileStarted → SURVIVED |
dispatcher.fireFileStarted(filePath); |
| 330 |
1
1. logMissingTranslation : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::log → KILLED |
log(0, MSG_KEY_MISSING_TRANSLATION_FILE, fileName); |
| 331 |
1
1. logMissingTranslation : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::fireErrors → KILLED |
fireErrors(filePath); |
| 332 |
1
1. logMissingTranslation : removed call to com/puppycrawl/tools/checkstyle/api/MessageDispatcher::fireFileFinished → SURVIVED |
dispatcher.fireFileFinished(filePath); |
| 333 | } | |
| 334 | ||
| 335 | /** | |
| 336 | * Groups a set of files into bundles. | |
| 337 | * Only files, which names match base name regexp pattern will be grouped. | |
| 338 | * @param files set of files. | |
| 339 | * @param baseNameRegexp base name regexp pattern. | |
| 340 | * @return set of ResourceBundles. | |
| 341 | */ | |
| 342 | private static Set<ResourceBundle> groupFilesIntoBundles(Set<File> files, | |
| 343 | Pattern baseNameRegexp) { | |
| 344 | final Set<ResourceBundle> resourceBundles = new HashSet<>(); | |
| 345 |
1
1. groupFilesIntoBundles : negated conditional → KILLED |
for (File currentFile : files) { |
| 346 | final String fileName = currentFile.getName(); | |
| 347 | final String baseName = extractBaseName(fileName); | |
| 348 | final Matcher baseNameMatcher = baseNameRegexp.matcher(baseName); | |
| 349 |
1
1. groupFilesIntoBundles : negated conditional → KILLED |
if (baseNameMatcher.matches()) { |
| 350 | final String extension = CommonUtils.getFileExtension(fileName); | |
| 351 | final String path = getPath(currentFile.getAbsolutePath()); | |
| 352 | final ResourceBundle newBundle = new ResourceBundle(baseName, path, extension); | |
| 353 | final Optional<ResourceBundle> bundle = findBundle(resourceBundles, newBundle); | |
| 354 |
1
1. groupFilesIntoBundles : negated conditional → KILLED |
if (bundle.isPresent()) { |
| 355 |
1
1. groupFilesIntoBundles : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck$ResourceBundle::addFile → KILLED |
bundle.get().addFile(currentFile); |
| 356 | } | |
| 357 | else { | |
| 358 |
1
1. groupFilesIntoBundles : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck$ResourceBundle::addFile → KILLED |
newBundle.addFile(currentFile); |
| 359 | resourceBundles.add(newBundle); | |
| 360 | } | |
| 361 | } | |
| 362 | } | |
| 363 |
1
1. groupFilesIntoBundles : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/TranslationCheck::groupFilesIntoBundles to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return resourceBundles; |
| 364 | } | |
| 365 | ||
| 366 | /** | |
| 367 | * Searches for specific resource bundle in a set of resource bundles. | |
| 368 | * @param bundles set of resource bundles. | |
| 369 | * @param targetBundle target bundle to search for. | |
| 370 | * @return Guava's Optional of resource bundle (present if target bundle is found). | |
| 371 | */ | |
| 372 | private static Optional<ResourceBundle> findBundle(Set<ResourceBundle> bundles, | |
| 373 | ResourceBundle targetBundle) { | |
| 374 | Optional<ResourceBundle> result = Optional.empty(); | |
| 375 |
1
1. findBundle : negated conditional → KILLED |
for (ResourceBundle currentBundle : bundles) { |
| 376 |
1
1. findBundle : negated conditional → KILLED |
if (targetBundle.getBaseName().equals(currentBundle.getBaseName()) |
| 377 |
1
1. findBundle : negated conditional → KILLED |
&& targetBundle.getExtension().equals(currentBundle.getExtension()) |
| 378 |
1
1. findBundle : negated conditional → KILLED |
&& targetBundle.getPath().equals(currentBundle.getPath())) { |
| 379 | result = Optional.of(currentBundle); | |
| 380 | break; | |
| 381 | } | |
| 382 | } | |
| 383 |
1
1. findBundle : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/TranslationCheck::findBundle to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return result; |
| 384 | } | |
| 385 | ||
| 386 | /** | |
| 387 | * Extracts the base name (the unique prefix) of resource bundle from translation file name. | |
| 388 | * For example "messages" is the base name of "messages.properties", | |
| 389 | * "messages_de_AT.properties", "messages_en.properties", etc. | |
| 390 | * @param fileName the fully qualified name of the translation file. | |
| 391 | * @return the extracted base name. | |
| 392 | */ | |
| 393 | private static String extractBaseName(String fileName) { | |
| 394 | final String regexp; | |
| 395 | final Matcher languageCountryVariantMatcher = | |
| 396 | LANGUAGE_COUNTRY_VARIANT_PATTERN.matcher(fileName); | |
| 397 | final Matcher languageCountryMatcher = LANGUAGE_COUNTRY_PATTERN.matcher(fileName); | |
| 398 | final Matcher languageMatcher = LANGUAGE_PATTERN.matcher(fileName); | |
| 399 |
1
1. extractBaseName : negated conditional → KILLED |
if (languageCountryVariantMatcher.matches()) { |
| 400 | regexp = LANGUAGE_COUNTRY_VARIANT_PATTERN.pattern(); | |
| 401 | } | |
| 402 |
1
1. extractBaseName : negated conditional → KILLED |
else if (languageCountryMatcher.matches()) { |
| 403 | regexp = LANGUAGE_COUNTRY_PATTERN.pattern(); | |
| 404 | } | |
| 405 |
1
1. extractBaseName : negated conditional → KILLED |
else if (languageMatcher.matches()) { |
| 406 | regexp = LANGUAGE_PATTERN.pattern(); | |
| 407 | } | |
| 408 | else { | |
| 409 | regexp = DEFAULT_TRANSLATION_REGEXP; | |
| 410 | } | |
| 411 | // We use substring(...) instead of replace(...), so that the regular expression does | |
| 412 | // not have to be compiled each time it is used inside 'replace' method. | |
| 413 | final String removePattern = regexp.substring("^.+".length(), regexp.length()); | |
| 414 |
1
1. extractBaseName : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/TranslationCheck::extractBaseName to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return fileName.replaceAll(removePattern, ""); |
| 415 | } | |
| 416 | ||
| 417 | /** | |
| 418 | * Extracts path from a file name which contains the path. | |
| 419 | * For example, if file nam is /xyz/messages.properties, then the method | |
| 420 | * will return /xyz/. | |
| 421 | * @param fileNameWithPath file name which contains the path. | |
| 422 | * @return file path. | |
| 423 | */ | |
| 424 | private static String getPath(String fileNameWithPath) { | |
| 425 |
1
1. getPath : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/TranslationCheck::getPath to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return fileNameWithPath |
| 426 | .substring(0, fileNameWithPath.lastIndexOf(File.separator)); | |
| 427 | } | |
| 428 | ||
| 429 | /** | |
| 430 | * Checks resource files in bundle for consistency regarding their keys. | |
| 431 | * All files in bundle must have the same key set. If this is not the case | |
| 432 | * an error message is posted giving information which key misses in which file. | |
| 433 | * @param bundle resource bundle. | |
| 434 | */ | |
| 435 | private void checkTranslationKeys(ResourceBundle bundle) { | |
| 436 | final Set<File> filesInBundle = bundle.getFiles(); | |
| 437 |
2
1. checkTranslationKeys : changed conditional boundary → SURVIVED 2. checkTranslationKeys : negated conditional → KILLED |
if (filesInBundle.size() > 1) { |
| 438 | // build a map from files to the keys they contain | |
| 439 | final Set<String> allTranslationKeys = new HashSet<>(); | |
| 440 | final SetMultimap<File, String> filesAssociatedWithKeys = HashMultimap.create(); | |
| 441 |
1
1. checkTranslationKeys : negated conditional → KILLED |
for (File currentFile : filesInBundle) { |
| 442 | final Set<String> keysInCurrentFile = getTranslationKeys(currentFile); | |
| 443 | allTranslationKeys.addAll(keysInCurrentFile); | |
| 444 | filesAssociatedWithKeys.putAll(currentFile, keysInCurrentFile); | |
| 445 | } | |
| 446 |
1
1. checkTranslationKeys : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::checkFilesForConsistencyRegardingTheirKeys → KILLED |
checkFilesForConsistencyRegardingTheirKeys(filesAssociatedWithKeys, allTranslationKeys); |
| 447 | } | |
| 448 | } | |
| 449 | ||
| 450 | /** | |
| 451 | * Compares th the specified key set with the key sets of the given translation files (arranged | |
| 452 | * in a map). All missing keys are reported. | |
| 453 | * @param fileKeys a Map from translation files to their key sets. | |
| 454 | * @param keysThatMustExist the set of keys to compare with. | |
| 455 | */ | |
| 456 | private void checkFilesForConsistencyRegardingTheirKeys(SetMultimap<File, String> fileKeys, | |
| 457 | Set<String> keysThatMustExist) { | |
| 458 |
1
1. checkFilesForConsistencyRegardingTheirKeys : negated conditional → KILLED |
for (File currentFile : fileKeys.keySet()) { |
| 459 | final MessageDispatcher dispatcher = getMessageDispatcher(); | |
| 460 | final String path = currentFile.getPath(); | |
| 461 |
1
1. checkFilesForConsistencyRegardingTheirKeys : removed call to com/puppycrawl/tools/checkstyle/api/MessageDispatcher::fireFileStarted → SURVIVED |
dispatcher.fireFileStarted(path); |
| 462 | final Set<String> currentFileKeys = fileKeys.get(currentFile); | |
| 463 | final Set<String> missingKeys = keysThatMustExist.stream() | |
| 464 |
2
1. lambda$checkFilesForConsistencyRegardingTheirKeys$0 : negated conditional → KILLED 2. lambda$checkFilesForConsistencyRegardingTheirKeys$0 : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
.filter(e -> !currentFileKeys.contains(e)).collect(Collectors.toSet()); |
| 465 |
1
1. checkFilesForConsistencyRegardingTheirKeys : negated conditional → KILLED |
if (!missingKeys.isEmpty()) { |
| 466 |
1
1. checkFilesForConsistencyRegardingTheirKeys : negated conditional → KILLED |
for (Object key : missingKeys) { |
| 467 |
1
1. checkFilesForConsistencyRegardingTheirKeys : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::log → KILLED |
log(0, MSG_KEY, key); |
| 468 | } | |
| 469 | } | |
| 470 |
1
1. checkFilesForConsistencyRegardingTheirKeys : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::fireErrors → KILLED |
fireErrors(path); |
| 471 |
1
1. checkFilesForConsistencyRegardingTheirKeys : removed call to com/puppycrawl/tools/checkstyle/api/MessageDispatcher::fireFileFinished → SURVIVED |
dispatcher.fireFileFinished(path); |
| 472 | } | |
| 473 | } | |
| 474 | ||
| 475 | /** | |
| 476 | * Loads the keys from the specified translation file into a set. | |
| 477 | * @param file translation file. | |
| 478 | * @return a Set object which holds the loaded keys. | |
| 479 | */ | |
| 480 | private Set<String> getTranslationKeys(File file) { | |
| 481 | Set<String> keys = new HashSet<>(); | |
| 482 | InputStream inStream = null; | |
| 483 | try { | |
| 484 | inStream = new FileInputStream(file); | |
| 485 | final Properties translations = new Properties(); | |
| 486 |
1
1. getTranslationKeys : removed call to java/util/Properties::load → KILLED |
translations.load(inStream); |
| 487 | keys = translations.stringPropertyNames(); | |
| 488 | } | |
| 489 | catch (final IOException ex) { | |
| 490 |
1
1. getTranslationKeys : removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::logIoException → SURVIVED |
logIoException(ex, file); |
| 491 | } | |
| 492 | finally { | |
| 493 |
1
1. getTranslationKeys : removed call to com/google/common/io/Closeables::closeQuietly → SURVIVED |
Closeables.closeQuietly(inStream); |
| 494 | } | |
| 495 |
1
1. getTranslationKeys : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/TranslationCheck::getTranslationKeys to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return keys; |
| 496 | } | |
| 497 | ||
| 498 | /** | |
| 499 | * Helper method to log an io exception. | |
| 500 | * @param exception the exception that occurred | |
| 501 | * @param file the file that could not be processed | |
| 502 | */ | |
| 503 | private void logIoException(IOException exception, File file) { | |
| 504 | String[] args = null; | |
| 505 | String key = "general.fileNotFound"; | |
| 506 |
1
1. logIoException : negated conditional → KILLED |
if (!(exception instanceof FileNotFoundException)) { |
| 507 | args = new String[] {exception.getMessage()}; | |
| 508 | key = "general.exception"; | |
| 509 | } | |
| 510 | final LocalizedMessage message = | |
| 511 | new LocalizedMessage( | |
| 512 | 0, | |
| 513 | Definitions.CHECKSTYLE_BUNDLE, | |
| 514 | key, | |
| 515 | args, | |
| 516 | getId(), | |
| 517 | getClass(), null); | |
| 518 | final SortedSet<LocalizedMessage> messages = new TreeSet<>(); | |
| 519 | messages.add(message); | |
| 520 |
1
1. logIoException : removed call to com/puppycrawl/tools/checkstyle/api/MessageDispatcher::fireErrors → KILLED |
getMessageDispatcher().fireErrors(file.getPath(), messages); |
| 521 | LOG.debug("IOException occurred.", exception); | |
| 522 | } | |
| 523 | ||
| 524 | /** Class which represents a resource bundle. */ | |
| 525 | private static class ResourceBundle { | |
| 526 | /** Bundle base name. */ | |
| 527 | private final String baseName; | |
| 528 | /** Common extension of files which are included in the resource bundle. */ | |
| 529 | private final String extension; | |
| 530 | /** Common path of files which are included in the resource bundle. */ | |
| 531 | private final String path; | |
| 532 | /** Set of files which are included in the resource bundle. */ | |
| 533 | private final Set<File> files; | |
| 534 | ||
| 535 | /** | |
| 536 | * Creates a ResourceBundle object with specific base name, common files extension. | |
| 537 | * @param baseName bundle base name. | |
| 538 | * @param path common path of files which are included in the resource bundle. | |
| 539 | * @param extension common extension of files which are included in the resource bundle. | |
| 540 | */ | |
| 541 | ResourceBundle(String baseName, String path, String extension) { | |
| 542 | this.baseName = baseName; | |
| 543 | this.path = path; | |
| 544 | this.extension = extension; | |
| 545 | files = new HashSet<>(); | |
| 546 | } | |
| 547 | ||
| 548 | public String getBaseName() { | |
| 549 | return baseName; | |
| 550 | } | |
| 551 | ||
| 552 | public String getPath() { | |
| 553 | return path; | |
| 554 | } | |
| 555 | ||
| 556 | public String getExtension() { | |
| 557 | return extension; | |
| 558 | } | |
| 559 | ||
| 560 | public Set<File> getFiles() { | |
| 561 | return Collections.unmodifiableSet(files); | |
| 562 | } | |
| 563 | ||
| 564 | /** | |
| 565 | * Adds a file into resource bundle. | |
| 566 | * @param file file which should be added into resource bundle. | |
| 567 | */ | |
| 568 | public void addFile(File file) { | |
| 569 | files.add(file); | |
| 570 | } | |
| 571 | ||
| 572 | /** | |
| 573 | * Checks whether a resource bundle contains a file which name matches file name regexp. | |
| 574 | * @param fileNameRegexp file name regexp. | |
| 575 | * @return true if a resource bundle contains a file which name matches file name regexp. | |
| 576 | */ | |
| 577 | public boolean containsFile(String fileNameRegexp) { | |
| 578 | boolean containsFile = false; | |
| 579 | for (File currentFile : files) { | |
| 580 | if (Pattern.matches(fileNameRegexp, currentFile.getName())) { | |
| 581 | containsFile = true; | |
| 582 | break; | |
| 583 | } | |
| 584 | } | |
| 585 | return containsFile; | |
| 586 | } | |
| 587 | } | |
| 588 | } | |
Mutations | ||
| 183 |
1.1 |
|
| 201 |
1.1 |
|
| 209 |
1.1 |
|
| 210 |
1.1 |
|
| 228 |
1.1 2.2 3.3 |
|
| 229 |
1.1 |
|
| 234 |
1.1 |
|
| 239 |
1.1 |
|
| 251 |
1.1 |
|
| 252 |
1.1 |
|
| 253 |
1.1 |
|
| 254 |
1.1 |
|
| 264 |
1.1 |
|
| 265 |
1.1 |
|
| 277 |
1.1 |
|
| 279 |
1.1 |
|
| 280 |
1.1 |
|
| 298 |
1.1 |
|
| 309 |
1.1 |
|
| 310 |
1.1 |
|
| 319 |
1.1 |
|
| 329 |
1.1 |
|
| 330 |
1.1 |
|
| 331 |
1.1 |
|
| 332 |
1.1 |
|
| 345 |
1.1 |
|
| 349 |
1.1 |
|
| 354 |
1.1 |
|
| 355 |
1.1 |
|
| 358 |
1.1 |
|
| 363 |
1.1 |
|
| 375 |
1.1 |
|
| 376 |
1.1 |
|
| 377 |
1.1 |
|
| 378 |
1.1 |
|
| 383 |
1.1 |
|
| 399 |
1.1 |
|
| 402 |
1.1 |
|
| 405 |
1.1 |
|
| 414 |
1.1 |
|
| 425 |
1.1 |
|
| 437 |
1.1 2.2 |
|
| 441 |
1.1 |
|
| 446 |
1.1 |
|
| 458 |
1.1 |
|
| 461 |
1.1 |
|
| 464 |
1.1 2.2 |
|
| 465 |
1.1 |
|
| 466 |
1.1 |
|
| 467 |
1.1 |
|
| 470 |
1.1 |
|
| 471 |
1.1 |
|
| 486 |
1.1 |
|
| 490 |
1.1 |
|
| 493 |
1.1 |
|
| 495 |
1.1 |
|
| 506 |
1.1 |
|
| 520 |
1.1 |