Annotation of FM2SQL/Convert.java, revision 1.63

1.51      rogo        1: /*
                      2:  * Convert.java -- Converter class -  Filemaker to SQL Converter 
1.52      rogo        3:  * Copyright (C) 2003 Robert Gordesch (rogo@mpiwg-berlin.mpg.de1.51      rogo        4:  * This program is free software; you can redistribute it and/or modify it
                      5:  * under the terms of the GNU General Public License as published by the Free
                      6:  * Software Foundation; either version 2 of the License, or (at your option)
                      7:  * any later version.  Please read license.txt for the full details. A copy of
                      8:  * the GPL may be found at http://www.gnu.org/copyleft/lgpl.html  You should
                      9:  * have received a copy of the GNU General Public License along with this
                     10:  * program; if not, write to the Free Software Foundation, Inc., 59 Temple
                     11:  * Place, Suite 330, Boston, MA 02111-1307 USA  Created on 15.09.2003 by
                     12:  * rogo  
                     13:  */
                     14: 
1.1       rogo       15: import java.awt.Cursor;
                     16: import java.io.BufferedReader;
                     17: import java.io.BufferedWriter;
                     18: import java.io.File;
                     19: import java.io.FileInputStream;
                     20: import java.io.FileNotFoundException;
                     21: import java.io.FileOutputStream;
1.60      rogo       22: import java.io.InputStream;
1.1       rogo       23: import java.io.InputStreamReader;
                     24: import java.io.OutputStreamWriter;
                     25: import java.io.PrintStream;
                     26: import java.io.UnsupportedEncodingException;
1.60      rogo       27: import java.net.URL;
1.57      rogo       28: import java.sql.PreparedStatement;
                     29: import java.sql.SQLException;
                     30: import java.sql.Statement;
                     31: import java.sql.Types;
                     32: import java.util.ArrayList;
                     33: import java.util.Iterator;
                     34: import java.util.List;
                     35: import java.util.StringTokenizer;
                     36: import java.util.TreeSet;
                     37: import java.util.Vector;
1.1       rogo       38: 
                     39: import com.exploringxml.xml.Node;
                     40: import com.exploringxml.xml.Xparse;
                     41: 
1.34      rogo       42: class Convert
1.1       rogo       43: {
1.34      rogo       44:   static DBBean bean = new DBBean();
                     45:   static DBBean beanDest = new DBBean();
1.1       rogo       46: 
1.34      rogo       47:   static String user = "", passwd = "e1nste1n";
                     48:   static String userDest = "postgres", passwdDest = "rogo";
                     49:   static boolean batchRun = false;
1.1       rogo       50:   static Vector databases = new Vector();
1.30      rogo       51:   final static int numHits = 5000;
                     52:   final static int numIntervalls = 2;
1.1       rogo       53:   public static void main(String args[])
                     54:   {
1.34      rogo       55:     /*    try
                     56:         {
                     57:           //byte[] b = "ö".getBytes("UTF-8");
                     58:         //  System.out.println("QueryString " +b[0]+" "+b[1]+(new String(b).getBytes()[0])+" "+new String(b).getBytes()[1]);
                     59:         //System.out.println(new String(b,"UTF-8"));
                     60:         } catch (UnsupportedEncodingException e)
                     61:         {
                     62:           e.printStackTrace();
                     63:         }*/
1.1       rogo       64:     FileOutputStream file = null;
1.34      rogo       65:     if (args.length != 1)
1.1       rogo       66:     {
1.34      rogo       67:       System.out.println("Usage: java Convert <xml config file>");
                     68:       System.exit(-1);
1.1       rogo       69:     }
                     70:     try
                     71:     {
                     72:       file = new FileOutputStream("./log.txt");
                     73:     } catch (FileNotFoundException e1)
                     74:     {
                     75:       e1.printStackTrace();
1.34      rogo       76:     }
                     77:     PrintStream stream = new PrintStream(file);
1.60      rogo       78:    // System.setOut(stream);
                     79:     //System.setErr(stream);
                     80: 
1.34      rogo       81:     readXMLFile(args[0]);
1.60      rogo       82:     if (!(new File(args[0]).exists()))
                     83:       System.exit(0);
                     84: 
1.34      rogo       85:     System.out.println("Finished!");
1.1       rogo       86:     //convert("jdbc:fmpro:http://141.14.237.74:8050","jdbc:postgresql://erebos/test",null,null);
                     87:   }
1.55      rogo       88:   public static void convertBatch(DBBean source, DBBean destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception
1.1       rogo       89:   {
1.34      rogo       90:     bean = source;
                     91:     beanDest = destination;
1.55      rogo       92:     convert(null, null, names, layouts, selects, creates, ids, mode, delimiter);
                     93:     if (true)
                     94:       return;
1.34      rogo       95:     StringBuffer command = null;
1.1       rogo       96:     try
                     97:     {
                     98:       bean.setConnection(source.url);
1.34      rogo       99:       if (names == null)
                    100:         names = bean.getTableNames();
1.1       rogo      101:       //Collections.sort(names);
1.34      rogo      102:       int tbIndex = 1;
                    103: 
                    104:       for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
1.1       rogo      105:       {
                    106:         Vector[] result = null;
1.34      rogo      107:         try
1.1       rogo      108:         {
1.34      rogo      109:           String query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
                    110:           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
                    111:           query = (selects != null) ? selects.get(tbIndex).toString() : query;
                    112:           //if  vectors[1].get(i) != null)
                    113:           if (!layout.equals(""))
                    114:           {
                    115:             System.out.println("before " + query + " table" + names.get(tbIndex));
                    116:             layout = " layout " + bean.getQC() + layout + bean.getQC();
                    117:             String name = names.get(tbIndex).toString();
                    118:             StringBuffer queryLayout = new StringBuffer(query);
                    119:             queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
                    120:             query = queryLayout.toString();
                    121:             System.out.println("added layout " + query);
                    122: 
                    123:           }
                    124:           System.out.println(" performing query " + query);
1.1       rogo      125:           //result = bean.getQueryData(query, null, 0);
1.34      rogo      126:           bean.getConnection();
                    127:           bean.makeQuery(query, 0);
                    128:         } catch (Exception e)
1.1       rogo      129:         {
                    130:           System.out.println(e.getMessage());
                    131:           e.printStackTrace();
                    132:           continue;
                    133:         }
                    134:         //beanDest.setConnection("jdbc:postgresql://erebos/test3");
                    135:         beanDest.setConnection(destination.url);
                    136: 
                    137:         Statement stm = beanDest.getConnection().createStatement();
                    138: 
                    139:         Vector tables = beanDest.getTableNames();
1.34      rogo      140:         //   Collections.sort(tables);
                    141:         System.out.println("converting table " + names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames()); 
1.1       rogo      142:         tables = beanDest.getTableNames();
                    143:         // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
                    144:         stm = beanDest.getConnection().createStatement();
                    145:         // System.exit(0);
1.34      rogo      146:         if (mode == Convert.DataBase.CONVERT_MODE)
1.1       rogo      147:         {
1.34      rogo      148:           if (tables.indexOf(names.get(tbIndex)) >= 0)
                    149:           {
                    150:             stm.executeUpdate("drop table " + beanDest.getQC() + names.get(tbIndex) + beanDest.getQC());
                    151:             tables.remove((String) names.get(tbIndex));
                    152:             System.out.println("dropped table " + names.get(tbIndex));
                    153:           } else if (tables.indexOf(convertText(names.get(tbIndex).toString())) >= 0)
1.1       rogo      154:           {
1.34      rogo      155:             stm.executeUpdate("drop table " + beanDest.getQC() + convertText((String) names.get(tbIndex)) + beanDest.getQC());
                    156:             tables.remove(convertText((String) names.get(tbIndex)));
                    157:             System.out.println("dropped table " + names.get(tbIndex));
                    158:           }
                    159: 
                    160:           if (tables.indexOf(names.get(tbIndex)) < 0 && tables.indexOf(convertText(names.get(tbIndex).toString())) < 0)
1.1       rogo      161:           {
1.34      rogo      162:             if (creates.get(tbIndex).equals("") || creates.get(tbIndex).toString().toLowerCase().indexOf("create") < 0)
                    163:             {
                    164:               System.out.println("Warning empty or invalid create statement - creating one for you\n");
                    165: 
                    166:               command = new StringBuffer(50);
                    167:               command.append("CREATE TABLE ");
                    168:               command.append(beanDest.getQC());
                    169:               command.append(convertText((String) names.get(tbIndex)));
                    170:               command.append(beanDest.getQC());
                    171:               command.append("(");
                    172:               String type = null;
                    173:               Vector columnNames = bean.getColumnNames();
                    174:               for (int i = 0; i < columnNames.size() - 1; ++i)
                    175:               {
                    176:                 type = bean.metaData.getColumnTypeName(i + 1);
                    177:                 //   System.out.println(i+" "+result[1].get(i)+" "+type);
                    178:                 type = (type.equals("NUMBER")) ? "INT4" : type;
                    179:                 type = (type.equals("CONTAINER")) ? "TEXT" : type;
                    180: 
                    181:                 command.append(beanDest.getQC() + convertText((String) columnNames.get(i)) + beanDest.getQC() + " " + type + ", ");
                    182:               }
                    183:               type = bean.metaData.getColumnTypeName(columnNames.size());
                    184:               type = (type.equals("NUMBER")) ? "INT4" : type;
                    185:               type = (type.equals("CONTAINER")) ? "TEXT" : type;
                    186:               command.append(beanDest.getQC() + convertText((String) columnNames.get(columnNames.size() - 1)) + beanDest.getQC() + " " + type);
                    187:               command.append(" )");
                    188:             } else
                    189:               command = new StringBuffer().append(creates.get(tbIndex).toString());
                    190: 
                    191:             System.out.println(command);
                    192:             //  System.exit(0);
                    193:             //command.append(DBBean.getQC());   
                    194:             stm.executeUpdate(command.toString());
1.1       rogo      195: 
1.34      rogo      196:           }
1.1       rogo      197:         }
1.34      rogo      198:         Vector row = null;
                    199:         command = new StringBuffer();
1.1       rogo      200: 
                    201:         command.append("INSERT  INTO ");
                    202:         command.append(beanDest.getQC());
                    203:         command.append(convertText((String) names.get(tbIndex)));
                    204:         command.append(beanDest.getQC());
                    205:         command.append(" values ( ");
1.3       rogo      206: 
1.34      rogo      207:         for (int i = 0; i < bean.getColumnNames().size() - 1; ++i)
                    208:           command.append("?,");
                    209:         command.append("?)");
                    210:         PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
                    211:         System.out.println(command);
                    212:         while ((row = bean.getNextRow()) != null)
                    213:         {
                    214:           //print rows
                    215:           Object obj = null;
                    216:           for (int k = 0; k < row.size(); ++k)
                    217:           {
                    218:             obj = row.get(k);
                    219:             if (obj instanceof ArrayList)
1.55      rogo      220:               obj = formatFileMakerArray((List) obj, "\n");
1.34      rogo      221:             String str = (obj == null) ? "NULL" : obj.toString();
                    222:             if (!str.equals("NULL"))
                    223:               pstm.setString(k + 1, str);
                    224:             else
                    225:               pstm.setNull(k + 1, Types.NULL);
                    226:           }
                    227:           pstm.execute();
                    228: 
                    229:         } // to for loop    
                    230: 
                    231:       }
                    232:     } catch (Exception e)
                    233:     {
                    234:       System.out.println("Error while connecting to database " + e);
                    235:       //dialog.setVisible(false);
                    236:       //dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    237:       //FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    238:       java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
                    239:       java.io.PrintStream stream = new java.io.PrintStream(b);
                    240:       stream.print(command + "\n\n");
                    241:       e.printStackTrace(stream);
                    242:       System.err.println(b);
                    243:       //FM2SQL.showErrorDialog(b.toString(), "Error occured !");
                    244: 
1.1       rogo      245:     }
1.34      rogo      246:     //  dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    247:     //FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
1.1       rogo      248: 
1.34      rogo      249:     //  dialog.setVisible(false); 
1.1       rogo      250:   }
1.55      rogo      251:   public static String formatFileMakerArray(List list, String delimiter)
                    252:   {
                    253:     StringBuffer formattedString = new StringBuffer();
                    254:     for (int i = 0; i < list.size(); ++i)
                    255:     {
                    256:       formattedString.append(list.get(i).toString());
                    257:       if (i < list.size() - 1)
                    258:         formattedString.append(delimiter);
                    259:     }
                    260:     return formattedString.toString();
                    261:   }
1.38      rogo      262:   /**
                    263:    * Method for SQL UPDATE
                    264:    * @param source
                    265:    * @param destination
                    266:    * @param names
                    267:    * @param layouts
                    268:    * @param selects
                    269:    * @param creates
                    270:    * @param ids
                    271:    * @param mode
                    272:    * @throws Exception
                    273:    */
1.34      rogo      274:   public static void update(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode) throws Exception
1.12      rogo      275:   {
                    276:     FM2SQL.ProgressDialog dialog = null;
                    277:     if (FM2SQL.fmInstance != null)
                    278:     {
1.55      rogo      279:       dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance, bean);
1.12      rogo      280:       dialog.setTitle("Conversion running ...");
                    281:       dialog.title.setText("Getting table data ...");
                    282:       dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y + (FM2SQL.fmInstance.getHeight() - 250) / 2);
                    283:       dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    284:       FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    285:       dialog.thread = Thread.currentThread();
                    286:     }
                    287:     // setting user and passwd 
                    288:     bean.setUserAndPasswd(user, passwd);
                    289:     // setting user and passwd 
                    290:     beanDest.setUserAndPasswd(userDest, passwdDest);
                    291:     if (dialog != null)
                    292:       dialog.setSize(400, 250);
                    293:     StringBuffer command = null;
                    294:     String query = null;
                    295:     try
                    296:     {
                    297:       //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");    
                    298:       //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
                    299:       bean.setConnection(source);
                    300:       if (names == null)
                    301:         names = bean.getTableNames();
                    302:       // Collections.sort(names);
                    303:       int tbIndex = 1;
1.55      rogo      304: 
1.12      rogo      305:       // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
                    306:       for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
                    307:       {
                    308:         Vector[] result = null;
1.45      rogo      309:         String destTableName = "";
1.12      rogo      310:         try
                    311:         {
                    312:           query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
                    313:           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
                    314:           query = (selects != null) ? selects.get(tbIndex).toString() : query;
                    315:           //if  vectors[1].get(i) != null)
                    316:           if (layout != "")
                    317:           {
                    318:             layout = " layout " + bean.getQC() + layout + bean.getQC();
                    319:             String name = names.get(tbIndex).toString();
                    320:             StringBuffer queryLayout = new StringBuffer(query);
                    321:             queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
                    322:             query = queryLayout.toString();
                    323:             System.out.println("added layout  " + query);
                    324: 
                    325:           }
                    326:           dialog.title.setText("Getting table data ...");
                    327:           dialog.table.setText(names.get(tbIndex).toString());
                    328:           dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
                    329:           dialog.show();
                    330:           bean.getConnection();
                    331:           bean.makeQuery(query, 0);
                    332:         } catch (Exception e)
                    333:         {
                    334:           continue;
                    335:         }
1.45      rogo      336:         // determine destTableName from createStatement or from source table name
1.55      rogo      337:         if (!creates.get(tbIndex).equals(""))
                    338:         {
                    339:           String create = creates.get(tbIndex).toString().toLowerCase();
                    340:           int fromIndex = create.indexOf("table") + 5;
                    341:           int toIndex = create.indexOf("(");
                    342:           destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "").trim();
                    343:           System.out.println("destTable " + destTableName);
                    344: 
                    345:         } else
                    346:           destTableName = convertText(names.get(tbIndex).toString());
1.45      rogo      347: 
1.12      rogo      348:         //beanDest.setConnection("jdbc:postgresql://erebos/test3");
                    349:         beanDest.setConnection(destination);
                    350: 
                    351:         Statement stm = beanDest.getConnection().createStatement();
                    352: 
                    353:         Vector tables = beanDest.getTableNames();
                    354:         // Collections.sort(tables);
                    355:         System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames()); 
                    356:         tables = beanDest.getTableNames();
                    357:         // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
                    358:         stm = beanDest.getConnection().createStatement();
                    359:         // System.exit(0);
1.34      rogo      360: 
1.12      rogo      361:         if (dialog != null)
1.36      rogo      362:           dialog.title.setText("Updating table data ...");
1.12      rogo      363: 
                    364:         int j = -1;
1.34      rogo      365: 
1.12      rogo      366:         Vector row = null;
                    367:         command = new StringBuffer();
                    368: 
                    369:         command.append("UPDATE ");
                    370:         command.append(beanDest.getQC());
1.45      rogo      371:         command.append(destTableName);
                    372:         //command.append(convertText((String) names.get(tbIndex)));
1.12      rogo      373:         command.append(beanDest.getQC());
                    374:         command.append(" SET  ");
                    375: 
                    376:         int size = bean.getColumnNames().size();
                    377:         for (int i = 0; i < size - 1; ++i)
1.34      rogo      378:           command.append(beanDest.getQC() + convertText((String) bean.getColumnNames().get(i)) + beanDest.getQC() + " = ? ,");
                    379:         command.append(convertText((String) bean.getColumnNames().get(size - 1)) + " = ? ");
                    380:         command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " =  ?");
