/**
*
*/
/**
* @author dnagorny
*
*/
import java.nio.file.*;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
public class Recoder {
private static Path startPath = Paths.get("/home/dnagorny/work/translit/sandbox");
private static final String sourceEncoding="windows-1251";
private static boolean echoingIsOn = true;
private static final String sourceChars = "абвгдеёзийклмнопрстуфхъыьэАБВГДЕЁЗИЙКЛМНОПРСТУФХЪЫЬЭ";
private static final String targetChars = "abvgdeezijklmnoprstufh'y'eABVGDEEZIJKLMNOPRSTUFH'Y'E";
private static final Map<Character, String> complexTransTable = new HashMap<Character, String>() {{
put('ж',"zh");
put('ц',"ts");
put('ч',"ch");
put('ш',"sh");
put('щ',"sch");
put('ю',"ju");
put('я',"ja");
put( 'Ж',"Zh");
put('Ц',"Ts");
put('Ч',"Ch");
put('Ш',"Sh");
put('Щ',"Sch");
put('Ю',"Ju");
put('Я',"Ja");
}};
/**
* @param args
*/
public static void processDirectory(String[] args) {
if (args.length == 0) {
System.out.println("Usage: translit start_path");
System.exit(0);
}
else
{
startPath = Paths.get(args[0]);
}
if (! java.nio.charset.Charset.isSupported(sourceEncoding)) {
System.out.println("Encoding" + sourceEncoding + " isn't supported");
System.exit(0);
}
try {
Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println(file.toString());
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file.toFile()),
sourceEncoding));
File temporaryFile = File.createTempFile("transliterator", null);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(temporaryFile)));
String line;
while ( (line = in.readLine()) != null) {
if (echoingIsOn) {
System.out.println("Before: " + line);
}
for(int i = 0; i < sourceChars.length(); i++)
{
line = line.replace(sourceChars.charAt(i), targetChars.charAt(i));
}
for(Character ch: complexTransTable.keySet())
{
line = line.replaceAll("" + ch, complexTransTable.get(ch));
}
out.write(line+"\n");
if (echoingIsOn) {
System.out.println("After: " + line);
}
}
in.close();
out.close();
file.toFile().delete();
Files.move(temporaryFile.toPath(), file);
return FileVisitResult.CONTINUE;
}
});
}
catch(IOException e) {
System.out.println(e.getMessage());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}