001: package net.sourceforge.squirrel_sql.client;
002:
003: /*
004: * Copyright (C) 2006 Gerd Wagner
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.io.BufferedReader;
022: import java.io.BufferedWriter;
023: import java.io.FileReader;
024: import java.io.FileWriter;
025:
026: import net.sourceforge.squirrel_sql.client.util.ApplicationFiles;
027: import net.sourceforge.squirrel_sql.client.gui.db.SQLAlias;
028:
029: /**
030: * When new SQuirreL versions need transform config files
031: * these should be done here.
032: */
033: public class FileTransformer {
034: /**
035: *
036: * @return Error message. Will cause SQuirreL to QUIT!!!
037: */
038: public static String transform(ApplicationFiles appFiles) {
039: return convertAliases_2_2_to_2_3(appFiles);
040: }
041:
042: private static String convertAliases_2_2_to_2_3(
043: ApplicationFiles appFiles) {
044:
045: if (appFiles.getDatabaseAliasesFile().exists()) {
046: return null;
047: }
048:
049: if (false == appFiles
050: .getDatabaseAliasesFile_before_version_2_3().exists()) {
051: return null;
052: }
053:
054: try {
055: FileReader fr = new FileReader(appFiles
056: .getDatabaseAliasesFile_before_version_2_3());
057: BufferedReader br = new BufferedReader(fr);
058:
059: FileWriter fw = new FileWriter(appFiles
060: .getDatabaseAliasesFile());
061: BufferedWriter bw = new BufferedWriter(fw);
062:
063: String oldClassName = "net.sourceforge.squirrel_sql.fw.sql.SQLAlias";
064: String newClassName = SQLAlias.class.getName();
065:
066: String line = br.readLine();
067: while (null != line) {
068: int ix = line.indexOf(oldClassName);
069: if (-1 != ix) {
070: line = line.substring(0, ix)
071: + newClassName
072: + line.substring(
073: ix + oldClassName.length(), line
074: .length());
075: }
076:
077: bw.write(line + "\n");
078: line = br.readLine();
079: }
080:
081: bw.flush();
082: fw.flush();
083: bw.close();
084: fw.close();
085:
086: br.close();
087: fr.close();
088:
089: return null;
090: } catch (Exception e) {
091: return "Conversion of Aliases file failed: Could not write new Aliases file named \n"
092: + appFiles.getDatabaseAliasesFile().getPath()
093: + "\n"
094: + "You can not start this new version of SQuirreL using your existing Aliases.\n"
095: + "You may either continue to use your former SQuirreL version or remove file\n"
096: + appFiles
097: .getDatabaseAliasesFile_before_version_2_3()
098: .getPath()
099: + "\n"
100: + "for your first start of this SQuirreL version. SQuirreL will then try to create an empty Alias file named\n"
101: + appFiles.getDatabaseAliasesFile().getPath()
102: + "\n"
103: + "Please contact us about this problem. Send a mail to squirrel-sql-users@lists.sourceforge.net.";
104: }
105: }
106: }
|