1.12      rogo      381:         PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
1.34      rogo      382:         System.out.println(command + " " + tbIndex);
                    383:         int rowCount = bean.getRowCount(query);
                    384:         int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex));
1.12      rogo      385:         while ((row = bean.getNextRow()) != null)
                    386:         {
                    387:           j++;
                    388:           //print rows
                    389:           Object obj = null;
                    390:           /* for(int k=0;k<row.size()-1;++k)
                    391:            {
                    392:                obj = row.get(k);
                    393:                //System.out.println("row "+obj+" "+k);
                    394:             if(obj!=null&&!(obj instanceof ArrayList))
                    395:             command.append("'"+convertUml(obj.toString())+"',"); 
                    396:             else if(obj!=null&&   obj instanceof ArrayList)
                    397:             command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',"); 
                    398:             else command.append("NULL,");
                    399:            }
                    400:            obj = row.get(row.size() - 1);
                    401:            if (obj != null && !(obj instanceof ArrayList))
                    402:            command.append("'"+convertUml(obj.toString())+"')"); 
                    403:            else
                    404:            if(obj!=null&&   obj instanceof ArrayList)
                    405:             command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')");         //command.append(obj.toString()+")");
                    406:             else command.append("NULL)");
                    407:            */
                    408:           //command.append("'"+row.get(row.size()-1)+"')"); 
                    409:           //command.append(" )");
                    410:           //  for(int k=0;k<row.size();++k)
                    411: 
                    412:           // System.out.println();
                    413:           //   System.out.println(command+" "+j+" "+row.size()+" "+  ((Vector)result2[0].get(j)).size());
                    414:           // System.out.println(command);
                    415:           for (int k = 0; k < row.size(); ++k)
                    416:           {
                    417:             obj = row.get(k);
                    418:             if (obj instanceof ArrayList)
                    419:               obj = ((List) obj).get(0);
                    420:             String str = (obj == null) ? "NULL" : obj.toString();
                    421:             if (!str.equals("NULL"))
                    422:               pstm.setString(k + 1, str);
                    423:             else
                    424:               pstm.setNull(k + 1, Types.NULL);
                    425:           }
1.34      rogo      426:           pstm.setString(row.size() + 1, row.get(idIndex).toString());
                    427:           //System.out.println(pstm.toString());
                    428:           // System.exit(0);
1.12      rogo      429:           pstm.execute();
                    430:           //stm.executeUpdate(command.toString());
1.34      rogo      431:           if (dialog != null)
                    432:             dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
1.12      rogo      433:           // System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j);
                    434:           command = null;
                    435:         } // to for loop    
                    436: 
                    437:       }
                    438:     } catch (Exception e)
                    439:     {
                    440:       System.out.println("Error while connecting to database " + e);
1.18      rogo      441:       if (dialog != null)
                    442:       {
                    443:         dialog.setVisible(false);
                    444:         dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    445:         FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    446:       }
1.12      rogo      447:       java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
                    448:       java.io.PrintStream stream = new java.io.PrintStream(b);
                    449:       stream.print(command + "\n\n");
                    450:       e.printStackTrace(stream);
                    451:       FM2SQL.showErrorDialog(b.toString(), "Error occured !");
                    452: 
                    453:     }
1.18      rogo      454:     if (dialog != null)
                    455:     {
                    456:       dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    457:       FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    458: 
                    459:       dialog.setVisible(false);
                    460:     }
1.12      rogo      461: 
                    462:   }
1.34      rogo      463:   /**
1.38      rogo      464:    *   transfers the specified array of tables  to the destination database
                    465:       and creates the table if it does not exist if it exists and mode is not append the table is dropped
1.55      rogo      466:   
1.38      rogo      467:    * @param source
                    468:    * @param destination
                    469:    * @param names
                    470:    * @param layouts
                    471:    * @param selects
                    472:    * @param creates
                    473:    * @param ids
                    474:    * @param mode
                    475:    * @throws Exception
1.55      rogo      476:    */
                    477: 
                    478:   public static void convert(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception
1.1       rogo      479:   {
1.34      rogo      480: 
                    481:     FM2SQL.ProgressDialog dialog = null;
                    482: 
                    483:     if (FM2SQL.fmInstance != null)
                    484:     {
1.55      rogo      485:       dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance, bean);
1.34      rogo      486:       dialog.setTitle("Conversion running ...");
                    487:       dialog.title.setText("Getting table data ...");
                    488:       dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y + (FM2SQL.fmInstance.getHeight() - 250) / 2);
                    489:       dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    490:       FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    491:       dialog.thread = Thread.currentThread();
                    492:       dialog.setSize(400, 250);
                    493:     }
                    494:     java.util.TreeSet myIds = new TreeSet();
                    495:     int deltaID = 1;
                    496:     String idField = "";
1.38      rogo      497:     String destTableName = "";
1.55      rogo      498:     String[] fieldNames = null;
1.34      rogo      499:     if (source != null && destination != null)
                    500:     {
                    501:       // setting user and passwd 
                    502:       bean.setUserAndPasswd(user, passwd);
                    503:       // setting user and passwd 
                    504:       beanDest.setUserAndPasswd(userDest, passwdDest);
                    505:     }
                    506:     StringBuffer command = null;
                    507:     String query = null;
1.1       rogo      508:     try
                    509:     {
1.55      rogo      510:       if (source != null)
                    511:         bean.setConnection(source);
1.34      rogo      512:       else
1.55      rogo      513:         bean.setConnection(bean.url);
                    514: 
1.34      rogo      515:       if (names == null)
                    516:         names = bean.getTableNames();
                    517:       // Collections.sort(names);
                    518:       int tbIndex = 1;
                    519: 
                    520:       // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
                    521:       for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
1.1       rogo      522:       {
                    523:         Vector[] result = null;
1.34      rogo      524:         try
1.1       rogo      525:         {
1.34      rogo      526:           query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
                    527:           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
                    528:           query = (selects != null) ? selects.get(tbIndex).toString() : query;
                    529:           //if  vectors[1].get(i) != null)
                    530:           if (layout != "")
                    531:           {
                    532:             layout = " layout " + bean.getQC() + layout + bean.getQC();
                    533:             String name = names.get(tbIndex).toString();
                    534:             StringBuffer queryLayout = new StringBuffer(query);
                    535:             queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
                    536:             query = queryLayout.toString();
                    537:             System.out.println("added layout  " + query);
1.21      rogo      538: 
1.34      rogo      539:           }
                    540:           //  if ( layout!= "")
                    541:           //    query += " layout " + bean.getQC() + layout + bean.getQC();
                    542:           if (dialog != null)
                    543:           {
                    544:             dialog.title.setText("Reading table data ...");
                    545:             dialog.table.setText(names.get(tbIndex).toString());
                    546:             dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
                    547:             dialog.show();
                    548:           }
                    549:           //result = bean.getQueryData(query, dialog, 0);
                    550:           bean.getConnection();
                    551:           bean.makeQuery(query, 50);
                    552:           idField = ids.get(tbIndex).toString();
                    553: 
                    554:         } catch (Exception e)
                    555:         {
                    556:           System.out.println(e);
                    557:           continue;
1.1       rogo      558:         }
1.55      rogo      559:         if (destination != null)
                    560:           beanDest.setConnection(destination);
1.34      rogo      561:         else
1.55      rogo      562:           beanDest.setConnection(beanDest.url);
1.1       rogo      563:         Statement stm = beanDest.getConnection().createStatement();
                    564: 
                    565:         Vector tables = beanDest.getTableNames();
1.34      rogo      566:         // Collections.sort(tables);
1.1       rogo      567:         System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames()); 
                    568:         tables = beanDest.getTableNames();
                    569:         // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
                    570:         stm = beanDest.getConnection().createStatement();
                    571:         // System.exit(0);
1.55      rogo      572: 
                    573:         // determine destTableName from createStatement or from source table name
                    574:         if (!creates.get(tbIndex).equals(""))
1.45      rogo      575:         {
1.55      rogo      576:           String create = creates.get(tbIndex).toString().toLowerCase();
                    577:           int fromIndex = create.indexOf("table") + 5;
                    578:           int toIndex = create.indexOf("(");
                    579:           int endIndex = create.indexOf(")", toIndex);
                    580: 
                    581:           destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "").trim();
                    582:           System.out.println("destTable " + destTableName);
                    583:           // retrieve field_names from select statement
                    584:           if (query.indexOf("*") < 0)
                    585:           {
                    586:             int selectEndIndex = query.indexOf("from");
                    587:             StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");
                    588:             int numFields = tokenizer.countTokens();
                    589:             fieldNames = new String[numFields];
                    590:             int fieldIndex = 0;
                    591:             while (tokenizer.hasMoreTokens())
                    592:             {
                    593:               String fieldName = tokenizer.nextToken().trim();
                    594:               fieldNames[fieldIndex] = convertText(fieldName);
                    595:               System.out.println(fieldNames[fieldIndex]);
                    596:               fieldIndex++;
                    597:             }
                    598: 
                    599:           } else
                    600:           {
                    601:             // use create statement for field names
                    602:             StringTokenizer tokenizer = new StringTokenizer(create.substring(toIndex + 1, endIndex), ",");
                    603:             int numFields = tokenizer.countTokens();
                    604:             fieldNames = new String[numFields];
                    605:             int fieldIndex = 0;
                    606:             while (tokenizer.hasMoreTokens())
                    607:             {
                    608:               String fieldName = tokenizer.nextToken().trim();
                    609:               int index = fieldName.lastIndexOf(" ");
                    610:               fieldNames[fieldIndex] = fieldName.substring(0, index);
                    611:               System.out.println(fieldNames[fieldIndex]);
                    612:               fieldIndex++;
                    613:             }
                    614:           }
1.45      rogo      615:         } else
1.55      rogo      616:         {
1.45      rogo      617:           destTableName = convertText(names.get(tbIndex).toString());
                    618: 
1.55      rogo      619:           // retrieve field_names from select statement
                    620:           if (query.indexOf("*") < 0)
                    621:           {
                    622:             int selectEndIndex = query.indexOf("from");
                    623:             StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");
                    624:             int numFields = tokenizer.countTokens();
                    625:             fieldNames = new String[numFields];
                    626:             int fieldIndex = 0;
                    627:             while (tokenizer.hasMoreTokens())
                    628:             {
                    629:               String fieldName = tokenizer.nextToken().trim();
                    630:               fieldNames[fieldIndex] = convertText(fieldName);
                    631:              // System.out.println("field "+ fieldNames[fieldIndex]);
                    632:               fieldIndex++;
                    633:             }
                    634: 
                    635:           } else
                    636:           {
                    637:             Vector fieldNamesVec = bean.getColumnNames();
                    638:             fieldNames = new String[fieldNamesVec.size()];
                    639:             int fieldIndex = -1;
                    640:             for (Iterator iter = fieldNamesVec.iterator(); iter.hasNext();)
                    641:             {
                    642:               String element = (String) iter.next();
                    643:               fieldNames[++fieldIndex] = bean.getQC() + convertText(element) + bean.getQC();
                    644:              // System.out.println("field " + fieldNames[fieldIndex]);
                    645:             }
                    646:           }
                    647:         }
1.34      rogo      648:         if (mode == Convert.DataBase.CONVERT_MODE)
1.1       rogo      649:         {
1.38      rogo      650: 
                    651:           if (tables.indexOf(destTableName) >= 0)
                    652:           {
                    653:             stm.executeUpdate("drop table " + beanDest.getQC() + destTableName + beanDest.getQC());
                    654:             tables.remove(destTableName);
                    655:             System.out.println("dropped table" + destTableName);
                    656: 
                    657:           }
                    658:           /*
                    659:           if(destTableName.equals(""))
1.34      rogo      660:           if (tables.indexOf(names.get(tbIndex)) >= 0)
                    661:           {
                    662:             stm.executeUpdate("drop table " + beanDest.getQC() + names.get(tbIndex) + beanDest.getQC());
                    663:             tables.remove((String) names.get(tbIndex));
                    664:             System.out.println("dropped table" + names.get(tbIndex));
                    665:           } else if (tables.indexOf(convertText(names.get(tbIndex).toString())) >= 0)
1.1       rogo      666:           {
1.34      rogo      667:             stm.executeUpdate("drop table " + beanDest.getQC() + convertText((String) names.get(tbIndex)) + beanDest.getQC());
                    668:             tables.remove(convertText((String) names.get(tbIndex)));
                    669:             System.out.println("dropped table" + names.get(tbIndex));
                    670:           }
1.55      rogo      671:           */
1.38      rogo      672:           if ((tables.indexOf(destTableName) < 0)) //&& tables.indexOf(names.get(tbIndex)) < 0 && tables.indexOf(convertText(names.get(tbIndex).toString())) < 0   )
1.1       rogo      673:           {
1.55      rogo      674: 
1.34      rogo      675:             if (creates.get(tbIndex).equals("") || creates.get(tbIndex).toString().toLowerCase().indexOf("create") < 0)
                    676:             {
                    677:               System.out.println("Warning empty or invalid create statement - creating one for you\n");
                    678: 
                    679:               command = new StringBuffer(50);
                    680:               command.append("CREATE TABLE ");
                    681:               command.append(beanDest.getQC());
                    682:               command.append(convertText((String) names.get(tbIndex)));
                    683:               command.append(beanDest.getQC());
                    684:               command.append("(");
                    685:               String type = null;
                    686:               Vector columnNames = bean.getColumnNames();
                    687:               for (int i = 0; i < columnNames.size() - 1; ++i)
                    688:               {
                    689:                 type = bean.metaData.getColumnTypeName(i + 1);
                    690:                 //   System.out.println(i+" "+result[1].get(i)+" "+type);
                    691:                 type = (type.equals("NUMBER")) ? "INT4" : type;
                    692:                 type = (type.equals("CONTAINER")) ? "TEXT" : type;
                    693: 
                    694:                 command.append(beanDest.getQC() + convertText((String) columnNames.get(i)) + beanDest.getQC() + " " + type + ", ");
                    695:               }
                    696:               type = bean.metaData.getColumnTypeName(columnNames.size());
                    697:               type = (type.equals("NUMBER")) ? "INT4" : type;
                    698:               type = (type.equals("CONTAINER")) ? "TEXT" : type;
                    699:               command.append(beanDest.getQC() + convertText((String) columnNames.get(columnNames.size() - 1)) + beanDest.getQC() + " " + type);
                    700:               command.append(" )");
                    701: 
                    702:               // System.out.println(command);
                    703:               //  System.exit(0);
                    704:               //command.append(DBBean.getQC());   
                    705:             } else
1.38      rogo      706:               command = new StringBuffer().append(creates.get(tbIndex).toString().toLowerCase());
1.34      rogo      707:             stm.executeUpdate(command.toString());
1.1       rogo      708: 
1.34      rogo      709:           }
1.1       rogo      710:         }
1.55      rogo      711:         if (dialog != null)
                    712:           dialog.title.setText("Writing table data ...");
1.34      rogo      713: 
                    714:         // prepare the insert statement
                    715:         int j = -1;
                    716:         Vector row = null;
                    717:         command = new StringBuffer();
                    718: 
                    719:         command.append("INSERT  INTO ");
                    720:         command.append(beanDest.getQC());
1.38      rogo      721:         command.append(destTableName); //convertText((String) names.get(tbIndex)));
1.34      rogo      722:         command.append(beanDest.getQC());
1.55      rogo      723:         command.append(" (");
                    724:         for (int i = 0; i < fieldNames.length; i++)
                    725:         {
                    726:           command.append(fieldNames[i]);
                    727:           if (i < fieldNames.length - 1)
                    728:             command.append(",");
                    729:         }
                    730:         command.append(") ");
                    731: 
1.34      rogo      732:         command.append(" values ( ");
                    733: 
