[prev in list] [next in list] [prev in thread] [next in thread] 

List:       aspell-user
Subject:    =?windows-1252?Q?=5BAspell-user=5D_German_Umlaut_issue=2C_?= =?windows-1252?Q?how_to_accept__=22a_as
From:       Michael Gruenewald <meatloaf () web ! de>
Date:       2003-03-11 14:02:34
Message-ID: 3E6DEC7A.7060001 () web ! de
[Download RAW message or body]

Hi, I have written a small java-prog to convert the files.
It searches for old values  and replaces it with new values of the 
corresponding index. It searches for all tex-files in the dirName
creates a backup and replace the old files.

I hope anybody will find this prog useful.

Greetings Michael



/**
  * <p>Title: ReplaceUmlaute</p>
  * <p>Description: Erstetzt die Umlaute, für aspell.</p>
  * <p>Copyright: Copyright (c) 2003</p>
  * <p>Company: Student</p>
  * @author Michael Grünewald
  * @version 0.01
  */
import java.io.*;

public class ReplaceUmlaute{
     public static String[] oldValues 
={"\"a","\"o","\"u","\"s","\"A","\"O","\"U"};
     public static String[] newValues ={"ä","ö","ü","ß","Ä","Ö","Ü"};

     public static String dirName = "I:\\Diplom\\DiplomText";
     public static String backupDirName = 
"I:\\Diplom\\DiplomText\\BackupDir";

     public static String suffix ="tex";

     public static void main(String[] args) {
         File dir = new File(dirName);
         File backupDir = new File(backupDirName);

         if ( dir.exists() && dir .canRead()){
                  File[] files = dir.listFiles(new FilenameFilter(){
                      public boolean accept(File dir, String name){
                          if (name.length() >= 4 && name.indexOf(".") > 0 ){
                              String compare = 
name.substring(name.length() - suffix.length());
                              if (compare.equalsIgnoreCase(suffix)) 
return true;
                          }
                          return false;
                      }
                  });
                  boolean success = false;
                  if ( !backupDir.exists()) success = backupDir.mkdir();
                  else success = true;

                  for (int i = 0; i <  files.length; i++){

                     File file = files[i];

                     FileReader fin = null;
                     BufferedReader in = null;

                     FileWriter fout = null;
                     PrintWriter out = null;

                     FileWriter bfout = null;
                     PrintWriter bout = null;

                     FileReader copyfin = null;
                     BufferedReader copyIn = null;


                     FileWriter copyfout = null;
                     PrintWriter copyOut = null;



                     File backup = null;
                     try {
                         File targetFile = 
File.createTempFile("temp","tex",backupDir);

                         // Backup
                         if (success){
                             String filename = files[i].getAbsolutePath();
                             int pos = filename.lastIndexOf(File.separator);
                             if (pos > 0) filename = 
filename.substring(pos);
                             filename = backupDirName + filename;
                             backup = new File(filename);
                             if ( backup.exists() ) backup.delete();
                             backup.createNewFile();
                         }

                         fin = new FileReader(file);
                         in = new BufferedReader(fin);
                         fout = new FileWriter(targetFile);
                         out = new PrintWriter(fout);

                         if (backup != null){
                             bfout = new FileWriter(backup);
                             bout = new PrintWriter(bfout);
                         }

                         while(in.ready()){
                             String currentLine = in.readLine();
                             if (bout != null) bout.println(currentLine);
//backup
                             for (int j= 0; j < oldValues.length; j++){
                                int pos = currentLine.indexOf(oldValues[j]);
                                while  (pos >= 0){
                                    currentLine = 
currentLine.substring(0,pos) + newValues[j] + 
currentLine.substring(pos+oldValues[j].length());
                                    pos = currentLine.indexOf(oldValues[j]);
                                 }
                             }
                             out.println(currentLine);
                         }
                         if (bout != null) bout.flush();
                         out.flush();
                         in.close();

			 // replace old file with new file (temp file)

                         file.delete();
                         file.createNewFile();

                         copyfin = new FileReader(targetFile);
                         copyIn = new BufferedReader(copyfin);

                         copyfout = new FileWriter(file);
                         copyOut = new PrintWriter(copyfout);

                         while(copyIn.ready())
                             copyOut.println(copyIn.readLine());
                         copyOut.flush();
                         copyIn.close();
                         targetFile.delete();

                     }
                     catch (Exception ex) {
                         ex.printStackTrace();
                     }
                     finally {
                         try {
                             if (in != null) in.close();
                             if (fin != null) fin.close();
                             if (out != null) out.close();
                             if (fout != null) fout.close();
                             if (copyIn != null) copyIn.close();
                             if (copyfin != null) copyfin.close();
                             if (copyOut != null) copyOut.close();
                             if (copyfout != null) copyfout.close();
                         }
                         catch (Exception ex) {

                         }

                     }

                  }

         }


     }
}





[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic