diff options
author | Zuy Alexey <zuy.alexey@gmail.com> | 2016-04-14 01:08:56 +0300 |
---|---|---|
committer | Roman Ivanov <romani@users.noreply.github.com> | 2016-04-13 15:08:56 -0700 |
commit | 6309b561944f8ca466e4667b34e28728af3ffd9d (patch) | |
tree | 5f922723a137e3189c77cb5f1f275073f7297b64 /src/main | |
parent | 9eeeaa9f9cf59d1ae81840e995df6177e061208a (diff) | |
download | checkstyle-6309b561944f8ca466e4667b34e28728af3ffd9d.tar.gz |
minor: reorganized TranslationCheck (#3097)
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java | 250 |
1 files changed, 125 insertions, 125 deletions
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java index 8965e6abe..f310fe9b1 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java @@ -201,30 +201,6 @@ public class TranslationCheck extends AbstractFileSetCheck { validateUserSpecifiedLanguageCodes(requiredTranslations); } - @Override - public void beginProcessing(String charset) { - super.beginProcessing(charset); - filesToProcess.clear(); - } - - @Override - protected void processFiltered(File file, List<String> lines) { - // We just collecting files for processing at finishProcessing() - filesToProcess.add(file); - } - - @Override - public void finishProcessing() { - super.finishProcessing(); - - final Set<ResourceBundle> bundles = groupFilesIntoBundles(filesToProcess, baseNamePattern); - for (ResourceBundle currentBundle : bundles) { - checkExistenceOfDefaultTranslation(currentBundle); - checkExistenceOfRequiredTranslations(currentBundle); - checkTranslationKeys(currentBundle); - } - } - /** * Validates the correctness of user specififed language codes for the check. * @param languageCodes user specified language codes for the check. @@ -233,9 +209,9 @@ public class TranslationCheck extends AbstractFileSetCheck { for (String code : languageCodes) { if (!isValidLanguageCode(code)) { final LocalizedMessage msg = new LocalizedMessage(0, TRANSLATION_BUNDLE, - WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null); + WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null); final String exceptionMessage = String.format(Locale.ROOT, - "%s [%s]", msg.getMessage(), TranslationCheck.class.getSimpleName()); + "%s [%s]", msg.getMessage(), TranslationCheck.class.getSimpleName()); throw new IllegalArgumentException(exceptionMessage); } } @@ -258,35 +234,28 @@ public class TranslationCheck extends AbstractFileSetCheck { return valid; } - /** - * Groups a set of files into bundles. - * Only files, which names match base name regexp pattern will be grouped. - * @param files set of files. - * @param baseNameRegexp base name regexp pattern. - * @return set of ResourceBundles. - */ - private static Set<ResourceBundle> groupFilesIntoBundles(Set<File> files, - Pattern baseNameRegexp) { - final Set<ResourceBundle> resourceBundles = Sets.newHashSet(); - for (File currentFile : files) { - final String fileName = currentFile.getName(); - final String baseName = extractBaseName(fileName); - final Matcher baseNameMatcher = baseNameRegexp.matcher(baseName); - if (baseNameMatcher.matches()) { - final String extension = Files.getFileExtension(fileName); - final String path = getPath(currentFile.getAbsolutePath()); - final ResourceBundle newBundle = new ResourceBundle(baseName, path, extension); - final Optional<ResourceBundle> bundle = findBundle(resourceBundles, newBundle); - if (bundle.isPresent()) { - bundle.get().addFile(currentFile); - } - else { - newBundle.addFile(currentFile); - resourceBundles.add(newBundle); - } - } + @Override + public void beginProcessing(String charset) { + super.beginProcessing(charset); + filesToProcess.clear(); + } + + @Override + protected void processFiltered(File file, List<String> lines) { + // We just collecting files for processing at finishProcessing() + filesToProcess.add(file); + } + + @Override + public void finishProcessing() { + super.finishProcessing(); + + final Set<ResourceBundle> bundles = groupFilesIntoBundles(filesToProcess, baseNamePattern); + for (ResourceBundle currentBundle : bundles) { + checkExistenceOfDefaultTranslation(currentBundle); + checkExistenceOfRequiredTranslations(currentBundle); + checkTranslationKeys(currentBundle); } - return resourceBundles; } /** @@ -317,47 +286,6 @@ public class TranslationCheck extends AbstractFileSetCheck { } /** - * Checks resource files in bundle for consistency regarding their keys. - * All files in bundle must have the same key set. If this is not the case - * an error message is posted giving information which key misses in which file. - * @param bundle resource bundle. - */ - private void checkTranslationKeys(ResourceBundle bundle) { - final Set<File> filesInBundle = bundle.getFiles(); - if (filesInBundle.size() > 1) { - // build a map from files to the keys they contain - final Set<String> allTranslationKeys = Sets.newHashSet(); - final SetMultimap<File, String> filesAssociatedWithKeys = HashMultimap.create(); - for (File currentFile : filesInBundle) { - final Set<String> keysInCurrentFile = getTranslationKeys(currentFile); - allTranslationKeys.addAll(keysInCurrentFile); - filesAssociatedWithKeys.putAll(currentFile, keysInCurrentFile); - } - checkFilesForConsistencyRegardingTheirKeys(filesAssociatedWithKeys, allTranslationKeys); - } - } - - /** - * Searches for specific resource bundle in a set of resource bundles. - * @param bundles set of resource bundles. - * @param targetBundle target bundle to search for. - * @return Guava's Optional of resource bundle (present if target bundle is found). - */ - private static Optional<ResourceBundle> findBundle(Set<ResourceBundle> bundles, - ResourceBundle targetBundle) { - Optional<ResourceBundle> result = Optional.absent(); - for (ResourceBundle currentBundle : bundles) { - if (targetBundle.getBaseName().equals(currentBundle.getBaseName()) - && targetBundle.getExtension().equals(currentBundle.getExtension()) - && targetBundle.getPath().equals(currentBundle.getPath())) { - result = Optional.of(currentBundle); - break; - } - } - return result; - } - - /** * Returns the name of translation file which is absent in resource bundle or Guava's Optional, * if there is not missing translation. * @param bundle resource bundle. @@ -373,7 +301,7 @@ public class TranslationCheck extends AbstractFileSetCheck { if (languageCode == null) { searchForDefaultTranslation = true; fileNameRegexp = String.format(Locale.ROOT, REGEXP_FORMAT_TO_CHECK_DEFAULT_TRANSLATIONS, - baseName, extension); + baseName, extension); } else { searchForDefaultTranslation = false; @@ -384,17 +312,81 @@ public class TranslationCheck extends AbstractFileSetCheck { if (!bundle.containsFile(fileNameRegexp)) { if (searchForDefaultTranslation) { missingFileName = Optional.of(String.format(Locale.ROOT, - DEFAULT_TRANSLATION_FILE_NAME_FORMATTER, baseName, extension)); + DEFAULT_TRANSLATION_FILE_NAME_FORMATTER, baseName, extension)); } else { missingFileName = Optional.of(String.format(Locale.ROOT, - FILE_NAME_WITH_LANGUAGE_CODE_FORMATTER, baseName, languageCode, extension)); + FILE_NAME_WITH_LANGUAGE_CODE_FORMATTER, baseName, languageCode, extension)); } } return missingFileName; } /** + * Logs that translation file is missing. + * @param filePath file path. + * @param fileName file name. + */ + private void logMissingTranslation(String filePath, String fileName) { + final MessageDispatcher dispatcher = getMessageDispatcher(); + dispatcher.fireFileStarted(filePath); + log(0, MSG_KEY_MISSING_TRANSLATION_FILE, fileName); + fireErrors(filePath); + dispatcher.fireFileFinished(filePath); + } + + /** + * Groups a set of files into bundles. + * Only files, which names match base name regexp pattern will be grouped. + * @param files set of files. + * @param baseNameRegexp base name regexp pattern. + * @return set of ResourceBundles. + */ + private static Set<ResourceBundle> groupFilesIntoBundles(Set<File> files, + Pattern baseNameRegexp) { + final Set<ResourceBundle> resourceBundles = Sets.newHashSet(); + for (File currentFile : files) { + final String fileName = currentFile.getName(); + final String baseName = extractBaseName(fileName); + final Matcher baseNameMatcher = baseNameRegexp.matcher(baseName); + if (baseNameMatcher.matches()) { + final String extension = Files.getFileExtension(fileName); + final String path = getPath(currentFile.getAbsolutePath()); + final ResourceBundle newBundle = new ResourceBundle(baseName, path, extension); + final Optional<ResourceBundle> bundle = findBundle(resourceBundles, newBundle); + if (bundle.isPresent()) { + bundle.get().addFile(currentFile); + } + else { + newBundle.addFile(currentFile); + resourceBundles.add(newBundle); + } + } + } + return resourceBundles; + } + + /** + * Searches for specific resource bundle in a set of resource bundles. + * @param bundles set of resource bundles. + * @param targetBundle target bundle to search for. + * @return Guava's Optional of resource bundle (present if target bundle is found). + */ + private static Optional<ResourceBundle> findBundle(Set<ResourceBundle> bundles, + ResourceBundle targetBundle) { + Optional<ResourceBundle> result = Optional.absent(); + for (ResourceBundle currentBundle : bundles) { + if (targetBundle.getBaseName().equals(currentBundle.getBaseName()) + && targetBundle.getExtension().equals(currentBundle.getExtension()) + && targetBundle.getPath().equals(currentBundle.getPath())) { + result = Optional.of(currentBundle); + break; + } + } + return result; + } + + /** * Extracts the base name (the unique prefix) of resource bundle from translation file name. * For example "messages" is the base name of "messages.properties", * "messages_de_AT.properties", "messages_en.properties", etc. @@ -438,26 +430,24 @@ public class TranslationCheck extends AbstractFileSetCheck { } /** - * Loads the keys from the specified translation file into a set. - * @param file translation file. - * @return a Set object which holds the loaded keys. + * Checks resource files in bundle for consistency regarding their keys. + * All files in bundle must have the same key set. If this is not the case + * an error message is posted giving information which key misses in which file. + * @param bundle resource bundle. */ - private Set<String> getTranslationKeys(File file) { - Set<String> keys = Sets.newHashSet(); - InputStream inStream = null; - try { - inStream = new FileInputStream(file); - final Properties translations = new Properties(); - translations.load(inStream); - keys = translations.stringPropertyNames(); - } - catch (final IOException ex) { - logIoException(ex, file); - } - finally { - Closeables.closeQuietly(inStream); + private void checkTranslationKeys(ResourceBundle bundle) { + final Set<File> filesInBundle = bundle.getFiles(); + if (filesInBundle.size() > 1) { + // build a map from files to the keys they contain + final Set<String> allTranslationKeys = Sets.newHashSet(); + final SetMultimap<File, String> filesAssociatedWithKeys = HashMultimap.create(); + for (File currentFile : filesInBundle) { + final Set<String> keysInCurrentFile = getTranslationKeys(currentFile); + allTranslationKeys.addAll(keysInCurrentFile); + filesAssociatedWithKeys.putAll(currentFile, keysInCurrentFile); + } + checkFilesForConsistencyRegardingTheirKeys(filesAssociatedWithKeys, allTranslationKeys); } - return keys; } /** @@ -485,16 +475,26 @@ public class TranslationCheck extends AbstractFileSetCheck { } /** - * Logs that translation file is missing. - * @param filePath file path. - * @param fileName file name. + * Loads the keys from the specified translation file into a set. + * @param file translation file. + * @return a Set object which holds the loaded keys. */ - private void logMissingTranslation(String filePath, String fileName) { - final MessageDispatcher dispatcher = getMessageDispatcher(); - dispatcher.fireFileStarted(filePath); - log(0, MSG_KEY_MISSING_TRANSLATION_FILE, fileName); - fireErrors(filePath); - dispatcher.fireFileFinished(filePath); + private Set<String> getTranslationKeys(File file) { + Set<String> keys = Sets.newHashSet(); + InputStream inStream = null; + try { + inStream = new FileInputStream(file); + final Properties translations = new Properties(); + translations.load(inStream); + keys = translations.stringPropertyNames(); + } + catch (final IOException ex) { + logIoException(ex, file); + } + finally { + Closeables.closeQuietly(inStream); + } + return keys; } /** |