1.38      rogo      734:         // add a question marks for every field 
1.34      rogo      735:         for (int i = 0; i < bean.getColumnNames().size() - 1; ++i)
                    736:           command.append("?,");
                    737:         command.append("?)");
                    738:         PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
                    739:         System.out.println(command);
                    740:         int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
                    741:         Vector vec = new Vector(myIds);
                    742:         int endIndex = -1;
                    743:         String tempQuery = query;
                    744:         String tempID = bean.getQC() + idField + bean.getQC();
1.38      rogo      745:         // if id_field not do incremental conversion else do it all at once
1.34      rogo      746:         if (!idField.equals(""))
                    747:         {
                    748:           long startTime = System.currentTimeMillis();
                    749:           int counter = -1;
                    750:           while (true)
                    751:           {
                    752:             ++counter;
1.55      rogo      753:             if (counter == 0 && dialog != null)
1.34      rogo      754:               dialog.title.setText("Check if data  is available");
1.55      rogo      755:             else if (dialog != null)
1.34      rogo      756:               dialog.title.setText("Check if more  data  is available");
                    757:             myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex), tempQuery, numHits);
                    758:             if (myIds.isEmpty())
                    759:               break;
                    760:             vec = new Vector(myIds);
                    761:             rowCount = vec.size();
                    762:             System.out.println("ID LIST SIZE " + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIds.size());
                    763:             deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
                    764:             if (vec.size() <= numIntervalls)
                    765:             {
                    766:               endIndex = 0;
                    767:               deltaID = vec.size();
                    768:             }
                    769:             for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)
                    770:             {
                    771:               System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());
                    772:               if (query.indexOf("where") > 0)
1.55      rogo      773:                 tempQuery = query + " and " + tempID + ">='" + vec.get(k) + "' and " + tempID + "<='" + vec.get(k + deltaID) + "'";
1.34      rogo      774:               else
1.55      rogo      775:                 tempQuery = query + " where " + tempID + ">='" + vec.get(k) + "' and " + tempID + "<='" + vec.get(k + deltaID) + "'";
1.34      rogo      776:               System.out.println(tempQuery);
1.55      rogo      777:               if (dialog != null)
                    778:                 dialog.title.setText("Reading table data ...");
1.34      rogo      779: 
                    780:               bean.makeQuery(tempQuery, deltaID);
1.55      rogo      781:               if (dialog != null)
                    782:                 dialog.title.setText("Writing table data ...");
1.34      rogo      783: 
1.55      rogo      784:               command = writeDatainDestTable(dialog, command, k, pstm, rowCount, delimiter);
1.34      rogo      785:               endIndex = k + deltaID;
                    786:             }
                    787:             System.out.println(endIndex);
1.38      rogo      788:             //all data written ? if not write last chunk of data
1.34      rogo      789:             if (endIndex == vec.size() - 1)
                    790:               System.out.println("fits");
                    791:             else
                    792:             {
                    793:               System.out.println(" last intervall from " + vec.get(endIndex) + " " + vec.lastElement());
1.1       rogo      794: 
1.34      rogo      795:               if (query.indexOf("where") > 0)
1.55      rogo      796:                 tempQuery = query + " and " + tempID + ">='" + vec.get(endIndex) + "' and " + tempID + "<='" + vec.lastElement() + "'";
1.34      rogo      797:               else
1.55      rogo      798:                 tempQuery = query + " where " + tempID + ">='" + vec.get(endIndex) + "' and " + tempID + "<='" + vec.lastElement() + "'";
1.34      rogo      799:               System.out.println(tempQuery);
1.55      rogo      800:               if (dialog != null)
                    801:                 dialog.title.setText("Reading table data ...");
1.34      rogo      802:               bean.makeQuery(tempQuery, 0);
1.55      rogo      803:               if (dialog != null)
                    804:                 dialog.title.setText("Writing table data ...");
                    805:               command = writeDatainDestTable(dialog, command, endIndex, pstm, rowCount, delimiter);
1.34      rogo      806:             }
1.38      rogo      807:             // prepare new query for next chunk
1.34      rogo      808:             if (query.indexOf("where") > 0)
1.55      rogo      809:               tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
1.34      rogo      810:             else
1.55      rogo      811:               tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
1.34      rogo      812: 
                    813:           }
                    814:           long endTime = System.currentTimeMillis();
                    815:           System.out.println("Time for incremental convert elapsed " + (endTime - startTime));
                    816:         } else
1.29      rogo      817:         {
1.38      rogo      818:           // read and write all in one big chunk
1.34      rogo      819:           long startTime = System.currentTimeMillis();
1.55      rogo      820: 
1.34      rogo      821:           bean.makeQuery(query, 0);
1.55      rogo      822:           command = writeDatainDestTable(dialog, command, j, pstm, rowCount, delimiter);
1.34      rogo      823:           long endTime = System.currentTimeMillis();
                    824:           System.out.println("Time for old convert elapsed " + (endTime - startTime));
1.29      rogo      825: 
                    826:         }
1.34      rogo      827:       }
                    828:     } catch (Exception e)
                    829:     {
                    830:       System.out.println("Error while connecting to database " + e);
                    831:       if (dialog != null)
                    832:       {
                    833:         dialog.setVisible(false);
                    834:         dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    835:         FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    836:         java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
                    837:         java.io.PrintStream stream = new java.io.PrintStream(b);
                    838:         stream.print(command + "\n\n");
                    839:         e.printStackTrace(stream);
                    840:         FM2SQL.showErrorDialog(b.toString(), "Error occured !");
                    841:       } else
                    842:       {
                    843:         e.printStackTrace();
1.33      rogo      844: 
1.3       rogo      845:       }
1.1       rogo      846:     }
1.34      rogo      847:     if (dialog != null)
                    848:     {
                    849:       dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    850:       FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    851:       dialog.setVisible(false);
                    852:     }
1.27      rogo      853:   }
1.38      rogo      854:   /**
                    855:    * Writes data to the destination table 
                    856:    * @param dialog  progress dialog
                    857:    * @param command 
                    858:    * @param j       data index for progress bar
                    859:    * @param pstm    prepared statement
                    860:    * @param rowCount number of datasets
                    861:    * @return   command
                    862:    * @throws Exception
                    863:    * @throws SQLException
                    864:    */
1.55      rogo      865:   private static StringBuffer writeDatainDestTable(FM2SQL.ProgressDialog dialog, StringBuffer command, int j, PreparedStatement pstm, int rowCount, String delimiter) throws Exception, SQLException
1.27      rogo      866:   {
                    867:     Vector row;
1.34      rogo      868:     while ((row = bean.getNextRow()) != null)
                    869:     {
                    870:       j++;
                    871:       // row = (Vector) result[0].get(j);
                    872:       /*   command = new StringBuffer();
                    873:       
                    874:            command.append("INSERT  INTO ");
                    875:            command.append(beanDest.getQC());
                    876:            command.append(convertText((String) names.get(tbIndex)));
                    877:            command.append(beanDest.getQC());
                    878:            command.append(" values ( ");
                    879:         */
                    880:       //print rows
                    881:       Object obj = null;
                    882:       /* for(int k=0;k<row.size()-1;++k)
                    883:        {
                    884:           obj = row.get(k);
                    885:           //System.out.println("row "+obj+" "+k);
                    886:          if(obj!=null&&!(obj instanceof ArrayList))
                    887:          command.append("'"+convertUml(obj.toString())+"',"); 
                    888:          else if(obj!=null&&   obj instanceof ArrayList)
                    889:          command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',"); 
                    890:          else command.append("NULL,");
                    891:        }
                    892:        obj = row.get(row.size() - 1);
                    893:        if (obj != null && !(obj instanceof ArrayList))
                    894:        command.append("'"+convertUml(obj.toString())+"')"); 
                    895:         else
                    896:         if(obj!=null&&   obj instanceof ArrayList)
                    897:          command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')");         //command.append(obj.toString()+")");
                    898:          else command.append("NULL)");
                    899:         */
                    900:       //command.append("'"+row.get(row.size()-1)+"')"); 
                    901:       //command.append(" )");
                    902:       //  for(int k=0;k<row.size();++k)
                    903: 
                    904:       // System.out.println();
                    905:       //   System.out.println(command+" "+j+" "+row.size()+" "+  ((Vector)result2[0].get(j)).size());
                    906:       // System.out.println(command);
                    907:       for (int k = 0; k < row.size(); ++k)
                    908:       {
                    909:         obj = row.get(k);
1.55      rogo      910: 
                    911:         if (obj instanceof ArrayList)
                    912:           obj = formatFileMakerArray((List) obj, delimiter);
                    913: 
1.34      rogo      914:         String str = (obj == null) ? "NULL" : obj.toString();
1.55      rogo      915:         if (obj instanceof Double)
                    916:         {
                    917:           pstm.setDouble(k + 1, ((Double) obj).doubleValue());
                    918:         } else if (!str.equals("NULL"))
1.34      rogo      919:           pstm.setString(k + 1, str);
                    920:         else
                    921:           pstm.setNull(k + 1, Types.NULL);
                    922:       }
                    923:       pstm.execute();
                    924:       //stm.executeUpdate(command.toString());
                    925:       if (dialog != null)
                    926:         dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
                    927:       // System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j);
                    928:       command = null;
                    929:     } // to while loop    
1.27      rogo      930:     return command;
1.1       rogo      931:   }
                    932: 
1.38      rogo      933:   /**
                    934:    *  removes special characters from the input string as well as .fp5 
                    935:    * @param newName String to change
                    936:    * @return
                    937:    */
1.34      rogo      938:   public static String convertText(String newName)
1.1       rogo      939:   {
                    940:     StringBuffer alterMe = new StringBuffer(newName.trim().toLowerCase());
                    941:     int length = alterMe.length();
                    942:     int j = 0;
1.34      rogo      943:     int index = alterMe.indexOf(".fp5");
                    944:     if (index >= 0)
                    945:     {
                    946:       alterMe.delete(index, index + 4);
                    947:       length = length - 4;
                    948:     }
                    949: 
1.1       rogo      950:     while (j < length)
1.34      rogo      951:     {
1.1       rogo      952:       if (alterMe.charAt(j) == ' ')
                    953:       {
                    954:         alterMe.setCharAt(j, '_');
1.34      rogo      955:         //    if(j<length-1) j=j+1;
                    956:       } else if (alterMe.charAt(j) == '_')
                    957:       {
                    958: 
                    959:         if (alterMe.charAt(j + 1) == '_')
                    960:           alterMe.deleteCharAt(j);
                    961:         length = length - 1;
                    962:         //  if(j<length-1) j=j+1;
                    963:       } else if (alterMe.charAt(j) == 'ä')
                    964:       {
                    965:         alterMe.setCharAt(j, 'a');
                    966:         alterMe.insert(j + 1, "e");
                    967:         length = length + 1;
                    968:         if (j < length - 1)
                    969:           j = j + 1;
                    970:       } else if (alterMe.charAt(j) == 'ö')
                    971:       {
                    972:         alterMe.setCharAt(j, 'o');
                    973:         alterMe.insert(j + 1, "e");
                    974:         length = length + 1;
                    975:         if (j < length - 1)
                    976:           j = j + 1;
                    977:       } else if (alterMe.charAt(j) == 'ü')
                    978:       {
                    979:         alterMe.setCharAt(j, 'u');
                    980:         alterMe.insert(j + 1, "e");
                    981:         length = length + 1;
                    982:         if (j < length - 1)
                    983:           j = j + 1;
                    984:       } else if (alterMe.charAt(j) == 'ß')
                    985:       {
                    986:         alterMe.setCharAt(j, 's');
                    987:         alterMe.insert(j + 1, "s");
                    988:         length = length + 1;
                    989:         if (j < length - 1)
                    990:           j = j + 1;
                    991:       } else if (alterMe.charAt(j) == ':')
1.1       rogo      992:       {
1.34      rogo      993:         if (j < length - 1)
                    994:         {
                    995:           if (alterMe.charAt(j + 1) == ':')
                    996:           {
                    997:             alterMe.setCharAt(j, '_');
                    998:             alterMe.delete(j + 1, j + 2);
                    999:             length = length - 1;
                   1000: 
                   1001:           }
1.1       rogo     1002: 
1.34      rogo     1003:           if (j < length - 1)
                   1004:             j = j + 1;
                   1005:         }
                   1006:       } else if (alterMe.charAt(j) == '-')
                   1007:       {
                   1008:         alterMe.setCharAt(j, '_');
1.16      rogo     1009: 
1.34      rogo     1010:       } else if (alterMe.charAt(j) == '?')
1.3       rogo     1011:       {
1.34      rogo     1012:         // changed ? to _ because of update statement
                   1013:         alterMe.setCharAt(j, '_');
                   1014:         // length = length + 1;
                   1015:         // j=j+1;
                   1016:         System.out.println(alterMe);
                   1017:       } else if (alterMe.charAt(j) == '.')
                   1018:       {
                   1019:         if (j == length - 1)
                   1020:         {
                   1021:           alterMe.delete(j, j);
                   1022:           length--;
                   1023:         } else
                   1024:           alterMe.setCharAt(j, '_');
1.3       rogo     1025:       }
1.34      rogo     1026: 
                   1027:       ++j;
1.1       rogo     1028:     }
                   1029:     return alterMe.toString();
                   1030:   }
1.38      rogo     1031:   /**
                   1032:    * Converts > and < in an entity (&gt; or &lt;)
                   1033:    * @param newName
                   1034:    * @return
                   1035:    */
1.4       rogo     1036:   public static String convertToEntities(String newName)
                   1037:   {
                   1038:     StringBuffer alterMe = new StringBuffer(newName.trim());
                   1039:     int length = alterMe.length();
                   1040:     int j = 0;
                   1041: 
                   1042:     while (j < length)
                   1043:     {
                   1044: 
                   1045:       if (alterMe.charAt(j) == '>')
                   1046:       {
                   1047:         alterMe.setCharAt(j, '&');
                   1048:         alterMe.insert(j + 1, "gt;");
                   1049:         length = length + 2;
                   1050:         if (j < length - 1)
                   1051:           j = j + 1;
                   1052: 
                   1053:       } else if (alterMe.charAt(j) == '<')
                   1054:       {
                   1055:         alterMe.setCharAt(j, '&');
                   1056:         alterMe.insert(j + 1, "lt;");
                   1057:         length = length + 2;
                   1058:         if (j < length - 1)
                   1059:           j = j + 1;
                   1060: 
                   1061:       }
                   1062:       ++j;
                   1063:     }
                   1064:     return alterMe.toString();
                   1065:   }
1.38      rogo     1066:   /**
                   1067:    * Masks the single quote character '-->\'
                   1068:    * @param newName
                   1069:    * @return
                   1070:    */
