Translator refactoring :

- deleted useless new StringBuilder allocation
- use of a new reusable FileNameFilter
- added javadoc
pull/12/head
luccioman 9 years ago
parent c1d937a90c
commit 7e4c1d2282

@ -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,19 +180,10 @@ 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, '/');
@ -205,7 +204,6 @@ public class Translator {
//serverLog.logInfo("TRANSLATOR", "No translation for file: "+relativePath);
}
}
}
return true;
}

@ -0,0 +1,83 @@
// SourceFileFilter.java
// -------------------------------------
// part of YACY
// (C) by Michael Peter Christen; mc@yacy.net
// 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;
}
}
Loading…
Cancel
Save