Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance
Java decentralizedintranet-searchintranet-search-enginep2pp2p-networkprivacyprivacy-protectionsearch-enginesearchenginewebweb-search-enginewebsearchwebseearch-engine
Translator refactoring : - deleted useless new StringBuilder allocation - use of a new reusable FileNameFilter - added javadoc
luccioman7e4c1d2282c3
diff --git a/source/net/yacy/data/Translator.java b/source/net/yacy/data/Translator.javaindex 884029bdf..11d34351d 100644--- a/source/net/yacy/data/Translator.java+++ b/source/net/yacy/data/Translator.java@@ -52,6 +52,7 @@ import net.yacy.kelondro.util.FileUtils;import net.yacy.kelondro.util.Formatter;import net.yacy.search.SwitchboardConstants;import net.yacy.server.serverSwitch;+import net.yacy.utils.translation.SourceFileFilter;import java.util.*;@@ -126,6 +127,13 @@ public class Translator {return translateFile(sourceFile, destFile, loadTranslationsLists(translationFile).get(sourceFile.getName()));}+ /**+ * Translate sourceFile to destFile using translationList.+ * @param sourceFile file to translate+ * @param destFile file to write+ * @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<String, String> translationList){StringBuilder content = new StringBuilder();@@ -147,11 +155,11 @@ public class Translator {}}- content = new StringBuilder(translate(content.toString(), translationList));+ String processedContent = translate(content.toString(), translationList);BufferedWriter bw = null;try{bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile),"UTF-8"));- bw.write(content.toString());+ bw.write(processedContent);bw.close();}catch(final IOException e){return false;@@ -172,38 +180,28 @@ public class Translator {public static boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final Map<String, Map<String, String>> translationLists, final String extensions){destDir.mkdirs();- final File[] sourceFiles = sourceDir.listFiles();final List<String> exts = ListManager.string2vector(extensions);- boolean rightExtension;+ final File[] sourceFiles = sourceDir.listFiles(new SourceFileFilter(exts));String relativePath;for (final File sourceFile : sourceFiles) {- rightExtension=false;- for (final String ext : exts) {- if (sourceFile.getName().endsWith(ext)) {- rightExtension=true;- break;- }- }- if (rightExtension) {- try {- 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());- relativePath = "wrong path"; //not in translationLists- }- if (translationLists.containsKey(relativePath)) {- ConcurrentLog.info("TRANSLATOR", "Translating file: "+ relativePath);- if(!translateFile(- sourceFile,- new File(destDir, sourceFile.getName().replace('/', File.separatorChar)),- translationLists.get(relativePath)))- {- ConcurrentLog.severe("TRANSLATOR", "File error while translating file "+relativePath);- }- //}else{- //serverLog.logInfo("TRANSLATOR", "No translation for file: "+relativePath);+ try {+ 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());+ relativePath = "wrong path"; //not in translationLists+ }+ if (translationLists.containsKey(relativePath)) {+ ConcurrentLog.info("TRANSLATOR", "Translating file: "+ relativePath);+ if(!translateFile(+ sourceFile,+ new File(destDir, sourceFile.getName().replace('/', File.separatorChar)),+ translationLists.get(relativePath)))+ {+ ConcurrentLog.severe("TRANSLATOR", "File error while translating file "+relativePath);}+ //}else{+ //serverLog.logInfo("TRANSLATOR", "No translation for file: "+relativePath);}}return true;diff --git a/source/net/yacy/utils/translation/SourceFileFilter.java b/source/net/yacy/utils/translation/SourceFileFilter.javanew file mode 100755index 000000000..4c82459aa--- /dev/null+++ b/source/net/yacy/utils/translation/SourceFileFilter.java@@ -0,0 +1,83 @@+// SourceFileFilter.java+// -------------------------------------+// part of YACY+// (C) by Michael Peter Christen; [email protected]+// first published on http://www.anomic.de+// Frankfurt, Germany, 2004+//+// $LastChangedDate$+// $LastChangedRevision$+// $LastChangedBy$+//+// This program is free software; you can redistribute it and/or modify+// it under the terms of the GNU General Public License as published by+// the Free Software Foundation; either version 2 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU General Public License for more details.+//+// You should have received a copy of the GNU General Public License+// along with this program; if not, write to the Free Software+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA++package net.yacy.utils.translation;++import java.io.File;+import java.io.FilenameFilter;+import java.util.ArrayList;+import java.util.List;++/**+ * File filter for source files to translate+ *+ * @author luc+ *+ */+public class SourceFileFilter implements FilenameFilter {++ /** Extensions required to pass filter */+ private List<String> extensions;++ /**+ * Contructor with extensions+ *+ * @param extensions+ * extensions required. When this list is null or empty, filter+ * let pass all files.+ */+ public SourceFileFilter(List<String> extensions) {+ if (extensions == null) {+ this.extensions = new ArrayList<>();+ } else {+ this.extensions = new ArrayList<>(extensions);+ }+ }++ /**+ * @param file+ * file to check+ * @return true when file name ends with one of the extensions list or+ * extensions list is empty+ */+ @Override+ public boolean accept(File dir, String name) {+ boolean accepted = false;+ if (name != null) {+ if (extensions.size() == 0) {+ accepted = true;+ } else {+ for (String ext : extensions) {+ if (name.endsWith(ext)) {+ accepted = true;+ break;+ }+ }+ }+ }+ return accepted;+ }++}