1.1       rogo     1071:   public static String convertUml(String newName)
1.34      rogo     1072:   {
                   1073:     StringBuffer alterMe = new StringBuffer(newName.trim());
                   1074:     int length = alterMe.length();
                   1075:     int j = 0;
                   1076: 
                   1077:     while (j < length)
                   1078:     {
                   1079: 
                   1080:       if (alterMe.charAt(j) == '\'')
                   1081:       {
                   1082:         alterMe.setCharAt(j, '\\');
                   1083:         alterMe.insert(j + 1, "'");
                   1084:         length = length + 1;
                   1085:         if (j < length - 1)
                   1086:           j = j + 1;
                   1087:       }
                   1088:       /*   else
                   1089:          if (alterMe.charAt(j) == '"')
                   1090:          {
                   1091:         alterMe.setCharAt(j, '\\');
                   1092:         alterMe.insert(j + 1, "\"");
                   1093:         length = length + 1;
                   1094:         if(j<length-1) j=j+1;
                   1095:          }
                   1096:         else
                   1097:         if (alterMe.charAt(j) == '>')
                   1098:          {
                   1099:         alterMe.setCharAt(j, '\\');
                   1100:         alterMe.insert(j + 1, ">");
                   1101:         length = length + 1;
                   1102:         if(j<length-1) j=j+1;
                   1103:          }
                   1104:         else
                   1105:         if (alterMe.charAt(j) == '<')
                   1106:          {
                   1107:         alterMe.setCharAt(j, '\\');
                   1108:         alterMe.insert(j + 1, "<");
                   1109:         length = length + 1;
                   1110:         if(j<length-1) j=j+1;
                   1111:          }
                   1112:        else
                   1113:        if (alterMe.charAt(j) == '?')
                   1114:          {
                   1115:         alterMe.setCharAt(j, '\\');
                   1116:         alterMe.insert(j + 1, "?");
                   1117:         length = length + 1;
                   1118:         if(j<length-1) j=j+1;
                   1119:          }
                   1120:        else
                   1121:        if (alterMe.charAt(j) == '&')
                   1122:          {
                   1123:         alterMe.setCharAt(j, '\\');
                   1124:         alterMe.insert(j + 1, "&");
                   1125:         length = length + 1;
                   1126:         if(j<length-1) j=j+1;
                   1127:          }
                   1128:        else
                   1129:        if (alterMe.charAt(j) == '=')
                   1130:          {
                   1131:         alterMe.setCharAt(j, '\\');
                   1132:         alterMe.insert(j + 1, "=");
                   1133:         length = length + 1;
                   1134:         if(j<length-1) j=j+1;
                   1135:          }
                   1136:        else
                   1137:        if (alterMe.charAt(j) == ',')
                   1138:          {
                   1139:         alterMe.setCharAt(j, '\\');
                   1140:         alterMe.insert(j + 1, ",");
                   1141:         length = length + 1;
                   1142:         if(j<length-1) j=j+1;
                   1143:          }
                   1144:        else
                   1145:        if (alterMe.charAt(j) == '.')
                   1146:          {
                   1147:         alterMe.setCharAt(j, '\\');
                   1148:         alterMe.insert(j + 1, ".");
                   1149:         length = length + 1;
                   1150:         if(j<length-1) j=j+1;
                   1151:          }
                   1152:        else
                   1153:        if (alterMe.charAt(j) == '[')
                   1154:          {
                   1155:         alterMe.setCharAt(j, '\\');
                   1156:         alterMe.insert(j + 1, ".");
                   1157:         length = length + 1;
                   1158:         if(j<length-1) j=j+1;
                   1159:          }
                   1160:       else
                   1161:        if (alterMe.charAt(j) == ']')
                   1162:          {
                   1163:         alterMe.setCharAt(j, '\\');
                   1164:         alterMe.insert(j + 1, ".");
                   1165:         length = length + 1;
                   1166:         if(j<length-1) j=j+1;
                   1167:          }
                   1168:       else
                   1169:        if (alterMe.charAt(j) == '%')
                   1170:          {
                   1171:         alterMe.setCharAt(j, '\\');
                   1172:         alterMe.insert(j + 1, "%");
                   1173:         length = length + 1;
                   1174:         if(j<length-1) j=j+1;
                   1175:          }*/
                   1176:       ++j;
                   1177:     }
                   1178:     return alterMe.toString();
                   1179:   }
1.55      rogo     1180:   /**
                   1181:    * parses the input xml file for batch conversion
                   1182:    * called from readXMLFile
                   1183:    * * @param sb
                   1184:    */
1.34      rogo     1185:   public static void parseXMLConfig(StringBuffer sb)
                   1186:   {
                   1187:     boolean finished = false;
                   1188:     // parse string and build document tree
                   1189:     Xparse parser = new Xparse();
                   1190:     parser.changeEntities = true;
                   1191:     Node root = parser.parse(sb.toString());
                   1192:     // printContents(root);
                   1193:     Vector databases = new Vector();
                   1194:     Vector tables = new Vector();
                   1195:     Vector layouts = new Vector();
                   1196:     Vector selects = new Vector();
                   1197:     Vector creates = new Vector();
                   1198:     Vector ids = new Vector();
1.42      rogo     1199:     String delimiter = "|";
1.34      rogo     1200:     int mode = -1;
                   1201: 
                   1202:     try
1.1       rogo     1203:     {
1.34      rogo     1204:       Node tempNode = root.find("convert/source", new int[] { 1, 1 });
                   1205:       if (tempNode == null)
                   1206:         throw new Error("parse error source tag missing");
                   1207:       System.out.println(tempNode.name);
                   1208:       int length = countNodes(tempNode);
                   1209:       for (int i = 1; i <= length; i++)
1.1       rogo     1210:       {
1.34      rogo     1211: 
1.1       rogo     1212:         DBBean database = new DBBean();
1.34      rogo     1213:         tables = new Vector();
                   1214:         layouts = new Vector();
                   1215:         selects = new Vector();
                   1216:         creates = new Vector();
                   1217:         ids = new Vector();
1.1       rogo     1218:         // parse dataBase
1.34      rogo     1219:         Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
                   1220:         Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
                   1221:         Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
                   1222:         Node node3 = root.find("convert/source/database", new int[] { 1, 1, i });
                   1223:         Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 });
1.55      rogo     1224:         Node delimiterNode = root.find("convert/source/database/delimiter", new int[] { 1, 1, i, 1, 1 });
1.59      rogo     1225:         Node useNormanToUnicodeMapper = root.find("convert/source/database/usenormantounicodemapper", new int[] { 1, 1, i, 1, 1 });
                   1226:     
1.34      rogo     1227:         if (node3 == null)
                   1228:           throw new Error("parse error database tag missing");
                   1229:         if (node == null)
                   1230:           throw new Error("parse error url tag missing");
                   1231:         if (node1 == null)
                   1232:           throw new Error("parse error user tag missing");
                   1233:         if (node2 == null)
                   1234:           throw new Error("parse error password tag missing");
1.55      rogo     1235:         if (delimiterNode != null)
                   1236:           delimiter = delimiterNode.getCharacters();
1.59      rogo     1237:         if(useNormanToUnicodeMapper!=null)
                   1238:         {
                   1239:           database.setUseNormanToUnicodeMapper(Boolean.valueOf(useNormanToUnicodeMapper.getCharacters()).booleanValue());
                   1240:           System.out.println("useMapper "+Boolean.valueOf(useNormanToUnicodeMapper.getCharacters().trim()).booleanValue());
                   1241:         } 
                   1242:    
1.1       rogo     1243:         String url = node.getCharacters();
                   1244:         String user = node1.getCharacters();
                   1245:         String password = node2.getCharacters();
                   1246:         database.setURL(url.trim());
                   1247:         database.setUserAndPasswd(user.trim(), password.trim());
1.34      rogo     1248:         System.out.println(node.name + " " + node.getCharacters());
                   1249:         System.out.println(node1.name + " " + node1.getCharacters());
                   1250:         System.out.println(node2.name + " " + node2.getCharacters());
                   1251:         String modeString = "";
                   1252:         if (nodeMode == null)
                   1253:           modeString = "convert";
                   1254:         else
                   1255:           modeString = nodeMode.getCharacters();
                   1256:         if (modeString.equals("convert"))
                   1257:           mode = DataBase.CONVERT_MODE;
                   1258:         else if (modeString.equals("append"))
                   1259:           mode = DataBase.APPEND_MODE;
                   1260:         else if (modeString.equals("update"))
                   1261:           mode = DataBase.UPDATE_MODE;
1.48      rogo     1262:         else if (modeString.equals("delete"))
                   1263:           mode = DataBase.DELETE_MODE;
1.62      rogo     1264:         else if (modeString.equals("synchronize"))
                   1265:             mode = DataBase.SYNCHRONIZE_MODE;
1.48      rogo     1266: 
1.34      rogo     1267:         //   if(node3!=null)
                   1268:         // System.out.println(node3.name);
                   1269: 
                   1270:         int length2 = countNodes(node3);
                   1271: 
                   1272:         System.out.println("number of tables " + length2);
                   1273: 
                   1274:         for (int j = 1; j <= length2; ++j)
                   1275:         {
                   1276:           Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
                   1277:           Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
                   1278:           Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
                   1279:           if (node4 != null)
                   1280:             System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
                   1281:           if (node5 != null)
                   1282:             System.out.println(node5.name + " " + node5.getCharacters());
                   1283:           if (node6 != null)
                   1284:             System.out.println(node6.name + " " + node6.getCharacters());
                   1285:           if (node4 == null)
                   1286:             throw new Error("parse error table tag missing");
                   1287:           // if(node5==null) throw new Error("parse error select tag missing");
                   1288:           // if(node6==null) throw new Error("parse error create tag missing");
                   1289:           String name = (String) node4.attributes.get("name");
                   1290:           String layout = (String) node4.attributes.get("layout");
                   1291:           String id = (String) node4.attributes.get("id");
                   1292:           System.out.println("id was " + id);
                   1293:           if (name == null)
                   1294:             throw new Error("parse error required table tag attribute name missing");
                   1295:           if (layout == null)
                   1296:             layout = "";
                   1297:           if (id == null)
                   1298:             id = "";
                   1299:           if (name.equals(""))
                   1300:             throw new Error("parse error table tag attribute must not be empty");
                   1301:           tables.add(name);
                   1302:           layouts.add(layout);
                   1303:           ids.add(id);
                   1304:           String query = (node5 == null) ? "" : node5.getCharacters();
                   1305:           if (query.equals(""))
                   1306:             System.err.println("Warning empty select tag or  select tag missing !!");
                   1307:           query = (query.equals("")) ? "select * from " + database.getQC() + name + database.getQC() : query;
                   1308:           selects.add(query);
                   1309:           if (node6 != null)
                   1310:             creates.add(node6.getCharacters().trim());
                   1311:           else
                   1312:             creates.add("");
                   1313: 
                   1314:         }
1.42      rogo     1315:         DataBase dataBase = new DataBase(database, tables, layouts, selects, creates, ids, mode);
                   1316:         dataBase.delimiter = delimiter;
                   1317:         databases.add(dataBase);
1.34      rogo     1318:       }
                   1319:       DBBean database = new DBBean();
                   1320:       // parse dataBase
                   1321:       Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
                   1322:       Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
                   1323:       Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
                   1324:       String url = node.getCharacters();
                   1325:       String user = node1.getCharacters();
                   1326:       String password = node2.getCharacters();
                   1327:       System.out.println(url);
                   1328:       database.setURL(url.trim());
                   1329:       database.setUserAndPasswd(user.trim(), password.trim());
                   1330:       //databases.add(database);
                   1331:       for (Iterator iter = databases.iterator(); iter.hasNext();)
1.1       rogo     1332:       {
                   1333:         DataBase db = (DataBase) iter.next();
1.62      rogo     1334:         if (mode == DataBase.CONVERT_MODE || mode == DataBase.APPEND_MODE)
1.55      rogo     1335:           convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids, mode, db.delimiter);
1.17      rogo     1336:         else
1.62      rogo     1337:         if (mode == DataBase.UPDATE_MODE)
                   1338:             update(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates, db.ids, mode);
                   1339:         else if(mode==DataBase.SYNCHRONIZE_MODE)
                   1340:         {
                   1341:           user =bean.user;
                   1342:           passwd =bean.passwd;
                   1343:           userDest =database.user;
                   1344:           passwdDest =database.passwd;
                   1345:           
                   1346:           synchronize(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates, db.ids, mode);
                   1347:         } 
