From a2e0f00456c8b7a2b5a840e312d479957465fda7 Mon Sep 17 00:00:00 2001 From: reger Date: Sun, 5 Jun 2016 03:57:08 +0200 Subject: [PATCH] optimize Translator - translateFilesRecursive: load translation once (reduce io), return true on complete success - remove resulting unused translateFiles() variant - translate: use StringBuilder parameter (skip toString conversion) - remove not needed static declaration - upd some javadoc --- source/net/yacy/data/Translator.java | 48 +++++++++++++++++++--------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/source/net/yacy/data/Translator.java b/source/net/yacy/data/Translator.java index 6f87bd79f..08aae9e06 100644 --- a/source/net/yacy/data/Translator.java +++ b/source/net/yacy/data/Translator.java @@ -67,11 +67,11 @@ public class Translator { /** * Translate source using entries in translationTable - * @param source text to translate. Mus be non null. + * @param source text to translate. Must be non null. * @param translationTable translation entries : text to translate -> translation * @return source translated */ - public static String translate(final String source, + public String translate(final StringBuilder source, final Map translationTable) { final Set> entries = translationTable.entrySet(); StringBuilder builder = new StringBuilder(source); @@ -146,7 +146,7 @@ public class Translator { * @param translationList map of translations * @return true when destFile was sucessfully written, false otherwise */ - public static boolean translateFile(final File sourceFile, final File destFile, final Map translationList){ + public boolean translateFile(final File sourceFile, final File destFile, final Map translationList){ StringBuilder content = new StringBuilder(); BufferedReader br = null; @@ -167,7 +167,7 @@ public class Translator { } } - String processedContent = translate(content.toString(), translationList); + String processedContent = translate(content, translationList); BufferedWriter bw = null; try{ bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile), StandardCharsets.UTF_8)); @@ -186,11 +186,17 @@ public class Translator { return true; } - public boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final File translationFile, final String extensions){ - return translateFiles(sourceDir, destDir, baseDir, loadTranslationsLists(translationFile), extensions); - } - - public static boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final Map> translationLists, final String extensions){ + /** + * Translate files in sourceDir (relative path of baseDir) write result to destDir + * + * @param sourceDir relative path + * @param destDir destination + * @param baseDir base dir of source + * @param translationLists translation to use + * @param extensions file extension to include in translation + * @return + */ + public boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final Map> translationLists, final String extensions){ destDir.mkdirs(); final List exts = ListManager.string2vector(extensions); final File[] sourceFiles = sourceDir.listFiles(new ExtensionsFileFilter(exts)); @@ -200,7 +206,7 @@ public class Translator { relativePath=sourceFile.getAbsolutePath().substring(baseDir.getAbsolutePath().length()+1); //+1 to get the "/" relativePath = relativePath.replace(File.separatorChar, '/'); } catch (final IndexOutOfBoundsException e) { - ConcurrentLog.severe("TRANSLATOR", "Error creating relative Path for "+sourceFile.getAbsolutePath()); + ConcurrentLog.severe("TRANSLATOR", "Error creating relative Path for "+sourceFile.getAbsolutePath()); relativePath = "wrong path"; //not in translationLists } if (translationLists.containsKey(relativePath)) { @@ -219,17 +225,29 @@ public class Translator { return true; } - public boolean translateFilesRecursive(final File sourceDir, final File destDir, final File translationFile, final String extensions, final String notdir){ - final List dirList=FileUtils.getDirsRecursive(sourceDir, notdir); + /** + * Translate files starting with sourceDir and all subdirectories. + * + * @param sourceDir + * @param destDir + * @param translationFile translation language file + * @param extensions extension of files to include in translation + * @param notdir directory to exclude + * @return true if all files translated (or none) + */ + public boolean translateFilesRecursive(final File sourceDir, final File destDir, final File translationFile, final String extensions, final String notdir) { + final List dirList = FileUtils.getDirsRecursive(sourceDir, notdir); dirList.add(sourceDir); + final Map> translationLists = loadTranslationsLists(translationFile); + boolean erg = true; for (final File file : dirList) { - if(file.isDirectory() && !file.getName().equals(notdir)) { + if (file.isDirectory() && !file.getName().equals(notdir)) { //cuts the sourcePath and prepends the destPath File file2 = new File(destDir, file.getPath().substring(sourceDir.getPath().length())); - translateFiles(file, file2, sourceDir, translationFile, extensions); + erg &= translateFiles(file, file2, sourceDir, translationLists, extensions); } } - return true; + return erg; } public static Map langMap(@SuppressWarnings("unused") final serverSwitch env) {