1.1       rogo     1348:       }
1.34      rogo     1349:       // printContents(node3);
                   1350:       //   FM2SQL.fmInstance=new FM2SQL();
                   1351:     } catch (Exception e)
                   1352:     {
1.55      rogo     1353: 
1.34      rogo     1354:       e.printStackTrace();
                   1355:     }
                   1356:   }
                   1357:   public static Vector getXMLConfig(String xmlFile)
                   1358:   {
                   1359:     StringBuffer sb = null;
                   1360:     try
                   1361:     {
                   1362:       // read XML Metadata from a file
                   1363:       FileInputStream fi = new FileInputStream(xmlFile);
                   1364:       InputStreamReader isr = new InputStreamReader(fi, "UTF-8");
                   1365:       BufferedReader buffr = new BufferedReader(isr);
                   1366:       sb = new StringBuffer();
                   1367:       int c = 0;
                   1368:       while ((c = buffr.read()) != -1)
                   1369:       {
                   1370:         char ch = (char) c;
                   1371:         sb.append(ch);
                   1372:         // System.out.print((char)c);
1.1       rogo     1373:       }
                   1374: 
1.34      rogo     1375:     } catch (Exception e)
                   1376:     {
                   1377:       e.printStackTrace();
                   1378:     }
                   1379: 
                   1380:     boolean finished = false;
                   1381:     // parse string and build document tree
                   1382:     Xparse parser = new Xparse();
                   1383:     parser.changeEntities = true;
                   1384:     Node root = parser.parse(sb.toString());
                   1385:     // printContents(root);
                   1386:     Vector databases = new Vector();
                   1387:     Vector tables = new Vector();
                   1388:     Vector layouts = new Vector();
                   1389:     Vector selects = new Vector();
                   1390:     Vector creates = new Vector();
                   1391:     Vector ids = new Vector();
1.55      rogo     1392:     String delimiter = "|";
1.34      rogo     1393:     int mode = -1;
                   1394:     try
1.1       rogo     1395:     {
1.34      rogo     1396:       Node tempNode = root.find("convert/source", new int[] { 1, 1 });
                   1397:       if (tempNode == null)
                   1398:         throw new Error("parse error source tag missing");
                   1399:       System.out.println(tempNode.name);
                   1400:       int length = countNodes(tempNode);
                   1401:       for (int i = 1; i <= length; i++)
1.1       rogo     1402:       {
1.34      rogo     1403: 
                   1404:         DBBean database = new DBBean();
                   1405:         tables = new Vector();
                   1406:         layouts = new Vector();
                   1407:         selects = new Vector();
                   1408:         creates = new Vector();
                   1409:         ids = new Vector();
                   1410:         // parse dataBase
                   1411:         Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
                   1412:         Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
                   1413:         Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
                   1414:         Node node3 = root.find("convert/source/database", new int[] { 1, 1, i });
                   1415:         Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 });
1.55      rogo     1416:         Node delimiterNode = root.find("convert/source/database/delimiter", new int[] { 1, 1, i, 1, 1 });
1.59      rogo     1417:         Node useNormanToUnicodeMapper = root.find("convert/source/database/usenormantounicodemapper", new int[] { 1, 1, i, 1, 1 });
1.55      rogo     1418: 
1.44      rogo     1419:         if (delimiterNode != null)
                   1420:           delimiter = delimiterNode.getCharacters();
1.59      rogo     1421:         if(useNormanToUnicodeMapper!=null)
                   1422:         {
                   1423:           database.setUseNormanToUnicodeMapper(Boolean.valueOf(useNormanToUnicodeMapper.getCharacters()).booleanValue());
                   1424:           System.out.println("useMapper "+Boolean.valueOf(useNormanToUnicodeMapper.getCharacters().trim()).booleanValue());
                   1425:         } 
                   1426: 
1.34      rogo     1427:         if (node3 == null)
                   1428:           throw new Error("parse error database tag missing");
                   1429:         if (node == null)
                   1430:           throw new Error("parse error url tag missing");
                   1431:         if (node1 == null)
                   1432:           throw new Error("parse error user tag missing");
                   1433:         if (node2 == null)
                   1434:           throw new Error("parse error password tag missing");
                   1435:         String url = node.getCharacters();
                   1436:         String user = node1.getCharacters();
                   1437:         String password = node2.getCharacters();
                   1438:         database.setURL(url.trim());
                   1439:         database.setUserAndPasswd(user.trim(), password.trim());
                   1440:         System.out.println(node.name + " " + node.getCharacters());
                   1441:         System.out.println(node1.name + " " + node1.getCharacters());
                   1442:         System.out.println(node2.name + " " + node2.getCharacters());
                   1443:         String modeString = "";
                   1444:         if (nodeMode == null)
                   1445:           modeString = "convert";
                   1446:         else
                   1447:           modeString = nodeMode.getCharacters();
                   1448:         if (modeString.equals("convert"))
                   1449:           mode = DataBase.CONVERT_MODE;
                   1450:         else if (modeString.equals("append"))
                   1451:           mode = DataBase.APPEND_MODE;
                   1452:         else if (modeString.equals("update"))
                   1453:           mode = DataBase.UPDATE_MODE;
1.48      rogo     1454:         else if (modeString.equals("delete"))
                   1455:           mode = DataBase.DELETE_MODE;
                   1456: 
1.34      rogo     1457:         //   if(node3!=null)
                   1458:         // System.out.println(node3.name);
                   1459: 
                   1460:         int length2 = countNodes(node3);
                   1461: 
                   1462:         System.out.println("number of tables " + length2);
                   1463: 
                   1464:         for (int j = 1; j <= length2; ++j)
                   1465:         {
                   1466:           Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
                   1467:           Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
                   1468:           Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
                   1469:           if (node4 != null)
                   1470:             System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
                   1471:           if (node5 != null)
                   1472:             System.out.println(node5.name + " " + node5.getCharacters());
                   1473:           if (node6 != null)
                   1474:             System.out.println(node6.name + " " + node6.getCharacters());
                   1475:           if (node4 == null)
                   1476:             throw new Error("parse error table tag missing");
                   1477:           // if(node5==null) throw new Error("parse error select tag missing");
                   1478:           // if(node6==null) throw new Error("parse error create tag missing");
                   1479:           String name = (String) node4.attributes.get("name");
                   1480:           String layout = (String) node4.attributes.get("layout");
                   1481:           String id = (String) node4.attributes.get("id");
                   1482:           System.out.println("id was " + id);
                   1483: 
                   1484:           if (name == null)
                   1485:             throw new Error("parse error required table tag attribute name missing");
                   1486:           if (layout == null)
                   1487:             layout = "";
                   1488:           if (id == null)
                   1489:             id = "";
                   1490:           if (name.equals(""))
                   1491:             throw new Error("parse error table tag attribute must not be empty");
                   1492:           tables.add(name);
                   1493:           layouts.add(layout);
                   1494:           ids.add(id);
                   1495:           String query = (node5 == null) ? "" : node5.getCharacters();
                   1496:           if (query.equals(""))
                   1497:             System.err.println("Warning empty select tag or  select tag missing !!");
                   1498:           query = (query.equals("")) ? "select * from " + database.getQC() + name + database.getQC() : query;
                   1499:           selects.add(query);
                   1500:           if (node6 != null)
                   1501:             creates.add(node6.getCharacters().trim());
                   1502:           else
                   1503:             creates.add("");
                   1504: 
                   1505:         }
1.55      rogo     1506:         DataBase dataBase = new DataBase(database, tables, layouts, selects, creates, ids, mode);
                   1507:         dataBase.delimiter = delimiter;
1.43      rogo     1508:         databases.add(dataBase);
1.1       rogo     1509:       }
1.34      rogo     1510:       DBBean database = new DBBean();
                   1511:       // parse dataBase
                   1512:       Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
                   1513:       Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
                   1514:       Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
                   1515:       String url = node.getCharacters();
                   1516:       String user = node1.getCharacters();
                   1517:       String password = node2.getCharacters();
                   1518:       System.out.println(url);
                   1519:       database.setURL(url.trim());
                   1520:       database.setUserAndPasswd(user.trim(), password.trim());
                   1521:       databases.add(new DataBase(database, null, null, null, null, null, 0));
                   1522:       //databases.add(database);
                   1523:       /*    for (Iterator iter = databases.iterator(); iter.hasNext();)
                   1524:          {
                   1525:            DataBase db = (DataBase) iter.next();
                   1526:            convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates);
                   1527:           
                   1528:          }*/
                   1529:       // printContents(node3);
                   1530:       //   FM2SQL.fmInstance=new FM2SQL();
                   1531:     } catch (Exception e)
                   1532:     {
                   1533:       // TODO Auto-generated catch block
                   1534:       e.printStackTrace();
1.1       rogo     1535:     }
1.34      rogo     1536:     return databases;
                   1537:   }
                   1538: 
                   1539:   private static int countNodes(Node tempNode)
                   1540:   {
                   1541:     int length = 0;
                   1542:     for (int i = 0; i < tempNode.contents.v.size(); ++i)
1.1       rogo     1543:     {
1.34      rogo     1544:       Node node = (Node) tempNode.contents.v.elementAt(i);
                   1545:       if (node.type.equals("element"))
1.1       rogo     1546:       {
1.34      rogo     1547:         if (node.name.equals("database"))
                   1548:           length++;
                   1549:         if (node.name.equals("table"))
                   1550:           length++;
1.1       rogo     1551:       }
1.34      rogo     1552: 
                   1553:       // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1.1       rogo     1554:     }
1.34      rogo     1555:     return length;
                   1556:   }
                   1557:   private static void printContents(Node root)
                   1558:   {
                   1559: 
                   1560:     Vector contents = (root.index == null) ? root.contents.v : root.index.v;
                   1561:     for (int i = 0; i < contents.size(); ++i)
1.1       rogo     1562:     {
1.34      rogo     1563:       Node n = (Node) contents.elementAt(i);
                   1564:       if (n.type.equals("element"))
                   1565:       {
                   1566:         System.out.println("tag " + n.name);
                   1567:         System.out.println(n.getCharacters());
                   1568:         //contents=n.contents.v i=0;
                   1569:       }
                   1570:       // System.out.println(n.type);
1.1       rogo     1571:     }
1.34      rogo     1572:   }
1.38      rogo     1573:   /**
                   1574:    * reads the specified xml file
                   1575:    * @param xmlFile
                   1576:    */
1.34      rogo     1577:   public static void readXMLFile(String xmlFile)
1.1       rogo     1578:   {
1.34      rogo     1579:     try
                   1580:     {
1.60      rogo     1581:       InputStream stream = null;
                   1582:       
                   1583:       if(xmlFile.indexOf("file://")>=0||xmlFile.indexOf("http://")>=0)
                   1584:       {
                   1585:         URL url=new URL(xmlFile);
                   1586:         stream = url.openStream();
                   1587:       }
                   1588:       else 
                   1589:      // read XML Metadata from a file
                   1590:        stream = new FileInputStream(xmlFile);
                   1591:       InputStreamReader isr = new InputStreamReader(stream, "UTF-8");
1.34      rogo     1592:       BufferedReader buffr = new BufferedReader(isr);
                   1593:       StringBuffer sb = new StringBuffer();
                   1594:       int c = 0;
                   1595:       while ((c = buffr.read()) != -1)
                   1596:       {
                   1597:         char ch = (char) c;
                   1598:         sb.append(ch);
                   1599:         // System.out.print((char)c);
                   1600:       }
                   1601:       parseXMLConfig(sb);
                   1602:     } catch (Exception e)
                   1603:     {
                   1604:       e.printStackTrace();
                   1605:     }
1.1       rogo     1606:   }
1.55      rogo     1607: 
1.38      rogo     1608:   /**
                   1609:    * Helper class for XML-File parsing
                   1610:    * Holds the parsed data
                   1611:    * @author rogo
                   1612:    *
                   1613:    */
1.34      rogo     1614:   public static class DataBase
                   1615:   {
                   1616:     DBBean bean;
                   1617:     Vector creates;
                   1618:     Vector selects;
                   1619:     Vector layouts;
                   1620:     Vector tables;
                   1621:     Vector ids;
1.43      rogo     1622:     String delimiter = "//";
1.59      rogo     1623:     boolean useNormanToUnicodeMapper = false;
                   1624:     
1.34      rogo     1625:     final static int CONVERT_MODE = 1;
                   1626:     final static int APPEND_MODE = 2;
                   1627:     final static int UPDATE_MODE = 3;
1.40      rogo     1628:     final static int DELETE_MODE = 4;
1.62      rogo     1629:     final static int SYNCHRONIZE_MODE = 5;
                   1630:  
1.34      rogo     1631:     int mode = -1;
                   1632: 
                   1633:     public DataBase(DBBean bean, Vector tables, Vector layouts, Vector selects, Vector creates, Vector ids, int mode)
                   1634:     {
                   1635:       this.bean = bean;
                   1636:       this.tables = tables;
                   1637:       this.layouts = layouts;
                   1638:       this.selects = selects;
                   1639:       this.creates = creates;
                   1640:       this.ids = ids;
                   1641:       this.mode = mode;
                   1642:       this.bean.setIDVector(ids);
                   1643:     }
1.38      rogo     1644:     /**
1.42      rogo     1645:      * writes the data contained in this object to the buffered writer
1.38      rogo     1646:      * * @param buffr
                   1647:      * @throws Exception
                   1648:      */
1.34      rogo     1649:     public void exportToXML(BufferedWriter buffr) throws Exception
                   1650:     {
                   1651:       // ids=bean.getIDVector();
                   1652:       buffr.write("    <database>\n");
                   1653:       buffr.write("      <url>" + bean.url + "</url>\n");
                   1654:       buffr.write("      <user>" + bean.user + "</user>\n");
                   1655:       buffr.write("      <password>" + bean.passwd + "</password>\n");
1.55      rogo     1656:       buffr.write("      <delimiter>" + delimiter + "</delimiter>\n");
1.34      rogo     1657:       String modeString = "";
                   1658:       if (mode == CONVERT_MODE)
                   1659:         modeString = "convert";
                   1660:       else if (mode == APPEND_MODE)
1.5       rogo     1661:         modeString = "append";
1.34      rogo     1662:       else if (mode == UPDATE_MODE)
                   1663:         modeString = "update";
1.48      rogo     1664:       else if (mode == DELETE_MODE)
1.55      rogo     1665:         modeString = "delete";
1.34      rogo     1666: 
                   1667:       buffr.write("      <mode>" + modeString + "</mode>\n");
1.59      rogo     1668:       buffr.write("      <usenormantounicodemapper>" + useNormanToUnicodeMapper + "</usenormantounicodemapper>\n");
                   1669:       
1.34      rogo     1670:       int index = 0;
                   1671:       while (index < tables.size())
                   1672:       {
                   1673:         String table = (String) tables.get(index);
                   1674:         String layout = (String) layouts.get(index);
                   1675:         String select = (String) selects.get(index);
                   1676:         String create = (String) creates.get(index);
                   1677:         String id = (String) ids.get(index);
                   1678: 
                   1679:         buffr.write("      <table name = \"" + table + "\" layout = \"" + layout + "\" id = \"" + id + "\" >\n");
                   1680:         buffr.write("         <select>" + convertToEntities(select) + "</select>\n");
                   1681:         if (!create.equals(""))
                   1682:           buffr.write("         <create>" + create + "         </create>\n");
                   1683:         buffr.write("      </table>\n");
                   1684:         index++;
                   1685:       }
                   1686:       buffr.write("    </database>\n");
                   1687:     }
                   1688:     public String toString()
                   1689:     {
                   1690:       return bean.url + " " + tables;
                   1691:     }
1.1       rogo     1692: 
1.34      rogo     1693:   }
                   1694:   public static String convertToUTF8(Object command)
1.1       rogo     1695:   {
1.34      rogo     1696:     String str = null;
                   1697:     try
                   1698:     {
                   1699:       str = new String(command.toString().getBytes("UTF-8"));
                   1700:     } catch (UnsupportedEncodingException e)
                   1701:     {
                   1702:       // TODO Auto-generated catch block
                   1703:       e.printStackTrace();
                   1704:     }
                   1705:     return str;
1.1       rogo     1706:   }
                   1707:   public static void writeConfig(String file, DataBase source, DataBase destination) throws Exception
                   1708:   {
1.34      rogo     1709:     if (!file.toLowerCase().endsWith(".xml"))
                   1710:       file += ".xml";
1.1       rogo     1711:     File f = new File(file);
1.34      rogo     1712: 
                   1713:     FileOutputStream fout = new FileOutputStream(f);
                   1714:     OutputStreamWriter outsw = new OutputStreamWriter(fout, "UTF-8");
1.1       rogo     1715:     BufferedWriter buffw = new BufferedWriter(outsw);
                   1716:     buffw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                   1717:     buffw.newLine();
                   1718:     buffw.write("<convert>\n");
                   1719:     buffw.write("  <source>\n");
                   1720:     source.exportToXML(buffw);
                   1721:     buffw.write("  </source>\n");
                   1722:     buffw.write("\n  <destination>\n");
                   1723:     destination.exportToXML(buffw);
                   1724:     buffw.write("  </destination>\n");
                   1725:     buffw.write("</convert>\n");
                   1726:     buffw.close();
1.34      rogo     1727:   }
1.46      rogo     1728:   public static void delete(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode) throws Exception
                   1729:   {
                   1730:     FM2SQL.ProgressDialog dialog = null;
                   1731:     if (FM2SQL.fmInstance != null)
                   1732:     {
1.55      rogo     1733:       dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance, bean);
1.46      rogo     1734:       dialog.setTitle("Conversion running ...");
                   1735:       dialog.title.setText("Getting table data ...");
                   1736:       dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y + (FM2SQL.fmInstance.getHeight() - 250) / 2);
                   1737:       dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                   1738:       FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                   1739:       dialog.thread = Thread.currentThread();
                   1740:     }
                   1741:     // setting user and passwd 
                   1742:     bean.setUserAndPasswd(user, passwd);
                   1743:     // setting user and passwd 
                   1744:     beanDest.setUserAndPasswd(userDest, passwdDest);
                   1745:     if (dialog != null)
                   1746:       dialog.setSize(400, 250);
                   1747:     StringBuffer command = null;
                   1748:     String query = null;
                   1749:     try
                   1750:     {
                   1751:       //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");    
                   1752:       //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
                   1753:       bean.setConnection(source);
                   1754:       if (names == null)
                   1755:         names = bean.getTableNames();
                   1756:       // Collections.sort(names);
                   1757:       int tbIndex = 1;
                   1758: 
                   1759:       // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
                   1760:       for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
                   1761:       {
                   1762:         Vector[] result = null;
                   1763:         java.util.TreeSet myIds = new TreeSet();
                   1764:         java.util.TreeSet myIdsDest = new TreeSet();
                   1765:         int deltaID = 1;
                   1766:         String idField = "";
                   1767:         String destTableName = "";
                   1768: 
                   1769:         try
                   1770:         {
                   1771:           query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
                   1772:           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
                   1773:           query = (selects != null) ? selects.get(tbIndex).toString() : query;
                   1774:           //if  vectors[1].get(i) != null)
                   1775:           if (layout != "")
                   1776:           {
                   1777:             layout = " layout " + bean.getQC() + layout + bean.getQC();
                   1778:             String name = names.get(tbIndex).toString();
                   1779:             StringBuffer queryLayout = new StringBuffer(query);
                   1780:             queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
                   1781:             query = queryLayout.toString();
                   1782:             System.out.println("added layout  " + query);
                   1783: 
                   1784:           }
                   1785:           dialog.title.setText("Getting table data ...");
                   1786:           dialog.table.setText(names.get(tbIndex).toString());
                   1787:           dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
                   1788:           dialog.show();
                   1789:           bean.getConnection();
                   1790:           bean.makeQuery(query, 50);
                   1791:           idField = ids.get(tbIndex).toString();
                   1792: 
                   1793:         } catch (Exception e)
                   1794:         {
                   1795:           continue;
                   1796:         }
                   1797:         // determine destTableName from createStatement or from source table name
                   1798:         if (!creates.get(tbIndex).equals(""))
                   1799:         {
                   1800:           String create = creates.get(tbIndex).toString().toLowerCase();
                   1801:           int fromIndex = create.indexOf("table") + 5;
                   1802:           int toIndex = create.indexOf("(");
                   1803:           destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "").trim();
                   1804:           System.out.println("destTable " + destTableName);
                   1805: 
                   1806:         } else
                   1807:           destTableName = convertText(names.get(tbIndex).toString());
                   1808: 
                   1809:         // for id kram
                   1810:         Vector vec = null;
                   1811:         Vector vecDest = null;
1.47      rogo     1812:         //      tempo
                   1813:         beanDest.setConnection(destination);
1.46      rogo     1814:         int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
                   1815:         String tempID = bean.getQC() + idField + bean.getQC();
                   1816:         String tempIDdest = beanDest.getQC() + convertText(idField) + beanDest.getQC();
1.47      rogo     1817: 
1.46      rogo     1818:         int endIndex = -1;
                   1819:         String tempQuery = query;
1.47      rogo     1820:         String destQuery = query.replaceAll(names.get(tbIndex).toString(), destTableName);
1.49      rogo     1821:         String tempQueryDest = destQuery;
                   1822:         // remove extra query parts destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1);
1.47      rogo     1823:         System.out.println("new Query " + tempQueryDest);
1.46      rogo     1824:         if (!idField.equals(""))
                   1825:         {
                   1826:           long startTime = System.currentTimeMillis();
                   1827:           int counter = -1;
                   1828:           while (true)
                   1829:           {
                   1830:             ++counter;
                   1831:             if (counter == 0 && dialog != null)
                   1832:               dialog.title.setText("Check if data  is available");
                   1833:             else if (dialog != null)
                   1834:               dialog.title.setText("Check if more  data  is available");
                   1835:             myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex), tempQuery, numHits);
                   1836:             myIdsDest = beanDest.getIDVector(convertText(idField), destTableName, tempQueryDest, numHits);
                   1837:             if (myIds.isEmpty())
                   1838:               break;
                   1839:             vec = new Vector(myIds);
                   1840:             vecDest = new Vector(myIdsDest);
                   1841:             rowCount = vec.size();
1.47      rogo     1842:             // Deletion will work this way
                   1843:             Vector deleted = new Vector(vec);
                   1844:             Vector linesToDelete = new Vector(vecDest);
                   1845:             // remove all lines that should not be deleted
                   1846:             linesToDelete.removeAll(deleted);
                   1847:             // System.out.println("ID LIST SIZE " + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIdsDest.size());
1.46      rogo     1848:             /// @TODO complete delete task remove query show lines to be deleted let user choose if he wants that
1.47      rogo     1849:             System.out.println("number of lines to  be deleted " + linesToDelete.size());
1.46      rogo     1850:             deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
1.47      rogo     1851:             beanDest.setConnection(destination);
1.46      rogo     1852: 
1.47      rogo     1853:             Statement stm = beanDest.getConnection().createStatement();
1.46      rogo     1854: 
1.47      rogo     1855:             Vector tables = beanDest.getTableNames();
                   1856:             // Collections.sort(tables);
                   1857:             System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames()); 
                   1858:             tables = beanDest.getTableNames();
                   1859:             // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
                   1860:             stm = beanDest.getConnection().createStatement();
                   1861: 
                   1862:             if (dialog != null)
                   1863:               dialog.title.setText(" Deleting table data ...");
                   1864: 
                   1865:             int j = -1;
                   1866: 
                   1867:             Vector row = null;
                   1868:             command = new StringBuffer();
                   1869: 
                   1870:             command.append("DELETE FROM");
                   1871:             command.append(beanDest.getQC());
                   1872:             command.append(destTableName);
                   1873:             //command.append(convertText((String) names.get(tbIndex)));
                   1874:             command.append(beanDest.getQC());
                   1875:             int size = bean.getColumnNames().size();
                   1876:             command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " =  ?");
                   1877:             PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
                   1878:             System.out.println(command + " " + tbIndex);
                   1879:             //int rowCount = bean.getRowCount(query);
                   1880:             //        int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex));
                   1881:             while (true)
1.46      rogo     1882:             {
                   1883: 
1.48      rogo     1884:               ++j;
1.47      rogo     1885:               if (j == linesToDelete.size())
                   1886:                 break;
                   1887:               //print rows
                   1888:               pstm.setString(1, linesToDelete.get(j).toString());
                   1889:               System.out.println(pstm.toString());
1.55      rogo     1890:               pstm.execute();
1.46      rogo     1891:               if (dialog != null)
1.47      rogo     1892:                 dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
                   1893:               command = null;
1.46      rogo     1894:             }
                   1895:             // prepare new query for next chunk
                   1896:             if (query.indexOf("where") > 0)
                   1897:               tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
                   1898:             else
                   1899:               tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
                   1900: 
1.47      rogo     1901:           } //to outer while
                   1902:         } // to idfield if  
                   1903:       } // table loop
                   1904: 
                   1905:     } catch (Exception e)
1.55      rogo     1906:     {
                   1907:       System.out.println("Error while connecting to database " + e);
1.47      rogo     1908:       if (dialog != null)
                   1909:       {
1.55      rogo     1910:         dialog.setVisible(false);
1.47      rogo     1911:         dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                   1912:         FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
1.55      rogo     1913:       }
                   1914:       java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
                   1915:       java.io.PrintStream stream = new java.io.PrintStream(b);
                   1916:       stream.print(command + "\n\n");
                   1917:       e.printStackTrace(stream);
                   1918:       FM2SQL.showErrorDialog(b.toString(), "Error occured !");
                   1919: 
                   1920:     }
                   1921:     if (dialog != null)
                   1922:     {
                   1923:       dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                   1924:       FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
1.46      rogo     1925: 
1.55      rogo     1926:       dialog.setVisible(false);
                   1927:     }
                   1928:   } // to method
1.56      rogo     1929:   
1.60      rogo     1930:   
                   1931:   /**
                   1932:    * synchronize method based on delete method code
                   1933:    * 
                   1934:    * @param source
                   1935:    * @param destination
                   1936:    * @param names
                   1937:    * @param layouts
                   1938:    * @param selects
                   1939:    * @param creates
                   1940:    * @param ids
                   1941:    * @param mode
                   1942:    * @throws Exception
                   1943:    */
                   1944:   // TODO implement append,update and delete in one method
                   1945:   // TODO using id based algorithm
                   1946:   public static void synchronize(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode) throws Exception
                   1947:   {
1.62      rogo     1948:     System.out.println(" bin in synchronize!!!");
1.60      rogo     1949:     FM2SQL.ProgressDialog dialog = null;
                   1950:     if (FM2SQL.fmInstance != null)
                   1951:     {
                   1952:       dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance, bean);
1.61      rogo     1953:       dialog.setTitle("Synchronize running ...");
1.60      rogo     1954:       dialog.title.setText("Getting table data ...");
                   1955:       dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y + (FM2SQL.fmInstance.getHeight() - 250) / 2);
                   1956:       dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                   1957:       FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                   1958:       dialog.thread = Thread.currentThread();
                   1959:     }
                   1960:     // setting user and passwd 
                   1961:     bean.setUserAndPasswd(user, passwd);
                   1962:     // setting user and passwd 
                   1963:     beanDest.setUserAndPasswd(userDest, passwdDest);
                   1964:     if (dialog != null)
                   1965:       dialog.setSize(400, 250);
                   1966:     StringBuffer command = null;
                   1967:     String query = null;
                   1968:     try
                   1969:     {
                   1970:       //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");    
                   1971:       //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
                   1972:       bean.setConnection(source);
                   1973:       if (names == null)
                   1974:         names = bean.getTableNames();
                   1975:       // Collections.sort(names);
                   1976:       int tbIndex = 1;
                   1977: 
                   1978:       // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
                   1979:       for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
                   1980:       {
                   1981:         Vector[] result = null;
                   1982:         java.util.TreeSet myIds = new TreeSet();
                   1983:         java.util.TreeSet myIdsDest = new TreeSet();
                   1984:         int deltaID = 1;
                   1985:         String idField = "";
                   1986:         String destTableName = "";
                   1987: 
                   1988:         try
                   1989:         {
                   1990:           query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
                   1991:           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
                   1992:           query = (selects != null) ? selects.get(tbIndex).toString() : query;
                   1993:           //if  vectors[1].get(i) != null)
1.62      rogo     1994:           if (!layout.equals(""))
1.60      rogo     1995:           {
                   1996:             layout = " layout " + bean.getQC() + layout + bean.getQC();
                   1997:             String name = names.get(tbIndex).toString();
                   1998:             StringBuffer queryLayout = new StringBuffer(query);
                   1999:             queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
                   2000:             query = queryLayout.toString();
                   2001:             System.out.println("added layout  " + query);
                   2002: 
                   2003:           }
1.62      rogo     2004:           if(dialog!=null) {
1.60      rogo     2005:           dialog.title.setText("Getting table data ...");
                   2006:           dialog.table.setText(names.get(tbIndex).toString());
                   2007:           dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
                   2008:           dialog.show();
1.62      rogo     2009:           }
1.60      rogo     2010:           bean.getConnection();
                   2011:           bean.makeQuery(query, 50);
                   2012:           idField = ids.get(tbIndex).toString();
                   2013: 
                   2014:         } catch (Exception e)
                   2015:         {
                   2016:           continue;
                   2017:         }
                   2018:         // determine destTableName from createStatement or from source table name
                   2019:         if (!creates.get(tbIndex).equals(""))
                   2020:         {
                   2021:           String create = creates.get(tbIndex).toString().toLowerCase();
                   2022:           int fromIndex = create.indexOf("table") + 5;
                   2023:           int toIndex = create.indexOf("(");
                   2024:           destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "").trim();
                   2025:           System.out.println("destTable " + destTableName);
                   2026: 
                   2027:         } else
                   2028:           destTableName = convertText(names.get(tbIndex).toString());
                   2029: 
                   2030:         // for id kram
                   2031:         Vector vec = null;
                   2032:         Vector vecDest = null;
                   2033:         //      tempo
                   2034:         beanDest.setConnection(destination);
                   2035:         int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
                   2036:         String tempID = bean.getQC() + idField + bean.getQC();
                   2037:         String tempIDdest = beanDest.getQC() + convertText(idField) + beanDest.getQC();
                   2038: 
                   2039:         int endIndex = -1;
                   2040:         String tempQuery = query;
                   2041:         String destQuery = query.replaceAll(names.get(tbIndex).toString(), destTableName);
                   2042:         String tempQueryDest = destQuery;
                   2043:         // remove extra query parts destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1);
                   2044:         System.out.println("new Query " + tempQueryDest);
                   2045:         if (!idField.equals(""))
                   2046:         {
                   2047:           long startTime = System.currentTimeMillis();
                   2048:           int counter = -1;
                   2049:           while (true)
                   2050:           {
                   2051:             ++counter;
                   2052:             if (counter == 0 && dialog != null)
                   2053:               dialog.title.setText("Check if data  is available");
                   2054:             else if (dialog != null)
                   2055:               dialog.title.setText("Check if more  data  is available");
                   2056:             myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex), tempQuery, numHits);
                   2057:             myIdsDest = beanDest.getIDVector(convertText(idField), destTableName, tempQueryDest, numHits);
                   2058:             if (myIds.isEmpty())
                   2059:               break;
                   2060:             vec = new Vector(myIds);
                   2061:             vecDest = new Vector(myIdsDest);
                   2062:             rowCount = vec.size();
                   2063:             // Deletion will work this way
                   2064:             Vector deleted = new Vector(vec);
                   2065:             Vector linesToDelete = new Vector(vecDest);
1.61      rogo     2066:             
                   2067:             Vector linesToAppend = new Vector(vec);
                   2068:             // remove all lines that are already in dest database
                   2069:             linesToAppend.removeAll(vecDest);
1.60      rogo     2070:             // remove all lines that should not be deleted
                   2071:             linesToDelete.removeAll(deleted);
1.62      rogo     2072:             System.out.println("linesToAppend "+linesToAppend+" "+vecDest+" "+destTableName);
1.63    ! rogo     2073:             System.out.println("linesToDelete "+linesToDelete+" "+vecDest+" "+destTableName);
        !          2074:             // prepare new query for next chunk
        !          2075:                 if (query.indexOf("where") > 0)
        !          2076:                   tempQuery = query + " and " + tempID + ">='" + vec.firstElement() + "' and "+tempID + "<='" + vec.lastElement() + "' order by "+tempID;
        !          2077:                 else
        !          2078:                   tempQuery = query + " where " + tempID + ">='" + vec.firstElement() + "' and "+tempID + "<='" + vec.lastElement() + "'";// order by "+tempID;
        !          2079:                   System.out.println("tempQuery is now "+tempQuery+" order by "+tempID);
        !          2080:           //  bean.makeQuery(tempQuery,vec.size());
        !          2081:            Vector[] vectors=bean.getQueryData(tempQuery,vec.size());
        !          2082:            System.out.println(vectors[0]);
        !          2083:            System.out.println( bean.getColumnNames().indexOf(idField));
        !          2084:            System.exit(0);
1.60      rogo     2085:             // System.out.println("ID LIST SIZE " + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIdsDest.size());
                   2086:             /// @TODO complete delete task remove query show lines to be deleted let user choose if he wants that
                   2087:             System.out.println("number of lines to  be deleted " + linesToDelete.size());
                   2088:             deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
                   2089:             beanDest.setConnection(destination);
                   2090: 
                   2091:             Statement stm = beanDest.getConnection().createStatement();
                   2092: 
                   2093:             Vector tables = beanDest.getTableNames();
                   2094:             // Collections.sort(tables);
                   2095:             System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames()); 
                   2096:             tables = beanDest.getTableNames();
                   2097:             // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
                   2098:             stm = beanDest.getConnection().createStatement();
                   2099: 
                   2100:             if (dialog != null)
                   2101:               dialog.title.setText(" Deleting table data ...");
                   2102: 
                   2103:             int j = -1;
                   2104: 
                   2105:             Vector row = null;
                   2106:             command = new StringBuffer();
                   2107: 
                   2108:             command.append("DELETE FROM");
                   2109:             command.append(beanDest.getQC());
                   2110:             command.append(destTableName);
                   2111:             //command.append(convertText((String) names.get(tbIndex)));
                   2112:             command.append(beanDest.getQC());
                   2113:             int size = bean.getColumnNames().size();
                   2114:             command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " =  ?");
                   2115:             PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
                   2116:             System.out.println(command + " " + tbIndex);
                   2117:             //int rowCount = bean.getRowCount(query);
                   2118:             //        int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex));
                   2119:             while (true)
                   2120:             {
                   2121: 
                   2122:               ++j;
                   2123:               if (j == linesToDelete.size())
                   2124:                 break;
                   2125:               //print rows
                   2126:               pstm.setString(1, linesToDelete.get(j).toString());
                   2127:               System.out.println(pstm.toString());
                   2128:               pstm.execute();
                   2129:               if (dialog != null)
                   2130:                 dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
                   2131:               command = null;
                   2132:             }
                   2133:             // prepare new query for next chunk
                   2134:             if (query.indexOf("where") > 0)
                   2135:               tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
                   2136:             else
                   2137:               tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
                   2138: 
                   2139:           } //to outer while
                   2140:         } // to idfield if  
                   2141:       } // table loop
                   2142: 
                   2143:     } catch (Exception e)
                   2144:     {
                   2145:       System.out.println("Error while connecting to database " + e);
                   2146:       if (dialog != null)
                   2147:       {
                   2148:         dialog.setVisible(false);
                   2149:         dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                   2150:         FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                   2151:       }
                   2152:       java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
                   2153:       java.io.PrintStream stream = new java.io.PrintStream(b);
                   2154:       stream.print(command + "\n\n");
                   2155:       e.printStackTrace(stream);
                   2156:       FM2SQL.showErrorDialog(b.toString(), "Error occured !");
                   2157: 
                   2158:     }
                   2159:     if (dialog != null)
                   2160:     {
                   2161:       dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                   2162:       FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                   2163: 
                   2164:       dialog.setVisible(false);
                   2165:     }
                   2166:   } // to method
                   2167:   
                   2168: 
                   2169:  
1.56      rogo     2170:   /**
                   2171:    * Converts input String in norman encoding to unicode 
                   2172:    * @param inp
                   2173:    * @return converted String
                   2174:    */
                   2175:   static public String normanToUnicode(String inp) {
                   2176:     StringBuffer buf = new StringBuffer();
                   2177:     for (int i = 0; i < inp.length(); i++) {
                   2178:       char c = inp.charAt(i);
1.57      rogo     2179:      // System.out.println("char "+c+" "+(int)c);
                   2180:       switch (c) {
                   2181:       case 1: buf.append("\u00d0"); break; // Eth
                   2182:       case 2: buf.append("\u00f0"); break; // eth
                   2183:       case 3: buf.append("\u0141"); break; // Lslash
                   2184:       case 4: buf.append("\u0142"); break; // lslash
                   2185:       case 5: buf.append("\u0160"); break; // S caron
                   2186:       case 6: buf.append("\u0161"); break; // s caron
                   2187:       case 7: buf.append("\u00dd"); break; // Y acute
                   2188:       case 8: buf.append("\u00fd"); break; // y acute
                   2189:       case 11: buf.append("\u00de"); break; // Thorn
                   2190:       case 12: buf.append("\u00fe"); break; // thorn
                   2191:       case 14: buf.append("\u017d"); break; // Z caron
                   2192:       case 15: buf.append("\u017e"); break; // z caron
                   2193:       case 17: buf.append("\u0073"); break; // asciitilde
                   2194:       case 18: buf.append("j\u0305"); break; // j macron [does a single char exist?]
                   2195:       case 19: buf.append("^"); break; // circumflex
                   2196:       case 20: buf.append("\u0303"); break; // tilde
                   2197:       case 21: buf.append("\u00bd"); break; // onehalf
                   2198:       case 22: buf.append("\u00bc"); break; // onequarter
                   2199:       case 23: buf.append("\u00b9"); break; // onesuperior
                   2200:       case 24: buf.append("\u00be"); break; // threequarters
                   2201:       case 25: buf.append("\u00b3"); break; // threesuperior
                   2202:       case 26: buf.append("\u00b2"); break; // twosuperior
                   2203:       case 27: buf.append("\u00a6"); break; // brokenbar
                   2204:       case 28: buf.append("-"); break; // minus
                   2205:       case 29: buf.append("\u00d7"); break; // multiply
                   2206:       case 39: buf.append("'"); break; // quotesingle
                   2207:       case 94: buf.append("\u0302"); break; // circumflex
                   2208:       case 96: buf.append("\u0300"); break; // grave
                   2209:       case 196: buf.append("\u00c4"); break; // A dieresis
                   2210:       case 197: buf.append("\u00c5"); break; // A ring
                   2211:       case 201: buf.append("\u00c9"); break; // E acute
                   2212:       case 209: buf.append("\u00d1"); break; // N tilde
                   2213:       case 214: buf.append("\u00d6"); break; // O dieresis
                   2214:       case 220: buf.append("\u00dc"); break; // U dieresis
                   2215:       case 225: buf.append("\u00e1"); break; // a acute
                   2216:       case 224: buf.append("\u00e0"); break; // a grave
                   2217:       case 226: buf.append("\u00e2"); break; // a circumflex
                   2218:       case 228: buf.append("\u00e4"); break; // a dieresis
                   2219:       case 227: buf.append("\u00e3"); break; // a tilde
                   2220:       case 229: buf.append("\u0101"); break; // a macron
                   2221:       case 231: buf.append("\u00e7"); break; // c cedilla
                   2222:       case 233: buf.append("\u00e9"); break; // e acute
                   2223:       case 232: buf.append("\u00e8"); break; // e grave
                   2224:       case 234: buf.append("\u00ea"); break; // e circumflex
                   2225:       case 235: buf.append("\u00eb"); break; // e dieresis
                   2226:       case 237: buf.append("\u00ed"); break; // i acute
                   2227:       case 236: buf.append("\u00ec"); break; // i grave
                   2228:       case 238: buf.append("\u00ee"); break; // i circumflex
                   2229:       case 239: buf.append("\u00ef"); break; // i dieresis
                   2230:       case 241: buf.append("\u00f1"); break; // n tilde
                   2231:       case 243: buf.append("\u00f3"); break; // o acute
                   2232:       case 242: buf.append("\u00f2"); break; // o grave
                   2233:       case 244: buf.append("\u00f4"); break; // o circumflex
                   2234:       case 246: buf.append("\u00f6"); break; // o dieresis
                   2235:       case 245: buf.append("\u00f5"); break; // o tilde
                   2236:       case 250: buf.append("\u00fa"); break; // u acute
                   2237:       case 249: buf.append("\u00f9"); break; // u grave
                   2238:       case 251: buf.append("\u00fb"); break; // u circumflex
                   2239:       case 252: buf.append("\u00fc"); break; // u dieresis
                   2240:       case 8224: buf.append("\u1e6d"); break; // t underdot
                   2241:       case 176: buf.append("\u00b0"); break; // degree
                   2242:       case 162: buf.append("\u1ebd"); break; // e tilde
                   2243:       case 163: buf.append("\u00a3"); break; // sterling
                   2244:       case 167: buf.append("\u00a7"); break; // section
                   2245:       case 182: buf.append("\u00b6"); break; // paragraph
                   2246:       case 223: buf.append("\u015b"); break; // s acute
                   2247:       case 174: buf.append("\u1e5b"); break; // r underdot
                   2248:       case 169: buf.append("\u1e45"); break; // n overdot
                   2249:       case 353: buf.append("\u1e45"); break; // n overdot
                   2250:       case 180: buf.append("\u0301"); break; // acute
                   2251:       case 168: buf.append("\u0308"); break; // dieresis
                   2252:       case 8800: buf.append("\u1e6d"); break; // t underdot
                   2253:       case 198: buf.append("\u00c6"); break; // AE
                   2254:       case 216: buf.append("\u014d"); break; // o macron
                   2255:       case 8734: buf.append("\u0129"); break; // i tilde
                   2256:       case 177: buf.append("\u00b1"); break; // plusminus
                   2257:       case 165: buf.append("\u012b"); break; // i macron
                   2258:       case 181: buf.append("\u1e43"); break; // m underdot
                   2259:       case 8706: buf.append("\u1e0d"); break; // d underdot
                   2260:       case 240: buf.append("\u1e0d"); break; // d underdot
                   2261: 
                   2262:       case 8721: buf.append("\u1e63"); break; // s underdot
                   2263:       case 960: buf.append("\u017a"); break; // z acute
                   2264:       case 8747: buf.append("\u1e45"); break; // n overdot
                   2265:       case 937: buf.append("\u0169"); break; // u tilde
                   2266:       case 230: buf.append("\u00e6"); break; // ae
                   2267:       case 248: buf.append("\u00f8"); break; // oslash
                   2268:       case 191: buf.append("\u0304\u0306"); break; // macron breve
                   2269:       case 172: buf.append("\u1e37"); break; // 
                   2270:       case 8730: buf.append("j\u0305"); break; // j macron [does a single char exist?]
                   2271:       case 402: buf.append("\u0103"); break; // a breve
                   2272:       case 8776: buf.append("\u016d"); break; // u breve
                   2273:       case 187: buf.append("\u1e42"); break; // M underdot
                   2274:       case 8230: buf.append("\u2026"); break; // ellipsis
                   2275:       case 192: buf.append("\u00c0"); break; // A grave
                   2276:       case 195: buf.append("\u00c3"); break; // A tilde
                   2277:       case 213: buf.append("\u00d5"); break; // O tilde
                   2278:       case 338: buf.append("m\u0306"); break; // m breve
                   2279:       case 339: buf.append("\u0153"); break; // oe
                   2280:       case 8211: buf.append("\u2013"); break; // endash
                   2281:       case 8212: buf.append("\u2014"); break; // emdash
                   2282:       case 8220: buf.append("\u201c"); break; // quotedblleft
                   2283:       case 8221: buf.append("\u201d"); break; // quotedblright
                   2284:       case 8216: buf.append("\u2018"); break; // quoteleft
                   2285:       case 8217: buf.append("\u2019"); break; // quoteright
                   2286:       case 247: buf.append("\u1e37"); break; // l underring [actually underdot]
                   2287:       case 9674: buf.append("\u1e41"); break; // m overdot
                   2288:       case 255: buf.append("n\u0306"); break; // n breve
                   2289:       case 376: buf.append("\u00d7"); break; // multiply
                   2290:       case 8364: buf.append("\u1e5b"); break; // r underring [actually underdot]
                   2291:       case 8249: buf.append("\u1e44"); break; // N overdot
                   2292:       case 8250: buf.append("\u1e62"); break; // S underdot
                   2293:       case 64257: buf.append("\u1e24"); break; // H underdot
                   2294:       case 64258: buf.append("\u1e0c"); break; // D underdot
                   2295:       case 8225: buf.append("\u2021"); break; // daggerdbl
                   2296:       case 8218: buf.append("\u1e36"); break; // L underdot
                   2297:       case 8222: buf.append("\u0113"); break; // e macron
                   2298:       case 194: buf.append("\u1e5f"); break; // r underbar
                   2299:       case 202: buf.append("r\u0324"); break; // r underdieresis
                   2300:       case 193: buf.append("\u012a"); break; // I macron
                   2301:       case 8486:
                   2302:       case 203: buf.append("\u016b"); break; // u macron
                   2303:       case 200: buf.append("\u1e6c"); break; // T underdot
                   2304:       case 205: buf.append("\u1e64"); break; // S acute
                   2305:       case 206: buf.append("\u2020"); break; // dagger
                   2306:       case 207: buf.append("\u0115"); break; // e breve
                   2307:       case 204: buf.append("\u014f"); break; // o breve
                   2308:       case 211: buf.append("\u0100"); break; // A macron
                   2309:       case 212: buf.append("\u1e46"); break; // N underdot
                   2310:       case 210: buf.append("\u1e3b"); break; // l underbar
                   2311:       case 218: buf.append("\u016a"); break; // U macron
                   2312:       case 219: buf.append("\u0179"); break; // Z acute
                   2313:       case 217: buf.append("\u1e5a"); break; // R underdot
                   2314:       case 305: buf.append("\u0131"); break; // dotlessi
                   2315:       case 710: buf.append("\u1e47"); break; // n underdot
                   2316:       case 732: buf.append("\u1e49"); break; // n underbar
                   2317:       case 175: buf.append("\u0304"); break; // macron
                   2318:       case 728: buf.append("\u0306"); break; // breve
                   2319:       case 729:case 215: buf.append("\u1e25"); break; // h underdot
                   2320:       case 730: buf.append("\u012d"); break; // i breve
                   2321:       case 184: buf.append("\u0327"); break; // cedilla
                   2322:       case 733: buf.append("\u030b"); break; // hungarumlaut
                   2323:       case 731: buf.append("\u0328"); break; // ogonek
                   2324:       case 711: buf.append("\u030c"); break; // caron
                   2325:       case 199: buf.append("\u012b\u0303"); break; // imacron tilde
                   2326:       case 8226: buf.append("\u1e5d"); break; // runderdot macron
                   2327:       case 8482: buf.append("\u016b\0306"); break; // umacron breve
                   2328:       case 8804: buf.append("\u0101\u0301"); break; // amacron acute
                   2329:       case 8805: buf.append("\u016b\u0301"); break; // umacron acute
                   2330:       case 8719: buf.append("\u0113\u0301"); break; // emacron acute
                   2331:       case 170: buf.append("\u0113\u0300"); break; // emacron breve
                   2332:       case 186: buf.append("\u014d\u0300"); break; // omacron breve
                   2333:       case 161: buf.append("\u0101\u0306"); break; // amacron breve
                   2334:       case 8710: buf.append("\u0101\u0303"); break; // amacron tilde
                   2335:       case 171: buf.append("\u012b\u0301"); break; // imacron acute
                   2336:       case 8260: buf.append("\u1e00"); break; // runderdotmacron acute
                   2337:       case 183: buf.append("\u1e5b\u0301"); break; // runderdot acute
                   2338:       case 8240: buf.append("\u012b\u0306"); break; // imacron breve
                   2339:       case 63743: buf.append("\u016b\u0303"); break; // umacron tilde
                   2340:       default: buf.append(c);    
                   2341:        if((int)c>127)
                   2342:        System.out.println("char "+c+" "+(int)c);
                   2343:        break;
                   2344:       }
                   2345:     }
                   2346:     return buf.toString();
                   2347:   }
                   2348: 
                   2349:   static public String normanToUnicodeOld(String inp) {
                   2350:     StringBuffer buf = new StringBuffer();
                   2351:     for (int i = 0; i < inp.length(); i++) {
                   2352:       char c = inp.charAt(i);
1.56      rogo     2353:       switch (c) {
                   2354:       case 1: buf.append("\u00d0"); break; // Eth
                   2355:       case 2: buf.append("\u00f0"); break; // eth
                   2356:       case 3: buf.append("\u0141"); break; // Lslash
                   2357:       case 4: buf.append("\u0142"); break; // lslash
                   2358:       case 5: buf.append("\u0160"); break; // S caron
                   2359:       case 6: buf.append("\u0161"); break; // s caron
                   2360:       case 7: buf.append("\u00dd"); break; // Y acute
                   2361:       case 8: buf.append("\u00fd"); break; // y acute
                   2362:       case 11: buf.append("\u00de"); break; // Thorn
                   2363:       case 12: buf.append("\u00fe"); break; // thorn
                   2364:       case 14: buf.append("\u017d"); break; // Z caron
                   2365:       case 15: buf.append("\u017e"); break; // z caron
                   2366:       case 17: buf.append("\u0073"); break; // asciitilde
                   2367:       case 18: buf.append("j\u0305"); break; // j macron [does a single char exist?]
                   2368:       case 19: buf.append("^"); break; // circumflex
                   2369:       case 20: buf.append("\u0303"); break; // tilde
                   2370:       case 21: buf.append("\u00bd"); break; // onehalf
                   2371:       case 22: buf.append("\u00bc"); break; // onequarter
                   2372:       case 23: buf.append("\u00b9"); break; // onesuperior
                   2373:       case 24: buf.append("\u00be"); break; // threequarters
                   2374:       case 25: buf.append("\u00b3"); break; // threesuperior
                   2375:       case 26: buf.append("\u00b2"); break; // twosuperior
                   2376:       case 27: buf.append("\u00a6"); break; // brokenbar
                   2377:       case 28: buf.append("-"); break; // minus
                   2378:       case 29: buf.append("\u00d7"); break; // multiply
                   2379:       case 39: buf.append("'"); break; // quotesingle
                   2380:       case 94: buf.append("\u0302"); break; // circumflex
                   2381:       case 96: buf.append("\u0300"); break; // grave
                   2382:       case 128: buf.append("\u00c4"); break; // A dieresis
                   2383:       case 129: buf.append("\u00c5"); break; // A ring
                   2384:       case 131: buf.append("\u00c9"); break; // E acute
                   2385:       case 132: buf.append("\u00d1"); break; // N tilde
                   2386:       case 133: buf.append("\u00d6"); break; // O dieresis
                   2387:       case 134: buf.append("\u00dc"); break; // U dieresis
                   2388:       case 135: buf.append("\u00e1"); break; // a acute
                   2389:       case 136: buf.append("\u00e0"); break; // a grave
                   2390:       case 137: buf.append("\u00e2"); break; // a circumflex
                   2391:       case 138: buf.append("\u00e4"); break; // a dieresis
                   2392:       case 139: buf.append("\u00e3"); break; // a tilde
                   2393:       case 140: buf.append("\u0101"); break; // a macron
                   2394:       case 141: buf.append("\u00e7"); break; // c cedilla
                   2395:       case 142: buf.append("\u00e9"); break; // e acute
                   2396:       case 143: buf.append("\u00e8"); break; // e grave
                   2397:       case 144: buf.append("\u00ea"); break; // e circumflex
                   2398:       case 145: buf.append("\u00eb"); break; // e dieresis
                   2399:       case 146: buf.append("\u00ed"); break; // i acute
                   2400:       case 147: buf.append("\u00ec"); break; // i grave
                   2401:       case 148: buf.append("\u00ee"); break; // i circumflex
                   2402:       case 149: buf.append("\u00ef"); break; // i dieresis
                   2403:       case 150: buf.append("\u00f1"); break; // n tilde
                   2404:       case 151: buf.append("\u00f3"); break; // o acute
                   2405:       case 152: buf.append("\u00f2"); break; // o grave
                   2406:       case 153: buf.append("\u00f4"); break; // o circumflex
                   2407:       case 154: buf.append("\u00f6"); break; // o dieresis
                   2408:       case 155: buf.append("\u00f5"); break; // o tilde
                   2409:       case 156: buf.append("\u00fa"); break; // u acute
                   2410:       case 157: buf.append("\u00f9"); break; // u grave
                   2411:       case 158: buf.append("\u00fb"); break; // u circumflex
                   2412:       case 159: buf.append("\u00fc"); break; // u dieresis
                   2413:       case 160: buf.append("\u1e6d"); break; // t underdot
                   2414:       case 161: buf.append("\u00b0"); break; // degree
                   2415:       case 162: buf.append("\u1ebd"); break; // e tilde
                   2416:       case 163: buf.append("\u00a3"); break; // sterling
                   2417:       case 164: buf.append("\u00a7"); break; // section
                   2418:       case 166: buf.append("\u00b6"); break; // paragraph
                   2419:       case 167: buf.append("\u015b"); break; // s acute
                   2420:       case 168: buf.append("\u1e5b"); break; // r underdot
                   2421:       case 169: buf.append("\u1e67"); break; // s caron
                   2422:       case 171: buf.append("\u0301"); break; // acute
                   2423:       case 172: buf.append("\u0308"); break; // dieresis
                   2424:       case 173: buf.append("\u1e6d"); break; // t underdot
                   2425:       case 174: buf.append("\u00c6"); break; // AE
                   2426:       case 175: buf.append("\u014d"); break; // o macron
                   2427:       case 176: buf.append("\u0129"); break; // i tilde
                   2428:       case 177: buf.append("\u00b1"); break; // plusminus
                   2429:       case 180: buf.append("\u012b"); break; // i macron
                   2430:       case 181: buf.append("\u1e43"); break; // m underdot
                   2431:       case 182: buf.append("\u1e0d"); break; // d underdot
                   2432:       case 183: buf.append("\u1e63"); break; // s underdot
                   2433:       case 185: buf.append("\u017a"); break; // z acute
                   2434:       case 186: buf.append("\u1e45"); break; // n overdot
                   2435:       case 189: buf.append("\u0169"); break; // u tilde
                   2436:       case 190: buf.append("\u00e6"); break; // ae
                   2437:       case 191: buf.append("\u00f8"); break; // oslash
                   2438:       case 192: buf.append("\u0304\u0306"); break; // macron breve
                   2439:       case 194: buf.append("\u1e37"); break; // 
                   2440:       case 195: buf.append("j\u0305"); break; // j macron [does a single char exist?]
                   2441:       case 196: buf.append("\u0103"); break; // a breve
                   2442:       case 197: buf.append("\u016d"); break; // u breve
                   2443:       case 200: buf.append("\u1e42"); break; // M underdot
                   2444:       case 201: buf.append("\u2026"); break; // ellipsis
                   2445:       case 203: buf.append("\u00c0"); break; // A grave
                   2446:       case 204: buf.append("\u00c3"); break; // A tilde
                   2447:       case 205: buf.append("\u00d5"); break; // O tilde
                   2448:       case 206: buf.append("m\u0306"); break; // m breve
                   2449:       case 207: buf.append("\u0153"); break; // oe
                   2450:       case 208: buf.append("\u2013"); break; // endash
                   2451:       case 209: buf.append("\u2014"); break; // emdash
                   2452:       case 210: buf.append("\u201c"); break; // quotedblleft
                   2453:       case 211: buf.append("\u201d"); break; // quotedblright
                   2454:       case 212: buf.append("\u2018"); break; // quoteleft
                   2455:       case 213: buf.append("\u2019"); break; // quoteright
                   2456:       case 214: buf.append("\u1e37"); break; // l underring [actually underdot]
                   2457:       case 215: buf.append("\u1e41"); break; // m overdot
                   2458:       case 216: buf.append("n\u0306"); break; // n breve
                   2459:       case 217: buf.append("\u00d7"); break; // multiply
                   2460:       case 219: buf.append("\u1e5b"); break; // r underring [actually underdot]
                   2461:       case 220: buf.append("\u1e44"); break; // N overdot
                   2462:       case 221: buf.append("\u1e62"); break; // S underdot
                   2463:       case 222: buf.append("\u1e24"); break; // H underdot
                   2464:       case 223: buf.append("\u1e0c"); break; // D underdot
                   2465:       case 224: buf.append("\u2021"); break; // daggerdbl
                   2466:       case 226: buf.append("\u1e36"); break; // L underdot
                   2467:       case 227: buf.append("\u0113"); break; // e macron
                   2468:       case 229: buf.append("\u1e5f"); break; // r underbar
                   2469:       case 230: buf.append("r\u0324"); break; // r underdieresis
                   2470:       case 231: buf.append("\u012a"); break; // I macron
                   2471:       case 232: buf.append("\u016b"); break; // u macron
                   2472:       case 233: buf.append("\u01e6c"); break; // T underdot
                   2473:       case 234: buf.append("\u1e64"); break; // S acute
                   2474:       case 235: buf.append("\u2020"); break; // dagger
                   2475:       case 236: buf.append("\u0115"); break; // e breve
                   2476:       case 237: buf.append("\u014f"); break; // o breve
                   2477:       case 238: buf.append("\u0100"); break; // A macron
                   2478:       case 239: buf.append("\u1e46"); break; // N underdot
                   2479:       case 241: buf.append("\u1e3b"); break; // l underbar
                   2480:       case 242: buf.append("\u016a"); break; // U macron
                   2481:       case 243: buf.append("\u0179"); break; // Z acute
                   2482:       case 244: buf.append("\u1e5a"); break; // R underdot
                   2483:       case 245: buf.append("\u0131"); break; // dotlessi
                   2484:       case 246: buf.append("\u1e47"); break; // n underdot
                   2485:       case 247: buf.append("\u1e49"); break; // n underbar
                   2486:       case 248: buf.append("\u0304"); break; // macron
                   2487:       case 249: buf.append("\u0306"); break; // breve
                   2488:       case 250: buf.append("\u1e25"); break; // h underdot
                   2489:       case 251: buf.append("\u012d"); break; // i breve
                   2490:       case 252: buf.append("\u0327"); break; // cedilla
                   2491:       case 253: buf.append("\u030b"); break; // hungarumlaut
                   2492:       case 254: buf.append("\u0328"); break; // ogonek
                   2493:       case 255: buf.append("\u030c"); break; // caron
                   2494:       case 130: buf.append("\u012b\u0303"); break; // imacron tilde
                   2495:       case 165: buf.append("\u1e5d"); break; // runderdot macron
                   2496:       case 170: buf.append("\u016b\0306"); break; // umacron breve
                   2497:       case 178: buf.append("\u0101\u0301"); break; // amacron acute
                   2498:       case 179: buf.append("\u016b\u0301"); break; // umacron acute
                   2499:       case 184: buf.append("\u0113\u0301"); break; // emacron acute
                   2500:       case 187: buf.append("\u0113\u0300"); break; // emacron breve
                   2501:       case 188: buf.append("\u014d\u0300"); break; // omacron breve
                   2502:       case 193: buf.append("\u0101\u0306"); break; // amacron breve
                   2503:       case 198: buf.append("\u0101\u0303"); break; // amacron tilde
                   2504:       case 199: buf.append("\u012b\u0301"); break; // imacron acute
                   2505:       case 218: buf.append("\u1e00"); break; // runderdotmacron acute
                   2506:       case 225: buf.append("\u1e5b\u0301"); break; // runderdot acute
                   2507:       case 228: buf.append("\u012b\u0306"); break; // imacron breve
                   2508:       case 240: buf.append("\u016b\u0303"); break; // umacron tilde
                   2509:       default: buf.append(c); break;
                   2510:       }
                   2511:     }
                   2512:     return buf.toString();
                   2513:   }
1.58      rogo     2514:   static public String normanToUnicodeNew(String inp) {
                   2515:     StringBuffer buf = new StringBuffer();
                   2516:     for (int i = 0; i < inp.length(); i++) {
                   2517:       char c = inp.charAt(i);
                   2518:       switch (c) {
                   2519:         case 1: buf.append("\u00d0"); break; // Eth
                   2520:         case 2: buf.append("\u00f0"); break; // eth
                   2521:         case 3: buf.append("\u0141"); break; // Lslash
                   2522:         case 4: buf.append("\u0142"); break; // lslash
                   2523:         case 5: buf.append("\u0160"); break; // S caron
                   2524:         case 6: buf.append("\u0161"); break; // s caron
                   2525:         case 7: buf.append("\u00dd"); break; // Y acute
                   2526:         case 8: buf.append("\u00fd"); break; // y acute
                   2527:         case 11: buf.append("\u00de"); break; // Thorn
                   2528:         case 12: buf.append("\u00fe"); break; // thorn
                   2529:         case 14: buf.append("\u017d"); break; // Z caron
                   2530:         case 15: buf.append("\u017e"); break; // z caron
                   2531:         case 17: buf.append("\u0073"); break; // asciitilde
                   2532:         case 18: buf.append("j\u0305"); break; // j macron [does a single char exist?]
                   2533:         case 19: buf.append("^"); break; // circumflex
                   2534:         case 20: buf.append("\u0303"); break; // tilde
                   2535:         case 21: buf.append("\u00bd"); break; // onehalf
                   2536:         case 22: buf.append("\u00bc"); break; // onequarter
                   2537:         case 23: buf.append("\u00b9"); break; // onesuperior
                   2538:         case 24: buf.append("\u00be"); break; // threequarters
                   2539:         case 25: buf.append("\u00b3"); break; // threesuperior
                   2540:         case 26: buf.append("\u00b2"); break; // twosuperior
                   2541:         case 27: buf.append("\u00a6"); break; // brokenbar
                   2542:         case 28: buf.append("-"); break; // minus
                   2543:         case 29: buf.append("\u00d7"); break; // multiply
                   2544:         case 39: buf.append("'"); break; // quotesingle
                   2545:         case 94: buf.append("\u0302"); break; // circumflex
                   2546:         case 96: buf.append("\u0300"); break; // grave
                   2547:         case 196: buf.append("\u00c4"); break; // A dieresis
                   2548:         case 197: buf.append("\u00c5"); break; // A ring
                   2549:         case 201: buf.append("\u00c9"); break; // E acute
                   2550:         case 209: buf.append("\u00d1"); break; // N tilde
                   2551:         case 214: buf.append("\u00d6"); break; // O dieresis
                   2552:         case 220: buf.append("\u00dc"); break; // U dieresis
                   2553:         case 225: buf.append("\u00e1"); break; // a acute
                   2554:         case 224: buf.append("\u00e0"); break; // a grave
                   2555:         case 226: buf.append("\u00e2"); break; // a circumflex
                   2556:         case 228: buf.append("\u00e4"); break; // a dieresis
                   2557:         case 227: buf.append("\u00e3"); break; // a tilde
                   2558:         case 229: buf.append("\u0101"); break; // a macron
                   2559:         case 231: buf.append("\u00e7"); break; // c cedilla
                   2560:         case 233: buf.append("\u00e9"); break; // e acute
                   2561:         case 232: buf.append("\u00e8"); break; // e grave
                   2562:         case 234: buf.append("\u00ea"); break; // e circumflex
                   2563:         case 235: buf.append("\u00eb"); break; // e dieresis
                   2564:         case 237: buf.append("\u00ed"); break; // i acute
                   2565:         case 236: buf.append("\u00ec"); break; // i grave
                   2566:         case 238: buf.append("\u00ee"); break; // i circumflex
                   2567:         case 239: buf.append("\u00ef"); break; // i dieresis
                   2568:         case 241: buf.append("\u00f1"); break; // n tilde
                   2569:         case 243: buf.append("\u00f3"); break; // o acute
                   2570:         case 242: buf.append("\u00f2"); break; // o grave
                   2571:         case 244: buf.append("\u00f4"); break; // o circumflex
                   2572:         case 246: buf.append("\u00f6"); break; // o dieresis
                   2573:         case 245: buf.append("\u00f5"); break; // o tilde
                   2574:         case 250: buf.append("\u00fa"); break; // u acute
                   2575:         case 249: buf.append("\u00f9"); break; // u grave
                   2576:         case 251: buf.append("\u00fb"); break; // u circumflex
                   2577:         case 252: buf.append("\u00fc"); break; // u dieresis
                   2578:         case 8224: buf.append("\u1e6d"); break; // t underdot
                   2579:         case 176: buf.append("\u00b0"); break; // degree
                   2580:         case 162: buf.append("\u1ebd"); break; // e tilde
                   2581:         case 163: buf.append("\u00a3"); break; // sterling
                   2582:         case 167: buf.append("\u00a7"); break; // section
                   2583:         case 182: buf.append("\u00b6"); break; // paragraph
                   2584:         case 223: buf.append("\u015b"); break; // s acute
                   2585:         case 174: buf.append("\u1e5b"); break; // r underdot
                   2586:         case 169: buf.append("\u1e45"); break; // n overdot
                   2587:         case 180: buf.append("\u0301"); break; // acute
                   2588:         case 168: buf.append("\u0308"); break; // dieresis
                   2589:         case 8800: buf.append("\u1e6d"); break; // t underdot
                   2590:         case 198: buf.append("\u00c6"); break; // AE
                   2591:         case 216: buf.append("\u014d"); break; // o macron
                   2592:         case 8734: buf.append("\u0129"); break; // i tilde
                   2593:         case 177: buf.append("\u00b1"); break; // plusminus
                   2594:         case 165: buf.append("\u012b"); break; // i macron
                   2595:         case 181: buf.append("\u1e43"); break; // m underdot
                   2596:         case 8706: buf.append("\u1e0d"); break; // d underdot
                   2597:         case 8721: buf.append("\u1e63"); break; // s underdot
                   2598:         case 960: buf.append("\u017a"); break; // z acute
                   2599:         case 8747: buf.append("\u1e45"); break; // n overdot
                   2600:         case 937: buf.append("\u0169"); break; // u tilde
                   2601:         case 230: buf.append("\u00e6"); break; // ae
                   2602:         case 248: buf.append("\u00f8"); break; // oslash
                   2603:         case 191: buf.append("\u0304\u0306"); break; // macron breve
                   2604:         case 172: buf.append("\u1e37"); break; // 
                   2605:         case 8730: buf.append("j\u0305"); break; // j macron [does a single char exist?]
                   2606:         case 402: buf.append("\u0103"); break; // a breve
                   2607:         case 8776: buf.append("\u016d"); break; // u breve
                   2608:         case 187: buf.append("\u1e42"); break; // M underdot
                   2609:         case 8230: buf.append("\u2026"); break; // ellipsis
                   2610:         case 192: buf.append("\u00c0"); break; // A grave
                   2611:         case 195: buf.append("\u00c3"); break; // A tilde
                   2612:         case 213: buf.append("\u00d5"); break; // O tilde
                   2613:         case 338: buf.append("m\u0306"); break; // m breve
                   2614:         case 339: buf.append("\u0153"); break; // oe
                   2615:         case 8211: buf.append("\u2013"); break; // endash
                   2616:         case 8212: buf.append("\u2014"); break; // emdash
                   2617:         case 8220: buf.append("\u201c"); break; // quotedblleft
                   2618:         case 8221: buf.append("\u201d"); break; // quotedblright
                   2619:         case 8216: buf.append("\u2018"); break; // quoteleft
                   2620:         case 8217: buf.append("\u2019"); break; // quoteright
                   2621:         case 247: buf.append("\u1e37"); break; // l underring [actually underdot]
                   2622:         case 9674: buf.append("\u1e41"); break; // m overdot
                   2623:         case 255: buf.append("n\u0306"); break; // n breve
                   2624:         case 376: buf.append("\u00d7"); break; // multiply
                   2625:         case 8364: buf.append("\u1e5b"); break; // r underring [actually underdot]
                   2626:         case 8249: buf.append("\u1e44"); break; // N overdot
                   2627:         case 8250: buf.append("\u1e62"); break; // S underdot
                   2628:         case 64257: buf.append("\u1e24"); break; // H underdot
                   2629:         case 64258: buf.append("\u1e0c"); break; // D underdot
                   2630:         case 8225: buf.append("\u2021"); break; // daggerdbl
                   2631:         case 8218: buf.append("\u1e36"); break; // L underdot
                   2632:         case 8222: buf.append("\u0113"); break; // e macron
                   2633:         case 194: buf.append("\u1e5f"); break; // r underbar
                   2634:         case 202: buf.append("r\u0324"); break; // r underdieresis
                   2635:         case 193: buf.append("\u012a"); break; // I macron
                   2636:         case 203: buf.append("\u016b"); break; // u macron
                   2637:         case 200: buf.append("\u1e6c"); break; // T underdot
                   2638:         case 205: buf.append("\u1e64"); break; // S acute
                   2639:         case 206: buf.append("\u2020"); break; // dagger
                   2640:         case 207: buf.append("\u0115"); break; // e breve
                   2641:         case 204: buf.append("\u014f"); break; // o breve
                   2642:         case 211: buf.append("\u0100"); break; // A macron
                   2643:         case 212: buf.append("\u1e46"); break; // N underdot
                   2644:         case 210: buf.append("\u1e3b"); break; // l underbar
                   2645:         case 218: buf.append("\u016a"); break; // U macron
                   2646:         case 219: buf.append("\u0179"); break; // Z acute
                   2647:         case 217: buf.append("\u1e5a"); break; // R underdot
                   2648:         case 305: buf.append("\u0131"); break; // dotlessi
                   2649:         case 710: buf.append("\u1e47"); break; // n underdot
                   2650:         case 732: buf.append("\u1e49"); break; // n underbar
                   2651:         case 175: buf.append("\u0304"); break; // macron
                   2652:         case 728: buf.append("\u0306"); break; // breve
                   2653:         case 729: buf.append("\u1e25"); break; // h underdot
                   2654:         case 730: buf.append("\u012d"); break; // i breve
                   2655:         case 184: buf.append("\u0327"); break; // cedilla
                   2656:         case 733: buf.append("\u030b"); break; // hungarumlaut
                   2657:         case 731: buf.append("\u0328"); break; // ogonek
                   2658:         case 711: buf.append("\u030c"); break; // caron
                   2659:         case 199: buf.append("\u012b\u0303"); break; // imacron tilde
                   2660:         case 8226: buf.append("\u1e5d"); break; // runderdot macron
                   2661:         case 8482: buf.append("\u016b\0306"); break; // umacron breve
                   2662:         case 8804: buf.append("\u0101\u0301"); break; // amacron acute
                   2663:         case 8805: buf.append("\u016b\u0301"); break; // umacron acute
                   2664:         case 8719: buf.append("\u0113\u0301"); break; // emacron acute
                   2665:         case 170: buf.append("\u0113\u0300"); break; // emacron breve
                   2666:         case 186: buf.append("\u014d\u0300"); break; // omacron breve
                   2667:         case 161: buf.append("\u0101\u0306"); break; // amacron breve
                   2668:         case 8710: buf.append("\u0101\u0303"); break; // amacron tilde
                   2669:         case 171: buf.append("\u012b\u0301"); break; // imacron acute
                   2670:         case 8260: buf.append("\u1e00"); break; // runderdotmacron acute
                   2671:         case 183: buf.append("\u1e5b\u0301"); break; // runderdot acute
                   2672:         case 8240: buf.append("\u012b\u0306"); break; // imacron breve
                   2673:         case 63743: buf.append("\u016b\u0303"); break; // umacron tilde
                   2674:         default: buf.append(c); break;
                   2675:       }
                   2676:     }
                   2677:     return buf.toString();
                   2678:   }
                   2679: 
                   2680: 
1.55      rogo     2681: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>