File:  [Repository] / FM2SQL / src / Convert.java
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Fri Mar 18 13:31:09 2005 UTC (19 years, 3 months ago) by rogo
Branches: MAIN
CVS tags: HEAD
pre/postSQlCommands implemented

    1: /*
    2:  * Convert.java -- Converter class - Filemaker to SQL Converter Copyright (C)
    3:  * 2003 Robert Gordesch (rogo@mpiwg-berlin.mpg.de) This program is free
    4:  * software; you can redistribute it and/or modify it under the terms of the GNU
    5:  * General Public License as published by the Free Software Foundation; either
    6:  * version 2 of the License, or (at your option) any later version. Please read
    7:  * license.txt for the full details. A copy of the GPL may be found at
    8:  * http://www.gnu.org/copyleft/lgpl.html You should have received a copy of the
    9:  * GNU General Public License along with this program; if not, write to the Free
   10:  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   11:  * USA Created on 15.09.2003 by rogo
   12:  */
   13: 
   14: import java.awt.Cursor;
   15: import java.io.BufferedReader;
   16: import java.io.BufferedWriter;
   17: import java.io.File;
   18: import java.io.FileInputStream;
   19: import java.io.FileNotFoundException;
   20: import java.io.FileOutputStream;
   21: import java.io.IOException;
   22: import java.io.InputStream;
   23: import java.io.InputStreamReader;
   24: import java.io.OutputStreamWriter;
   25: import java.io.PrintStream;
   26: import java.net.URL;
   27: import java.sql.PreparedStatement;
   28: import java.sql.ResultSet;
   29: import java.sql.SQLException;
   30: import java.sql.Statement;
   31: import java.sql.Types;
   32: import java.text.ParseException;
   33: import java.util.ArrayList;
   34: import java.util.Hashtable;
   35: import java.util.Iterator;
   36: import java.util.List;
   37: import java.util.StringTokenizer;
   38: import java.util.TreeSet;
   39: import java.util.Vector;
   40: 
   41: import javax.swing.JDialog;
   42: import javax.swing.JLabel;
   43: import javax.swing.JPanel;
   44: 
   45: import com.exploringxml.xml.Node;
   46: import com.exploringxml.xml.Xparse;
   47: 
   48: class Convert
   49: {
   50:   /**
   51:    * Helper class for index creation
   52:    * 
   53:    * @author rogo
   54:    * 
   55:    */
   56:   public static class IndexList extends Vector
   57:   {
   58:     public String toString()
   59:     {
   60:       StringBuffer buff = new StringBuffer(1000);
   61:       int count = 0;
   62:       for (Iterator iter = this.iterator(); iter.hasNext();)
   63:       {
   64:         String element = (String) iter.next();
   65:         if (count < elementCount - 1)
   66:         {
   67:           buff.append(element).append(", ");
   68:           count++;
   69:         } else
   70:           buff.append(element);
   71: 
   72:       }
   73: 
   74:       return buff.toString();
   75:     }
   76:   }
   77: 
   78:   /**
   79:    * Helper class for pre/or post execution of SQLCommands
   80:    * @author rogo
   81:    *
   82:    */
   83:   public static class SQLCommand
   84:   {
   85:     private DBBean bean;
   86:     private String command;
   87: 
   88:     public SQLCommand(DBBean bean, String command)
   89:     {
   90:       this.bean = bean;
   91:       this.command = command;
   92:     }
   93: 
   94:     public void executeCommand() throws SQLException, Exception
   95:     {
   96:       java.sql.Connection con = bean.getConnection();
   97:       Statement stm = con.createStatement();
   98:       stm.execute(command);
   99:     }
  100:   }
  101: 
  102:   static DBBean bean = new DBBean();
  103: 
  104:   static DBBean beanDest = new DBBean();
  105: 
  106:   static String user = "", passwd = "e1nste1n";
  107: 
  108:   static String userDest = "postgres", passwdDest = "rogo";
  109: 
  110:   static boolean batchRun = false;
  111: 
  112:   static Vector databases = new Vector();
  113: 
  114:   final static int numHits = 5000;
  115: 
  116:   final static int numIntervalls = 4;
  117: 
  118:   static boolean debug = true;
  119: 
  120:   static boolean isGUI = true;
  121: 
  122:   /**
  123:    * Vector for all SQLCommands to executed before any conversion action is performed
  124:    */
  125:   static Vector preSQLCommands = new Vector();
  126:   /**
  127:    * Vector for all SQLCommands to executed after any conversion action has been performed
  128:    */
  129: 
  130:   static Vector postSQLCommands = new Vector();
  131: 
  132:   public static void main(String args[]) throws IOException
  133:   {
  134:     /*
  135:      * try { //byte[] b = "�".getBytes("UTF-8"); //
  136:      * System.out.println("QueryString " +b[0]+" "+b[1]+(new
  137:      * String(b).getBytes()[0])+" "+new String(b).getBytes()[1]);
  138:      * //System.out.println(new String(b,"UTF-8")); } catch
  139:      * (UnsupportedEncodingException e) { e.printStackTrace(); }
  140:      */
  141:     isGUI = false;
  142:     FileOutputStream file = null;
  143:     if (args.length != 1)
  144:     {
  145:       System.out.println("Usage: java Convert <xml config file>");
  146:       System.exit(-1);
  147:     }
  148:     try
  149:     {
  150:       File temp = File.createTempFile("fm2sql", ".txt");
  151:       file = new FileOutputStream(temp);
  152:     } catch (FileNotFoundException e1)
  153:     {
  154:       e1.printStackTrace();
  155:     }
  156:     PrintStream stream = new PrintStream(file, true);
  157:     if (!debug)
  158:     {
  159:       System.setOut(stream);
  160:       System.setErr(stream);
  161:     }
  162:     StringBuffer sb = readXMLFile(args[0]);
  163:     parseXMLConfig(sb);
  164:     if (!(new File(args[0]).exists()))
  165:       System.exit(0);
  166: 
  167:     System.out.println("Finished!");
  168:     // convert("jdbc:fmpro:http://141.14.237.74:8050","jdbc:postgresql://erebos/test",null,null);
  169:   }
  170: 
  171:   public static void convertBatch(DBBean source, DBBean destination, Vector names, Vector layouts,
  172:       Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception
  173:   {
  174:     bean = source;
  175:     beanDest = destination;
  176:     convert(null, null, names, layouts, selects, creates, ids, mode, delimiter);
  177:   }
  178: 
  179:   public static String formatFileMakerArray(List list, String delimiter)
  180:   {
  181:     StringBuffer formattedString = new StringBuffer();
  182:     for (int i = 0; i < list.size(); ++i)
  183:     {
  184:       formattedString.append(list.get(i).toString());
  185:       if (i < list.size() - 1)
  186:         formattedString.append(delimiter);
  187:     }
  188:     return formattedString.toString();
  189:   }
  190: 
  191:   /**
  192:    * Method for SQL UPDATE
  193:    * 
  194:    * @param source
  195:    * @param destination
  196:    * @param names
  197:    * @param layouts
  198:    * @param selects
  199:    * @param creates
  200:    * @param ids
  201:    * @param mode
  202:    * @throws Exception
  203:    */
  204:   public static void update(String source, String destination, Vector names, Vector layouts,
  205:       Vector selects, Vector creates, Vector ids, int mode) throws Exception
  206:   {
  207: 
  208:     FM2SQL.ProgressDialog dialog = null;
  209:     if (isGUI)
  210:     {
  211:       dialog = initDialog();
  212:     }
  213:     // setting user and passwd
  214:     bean.setUserAndPasswd(user, passwd);
  215:     // setting user and passwd
  216:     beanDest.setUserAndPasswd(userDest, passwdDest);
  217: 
  218:     StringBuffer command = null;
  219:     String query = null;
  220:     try
  221:     {
  222: 
  223:       bean.setConnection(source);
  224: 
  225:       if (names == null)
  226:         names = bean.getTableNames();
  227:       // Collections.sort(names);
  228:       int tbIndex = 1;
  229: 
  230:       System.out.println("Start at table " + names.firstElement());
  231:       for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
  232:       {
  233:         Vector[] result = null;
  234:         String destTableName = "";
  235:         try
  236:         {
  237:           query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
  238:           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
  239:           query = (selects != null) ? selects.get(tbIndex).toString() : query;
  240:           // if vectors[1].get(i) != null)
  241:           if (layout != "")
  242:           {
  243:             query = addLayoutToQuery(names, query, tbIndex, layout);
  244: 
  245:           }
  246:           if (dialog != null)
  247:           {
  248:             prepareDialogforUse(names, dialog, tbIndex);
  249:           }
  250:           bean.getConnection();
  251:           bean.makeQuery(query, 0);
  252: 
  253:         } catch (Exception e)
  254:         {
  255:           System.out.println("Warning exception occured \n " + e);
  256: 
  257:           continue;
  258:         }
  259:         // determine destTableName from createStatement or from source
  260:         // table name
  261:         if (!creates.get(tbIndex).equals(""))
  262:         {
  263:           String create = creates.get(tbIndex).toString().toLowerCase();
  264:           int fromIndex = create.indexOf("table") + 5;
  265:           int toIndex = create.indexOf("(");
  266:           destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "")
  267:               .trim();
  268:           System.out.println("destTable " + destTableName);
  269: 
  270:         } else
  271:           destTableName = convertText(names.get(tbIndex).toString());
  272: 
  273:         beanDest.setConnection(destination);
  274: 
  275:         Statement stm = beanDest.getConnection().createStatement();
  276: 
  277:         Vector tables = beanDest.getTableNames();
  278: 
  279:         System.out.println(names.get(tbIndex) + " "
  280:             + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
  281:         tables = beanDest.getTableNames();
  282:         stm = beanDest.getConnection().createStatement();
  283: 
  284:         if (dialog != null)
  285:           dialog.title.setText("Updating table data ...");
  286:         else
  287:           System.out.println("Updating table data ...");
  288:         int j = -1;
  289: 
  290:         Vector row = null;
  291:         command = new StringBuffer();
  292: 
  293:         command.append("UPDATE ");
  294:         command.append(beanDest.getQC());
  295:         command.append(destTableName);
  296:         // command.append(convertText((String) names.get(tbIndex)));
  297:         command.append(beanDest.getQC());
  298:         command.append(" SET  ");
  299: 
  300:         int size = bean.getColumnNames().size();
  301:         for (int i = 0; i < size - 1; ++i)
  302:           command.append(beanDest.getQC() + convertText((String) bean.getColumnNames().get(i))
  303:               + beanDest.getQC() + " = ? ,");
  304:         command.append(convertText((String) bean.getColumnNames().get(size - 1)) + " = ? ");
  305:         command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " =  ?");
  306:         PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
  307:         System.out.println(command + " " + tbIndex);
  308:         int rowCount = bean.getRowCount(query);
  309:         int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex));
  310:         while ((row = bean.getNextRow()) != null)
  311:         {
  312:           j++;
  313:           // print rows
  314:           Object obj = null;
  315:           for (int k = 0; k < row.size(); ++k)
  316:           {
  317:             obj = row.get(k);
  318:             if (obj instanceof ArrayList)
  319:               obj = ((List) obj).get(0);
  320:             String str = (obj == null) ? "NULL" : obj.toString();
  321:             if (!str.equals("NULL"))
  322:               pstm.setString(k + 1, str);
  323:             else
  324:               pstm.setNull(k + 1, Types.NULL);
  325:           }
  326:           pstm.setString(row.size() + 1, row.get(idIndex).toString());
  327:           pstm.execute();
  328:           if (dialog != null)
  329:             dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
  330:           command = null;
  331:         } // to for loop
  332: 
  333:       }
  334:     } catch (Exception e)
  335:     {
  336:       System.out.println("Error while connecting to database " + e);
  337:       if (isGUI)
  338:       {
  339:         showExceptionDialog(dialog, command, e);
  340:       } else
  341:       {
  342:         e.printStackTrace();
  343: 
  344:       }
  345:     } finally
  346:     {
  347:       if (isGUI)
  348:       {
  349:         resetGUI(dialog);
  350:       }
  351:     }
  352:   }
  353: 
  354:   /**
  355:    * @param dialog
  356:    */
  357:   private static void resetGUI(FM2SQL.ProgressDialog dialog)
  358:   {
  359:     dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  360:     FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  361:     dialog.setVisible(false);
  362:   }
  363: 
  364:   /**
  365:    * @param dialog
  366:    * @param command
  367:    * @param e
  368:    */
  369:   private static void showExceptionDialog(FM2SQL.ProgressDialog dialog, StringBuffer command,
  370:       Exception e)
  371:   {
  372:     dialog.setVisible(false);
  373:     dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  374:     FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  375:     java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
  376:     java.io.PrintStream stream = new java.io.PrintStream(b);
  377:     stream.print(command + "\n\n");
  378:     e.printStackTrace(stream);
  379:     FM2SQL.showErrorDialog(b.toString(), "Error occured !");
  380:   }
  381: 
  382:   /**
  383:    * @return
  384:    */
  385:   private static FM2SQL.ProgressDialog initDialog()
  386:   {
  387:     FM2SQL.ProgressDialog dialog;
  388:     dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance, bean);
  389:     dialog.setTitle("Conversion running ...");
  390:     dialog.title.setText("Getting table data ...");
  391:     dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x
  392:         + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y
  393:         + (FM2SQL.fmInstance.getHeight() - 250) / 2);
  394:     dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  395:     FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  396:     dialog.thread = Thread.currentThread();
  397:     dialog.setSize(420, 250);
  398:     return dialog;
  399:   }
  400: 
  401:   /**
  402:    * transfers the specified array of tables to the destination database and
  403:    * creates the table if it does not exist if it exists and mode is not append
  404:    * the table is dropped
  405:    * 
  406:    * @param source
  407:    * @param destination
  408:    * @param names
  409:    * @param layouts
  410:    * @param selects
  411:    * @param creates
  412:    * @param ids
  413:    * @param mode
  414:    * @throws Exception
  415:    */
  416: 
  417:   public static void convert(String source, String destination, Vector names, Vector layouts,
  418:       Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception
  419:   {
  420: 
  421:     FM2SQL.ProgressDialog dialog = null;
  422: 
  423:     if (isGUI)
  424:     {
  425:       dialog = initDialog();
  426:     }
  427: 
  428:     System.out.println("connection established " + source + " " + bean.url);
  429: 
  430:     java.util.TreeSet myIds = new TreeSet();
  431:     int deltaID = 1;
  432:     String idField = "";
  433:     String destTableName = "";
  434:     String[] fieldNames = null;
  435: 
  436:     if (source != null && destination != null)
  437:     {
  438:       // setting user and passwd
  439:       bean.setUserAndPasswd(user, passwd);
  440:       // setting user and passwd
  441:       beanDest.setUserAndPasswd(userDest, passwdDest);
  442:     }
  443: 
  444:     StringBuffer command = null;
  445:     String query = null;
  446:     try
  447:     {
  448: 
  449:       if (source != null)
  450:         bean.setConnection(source);
  451:       else
  452:         bean.setConnection(bean.url);
  453:       if (names == null)
  454:         names = bean.getTableNames();
  455:       int tbIndex = 1;
  456: 
  457:       for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
  458:       {
  459:         Vector[] result = null;
  460:         try
  461:         {
  462:           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
  463:           query = (selects != null) ? selects.get(tbIndex).toString() : "select * from "
  464:               + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
  465:           if (layout != "")
  466:           {
  467:             query = addLayoutToQuery(names, query, tbIndex, layout);
  468: 
  469:           }
  470:           if (dialog != null)
  471:           {
  472:             prepareDialogforUse(names, dialog, tbIndex);
  473:           }
  474: 
  475:           bean.getConnection();
  476:           bean.makeQuery(query, 50);
  477:           idField = ids.get(tbIndex).toString();
  478: 
  479:         } catch (Exception e)
  480:         {
  481:           System.out.println("Warning exception occured \n " + e);
  482: 
  483:           continue;
  484:         }
  485:         if (destination != null)
  486:           beanDest.setConnection(destination);
  487:         else
  488:           beanDest.setConnection(beanDest.url);
  489:         Statement stm = beanDest.getConnection().createStatement();
  490: 
  491:         Vector tables = beanDest.getTableNames();
  492:         // Collections.sort(tables);
  493:         System.out.println(names.get(tbIndex) + " "
  494:             + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
  495:         tables = beanDest.getTableNames();
  496:         // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
  497:         stm = beanDest.getConnection().createStatement();
  498:         // System.exit(0);
  499: 
  500:         // determine destTableName from createStatement or from source
  501:         // table name
  502:         ConversionProperties prop = getFieldNamesAndDestTableName(creates.get(tbIndex).toString(),
  503:             query, names.get(tbIndex).toString());
  504:         destTableName = prop.destTableName;
  505:         fieldNames = prop.fieldNames;
  506:         if (mode == Convert.DataBase.CONVERT_MODE)
  507:         {
  508: 
  509:           if (tables.indexOf(destTableName) >= 0)
  510:           {
  511:             stm.executeUpdate("drop table " + beanDest.getQC() + destTableName + beanDest.getQC());
  512:             tables.remove(destTableName);
  513:             System.out.println("dropped table" + destTableName);
  514: 
  515:           }
  516:           if ((tables.indexOf(destTableName) < 0))
  517:           {
  518: 
  519:             if (creates.get(tbIndex).equals("")
  520:                 || creates.get(tbIndex).toString().toLowerCase().indexOf("create") < 0)
  521:             {
  522:               System.out
  523:                   .println("Warning empty or invalid create statement - creating one for you\n");
  524: 
  525:               command = new StringBuffer(50);
  526:               command.append("CREATE TABLE ");
  527:               command.append(beanDest.getQC());
  528:               command.append(convertText((String) names.get(tbIndex)));
  529:               command.append(beanDest.getQC());
  530:               command.append("(");
  531:               String type = null;
  532:               Vector columnNames = bean.getColumnNames();
  533:               for (int i = 0; i < columnNames.size() - 1; ++i)
  534:               {
  535:                 type = bean.metaData.getColumnTypeName(i + 1);
  536:                 // System.out.println(i+" "+result[1].get(i)+"
  537:                 // "+type);
  538:                 type = (type.equals("NUMBER")) ? "INT4" : type;
  539:                 type = (type.equals("CONTAINER")) ? "TEXT" : type;
  540: 
  541:                 command.append(beanDest.getQC() + convertText((String) columnNames.get(i))
  542:                     + beanDest.getQC() + " " + type + ", ");
  543:               }
  544:               type = bean.metaData.getColumnTypeName(columnNames.size());
  545:               type = (type.equals("NUMBER")) ? "INT4" : type;
  546:               type = (type.equals("CONTAINER")) ? "TEXT" : type;
  547:               command.append(beanDest.getQC()
  548:                   + convertText((String) columnNames.get(columnNames.size() - 1))
  549:                   + beanDest.getQC() + " " + type);
  550:               command.append(" )");
  551: 
  552:             } else
  553:               command = new StringBuffer().append(creates.get(tbIndex).toString().toLowerCase());
  554:             stm.executeUpdate(command.toString());
  555: 
  556:           }
  557:         }
  558:         if (dialog != null)
  559:           dialog.title.setText("Writing table data ...");
  560: 
  561:         // prepare the insert statement
  562:         int j = -1;
  563:         Vector row = null;
  564:         command = new StringBuffer();
  565: 
  566:         command.append("INSERT  INTO ");
  567:         command.append(beanDest.getQC());
  568:         command.append(destTableName);
  569:         command.append(beanDest.getQC());
  570:         command.append(" (");
  571:         for (int i = 0; i < fieldNames.length; i++)
  572:         {
  573:           command.append(fieldNames[i]);
  574:           if (i < fieldNames.length - 1)
  575:             command.append(",");
  576:         }
  577:         command.append(") ");
  578: 
  579:         command.append(" values ( ");
  580: 
  581:         // add a question marks for every field
  582:         for (int i = 0; i < bean.getColumnNames().size() - 1; ++i)
  583:           command.append("?,");
  584:         command.append("?)");
  585:         PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
  586:         System.out.println(command);
  587:         int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
  588:         Vector vec = new Vector(myIds);
  589:         int endIndex = -1;
  590:         String tempQuery = query;
  591:         String tempID = bean.getQC() + idField + bean.getQC();
  592:         // if id_field not do incremental conversion else do it all at
  593:         // once
  594:         if (!idField.equals(""))
  595:         {
  596:           long startTime = System.currentTimeMillis();
  597:           int counter = -1;
  598:           while (true)
  599:           {
  600:             ++counter;
  601:             if (counter == 0 && dialog != null)
  602:               dialog.title.setText("Check if data  is available");
  603:             else if (dialog != null)
  604:               dialog.title.setText("Check if more  data  is available");
  605:             myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex),
  606:                 tempQuery, numHits);
  607:             if (myIds.isEmpty())
  608:               break;
  609:             vec = new Vector(myIds);
  610:             rowCount = vec.size();
  611:             System.out.println("ID LIST SIZE "
  612:                 + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIds.size());
  613:             deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
  614:             if (vec.size() <= numIntervalls)
  615:             {
  616:               endIndex = 0;
  617:               deltaID = vec.size();
  618:             }
  619:             for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)
  620:             {
  621:               System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());
  622:               if (query.indexOf("where") > 0)
  623:                 tempQuery = query + " and " + tempID + ">='" + vec.get(k) + "' and " + tempID
  624:                     + "<='" + vec.get(k + deltaID) + "'";
  625:               else
  626:                 tempQuery = query + " where " + tempID + ">='" + vec.get(k) + "' and " + tempID
  627:                     + "<='" + vec.get(k + deltaID) + "'";
  628:               System.out.println(tempQuery);
  629:               if (dialog != null)
  630:                 dialog.title.setText("Reading table data ...");
  631: 
  632:               bean.makeQuery(tempQuery, deltaID);
  633:               if (dialog != null)
  634:                 dialog.title.setText("Writing table data ...");
  635: 
  636:               command = writeDatainDestTable(dialog, command, k, pstm, rowCount, delimiter);
  637:               endIndex = k + deltaID;
  638:             }
  639:             System.out.println(endIndex);
  640:             // all data written ? if not write last chunk of data
  641:             if (endIndex == vec.size() - 1)
  642:               System.out.println("fits");
  643:             else
  644:             {
  645:               System.out.println(" last intervall from " + vec.get(endIndex) + " "
  646:                   + vec.lastElement());
  647: 
  648:               if (query.indexOf("where") > 0)
  649:                 tempQuery = query + " and " + tempID + ">='" + vec.get(endIndex) + "' and "
  650:                     + tempID + "<='" + vec.lastElement() + "'";
  651:               else
  652:                 tempQuery = query + " where " + tempID + ">='" + vec.get(endIndex) + "' and "
  653:                     + tempID + "<='" + vec.lastElement() + "'";
  654:               System.out.println(tempQuery);
  655:               if (dialog != null)
  656:                 dialog.title.setText("Reading table data ...");
  657:               bean.makeQuery(tempQuery, 0);
  658:               if (dialog != null)
  659:                 dialog.title.setText("Writing table data ...");
  660:               command = writeDatainDestTable(dialog, command, endIndex, pstm, rowCount, delimiter);
  661:             }
  662:             // prepare new query for next chunk
  663:             if (query.indexOf("where") > 0)
  664:               tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
  665:             else
  666:               tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
  667: 
  668:           }
  669:           long endTime = System.currentTimeMillis();
  670:           System.out.println("Time for incremental convert elapsed " + (endTime - startTime));
  671:         } else
  672:         {
  673:           // read and write all in one big chunk
  674:           long startTime = System.currentTimeMillis();
  675: 
  676:           bean.makeQuery(query, 0);
  677:           command = writeDatainDestTable(dialog, command, j, pstm, rowCount, delimiter);
  678:           long endTime = System.currentTimeMillis();
  679:           System.out.println("Time for old convert elapsed " + (endTime - startTime));
  680: 
  681:         }
  682:         if (isGUI)
  683:           resetGUI(dialog);
  684:       }
  685:     } catch (Exception e)
  686:     {
  687:       System.out.println("Error while connecting to database " + e);
  688:       if (isGUI)
  689:       {
  690:         showExceptionDialog(dialog, command, e);
  691:         resetGUI(dialog);
  692:       } else
  693:       {
  694:         e.printStackTrace();
  695: 
  696:       }
  697:     }
  698: 
  699:   }
  700: 
  701:   /**
  702:    * @param names
  703:    * @param dialog
  704:    * @param tbIndex
  705:    */
  706:   private static void prepareDialogforUse(Vector names, FM2SQL.ProgressDialog dialog, int tbIndex)
  707:   {
  708:     dialog.title.setText("Reading table data ...");
  709:     dialog.table.setText(names.get(tbIndex).toString());
  710:     dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
  711:     dialog.setVisible(true);
  712:   }
  713: 
  714:   /**
  715:    * @param names
  716:    * @param query
  717:    * @param tbIndex
  718:    * @param layout
  719:    * @return
  720:    */
  721:   private static String addLayoutToQuery(Vector names, String query, int tbIndex, String layout)
  722:   {
  723:     layout = " layout " + bean.getQC() + layout + bean.getQC();
  724:     String name = names.get(tbIndex).toString();
  725:     StringBuffer queryLayout = new StringBuffer(query);
  726:     queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
  727:     query = queryLayout.toString();
  728:     System.out.println("added layout  " + query);
  729:     return query;
  730:   }
  731: 
  732:   /**
  733:    * Writes data to the destination table
  734:    * 
  735:    * @param dialog
  736:    *          progress dialog
  737:    * @param command
  738:    * @param j
  739:    *          data index for progress bar
  740:    * @param pstm
  741:    *          prepared statement
  742:    * @param rowCount
  743:    *          number of datasets
  744:    * @return command
  745:    * @throws Exception
  746:    * @throws SQLException
  747:    */
  748:   private static StringBuffer writeDatainDestTable(FM2SQL.ProgressDialog dialog,
  749:       StringBuffer command, int j, PreparedStatement pstm, int rowCount, String delimiter)
  750:       throws Exception, SQLException
  751:   {
  752:     Vector row;
  753:     while ((row = bean.getNextRow()) != null)
  754:     {
  755:       j++;
  756:       Object obj = null;
  757:       for (int k = 0; k < row.size(); ++k)
  758:       {
  759:         obj = row.get(k);
  760: 
  761:         if (obj instanceof ArrayList)
  762:           obj = formatFileMakerArray((List) obj, delimiter);
  763: 
  764:         String str = (obj == null) ? "NULL" : obj.toString();
  765:         if (obj instanceof Double)
  766:         {
  767:           pstm.setDouble(k + 1, ((Double) obj).doubleValue());
  768:         } else if (!str.equals("NULL"))
  769:           pstm.setString(k + 1, str);
  770:         else
  771:           pstm.setNull(k + 1, Types.NULL);
  772:       }
  773:       pstm.execute();
  774:       if (isGUI)
  775:         dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
  776:       command = null;
  777:     } // to while loop
  778:     return command;
  779:   }
  780: 
  781:   /**
  782:    * removes special characters from the input string as well as .fp5
  783:    * 
  784:    * @param newName
  785:    *          String to change
  786:    * @return
  787:    */
  788:   public static String convertText(String newName)
  789:   {
  790:     StringBuffer alterMe = new StringBuffer(newName.trim().toLowerCase());
  791:     int length = alterMe.length();
  792:     int j = 0;
  793:     int index = alterMe.indexOf(".fp5");
  794:     if (index >= 0)
  795:     {
  796:       alterMe.delete(index, index + 4);
  797:       length = length - 4;
  798:     }
  799: 
  800:     while (j < length)
  801:     {
  802:       if (alterMe.charAt(j) == ' ')
  803:       {
  804:         alterMe.setCharAt(j, '_');
  805:         // if(j<length-1) j=j+1;
  806:       } else if (alterMe.charAt(j) == '_')
  807:       {
  808: 
  809:         if (alterMe.charAt(j + 1) == '_')
  810:           alterMe.deleteCharAt(j);
  811:         length = length - 1;
  812:         // if(j<length-1) j=j+1;
  813:       } else if (alterMe.charAt(j) == 'ä')
  814:       {
  815:         alterMe.setCharAt(j, 'a');
  816:         alterMe.insert(j + 1, "e");
  817:         length = length + 1;
  818:         if (j < length - 1)
  819:           j = j + 1;
  820:       } else if (alterMe.charAt(j) == 'ö')
  821:       {
  822:         alterMe.setCharAt(j, 'o');
  823:         alterMe.insert(j + 1, "e");
  824:         length = length + 1;
  825:         if (j < length - 1)
  826:           j = j + 1;
  827:       } else if (alterMe.charAt(j) == 'ü')
  828:       {
  829:         alterMe.setCharAt(j, 'u');
  830:         alterMe.insert(j + 1, "e");
  831:         length = length + 1;
  832:         if (j < length - 1)
  833:           j = j + 1;
  834:       } else if (alterMe.charAt(j) == 'ß')
  835:       {
  836:         alterMe.setCharAt(j, 's');
  837:         alterMe.insert(j + 1, "s");
  838:         length = length + 1;
  839:         if (j < length - 1)
  840:           j = j + 1;
  841:       } else if (alterMe.charAt(j) == ':')
  842:       {
  843:         if (j < length - 1)
  844:         {
  845:           if (alterMe.charAt(j + 1) == ':')
  846:           {
  847:             alterMe.setCharAt(j, '_');
  848:             alterMe.delete(j + 1, j + 2);
  849:             length = length - 1;
  850: 
  851:           }
  852: 
  853:           if (j < length - 1)
  854:             j = j + 1;
  855:         }
  856:       } else if (alterMe.charAt(j) == '-')
  857:       {
  858:         alterMe.setCharAt(j, '_');
  859: 
  860:       } else if (alterMe.charAt(j) == '?')
  861:       {
  862:         // changed ? to _ because of update statement
  863:         alterMe.setCharAt(j, '_');
  864:         // length = length + 1;
  865:         // j=j+1;
  866:         System.out.println(alterMe);
  867:       } else if (alterMe.charAt(j) == '.')
  868:       {
  869:         if (j == length - 1)
  870:         {
  871:           alterMe.delete(j, j);
  872:           length--;
  873:         } else
  874:           alterMe.setCharAt(j, '_');
  875:       }
  876: 
  877:       ++j;
  878:     }
  879:     return alterMe.toString();
  880:   }
  881: 
  882:   /**
  883:    * Converts > and < in an entity (&gt; or &lt;)
  884:    * 
  885:    * @param newName
  886:    * @return
  887:    */
  888:   public static String convertToEntities(String newName)
  889:   {
  890:     StringBuffer alterMe = new StringBuffer(newName.trim());
  891:     int length = alterMe.length();
  892:     int j = 0;
  893: 
  894:     while (j < length)
  895:     {
  896: 
  897:       if (alterMe.charAt(j) == '>')
  898:       {
  899:         alterMe.setCharAt(j, '&');
  900:         alterMe.insert(j + 1, "gt;");
  901:         length = length + 2;
  902:         if (j < length - 1)
  903:           j = j + 1;
  904: 
  905:       } else if (alterMe.charAt(j) == '<')
  906:       {
  907:         alterMe.setCharAt(j, '&');
  908:         alterMe.insert(j + 1, "lt;");
  909:         length = length + 2;
  910:         if (j < length - 1)
  911:           j = j + 1;
  912: 
  913:       }
  914:       ++j;
  915:     }
  916:     return alterMe.toString();
  917:   }
  918: 
  919:   /**
  920:    * Masks the single quote character '-->\'
  921:    * 
  922:    * @param newName
  923:    * @return
  924:    */
  925:   public static String convertUml(String newName)
  926:   {
  927:     StringBuffer alterMe = new StringBuffer(newName.trim());
  928:     int length = alterMe.length();
  929:     int j = 0;
  930: 
  931:     while (j < length)
  932:     {
  933: 
  934:       if (alterMe.charAt(j) == '\'')
  935:       {
  936:         alterMe.setCharAt(j, '\\');
  937:         alterMe.insert(j + 1, "'");
  938:         length = length + 1;
  939:         if (j < length - 1)
  940:           j = j + 1;
  941:       }
  942:       /*
  943:        * else if (alterMe.charAt(j) == '"') { alterMe.setCharAt(j, '\\');
  944:        * alterMe.insert(j + 1, "\""); length = length + 1; if(j <length-1)
  945:        * j=j+1; } else if (alterMe.charAt(j) == '>') { alterMe.setCharAt(j,
  946:        * '\\'); alterMe.insert(j + 1, ">"); length = length + 1; if(j <length-1)
  947:        * j=j+1; } else if (alterMe.charAt(j) == ' <') { alterMe.setCharAt(j,
  948:        * '\\'); alterMe.insert(j + 1, " <"); length = length + 1; if(j
  949:        * <length-1) j=j+1; } else if (alterMe.charAt(j) == '?') {
  950:        * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "?"); length = length +
  951:        * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '&') {
  952:        * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "&"); length = length +
  953:        * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '=') {
  954:        * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "="); length = length +
  955:        * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == ',') {
  956:        * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, ","); length = length +
  957:        * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '.') {
  958:        * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "."); length = length +
  959:        * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '[') {
  960:        * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "."); length = length +
  961:        * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == ']') {
  962:        * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "."); length = length +
  963:        * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '%') {
  964:        * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "%"); length = length +
  965:        * 1; if(j <length-1) j=j+1; }
  966:        */
  967:       ++j;
  968:     }
  969:     return alterMe.toString();
  970:   }
  971: 
  972:   /**
  973:    * parses the input xml file for batch conversion called from readXMLFile *
  974:    * 
  975:    * @param sb
  976:    */
  977:   public static void parseXMLConfig(StringBuffer sb)
  978:   {
  979:     // boolean finished = false;
  980:     Vector databases = new Vector();
  981:     try
  982:     {
  983:       databases = getXMLConfig(sb);
  984:       
  985:       DBBean database = ((DataBase) databases.lastElement()).bean;
  986:       databases.remove(databases.size() - 1);
  987:       // databases.add(database);
  988:       for (Iterator iter = databases.iterator(); iter.hasNext();)
  989:       {
  990:         DataBase db = (DataBase) iter.next();
  991:         for (Iterator iterator = db.preCommands.iterator(); iterator.hasNext();)
  992:         {
  993:           SQLCommand sqlCommand = (SQLCommand) iterator.next();
  994:           sqlCommand.executeCommand();
  995:         }
  996:         int mode = db.mode;
  997:         if (mode == DataBase.CONVERT_MODE || mode == DataBase.APPEND_MODE)
  998:           convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids,
  999:               mode, db.delimiter);
 1000:         else if (mode == DataBase.UPDATE_MODE)
 1001:         {
 1002: 
 1003:           Convert.user = db.bean.user;
 1004:           Convert.passwd = db.bean.passwd;
 1005:           userDest = database.user;
 1006:           passwdDest = database.passwd;
 1007: 
 1008:           update(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates, db.ids,
 1009:               mode);
 1010:         } else if (mode == DataBase.SYNCHRONIZE_MODE)
 1011:         {
 1012:           Convert.user = db.bean.user;
 1013:           Convert.passwd = db.bean.passwd;
 1014:           userDest = database.user;
 1015:           passwdDest = database.passwd;
 1016: 
 1017:           synchronize(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates,
 1018:               db.ids, mode, db.delimiter, new Vector(db.htIndex.values()));
 1019:         }
 1020:         for (Iterator iterator = db.postCommands.iterator(); iterator.hasNext();)
 1021:         {
 1022:           SQLCommand sqlCommand = (SQLCommand) iterator.next();
 1023:           sqlCommand.executeCommand();
 1024:         }
 1025: 
 1026:       }
 1027: 
 1028:     } catch (Exception e)
 1029:     {
 1030: 
 1031:       e.printStackTrace();
 1032: 
 1033:     } finally
 1034:     {
 1035:       bean.closeAllConnections();
 1036:       beanDest.closeAllConnections();
 1037:     }
 1038:   }
 1039: 
 1040:   public static Vector getXMLConfig(StringBuffer sb)
 1041:   {
 1042: 
 1043:     boolean finished = false;
 1044:     // parse string and build document tree
 1045:     Xparse parser = new Xparse();
 1046:     parser.changeEntities = true;
 1047:     Node root = parser.parse(sb.toString());
 1048:     // printContents(root);
 1049:     Vector databases = new Vector();
 1050:     Vector tables = new Vector();
 1051:     Vector layouts = new Vector();
 1052:     Vector selects = new Vector();
 1053:     Vector creates = new Vector();
 1054:     Vector ids = new Vector();
 1055:     Vector preSQLCommands = new Vector();
 1056:     Vector postSQLCommands = new Vector();
 1057: 
 1058:     String delimiter = "|";
 1059:     int mode = -1;
 1060:     try
 1061:     {
 1062:       Node tempNode = root.find("convert/source", new int[]
 1063:       { 1, 1 });
 1064:       if (tempNode == null)
 1065:         throw new Error("parse error source tag missing");
 1066:       System.out.println(tempNode.name);
 1067:       int length = countNodes(tempNode);
 1068:       for (int i = 1; i <= length; i++)
 1069:       {
 1070: 
 1071:         DBBean database = new DBBean();
 1072:         tables = new Vector();
 1073:         layouts = new Vector();
 1074:         selects = new Vector();
 1075:         creates = new Vector();
 1076:         ids = new Vector();
 1077:         preSQLCommands = new Vector();
 1078:         postSQLCommands = new Vector();
 1079: 
 1080:         // parse dataBase
 1081:         Node node = root.find("convert/source/database/url", new int[]
 1082:         { 1, 1, i, 1 });
 1083:         Node node1 = root.find("convert/source/database/user", new int[]
 1084:         { 1, 1, i, 1, 1 });
 1085:         Node node2 = root.find("convert/source/database/password", new int[]
 1086:         { 1, 1, i, 1, 1 });
 1087:         Node node3 = root.find("convert/source/database", new int[]
 1088:         { 1, 1, i });
 1089:         Node nodeMode = root.find("convert/source/database/mode", new int[]
 1090:         { 1, 1, i, 1, 1 });
 1091:         Node delimiterNode = root.find("convert/source/database/delimiter", new int[]
 1092:         { 1, 1, i, 1, 1 });
 1093: 
 1094:         Node commandNodes = root.find("convert/source/database/sqlcommands", new int[]
 1095:         { 1, 1, i, 1, 1 });
 1096:         if (commandNodes != null)
 1097:         {
 1098:           parseCommandNode(commandNodes, database, preSQLCommands, postSQLCommands);
 1099:         }
 1100:         Node useNormanToUnicodeMapper = root.find(
 1101:             "convert/source/database/usenormantounicodemapper", new int[]
 1102:             { 1, 1, i, 1, 1 });
 1103: 
 1104:         if (delimiterNode != null)
 1105:           delimiter = delimiterNode.getCharacters();
 1106:         if (useNormanToUnicodeMapper != null)
 1107:         {
 1108:           database.setUseNormanToUnicodeMapper(Boolean.valueOf(
 1109:               useNormanToUnicodeMapper.getCharacters()).booleanValue());
 1110:           System.out.println("useMapper "
 1111:               + Boolean.valueOf(useNormanToUnicodeMapper.getCharacters().trim()).booleanValue());
 1112:         }
 1113: 
 1114:         if (node3 == null)
 1115:           throw new Error("parse error database tag missing");
 1116:         if (node == null)
 1117:           throw new Error("parse error url tag missing");
 1118:         if (node1 == null)
 1119:           throw new Error("parse error user tag missing");
 1120:         if (node2 == null)
 1121:           throw new Error("parse error password tag missing");
 1122:         String url = node.getCharacters();
 1123:         String user = node1.getCharacters();
 1124:         String password = node2.getCharacters();
 1125:         database.setURL(url.trim());
 1126:         database.setUserAndPasswd(user.trim(), password.trim());
 1127:         System.out.println(node.name + " " + node.getCharacters());
 1128:         System.out.println(node1.name + " " + node1.getCharacters());
 1129:         System.out.println(node2.name + " " + node2.getCharacters());
 1130:         String modeString = "";
 1131:         if (nodeMode == null)
 1132:           modeString = "convert";
 1133:         else
 1134:           modeString = nodeMode.getCharacters();
 1135:         if (modeString.equals("convert"))
 1136:           mode = DataBase.CONVERT_MODE;
 1137:         else if (modeString.equals("append"))
 1138:           mode = DataBase.APPEND_MODE;
 1139:         else if (modeString.equals("update"))
 1140:           mode = DataBase.UPDATE_MODE;
 1141:         else if (modeString.equals("delete"))
 1142:           mode = DataBase.DELETE_MODE;
 1143: 
 1144:         else if (modeString.equals("synchronize"))
 1145:           mode = DataBase.SYNCHRONIZE_MODE;
 1146:         // if(node3!=null)
 1147:         // System.out.println(node3.name);
 1148: 
 1149:         int length2 = countNodes(node3);
 1150: 
 1151:         System.out.println("number of tables " + length2);
 1152: 
 1153:         for (int j = 1; j <= length2; ++j)
 1154:         {
 1155:           Node node4 = root.find("convert/source/database/table", new int[]
 1156:           { 1, 1, i, j });
 1157:           Node node5 = root.find("convert/source/database/table/select", new int[]
 1158:           { 1, 1, i, j, 1 });
 1159:           Node node6 = root.find("convert/source/database/table/create", new int[]
 1160:           { 1, 1, i, j, 1 });
 1161: 
 1162:           if (node4 != null)
 1163:             System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
 1164:           if (node5 != null)
 1165:             System.out.println(node5.name + " " + node5.getCharacters());
 1166:           if (node6 != null)
 1167:             System.out.println(node6.name + " " + node6.getCharacters());
 1168:           if (node4 == null)
 1169:             throw new Error("parse error table tag missing");
 1170:           // if(node5==null) throw new Error("parse error select tag
 1171:           // missing");
 1172:           // if(node6==null) throw new Error("parse error create tag
 1173:           // missing");
 1174:           String name = (String) node4.attributes.get("name");
 1175:           String layout = (String) node4.attributes.get("layout");
 1176:           String id = (String) node4.attributes.get("id");
 1177:           System.out.println("id was " + id);
 1178: 
 1179:           if (name == null)
 1180:             throw new Error("parse error required table tag attribute name missing");
 1181:           if (layout == null)
 1182:             layout = "";
 1183:           if (id == null)
 1184:             id = "";
 1185:           if (name.equals(""))
 1186:             throw new Error("parse error table tag attribute must not be empty");
 1187:           tables.add(name);
 1188:           layouts.add(layout);
 1189:           ids.add(id);
 1190:           String query = (node5 == null) ? "" : node5.getCharacters();
 1191:           if (query.equals(""))
 1192:             System.err.println("Warning empty select tag or  select tag missing !!");
 1193:           query = (query.equals("")) ? "select * from " + database.getQC() + name
 1194:               + database.getQC() : query;
 1195:           selects.add(query);
 1196:           if (node6 != null)
 1197:             creates.add(node6.getCharacters().trim());
 1198:           else
 1199:             creates.add("");
 1200: 
 1201:         }
 1202:         DataBase dataBase = new DataBase(database, tables, layouts, selects, creates, ids, mode);
 1203:         dataBase.delimiter = delimiter;
 1204:         dataBase.preCommands = new Vector(preSQLCommands);
 1205:         dataBase.postCommands = new Vector(postSQLCommands);
 1206:         databases.add(dataBase);
 1207:       }
 1208:       DBBean database = new DBBean();
 1209: 
 1210:       preSQLCommands.clear();
 1211:       postSQLCommands.clear();
 1212: 
 1213:       // parse dataBase
 1214:       Node node = root.find("convert/destination/database/url", new int[]
 1215:       { 1, 1, 1, 1 });
 1216:       Node node1 = root.find("convert/destination/database/user", new int[]
 1217:       { 1, 1, 1, 1, 1 });
 1218:       Node node2 = root.find("convert/destination/database/password", new int[]
 1219:       { 1, 1, 1, 1, 1 });
 1220:       Node commandNodes = root.find("convert/destination/database/sqlcommands", new int[]
 1221:       { 1, 1, 1, 1, 1 });
 1222:       if (commandNodes != null)
 1223:       {
 1224:         parseCommandNode(commandNodes, database, preSQLCommands, postSQLCommands);
 1225:       }
 1226: 
 1227:       String url = node.getCharacters();
 1228:       String user = node1.getCharacters();
 1229:       String password = node2.getCharacters();
 1230:       System.out.println(url);
 1231:       database.setURL(url.trim());
 1232:       database.setUserAndPasswd(user.trim(), password.trim());
 1233:       DataBase db = new DataBase(database, new Vector(), null, null, null, null, 0);
 1234:       databases.add(db);
 1235:       db.preCommands = new Vector(preSQLCommands);
 1236:       db.postCommands = new Vector(postSQLCommands);
 1237: 
 1238:     } catch (Exception e)
 1239:     {
 1240:       e.printStackTrace();
 1241:     }
 1242:     return databases;
 1243:   }
 1244: 
 1245:   /**
 1246:    *
 1247:    * @param commandNode
 1248:    * @param database
 1249:    * @param preSQLCommands 
 1250:    * @param postSQLCommand 
 1251:    */
 1252:   private static void parseCommandNode(Node commandNode, DBBean database,
 1253:       java.util.Vector preSQLCommands, java.util.Vector postSQLCommands)
 1254:   {
 1255:     // System.out.println(commandNode.name + " " + countNodes(commandNode));
 1256:     int numCommands = commandNode.contents.length();
 1257:     for (int j = 0; j < numCommands; ++j)
 1258:     {
 1259:       Node node = (Node) commandNode.contents.v.elementAt(j);
 1260:       if (node.type.equals("element"))
 1261:       {
 1262:         // System.out.println(node.name + " " + node.getCharacters() + database);
 1263:         String execute = node.attributes.get("execute").toString();
 1264:         if (execute.equals("before"))
 1265:         {
 1266:           preSQLCommands.add(new SQLCommand(database, node.getCharacters()));
 1267:         }
 1268:         if (execute.equals("after"))
 1269:         {
 1270:           postSQLCommands.add(new SQLCommand(database, node.getCharacters()));
 1271:         }
 1272: 
 1273:       }
 1274: 
 1275:     }
 1276:   }
 1277: 
 1278:   private static int countNodes(Node tempNode)
 1279:   {
 1280:     int length = 0;
 1281:     for (int i = 0; i < tempNode.contents.v.size(); ++i)
 1282:     {
 1283:       Node node = (Node) tempNode.contents.v.elementAt(i);
 1284:       if (node.type.equals("element"))
 1285:       {
 1286:         if (node.name.equals("database"))
 1287:           length++;
 1288:         if (node.name.equals("table"))
 1289:           length++;
 1290:         if (node.name.equals("sqlcommand"))
 1291:           length++;
 1292: 
 1293:       }
 1294: 
 1295:       // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+"
 1296:       // "+i);
 1297:     }
 1298:     return length;
 1299:   }
 1300: 
 1301:   private static void printContents(Node root)
 1302:   {
 1303: 
 1304:     Vector contents = (root.index == null) ? root.contents.v : root.index.v;
 1305:     for (int i = 0; i < contents.size(); ++i)
 1306:     {
 1307:       Node n = (Node) contents.elementAt(i);
 1308:       if (n.type.equals("element"))
 1309:       {
 1310:         System.out.println("tag " + n.name);
 1311:         System.out.println(n.getCharacters());
 1312:         // contents=n.contents.v i=0;
 1313:       }
 1314:       // System.out.println(n.type);
 1315:     }
 1316:   }
 1317: 
 1318:   /**
 1319:    * reads the specified xml file
 1320:    * 
 1321:    * @param xmlFile
 1322:    */
 1323:   public static StringBuffer readXMLFile(String xmlFile)
 1324:   {
 1325:     InputStream stream = null;
 1326:     StringBuffer sb = new StringBuffer();
 1327: 
 1328:     try
 1329:     {
 1330: 
 1331:       if (xmlFile.indexOf("file://") >= 0 || xmlFile.indexOf("http://") >= 0)
 1332:       {
 1333:         URL url = new URL(xmlFile);
 1334:         stream = url.openStream();
 1335:       } else
 1336:         // read XML Metadata from a file
 1337:         stream = new FileInputStream(xmlFile);
 1338:       InputStreamReader isr = new InputStreamReader(stream, "UTF-8");
 1339:       BufferedReader buffr = new BufferedReader(isr);
 1340:       int c = 0;
 1341:       while ((c = buffr.read()) != -1)
 1342:       {
 1343:         char ch = (char) c;
 1344:         sb.append(ch);
 1345:         // System.out.print((char)c);
 1346:       }
 1347:     } catch (Exception e)
 1348:     {
 1349:       e.printStackTrace();
 1350:     } finally
 1351:     {
 1352: 
 1353:       try
 1354:       {
 1355:         stream.close();
 1356:       } catch (IOException e1)
 1357:       {
 1358:         // TODO Auto-generated catch block
 1359:         e1.printStackTrace();
 1360:       }
 1361:     }
 1362:     return sb;
 1363:   }
 1364: 
 1365:   /**
 1366:    * Helper class for Conversion etc. Contains data needed for the conversion
 1367:    * 
 1368:    * @author rogo
 1369:    * 
 1370:    */
 1371: 
 1372:   public static class ConversionProperties
 1373:   {
 1374:     String destTableName;
 1375: 
 1376:     String[] fieldNames;
 1377: 
 1378:     public ConversionProperties()
 1379:     {
 1380:     }
 1381: 
 1382:     public ConversionProperties(String destTableName, String[] fieldNames)
 1383:     {
 1384:       this.destTableName = destTableName;
 1385:       this.fieldNames = fieldNames;
 1386: 
 1387:     }
 1388: 
 1389:   }
 1390: 
 1391:   /**
 1392:    * Helper class for XML-File parsing Holds the parsed data
 1393:    * 
 1394:    * @author rogo
 1395:    * 
 1396:    */
 1397:   public static class DataBase
 1398:   {
 1399:     DBBean bean;
 1400: 
 1401:     Vector creates;
 1402: 
 1403:     Vector selects;
 1404: 
 1405:     Vector layouts;
 1406: 
 1407:     Vector tables=new Vector();
 1408: 
 1409:     Vector ids;
 1410: 
 1411:     String delimiter = "//";
 1412: 
 1413:     Vector preCommands;
 1414:     Vector postCommands;
 1415: 
 1416:     /**
 1417:      * maps table name to index fields
 1418:      */
 1419:     Hashtable htIndex = new Hashtable();
 1420: 
 1421:     boolean useNormanToUnicodeMapper = false;
 1422: 
 1423:     final static int CONVERT_MODE = 1;
 1424: 
 1425:     final static int APPEND_MODE = 2;
 1426: 
 1427:     final static int UPDATE_MODE = 3;
 1428: 
 1429:     final static int DELETE_MODE = 4;
 1430: 
 1431:     final static int SYNCHRONIZE_MODE = 5;
 1432: 
 1433:     int mode = -1;
 1434: 
 1435:     public DataBase(DBBean bean, Vector tables, Vector layouts, Vector selects, Vector creates,
 1436:         Vector ids, int mode)
 1437:     {
 1438:       this.bean = bean;
 1439:       this.tables = tables;
 1440:       this.layouts = layouts;
 1441:       this.selects = selects;
 1442:       this.creates = creates;
 1443:       this.ids = ids;
 1444:       this.mode = mode;
 1445:       this.bean.setIDVector(ids);
 1446:     }
 1447: 
 1448:     /**
 1449:      * @param indexListVec
 1450:      */
 1451:     public void buildIndexTable(Vector indexListVec)
 1452:     {
 1453:       for (int i = 0; i < tables.size(); i++)
 1454:       {
 1455:         fillIndexList((String) tables.get(i), (String) indexListVec.get(i));
 1456:       }
 1457:     }
 1458: 
 1459:     /**
 1460:      * writes the data contained in this object to the buffered writer *
 1461:      * 
 1462:      * @param buffr
 1463:      * @throws Exception
 1464:      */
 1465:     public void exportToXML(BufferedWriter buffr) throws Exception
 1466:     {
 1467:       // ids=bean.getIDVector();
 1468:       buffr.write("    <database>\n");
 1469:       buffr.write("      <url>" + bean.url + "</url>\n");
 1470:       buffr.write("      <user>" + bean.user + "</user>\n");
 1471:       buffr.write("      <password>" + bean.passwd + "</password>\n");
 1472:       buffr.write("      <delimiter>" + delimiter + "</delimiter>\n");
 1473:       String modeString = "";
 1474:       if (mode == CONVERT_MODE)
 1475:         modeString = "convert";
 1476:       else if (mode == APPEND_MODE)
 1477:         modeString = "append";
 1478:       else if (mode == UPDATE_MODE)
 1479:         modeString = "update";
 1480:       else if (mode == DELETE_MODE)
 1481:         modeString = "delete";
 1482:       else if (mode == SYNCHRONIZE_MODE)
 1483:         modeString = "synchronize";
 1484: 
 1485:       buffr.write("      <mode>" + modeString + "</mode>\n");
 1486:       buffr.write("      <usenormantounicodemapper>" + useNormanToUnicodeMapper
 1487:           + "</usenormantounicodemapper>\n");
 1488:       if (preCommands != null || postCommands != null)
 1489:       {
 1490:         int count = 0;
 1491: 
 1492:         buffr.write("      <sqlcommands> \n");
 1493: 
 1494:         if (preCommands != null)
 1495:         {
 1496:           while (count < preCommands.size())
 1497:           {
 1498:             SQLCommand sqlcommand=(SQLCommand)preCommands.get(count);
 1499:             buffr.write("        <sqlcommand execute=\"before\">" + sqlcommand.command + "</sqlcommand>\n");
 1500:             count++;
 1501:           }
 1502:         }
 1503:         if (postCommands != null)
 1504:         {
 1505:           count = 0;
 1506:           while (count < postCommands.size())
 1507:           {
 1508:             SQLCommand sqlcommand=(SQLCommand)postCommands.get(count);
 1509:             
 1510:             buffr.write("        <sqlcommand execute=\"after\">" + sqlcommand.command + "</sqlcommand>\n");
 1511:             count++;
 1512:           }
 1513:         }
 1514:         buffr.write("      </sqlcommands> \n");
 1515: 
 1516:       }
 1517:       int index = 0;
 1518:       while (index < tables.size())
 1519:       {
 1520:         String table = (String) tables.get(index);
 1521:         String layout = (String) layouts.get(index);
 1522:         String select = (String) selects.get(index);
 1523:         String create = (String) creates.get(index);
 1524:         String id = (String) ids.get(index);
 1525:         IndexList indexList = (IndexList) htIndex.get(table);
 1526:         if (indexList == null)
 1527:           indexList = new IndexList();
 1528:         buffr.write("      <table name = \"" + table + "\" layout = \"" + layout + "\" id = \""
 1529:             + id + "\" indexList =\"" + indexList + "\">\n");
 1530:         buffr.write("         <select>" + convertToEntities(select) + "</select>\n");
 1531:         if (!create.equals(""))
 1532:           buffr.write("         <create>" + create + "         </create>\n");
 1533:         buffr.write("      </table>\n");
 1534:         index++;
 1535:       }
 1536:       buffr.write("    </database>\n");
 1537:     }
 1538: 
 1539:     public void fillIndexList(String table, String list)
 1540:     {
 1541:       IndexList indexList = new IndexList();
 1542:       StringTokenizer tokenizer = new StringTokenizer(list, ",");
 1543:       while (tokenizer.hasMoreTokens())
 1544:       {
 1545:         indexList.add(tokenizer.nextToken());
 1546:       }
 1547:       System.out.println(indexList);
 1548: 
 1549:       htIndex.put(table, indexList);
 1550:     }
 1551: 
 1552:     public String toString()
 1553:     {
 1554:       return bean.url + " " + tables;
 1555:     }
 1556: 
 1557:   }
 1558: 
 1559:   public static void writeConfig(String file, DataBase source, DataBase destination)
 1560:       throws Exception
 1561:   {
 1562:     if (!file.toLowerCase().endsWith(".xml"))
 1563:       file += ".xml";
 1564:     File f = new File(file);
 1565: 
 1566:     FileOutputStream fout = new FileOutputStream(f);
 1567:     OutputStreamWriter outsw = new OutputStreamWriter(fout, "UTF-8");
 1568:     BufferedWriter buffw = new BufferedWriter(outsw);
 1569:     buffw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
 1570:     buffw.newLine();
 1571:     buffw.write("<convert>\n");
 1572:     buffw.write("  <source>\n");
 1573:     source.exportToXML(buffw);
 1574:     buffw.write("  </source>\n");
 1575:     buffw.write("\n  <destination>\n");
 1576:     destination.exportToXML(buffw);
 1577:     buffw.write("  </destination>\n");
 1578:     buffw.write("</convert>\n");
 1579:     buffw.close();
 1580:   }
 1581: 
 1582:   public static void delete(String source, String destination, Vector names, Vector layouts,
 1583:       Vector selects, Vector creates, Vector ids, int mode) throws Exception
 1584:   {
 1585:     FM2SQL.ProgressDialog dialog = null;
 1586:     if (isGUI)
 1587:     {
 1588:       dialog = initDialog();
 1589:     }
 1590:     // setting user and passwd
 1591:     bean.setUserAndPasswd(user, passwd);
 1592:     // setting user and passwd
 1593:     beanDest.setUserAndPasswd(userDest, passwdDest);
 1594:     StringBuffer command = null;
 1595:     String query = null;
 1596:     try
 1597:     {
 1598:       // bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");
 1599:       // bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
 1600:       bean.setConnection(source);
 1601:       if (names == null)
 1602:         names = bean.getTableNames();
 1603:       // Collections.sort(names);
 1604:       int tbIndex = 1;
 1605: 
 1606:       // System.out.println("Start at
 1607:       // "+names.indexOf("archimedes_facsimiles"));
 1608:       for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
 1609:       {
 1610:         Vector[] result = null;
 1611:         java.util.TreeSet myIds = new TreeSet();
 1612:         java.util.TreeSet myIdsDest = new TreeSet();
 1613:         int deltaID = 1;
 1614:         String idField = "";
 1615:         String destTableName = "";
 1616: 
 1617:         try
 1618:         {
 1619:           query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
 1620:           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
 1621:           query = (selects != null) ? selects.get(tbIndex).toString() : query;
 1622:           // if vectors[1].get(i) != null)
 1623:           if (layout != "")
 1624:           {
 1625:             layout = " layout " + bean.getQC() + layout + bean.getQC();
 1626:             String name = names.get(tbIndex).toString();
 1627:             StringBuffer queryLayout = new StringBuffer(query);
 1628:             queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
 1629:             query = queryLayout.toString();
 1630:             System.out.println("added layout  " + query);
 1631: 
 1632:           }
 1633:           dialog.title.setText("Getting table data ...");
 1634:           dialog.table.setText(names.get(tbIndex).toString());
 1635:           dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
 1636:           dialog.setVisible(true);
 1637:           bean.getConnection();
 1638:           bean.makeQuery(query, 50);
 1639:           idField = ids.get(tbIndex).toString();
 1640: 
 1641:         } catch (Exception e)
 1642:         {
 1643:           continue;
 1644:         }
 1645:         // determine destTableName from createStatement or from source
 1646:         // table name
 1647:         if (!creates.get(tbIndex).equals(""))
 1648:         {
 1649:           String create = creates.get(tbIndex).toString().toLowerCase();
 1650:           int fromIndex = create.indexOf("table") + 5;
 1651:           int toIndex = create.indexOf("(");
 1652:           destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "")
 1653:               .trim();
 1654:           System.out.println("destTable " + destTableName);
 1655: 
 1656:         } else
 1657:           destTableName = convertText(names.get(tbIndex).toString());
 1658: 
 1659:         // for id kram
 1660:         Vector vec = null;
 1661:         Vector vecDest = null;
 1662:         // tempo
 1663:         beanDest.setConnection(destination);
 1664:         int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
 1665:         String tempID = bean.getQC() + idField + bean.getQC();
 1666:         String tempIDdest = beanDest.getQC() + convertText(idField) + beanDest.getQC();
 1667: 
 1668:         int endIndex = -1;
 1669:         String tempQuery = query;
 1670:         String destQuery = query.replaceAll(names.get(tbIndex).toString(), destTableName);
 1671:         String tempQueryDest = destQuery;
 1672:         // remove extra query parts
 1673:         // destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1);
 1674:         System.out.println("new Query " + tempQueryDest);
 1675:         if (!idField.equals(""))
 1676:         {
 1677:           long startTime = System.currentTimeMillis();
 1678:           int counter = -1;
 1679:           while (true)
 1680:           {
 1681:             ++counter;
 1682:             if (counter == 0 && dialog != null)
 1683:               dialog.title.setText("Check if data  is available");
 1684:             else if (dialog != null)
 1685:               dialog.title.setText("Check if more  data  is available");
 1686:             myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex),
 1687:                 tempQuery, numHits);
 1688:             myIdsDest = beanDest.getIDVector(convertText(idField), destTableName, tempQueryDest,
 1689:                 numHits);
 1690:             if (myIds.isEmpty())
 1691:               break;
 1692:             vec = new Vector(myIds);
 1693:             vecDest = new Vector(myIdsDest);
 1694:             rowCount = vec.size();
 1695:             // Deletion will work this way
 1696:             Vector deleted = new Vector(vec);
 1697:             Vector linesToDelete = new Vector(vecDest);
 1698:             // remove all lines that should not be deleted
 1699:             linesToDelete.removeAll(deleted);
 1700:             // System.out.println("ID LIST SIZE " +
 1701:             // Math.round((double) myIds.size() / (double)
 1702:             // numIntervalls) + " " + myIdsDest.size());
 1703:             // / @TODO complete delete task remove query show lines
 1704:             // to be deleted let user choose if he wants that
 1705:             System.out.println("number of lines to  be deleted " + linesToDelete.size());
 1706:             deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
 1707:             beanDest.setConnection(destination);
 1708: 
 1709:             Statement stm = beanDest.getConnection().createStatement();
 1710: 
 1711:             Vector tables = beanDest.getTableNames();
 1712:             // Collections.sort(tables);
 1713:             System.out.println(names.get(tbIndex) + " "
 1714:                 + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
 1715:             tables = beanDest.getTableNames();
 1716:             // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
 1717:             stm = beanDest.getConnection().createStatement();
 1718: 
 1719:             if (dialog != null)
 1720:               dialog.title.setText(" Deleting table data ...");
 1721: 
 1722:             int j = -1;
 1723: 
 1724:             Vector row = null;
 1725:             command = new StringBuffer();
 1726: 
 1727:             command.append("DELETE FROM");
 1728:             command.append(beanDest.getQC());
 1729:             command.append(destTableName);
 1730:             command.append(beanDest.getQC());
 1731:             int size = bean.getColumnNames().size();
 1732:             command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " =  ?");
 1733:             PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
 1734:             System.out.println(command + " " + tbIndex);
 1735:             while (true)
 1736:             {
 1737: 
 1738:               ++j;
 1739:               if (j == linesToDelete.size())
 1740:                 break;
 1741:               // print rows
 1742:               pstm.setString(1, linesToDelete.get(j).toString());
 1743:               System.out.println(pstm.toString());
 1744:               pstm.execute();
 1745:               if (isGUI)
 1746:                 dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
 1747:               command = null;
 1748:             }
 1749:             // prepare new query for next chunk
 1750:             if (query.indexOf("where") > 0)
 1751:               tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
 1752:             else
 1753:               tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
 1754: 
 1755:           } // to outer while
 1756:         } // to idfield if
 1757:       } // table loop
 1758: 
 1759:     } catch (Exception e)
 1760:     {
 1761:       System.out.println("Error while connecting to database " + e);
 1762:       if (isGUI)
 1763:       {
 1764:         showExceptionDialog(dialog, command, e);
 1765:         resetGUI(dialog);
 1766:       } else
 1767:       {
 1768:         e.printStackTrace();
 1769:       }
 1770:     }
 1771:   } // to method
 1772: 
 1773:   /**
 1774:    * synchronize method based on delete method code
 1775:    * 
 1776:    * @param source
 1777:    * @param destination
 1778:    * @param names
 1779:    * @param layouts
 1780:    * @param selects
 1781:    * @param creates
 1782:    * @param ids
 1783:    * @param mode
 1784:    * @throws Exception
 1785:    */
 1786:   // TODO implement append,update and delete in one method
 1787:   // TODO using id based algorithm
 1788:   public static void synchronize(String source, String destination, Vector names, Vector layouts,
 1789:       Vector selects, Vector creates, Vector ids, int mode, String delimiter, Vector indexList)
 1790:       throws Exception
 1791:   {
 1792:     System.out.println(" bin in synchronize!!!");
 1793:     FM2SQL.ProgressDialog dialog = null;
 1794:     if (isGUI)
 1795:     {
 1796:       dialog = initDialog();
 1797:       dialog.setTitle("Synchronize running ...");
 1798: 
 1799:     }
 1800:     // setting user and passwd
 1801:     bean.setUserAndPasswd(user, passwd);
 1802:     // setting user and passwd
 1803:     beanDest.setUserAndPasswd(userDest, passwdDest);
 1804:     StringBuffer command = null;
 1805:     String query = null;
 1806:     try
 1807:     {
 1808:       bean.setConnection(source);
 1809:       if (names == null)
 1810:         names = bean.getTableNames();
 1811:       int tbIndex = 1;
 1812: 
 1813:       for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
 1814:       {
 1815:         Vector[] result = null;
 1816:         java.util.TreeSet myIds = new TreeSet();
 1817:         java.util.TreeSet myIdsDest = new TreeSet();
 1818:         int deltaID = 1;
 1819:         String idField = "";
 1820:         String destTableName = "";
 1821: 
 1822:         try
 1823:         {
 1824:           query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
 1825:           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
 1826:           query = (selects != null) ? selects.get(tbIndex).toString() : query;
 1827:           // if vectors[1].get(i) != null)
 1828:           if (!layout.equals(""))
 1829:           {
 1830:             query = addLayoutToQuery(names, query, tbIndex, layout);
 1831: 
 1832:           }
 1833:           if (dialog != null)
 1834:           {
 1835:             prepareDialogforUse(names, dialog, tbIndex);
 1836:           }
 1837:           bean.getConnection();
 1838:           bean.makeQuery(query, 50);
 1839:           idField = ids.get(tbIndex).toString();
 1840: 
 1841:         } catch (Exception e)
 1842:         {
 1843:           System.out.println("Warning exception occured \n " + e);
 1844: 
 1845:           continue;
 1846:         }
 1847:         // determine destTableName from createStatement or from source
 1848:         // table name
 1849:         if (!creates.get(tbIndex).equals(""))
 1850:         {
 1851:           String create = creates.get(tbIndex).toString().toLowerCase();
 1852:           int fromIndex = create.indexOf("table") + 5;
 1853:           int toIndex = create.indexOf("(");
 1854:           destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "")
 1855:               .trim();
 1856:           System.out.println("destTable " + destTableName);
 1857: 
 1858:         } else
 1859:           destTableName = convertText(names.get(tbIndex).toString());
 1860: 
 1861:         // for id kram
 1862:         Vector vec = null;
 1863:         Vector vecDest = null;
 1864:         // tempo
 1865:         beanDest.setConnection(destination);
 1866:         int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
 1867:         String tempID = bean.getQC() + idField + bean.getQC();
 1868:         String tempIDdest = beanDest.getQC() + convertText(idField) + beanDest.getQC();
 1869: 
 1870:         int endIndex = -1;
 1871:         String tempQuery = query;
 1872:         String destQuery = query.replaceAll(names.get(tbIndex).toString(), destTableName);
 1873:         destQuery = destQuery.replaceAll(bean.getQC(), beanDest.getQC());
 1874:         destQuery = removeLayoutPartFromQuery(destQuery, layouts.get(tbIndex).toString());
 1875:         // TODO remove layout part for destQuery
 1876:         String tempQueryDest = destQuery;
 1877:         // remove extra query parts
 1878:         // destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1);
 1879:         System.out.println("new Query " + tempQueryDest);
 1880:         System.out.println("idfield " + idField + " " + ids.get(tbIndex).toString());
 1881:         if (!idField.equals(""))
 1882:         {
 1883:           long startTime = System.currentTimeMillis();
 1884:           int counter = -1;
 1885:           TreeSet linesToDelete = null;
 1886:           PreparedStatement delPSt = null;
 1887:           while (true)
 1888:           {
 1889:             ++counter;
 1890:             if (counter == 0 && dialog != null)
 1891:               dialog.title.setText("Check if data  is available");
 1892:             else if (dialog != null)
 1893:               dialog.title.setText("Check if more  data  is available");
 1894: 
 1895:             myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex),
 1896:                 tempQuery, 0);
 1897:             myIdsDest = beanDest.getIDVector(convertText(idField), destTableName, tempQueryDest, 0);
 1898:             // System.out.println("status of remove
 1899:             // "+myIds.remove("b015892"));
 1900:             System.out.println("ids found for " + idField + " " + !myIds.isEmpty());
 1901:             if (myIds.isEmpty())
 1902:               break;
 1903:             vec = new Vector(myIds);
 1904:             vecDest = new Vector(myIdsDest);
 1905:             rowCount = vec.size();
 1906:             // Deletion will work this way
 1907:             Vector deleted = new Vector(vec);
 1908: 
 1909:             TreeSet linesToAppend = new TreeSet(vec);
 1910:             linesToAppend.addAll(vec);
 1911:             linesToDelete = new TreeSet(vecDest);
 1912:             // remove all lines that are already in dest database
 1913:             linesToAppend.removeAll(vecDest);
 1914:             // remove all lines that should not be deleted
 1915:             linesToDelete.removeAll(deleted);
 1916:             System.out.println("linesToAppend " + linesToAppend.size() + " " + destTableName);
 1917:             System.out.println("linesToDelete " + linesToDelete.size() + " " + destTableName);
 1918:             System.out.println("ID LIST SIZE "
 1919:                 + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIds.size());
 1920:             deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
 1921:             ConversionProperties prop = getFieldNamesAndDestTableName(creates.get(tbIndex)
 1922:                 .toString(), query, names.get(tbIndex).toString());
 1923:             StringBuffer insCommand = createInsertCommand(prop.destTableName, prop.fieldNames);
 1924:             StringBuffer updCommand = createUpdateCommand(prop.destTableName, prop.fieldNames,
 1925:                 tempIDdest);
 1926:             StringBuffer delCommand = createDeleteCommand(destTableName, tempIDdest);
 1927:             PreparedStatement insPst = beanDest.getConnection().prepareStatement(
 1928:                 insCommand.toString());
 1929:             PreparedStatement updPst = beanDest.getConnection().prepareStatement(
 1930:                 updCommand.toString());
 1931:             delPSt = beanDest.getConnection().prepareStatement(delCommand.toString());
 1932:             // delPSt.setString(1,"b015892");
 1933:             // delPSt.execute();
 1934:             // if (true)
 1935:             // return;
 1936:             if (vec.size() <= numIntervalls)
 1937:             {
 1938:               endIndex = 0;
 1939:               deltaID = vec.size();
 1940:             }
 1941:             for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)
 1942:             {
 1943:               System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());
 1944:               if (query.indexOf("where") > 0)
 1945:                 tempQuery = query + " and " + tempID + ">='" + vec.get(k) + "' and " + tempID
 1946:                     + "<='" + vec.get(k + deltaID) + "'";
 1947:               else
 1948:                 tempQuery = query + " where " + tempID + ">='" + vec.get(k) + "' and " + tempID
 1949:                     + "<='" + vec.get(k + deltaID) + "'";
 1950:               System.out.println(tempQuery);
 1951:               if (dialog != null)
 1952:                 dialog.title.setText("Reading table data ...");
 1953: 
 1954:               // bean.makeQuery(tempQuery, deltaID);
 1955:               if (dialog != null)
 1956:                 dialog.title.setText("Writing table data ...");
 1957: 
 1958:               performSynchronize(idField, vec, tempQuery, linesToDelete, linesToAppend, insPst,
 1959:                   updPst, delPSt, deltaID, delimiter, dialog);
 1960:               // System.out.println("ID LIST SIZE " +
 1961:               // Math.round((double) myIds.size() / (double)
 1962:               // numIntervalls) + " " + myIdsDest.size());
 1963:               endIndex = k + deltaID;
 1964:             }
 1965:             System.out.println(endIndex);
 1966:             // all data written ? if not write last chunk of data
 1967:             if (endIndex == vec.size() - 1)
 1968:               System.out.println("fits");
 1969:             else
 1970:             {
 1971:               System.out.println(" last intervall from " + vec.get(endIndex) + " "
 1972:                   + vec.lastElement());
 1973: 
 1974:               if (query.indexOf("where") > 0)
 1975:                 tempQuery = query + " and " + tempID + ">='" + vec.get(endIndex) + "' and "
 1976:                     + tempID + "<='" + vec.lastElement() + "'";
 1977:               else
 1978:                 tempQuery = query + " where " + tempID + ">='" + vec.get(endIndex) + "' and "
 1979:                     + tempID + "<='" + vec.lastElement() + "'";
 1980:               System.out.println(tempQuery);
 1981:               if (dialog != null)
 1982:                 dialog.title.setText("Reading table data ...");
 1983:               // bean.makeQuery(tempQuery, 0);
 1984:               if (dialog != null)
 1985:                 dialog.title.setText("Writing table data ...");
 1986:               performSynchronize(idField, vec, tempQuery, linesToDelete, linesToAppend, insPst,
 1987:                   updPst, delPSt, deltaID, delimiter, dialog);
 1988:               // System.out.println("ID LIST SIZE " +
 1989:               // Math.round((double) myIds.size() / (double)
 1990:               // numIntervalls) + " " + myIdsDest.size());
 1991:             }
 1992:             // prepare new query for next chunk
 1993:             if (query.indexOf("where") > 0)
 1994:               tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
 1995:             else
 1996:               tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
 1997: 
 1998:           }
 1999:           String tableName = names.get(tbIndex).toString();
 2000:           if (!indexList.isEmpty())
 2001:           {
 2002:             IndexList idList = (IndexList) indexList.get(0);
 2003:             System.out.println("found list " + idList);
 2004:             Statement stm = beanDest.getConnection().createStatement();
 2005:             Vector destTables = beanDest.getTableNames();
 2006:             System.out.println("tempQueryDest" + tempQueryDest);
 2007:             beanDest.makeQuery(tempQueryDest, 0);
 2008:             for (Iterator iter = idList.iterator(); iter.hasNext();)
 2009:             {
 2010:               String indexField = (String) iter.next();
 2011:               indexField = convertText(indexField);
 2012:               String indexName = destTableName + "_" + indexField;
 2013:               if (destTables.contains(indexName))
 2014:               {
 2015:                 stm.execute("DROP  INDEX " + destTableName + "_" + indexField);
 2016:                 // continue;
 2017:               }
 2018:               // stm.execute("DROP INDEX
 2019:               // "+destTableName+"_"+indexField);
 2020: 
 2021:               String type = beanDest.getColumnType(indexField).toLowerCase();
 2022:               // System.out.println(indexField+" "+type+"
 2023:               // "+(type.indexOf("text") >= 0 ||
 2024:               // type.indexOf("varchar") >= 0 || type.indexOf("char")
 2025:               // >= 0));
 2026:               if (type.indexOf("text") >= 0 || type.indexOf("varchar") >= 0
 2027:                   || type.indexOf("char") >= 0)
 2028:               {
 2029:                 if (beanDest.url.indexOf("mysql") >= 0)
 2030:                 {
 2031:                   // System.out.println("CREATE INDEX " +
 2032:                   // indexName + " ON " + destTableName + " (" +
 2033:                   // indexField + "(10))");
 2034:                   // TODO problem if index exist !!!
 2035:                   stm.execute("CREATE  INDEX " + indexName + " ON " + destTableName + " ("
 2036:                       + indexField + "(10))");
 2037:                 } else
 2038:                 {
 2039:                   stm.execute("CREATE  INDEX " + indexName + " ON " + destTableName + " (lower( "
 2040:                       + indexField + "))");
 2041: 
 2042:                 }
 2043: 
 2044:               } else
 2045:               {
 2046:                 stm.execute("CREATE  INDEX " + destTableName + "_" + indexField + " ON "
 2047:                     + destTableName + "(" + indexField + ")");
 2048: 
 2049:               }
 2050: 
 2051:               // stm.execute("DROP INDEX
 2052:               // "+destTableName+"_"+indexField);
 2053: 
 2054:             }
 2055:           }
 2056:           // CREATE UNIQUE INDEX title_idx ON films (title);
 2057:           for (Iterator iter = linesToDelete.iterator(); iter.hasNext();)
 2058:           {
 2059:             String id = (String) iter.next();
 2060:             delPSt.setString(1, id);
 2061:             delPSt.execute();
 2062: 
 2063:           }
 2064: 
 2065:           long endTime = System.currentTimeMillis();
 2066:           System.out.println("Time for incremental synchronize  elapsed " + (endTime - startTime));
 2067:         } // to idfield if
 2068:       } // table loop
 2069: 
 2070:     } catch (Exception e)
 2071:     {
 2072:       System.out.println("Error while connecting to database " + e);
 2073:       e.printStackTrace();
 2074:       if (isGUI)
 2075:         showExceptionDialog(dialog, command, e);
 2076:     }
 2077:     if (isGUI)
 2078:     {
 2079:       resetGUI(dialog);
 2080:     }
 2081:   }
 2082: 
 2083:   /**
 2084:    * @param destQuery
 2085:    * @param string
 2086:    * @return
 2087:    */
 2088:   private static String removeLayoutPartFromQuery(String destQuery, String layoutName)
 2089:   {
 2090:     String removeString = "layout " + beanDest.getQC() + layoutName + beanDest.getQC();
 2091:     destQuery = destQuery.replaceFirst(removeString, "");
 2092:     System.out.println("destQuery change to " + destQuery);
 2093:     return destQuery;
 2094:   }
 2095: 
 2096:   private static void performSynchronize(String idField, Vector vec, String tempQuery,
 2097:       TreeSet linesToDelete, TreeSet linesToAppend, PreparedStatement insPst,
 2098:       PreparedStatement updPst, PreparedStatement delPSt, int deltaID, String delimiter,
 2099:       FM2SQL.ProgressDialog dialog) throws SQLException, ParseException, Exception
 2100:   {
 2101:     if (dialog != null)
 2102:     {
 2103:       dialog.progress.setValue(0);
 2104:       dialog.title.setText("Retrieving new data");
 2105:     }
 2106: 
 2107:     Vector[] vectors = bean.getQueryData(tempQuery, deltaID);
 2108:     int count = 0, size = vectors[0].size();
 2109:     int idIndex = vectors[1].indexOf(idField);
 2110:     // System.out.println(idIndex + " " + vectors[1] + " " + idField);
 2111:     // todo arraylist code has to be added
 2112:     if (dialog != null)
 2113:       dialog.title.setText("Synchronize with new data");
 2114: 
 2115:     for (Iterator iter = vectors[0].iterator(); iter.hasNext();)
 2116:     {
 2117:       Vector line = (Vector) iter.next();
 2118:       Object lineIDIndex = line.get(idIndex);
 2119:       if (linesToAppend.contains(lineIDIndex))
 2120:         System.out.println("line " + linesToAppend.contains(line.get(idIndex)) + " " + lineIDIndex);
 2121:       if (linesToAppend.contains(lineIDIndex))
 2122:       {
 2123:         for (int l = 0; l < line.size(); ++l)
 2124:         {
 2125:           Object obj = line.get(l);
 2126:           if (obj instanceof ArrayList)
 2127:             obj = formatFileMakerArray((List) obj, delimiter);
 2128:           if (obj != null)
 2129:             insPst.setString(l + 1, obj.toString());
 2130:           else
 2131:             insPst.setNull(l + 1, Types.NULL);
 2132:         }
 2133:         insPst.execute();
 2134: 
 2135:       } else
 2136:       // update
 2137:       {
 2138:         for (int l = 0; l < line.size(); ++l)
 2139:         {
 2140:           Object obj = line.get(l);
 2141:           if (obj instanceof ArrayList)
 2142:             obj = formatFileMakerArray((List) obj, delimiter);
 2143:           if (obj != null)
 2144:             updPst.setString(l + 1, obj.toString());
 2145:           else
 2146:             updPst.setNull(l + 1, Types.NULL);
 2147:         }
 2148:         updPst.setString(line.size() + 1, line.get(idIndex).toString());
 2149:         // updPst.addBatch();
 2150:         // updPst.execute();
 2151:       }
 2152:       if (dialog != null)
 2153:       {
 2154:         int value = (int) Math.round(((double) count / (double) size) * 100.0);
 2155:         dialog.progress.setValue(value);
 2156:         count++;
 2157:       }
 2158:     }
 2159:     // updPst.executeBatch();
 2160:   } // to method
 2161: 
 2162:   /**
 2163:    * Converts input String in norman encoding to unicode
 2164:    * 
 2165:    * @param inp
 2166:    * @return converted String
 2167:    */
 2168:   static public String normanToUnicode(String inp)
 2169:   {
 2170:     StringBuffer buf = new StringBuffer();
 2171:     for (int i = 0; i < inp.length(); i++)
 2172:     {
 2173:       char c = inp.charAt(i);
 2174:       // System.out.println("char "+c+" "+(int)c);
 2175:       switch (c)
 2176:       {
 2177:         case 1:
 2178:           buf.append("\u00d0");
 2179:           break; // Eth
 2180:         case 2:
 2181:           buf.append("\u00f0");
 2182:           break; // eth
 2183:         case 3:
 2184:           buf.append("\u0141");
 2185:           break; // Lslash
 2186:         case 4:
 2187:           buf.append("\u0142");
 2188:           break; // lslash
 2189:         case 5:
 2190:           buf.append("\u0160");
 2191:           break; // S caron
 2192:         case 6:
 2193:           buf.append("\u0161");
 2194:           break; // s caron
 2195:         case 7:
 2196:           buf.append("\u00dd");
 2197:           break; // Y acute
 2198:         case 8:
 2199:           buf.append("\u00fd");
 2200:           break; // y acute
 2201:         case 11:
 2202:           buf.append("\u00de");
 2203:           break; // Thorn
 2204:         case 12:
 2205:           buf.append("\u00fe");
 2206:           break; // thorn
 2207:         case 14:
 2208:           buf.append("\u017d");
 2209:           break; // Z caron
 2210:         case 15:
 2211:           buf.append("\u017e");
 2212:           break; // z caron
 2213:         case 17:
 2214:           buf.append("\u0073");
 2215:           break; // asciitilde
 2216:         case 18:
 2217:           buf.append("j\u0305");
 2218:           break; // j macron [does a single char exist?]
 2219:         case 19:
 2220:           buf.append("^");
 2221:           break; // circumflex
 2222:         case 20:
 2223:           buf.append("\u0303");
 2224:           break; // tilde
 2225:         case 21:
 2226:           buf.append("\u00bd");
 2227:           break; // onehalf
 2228:         case 22:
 2229:           buf.append("\u00bc");
 2230:           break; // onequarter
 2231:         case 23:
 2232:           buf.append("\u00b9");
 2233:           break; // onesuperior
 2234:         case 24:
 2235:           buf.append("\u00be");
 2236:           break; // threequarters
 2237:         case 25:
 2238:           buf.append("\u00b3");
 2239:           break; // threesuperior
 2240:         case 26:
 2241:           buf.append("\u00b2");
 2242:           break; // twosuperior
 2243:         case 27:
 2244:           buf.append("\u00a6");
 2245:           break; // brokenbar
 2246:         case 28:
 2247:           buf.append("-");
 2248:           break; // minus
 2249:         case 29:
 2250:           buf.append("\u00d7");
 2251:           break; // multiply
 2252:         case 39:
 2253:           buf.append("'");
 2254:           break; // quotesingle
 2255:         case 94:
 2256:           buf.append("\u0302");
 2257:           break; // circumflex
 2258:         case 96:
 2259:           buf.append("\u0300");
 2260:           break; // grave
 2261:         case 196:
 2262:           buf.append("\u00c4");
 2263:           break; // A dieresis
 2264:         case 197:
 2265:           buf.append("\u00c5");
 2266:           break; // A ring
 2267:         case 201:
 2268:           buf.append("\u00c9");
 2269:           break; // E acute
 2270:         case 209:
 2271:           buf.append("\u00d1");
 2272:           break; // N tilde
 2273:         case 214:
 2274:           buf.append("\u00d6");
 2275:           break; // O dieresis
 2276:         case 220:
 2277:           buf.append("\u00dc");
 2278:           break; // U dieresis
 2279:         case 225:
 2280:           buf.append("\u00e1");
 2281:           break; // a acute
 2282:         case 224:
 2283:           buf.append("\u00e0");
 2284:           break; // a grave
 2285:         case 226:
 2286:           buf.append("\u00e2");
 2287:           break; // a circumflex
 2288:         case 228:
 2289:           buf.append("\u00e4");
 2290:           break; // a dieresis
 2291:         case 227:
 2292:           buf.append("\u00e3");
 2293:           break; // a tilde
 2294:         case 229:
 2295:           buf.append("\u0101");
 2296:           break; // a macron
 2297:         case 231:
 2298:           buf.append("\u00e7");
 2299:           break; // c cedilla
 2300:         case 233:
 2301:           buf.append("\u00e9");
 2302:           break; // e acute
 2303:         case 232:
 2304:           buf.append("\u00e8");
 2305:           break; // e grave
 2306:         case 234:
 2307:           buf.append("\u00ea");
 2308:           break; // e circumflex
 2309:         case 235:
 2310:           buf.append("\u00eb");
 2311:           break; // e dieresis
 2312:         case 237:
 2313:           buf.append("\u00ed");
 2314:           break; // i acute
 2315:         case 236:
 2316:           buf.append("\u00ec");
 2317:           break; // i grave
 2318:         case 238:
 2319:           buf.append("\u00ee");
 2320:           break; // i circumflex
 2321:         case 239:
 2322:           buf.append("\u00ef");
 2323:           break; // i dieresis
 2324:         case 241:
 2325:           buf.append("\u00f1");
 2326:           break; // n tilde
 2327:         case 243:
 2328:           buf.append("\u00f3");
 2329:           break; // o acute
 2330:         case 242:
 2331:           buf.append("\u00f2");
 2332:           break; // o grave
 2333:         case 244:
 2334:           buf.append("\u00f4");
 2335:           break; // o circumflex
 2336:         case 246:
 2337:           buf.append("\u00f6");
 2338:           break; // o dieresis
 2339:         case 245:
 2340:           buf.append("\u00f5");
 2341:           break; // o tilde
 2342:         case 250:
 2343:           buf.append("\u00fa");
 2344:           break; // u acute
 2345:         case 249:
 2346:           buf.append("\u00f9");
 2347:           break; // u grave
 2348:         case 251:
 2349:           buf.append("\u00fb");
 2350:           break; // u circumflex
 2351:         case 252:
 2352:           buf.append("\u00fc");
 2353:           break; // u dieresis
 2354:         case 8224:
 2355:           buf.append("\u1e6d");
 2356:           break; // t underdot
 2357:         case 176:
 2358:           buf.append("\u00b0");
 2359:           break; // degree
 2360:         case 162:
 2361:           buf.append("\u1ebd");
 2362:           break; // e tilde
 2363:         case 163:
 2364:           buf.append("\u00a3");
 2365:           break; // sterling
 2366:         case 167:
 2367:           buf.append("\u00a7");
 2368:           break; // section
 2369:         case 182:
 2370:           buf.append("\u00b6");
 2371:           break; // paragraph
 2372:         case 223:
 2373:           buf.append("\u015b");
 2374:           break; // s acute
 2375:         case 174:
 2376:           buf.append("\u1e5b");
 2377:           break; // r underdot
 2378:         case 169:
 2379:           buf.append("\u1e45");
 2380:           break; // n overdot
 2381:         case 353:
 2382:           buf.append("\u1e45");
 2383:           break; // n overdot
 2384:         case 180:
 2385:           buf.append("\u0301");
 2386:           break; // acute
 2387:         case 168:
 2388:           buf.append("\u0308");
 2389:           break; // dieresis
 2390:         case 8800:
 2391:           buf.append("\u1e6d");
 2392:           break; // t underdot
 2393:         case 198:
 2394:           buf.append("\u00c6");
 2395:           break; // AE
 2396:         case 216:
 2397:           buf.append("\u014d");
 2398:           break; // o macron
 2399:         case 8734:
 2400:           buf.append("\u0129");
 2401:           break; // i tilde
 2402:         case 177:
 2403:           buf.append("\u00b1");
 2404:           break; // plusminus
 2405:         case 165:
 2406:           buf.append("\u012b");
 2407:           break; // i macron
 2408:         case 181:
 2409:           buf.append("\u1e43");
 2410:           break; // m underdot
 2411:         case 8706:
 2412:           buf.append("\u1e0d");
 2413:           break; // d underdot
 2414:         case 240:
 2415:           buf.append("\u1e0d");
 2416:           break; // d underdot
 2417: 
 2418:         case 8721:
 2419:           buf.append("\u1e63");
 2420:           break; // s underdot
 2421:         case 960:
 2422:           buf.append("\u017a");
 2423:           break; // z acute
 2424:         case 8747:
 2425:           buf.append("\u1e45");
 2426:           break; // n overdot
 2427:         case 937:
 2428:           buf.append("\u0169");
 2429:           break; // u tilde
 2430:         case 230:
 2431:           buf.append("\u00e6");
 2432:           break; // ae
 2433:         case 248:
 2434:           buf.append("\u00f8");
 2435:           break; // oslash
 2436:         case 191:
 2437:           buf.append("\u0304\u0306");
 2438:           break; // macron breve
 2439:         case 172:
 2440:           buf.append("\u1e37");
 2441:           break; // 
 2442:         case 8730:
 2443:           buf.append("j\u0305");
 2444:           break; // j macron [does a single char exist?]
 2445:         case 402:
 2446:           buf.append("\u0103");
 2447:           break; // a breve
 2448:         case 8776:
 2449:           buf.append("\u016d");
 2450:           break; // u breve
 2451:         case 187:
 2452:           buf.append("\u1e42");
 2453:           break; // M underdot
 2454:         case 8230:
 2455:           buf.append("\u2026");
 2456:           break; // ellipsis
 2457:         case 192:
 2458:           buf.append("\u00c0");
 2459:           break; // A grave
 2460:         case 195:
 2461:           buf.append("\u00c3");
 2462:           break; // A tilde
 2463:         case 213:
 2464:           buf.append("\u00d5");
 2465:           break; // O tilde
 2466:         case 338:
 2467:           buf.append("m\u0306");
 2468:           break; // m breve
 2469:         case 339:
 2470:           buf.append("\u0153");
 2471:           break; // oe
 2472:         case 8211:
 2473:           buf.append("\u2013");
 2474:           break; // endash
 2475:         case 8212:
 2476:           buf.append("\u2014");
 2477:           break; // emdash
 2478:         case 8220:
 2479:           buf.append("\u201c");
 2480:           break; // quotedblleft
 2481:         case 8221:
 2482:           buf.append("\u201d");
 2483:           break; // quotedblright
 2484:         case 8216:
 2485:           buf.append("\u2018");
 2486:           break; // quoteleft
 2487:         case 8217:
 2488:           buf.append("\u2019");
 2489:           break; // quoteright
 2490:         case 247:
 2491:           buf.append("\u1e37");
 2492:           break; // l underring [actually underdot]
 2493:         case 9674:
 2494:           buf.append("\u1e41");
 2495:           break; // m overdot
 2496:         case 255:
 2497:           buf.append("n\u0306");
 2498:           break; // n breve
 2499:         case 376:
 2500:           buf.append("\u00d7");
 2501:           break; // multiply
 2502:         case 8364:
 2503:           buf.append("\u1e5b");
 2504:           break; // r underring [actually underdot]
 2505:         case 8249:
 2506:           buf.append("\u1e44");
 2507:           break; // N overdot
 2508:         case 8250:
 2509:           buf.append("\u1e62");
 2510:           break; // S underdot
 2511:         case 64257:
 2512:           buf.append("\u1e24");
 2513:           break; // H underdot
 2514:         case 64258:
 2515:           buf.append("\u1e0c");
 2516:           break; // D underdot
 2517:         case 8225:
 2518:           buf.append("\u2021");
 2519:           break; // daggerdbl
 2520:         case 8218:
 2521:           buf.append("\u1e36");
 2522:           break; // L underdot
 2523:         case 8222:
 2524:           buf.append("\u0113");
 2525:           break; // e macron
 2526:         case 194:
 2527:           buf.append("\u1e5f");
 2528:           break; // r underbar
 2529:         case 202:
 2530:           buf.append("r\u0324");
 2531:           break; // r underdieresis
 2532:         case 193:
 2533:           buf.append("\u012a");
 2534:           break; // I macron
 2535:         case 8486:
 2536:         case 203:
 2537:           buf.append("\u016b");
 2538:           break; // u macron
 2539:         case 200:
 2540:           buf.append("\u1e6c");
 2541:           break; // T underdot
 2542:         case 205:
 2543:           buf.append("\u1e64");
 2544:           break; // S acute
 2545:         case 206:
 2546:           buf.append("\u2020");
 2547:           break; // dagger
 2548:         case 207:
 2549:           buf.append("\u0115");
 2550:           break; // e breve
 2551:         case 204:
 2552:           buf.append("\u014f");
 2553:           break; // o breve
 2554:         case 211:
 2555:           buf.append("\u0100");
 2556:           break; // A macron
 2557:         case 212:
 2558:           buf.append("\u1e46");
 2559:           break; // N underdot
 2560:         case 210:
 2561:           buf.append("\u1e3b");
 2562:           break; // l underbar
 2563:         case 218:
 2564:           buf.append("\u016a");
 2565:           break; // U macron
 2566:         case 219:
 2567:           buf.append("\u0179");
 2568:           break; // Z acute
 2569:         case 217:
 2570:           buf.append("\u1e5a");
 2571:           break; // R underdot
 2572:         case 305:
 2573:           buf.append("\u0131");
 2574:           break; // dotlessi
 2575:         case 710:
 2576:           buf.append("\u1e47");
 2577:           break; // n underdot
 2578:         case 732:
 2579:           buf.append("\u1e49");
 2580:           break; // n underbar
 2581:         case 175:
 2582:           buf.append("\u0304");
 2583:           break; // macron
 2584:         case 728:
 2585:           buf.append("\u0306");
 2586:           break; // breve
 2587:         case 729:
 2588:         case 215:
 2589:           buf.append("\u1e25");
 2590:           break; // h underdot
 2591:         case 730:
 2592:           buf.append("\u012d");
 2593:           break; // i breve
 2594:         case 184:
 2595:           buf.append("\u0327");
 2596:           break; // cedilla
 2597:         case 733:
 2598:           buf.append("\u030b");
 2599:           break; // hungarumlaut
 2600:         case 731:
 2601:           buf.append("\u0328");
 2602:           break; // ogonek
 2603:         case 711:
 2604:           buf.append("\u030c");
 2605:           break; // caron
 2606:         case 199:
 2607:           buf.append("\u012b\u0303");
 2608:           break; // imacron tilde
 2609:         case 8226:
 2610:           buf.append("\u1e5d");
 2611:           break; // runderdot macron
 2612:         case 8482:
 2613:           buf.append("\u016b\0306");
 2614:           break; // umacron breve
 2615:         case 8804:
 2616:           buf.append("\u0101\u0301");
 2617:           break; // amacron acute
 2618:         case 8805:
 2619:           buf.append("\u016b\u0301");
 2620:           break; // umacron acute
 2621:         case 8719:
 2622:           buf.append("\u0113\u0301");
 2623:           break; // emacron acute
 2624:         case 170:
 2625:           buf.append("\u0113\u0300");
 2626:           break; // emacron breve
 2627:         case 186:
 2628:           buf.append("\u014d\u0300");
 2629:           break; // omacron breve
 2630:         case 161:
 2631:           buf.append("\u0101\u0306");
 2632:           break; // amacron breve
 2633:         case 8710:
 2634:           buf.append("\u0101\u0303");
 2635:           break; // amacron tilde
 2636:         case 171:
 2637:           buf.append("\u012b\u0301");
 2638:           break; // imacron acute
 2639:         case 8260:
 2640:           buf.append("\u1e00");
 2641:           break; // runderdotmacron acute
 2642:         case 183:
 2643:           buf.append("\u1e5b\u0301");
 2644:           break; // runderdot acute
 2645:         case 8240:
 2646:           buf.append("\u012b\u0306");
 2647:           break; // imacron breve
 2648:         case 63743:
 2649:           buf.append("\u016b\u0303");
 2650:           break; // umacron tilde
 2651:         default:
 2652:           buf.append(c);
 2653:           if ((int) c > 127)
 2654:             System.out.println("char " + c + " " + (int) c);
 2655:           break;
 2656:       }
 2657:     }
 2658:     return buf.toString();
 2659:   }
 2660: 
 2661:   static public String normanToUnicodeOld(String inp)
 2662:   {
 2663:     StringBuffer buf = new StringBuffer();
 2664:     for (int i = 0; i < inp.length(); i++)
 2665:     {
 2666:       char c = inp.charAt(i);
 2667:       switch (c)
 2668:       {
 2669:         case 1:
 2670:           buf.append("\u00d0");
 2671:           break; // Eth
 2672:         case 2:
 2673:           buf.append("\u00f0");
 2674:           break; // eth
 2675:         case 3:
 2676:           buf.append("\u0141");
 2677:           break; // Lslash
 2678:         case 4:
 2679:           buf.append("\u0142");
 2680:           break; // lslash
 2681:         case 5:
 2682:           buf.append("\u0160");
 2683:           break; // S caron
 2684:         case 6:
 2685:           buf.append("\u0161");
 2686:           break; // s caron
 2687:         case 7:
 2688:           buf.append("\u00dd");
 2689:           break; // Y acute
 2690:         case 8:
 2691:           buf.append("\u00fd");
 2692:           break; // y acute
 2693:         case 11:
 2694:           buf.append("\u00de");
 2695:           break; // Thorn
 2696:         case 12:
 2697:           buf.append("\u00fe");
 2698:           break; // thorn
 2699:         case 14:
 2700:           buf.append("\u017d");
 2701:           break; // Z caron
 2702:         case 15:
 2703:           buf.append("\u017e");
 2704:           break; // z caron
 2705:         case 17:
 2706:           buf.append("\u0073");
 2707:           break; // asciitilde
 2708:         case 18:
 2709:           buf.append("j\u0305");
 2710:           break; // j macron [does a single char exist?]
 2711:         case 19:
 2712:           buf.append("^");
 2713:           break; // circumflex
 2714:         case 20:
 2715:           buf.append("\u0303");
 2716:           break; // tilde
 2717:         case 21:
 2718:           buf.append("\u00bd");
 2719:           break; // onehalf
 2720:         case 22:
 2721:           buf.append("\u00bc");
 2722:           break; // onequarter
 2723:         case 23:
 2724:           buf.append("\u00b9");
 2725:           break; // onesuperior
 2726:         case 24:
 2727:           buf.append("\u00be");
 2728:           break; // threequarters
 2729:         case 25:
 2730:           buf.append("\u00b3");
 2731:           break; // threesuperior
 2732:         case 26:
 2733:           buf.append("\u00b2");
 2734:           break; // twosuperior
 2735:         case 27:
 2736:           buf.append("\u00a6");
 2737:           break; // brokenbar
 2738:         case 28:
 2739:           buf.append("-");
 2740:           break; // minus
 2741:         case 29:
 2742:           buf.append("\u00d7");
 2743:           break; // multiply
 2744:         case 39:
 2745:           buf.append("'");
 2746:           break; // quotesingle
 2747:         case 94:
 2748:           buf.append("\u0302");
 2749:           break; // circumflex
 2750:         case 96:
 2751:           buf.append("\u0300");
 2752:           break; // grave
 2753:         case 128:
 2754:           buf.append("\u00c4");
 2755:           break; // A dieresis
 2756:         case 129:
 2757:           buf.append("\u00c5");
 2758:           break; // A ring
 2759:         case 131:
 2760:           buf.append("\u00c9");
 2761:           break; // E acute
 2762:         case 132:
 2763:           buf.append("\u00d1");
 2764:           break; // N tilde
 2765:         case 133:
 2766:           buf.append("\u00d6");
 2767:           break; // O dieresis
 2768:         case 134:
 2769:           buf.append("\u00dc");
 2770:           break; // U dieresis
 2771:         case 135:
 2772:           buf.append("\u00e1");
 2773:           break; // a acute
 2774:         case 136:
 2775:           buf.append("\u00e0");
 2776:           break; // a grave
 2777:         case 137:
 2778:           buf.append("\u00e2");
 2779:           break; // a circumflex
 2780:         case 138:
 2781:           buf.append("\u00e4");
 2782:           break; // a dieresis
 2783:         case 139:
 2784:           buf.append("\u00e3");
 2785:           break; // a tilde
 2786:         case 140:
 2787:           buf.append("\u0101");
 2788:           break; // a macron
 2789:         case 141:
 2790:           buf.append("\u00e7");
 2791:           break; // c cedilla
 2792:         case 142:
 2793:           buf.append("\u00e9");
 2794:           break; // e acute
 2795:         case 143:
 2796:           buf.append("\u00e8");
 2797:           break; // e grave
 2798:         case 144:
 2799:           buf.append("\u00ea");
 2800:           break; // e circumflex
 2801:         case 145:
 2802:           buf.append("\u00eb");
 2803:           break; // e dieresis
 2804:         case 146:
 2805:           buf.append("\u00ed");
 2806:           break; // i acute
 2807:         case 147:
 2808:           buf.append("\u00ec");
 2809:           break; // i grave
 2810:         case 148:
 2811:           buf.append("\u00ee");
 2812:           break; // i circumflex
 2813:         case 149:
 2814:           buf.append("\u00ef");
 2815:           break; // i dieresis
 2816:         case 150:
 2817:           buf.append("\u00f1");
 2818:           break; // n tilde
 2819:         case 151:
 2820:           buf.append("\u00f3");
 2821:           break; // o acute
 2822:         case 152:
 2823:           buf.append("\u00f2");
 2824:           break; // o grave
 2825:         case 153:
 2826:           buf.append("\u00f4");
 2827:           break; // o circumflex
 2828:         case 154:
 2829:           buf.append("\u00f6");
 2830:           break; // o dieresis
 2831:         case 155:
 2832:           buf.append("\u00f5");
 2833:           break; // o tilde
 2834:         case 156:
 2835:           buf.append("\u00fa");
 2836:           break; // u acute
 2837:         case 157:
 2838:           buf.append("\u00f9");
 2839:           break; // u grave
 2840:         case 158:
 2841:           buf.append("\u00fb");
 2842:           break; // u circumflex
 2843:         case 159:
 2844:           buf.append("\u00fc");
 2845:           break; // u dieresis
 2846:         case 160:
 2847:           buf.append("\u1e6d");
 2848:           break; // t underdot
 2849:         case 161:
 2850:           buf.append("\u00b0");
 2851:           break; // degree
 2852:         case 162:
 2853:           buf.append("\u1ebd");
 2854:           break; // e tilde
 2855:         case 163:
 2856:           buf.append("\u00a3");
 2857:           break; // sterling
 2858:         case 164:
 2859:           buf.append("\u00a7");
 2860:           break; // section
 2861:         case 166:
 2862:           buf.append("\u00b6");
 2863:           break; // paragraph
 2864:         case 167:
 2865:           buf.append("\u015b");
 2866:           break; // s acute
 2867:         case 168:
 2868:           buf.append("\u1e5b");
 2869:           break; // r underdot
 2870:         case 169:
 2871:           buf.append("\u1e67");
 2872:           break; // s caron
 2873:         case 171:
 2874:           buf.append("\u0301");
 2875:           break; // acute
 2876:         case 172:
 2877:           buf.append("\u0308");
 2878:           break; // dieresis
 2879:         case 173:
 2880:           buf.append("\u1e6d");
 2881:           break; // t underdot
 2882:         case 174:
 2883:           buf.append("\u00c6");
 2884:           break; // AE
 2885:         case 175:
 2886:           buf.append("\u014d");
 2887:           break; // o macron
 2888:         case 176:
 2889:           buf.append("\u0129");
 2890:           break; // i tilde
 2891:         case 177:
 2892:           buf.append("\u00b1");
 2893:           break; // plusminus
 2894:         case 180:
 2895:           buf.append("\u012b");
 2896:           break; // i macron
 2897:         case 181:
 2898:           buf.append("\u1e43");
 2899:           break; // m underdot
 2900:         case 182:
 2901:           buf.append("\u1e0d");
 2902:           break; // d underdot
 2903:         case 183:
 2904:           buf.append("\u1e63");
 2905:           break; // s underdot
 2906:         case 185:
 2907:           buf.append("\u017a");
 2908:           break; // z acute
 2909:         case 186:
 2910:           buf.append("\u1e45");
 2911:           break; // n overdot
 2912:         case 189:
 2913:           buf.append("\u0169");
 2914:           break; // u tilde
 2915:         case 190:
 2916:           buf.append("\u00e6");
 2917:           break; // ae
 2918:         case 191:
 2919:           buf.append("\u00f8");
 2920:           break; // oslash
 2921:         case 192:
 2922:           buf.append("\u0304\u0306");
 2923:           break; // macron breve
 2924:         case 194:
 2925:           buf.append("\u1e37");
 2926:           break; // 
 2927:         case 195:
 2928:           buf.append("j\u0305");
 2929:           break; // j macron [does a single char exist?]
 2930:         case 196:
 2931:           buf.append("\u0103");
 2932:           break; // a breve
 2933:         case 197:
 2934:           buf.append("\u016d");
 2935:           break; // u breve
 2936:         case 200:
 2937:           buf.append("\u1e42");
 2938:           break; // M underdot
 2939:         case 201:
 2940:           buf.append("\u2026");
 2941:           break; // ellipsis
 2942:         case 203:
 2943:           buf.append("\u00c0");
 2944:           break; // A grave
 2945:         case 204:
 2946:           buf.append("\u00c3");
 2947:           break; // A tilde
 2948:         case 205:
 2949:           buf.append("\u00d5");
 2950:           break; // O tilde
 2951:         case 206:
 2952:           buf.append("m\u0306");
 2953:           break; // m breve
 2954:         case 207:
 2955:           buf.append("\u0153");
 2956:           break; // oe
 2957:         case 208:
 2958:           buf.append("\u2013");
 2959:           break; // endash
 2960:         case 209:
 2961:           buf.append("\u2014");
 2962:           break; // emdash
 2963:         case 210:
 2964:           buf.append("\u201c");
 2965:           break; // quotedblleft
 2966:         case 211:
 2967:           buf.append("\u201d");
 2968:           break; // quotedblright
 2969:         case 212:
 2970:           buf.append("\u2018");
 2971:           break; // quoteleft
 2972:         case 213:
 2973:           buf.append("\u2019");
 2974:           break; // quoteright
 2975:         case 214:
 2976:           buf.append("\u1e37");
 2977:           break; // l underring [actually underdot]
 2978:         case 215:
 2979:           buf.append("\u1e41");
 2980:           break; // m overdot
 2981:         case 216:
 2982:           buf.append("n\u0306");
 2983:           break; // n breve
 2984:         case 217:
 2985:           buf.append("\u00d7");
 2986:           break; // multiply
 2987:         case 219:
 2988:           buf.append("\u1e5b");
 2989:           break; // r underring [actually underdot]
 2990:         case 220:
 2991:           buf.append("\u1e44");
 2992:           break; // N overdot
 2993:         case 221:
 2994:           buf.append("\u1e62");
 2995:           break; // S underdot
 2996:         case 222:
 2997:           buf.append("\u1e24");
 2998:           break; // H underdot
 2999:         case 223:
 3000:           buf.append("\u1e0c");
 3001:           break; // D underdot
 3002:         case 224:
 3003:           buf.append("\u2021");
 3004:           break; // daggerdbl
 3005:         case 226:
 3006:           buf.append("\u1e36");
 3007:           break; // L underdot
 3008:         case 227:
 3009:           buf.append("\u0113");
 3010:           break; // e macron
 3011:         case 229:
 3012:           buf.append("\u1e5f");
 3013:           break; // r underbar
 3014:         case 230:
 3015:           buf.append("r\u0324");
 3016:           break; // r underdieresis
 3017:         case 231:
 3018:           buf.append("\u012a");
 3019:           break; // I macron
 3020:         case 232:
 3021:           buf.append("\u016b");
 3022:           break; // u macron
 3023:         case 233:
 3024:           buf.append("\u01e6c");
 3025:           break; // T underdot
 3026:         case 234:
 3027:           buf.append("\u1e64");
 3028:           break; // S acute
 3029:         case 235:
 3030:           buf.append("\u2020");
 3031:           break; // dagger
 3032:         case 236:
 3033:           buf.append("\u0115");
 3034:           break; // e breve
 3035:         case 237:
 3036:           buf.append("\u014f");
 3037:           break; // o breve
 3038:         case 238:
 3039:           buf.append("\u0100");
 3040:           break; // A macron
 3041:         case 239:
 3042:           buf.append("\u1e46");
 3043:           break; // N underdot
 3044:         case 241:
 3045:           buf.append("\u1e3b");
 3046:           break; // l underbar
 3047:         case 242:
 3048:           buf.append("\u016a");
 3049:           break; // U macron
 3050:         case 243:
 3051:           buf.append("\u0179");
 3052:           break; // Z acute
 3053:         case 244:
 3054:           buf.append("\u1e5a");
 3055:           break; // R underdot
 3056:         case 245:
 3057:           buf.append("\u0131");
 3058:           break; // dotlessi
 3059:         case 246:
 3060:           buf.append("\u1e47");
 3061:           break; // n underdot
 3062:         case 247:
 3063:           buf.append("\u1e49");
 3064:           break; // n underbar
 3065:         case 248:
 3066:           buf.append("\u0304");
 3067:           break; // macron
 3068:         case 249:
 3069:           buf.append("\u0306");
 3070:           break; // breve
 3071:         case 250:
 3072:           buf.append("\u1e25");
 3073:           break; // h underdot
 3074:         case 251:
 3075:           buf.append("\u012d");
 3076:           break; // i breve
 3077:         case 252:
 3078:           buf.append("\u0327");
 3079:           break; // cedilla
 3080:         case 253:
 3081:           buf.append("\u030b");
 3082:           break; // hungarumlaut
 3083:         case 254:
 3084:           buf.append("\u0328");
 3085:           break; // ogonek
 3086:         case 255:
 3087:           buf.append("\u030c");
 3088:           break; // caron
 3089:         case 130:
 3090:           buf.append("\u012b\u0303");
 3091:           break; // imacron tilde
 3092:         case 165:
 3093:           buf.append("\u1e5d");
 3094:           break; // runderdot macron
 3095:         case 170:
 3096:           buf.append("\u016b\0306");
 3097:           break; // umacron breve
 3098:         case 178:
 3099:           buf.append("\u0101\u0301");
 3100:           break; // amacron acute
 3101:         case 179:
 3102:           buf.append("\u016b\u0301");
 3103:           break; // umacron acute
 3104:         case 184:
 3105:           buf.append("\u0113\u0301");
 3106:           break; // emacron acute
 3107:         case 187:
 3108:           buf.append("\u0113\u0300");
 3109:           break; // emacron breve
 3110:         case 188:
 3111:           buf.append("\u014d\u0300");
 3112:           break; // omacron breve
 3113:         case 193:
 3114:           buf.append("\u0101\u0306");
 3115:           break; // amacron breve
 3116:         case 198:
 3117:           buf.append("\u0101\u0303");
 3118:           break; // amacron tilde
 3119:         case 199:
 3120:           buf.append("\u012b\u0301");
 3121:           break; // imacron acute
 3122:         case 218:
 3123:           buf.append("\u1e00");
 3124:           break; // runderdotmacron acute
 3125:         case 225:
 3126:           buf.append("\u1e5b\u0301");
 3127:           break; // runderdot acute
 3128:         case 228:
 3129:           buf.append("\u012b\u0306");
 3130:           break; // imacron breve
 3131:         case 240:
 3132:           buf.append("\u016b\u0303");
 3133:           break; // umacron tilde
 3134:         default:
 3135:           buf.append(c);
 3136:           break;
 3137:       }
 3138:     }
 3139:     return buf.toString();
 3140:   }
 3141: 
 3142:   static public String normanToUnicodeNew(String inp)
 3143:   {
 3144:     StringBuffer buf = new StringBuffer();
 3145:     for (int i = 0; i < inp.length(); i++)
 3146:     {
 3147:       char c = inp.charAt(i);
 3148:       switch (c)
 3149:       {
 3150:         case 1:
 3151:           buf.append("\u00d0");
 3152:           break; // Eth
 3153:         case 2:
 3154:           buf.append("\u00f0");
 3155:           break; // eth
 3156:         case 3:
 3157:           buf.append("\u0141");
 3158:           break; // Lslash
 3159:         case 4:
 3160:           buf.append("\u0142");
 3161:           break; // lslash
 3162:         case 5:
 3163:           buf.append("\u0160");
 3164:           break; // S caron
 3165:         case 6:
 3166:           buf.append("\u0161");
 3167:           break; // s caron
 3168:         case 7:
 3169:           buf.append("\u00dd");
 3170:           break; // Y acute
 3171:         case 8:
 3172:           buf.append("\u00fd");
 3173:           break; // y acute
 3174:         case 11:
 3175:           buf.append("\u00de");
 3176:           break; // Thorn
 3177:         case 12:
 3178:           buf.append("\u00fe");
 3179:           break; // thorn
 3180:         case 14:
 3181:           buf.append("\u017d");
 3182:           break; // Z caron
 3183:         case 15:
 3184:           buf.append("\u017e");
 3185:           break; // z caron
 3186:         case 17:
 3187:           buf.append("\u0073");
 3188:           break; // asciitilde
 3189:         case 18:
 3190:           buf.append("j\u0305");
 3191:           break; // j macron [does a single char exist?]
 3192:         case 19:
 3193:           buf.append("^");
 3194:           break; // circumflex
 3195:         case 20:
 3196:           buf.append("\u0303");
 3197:           break; // tilde
 3198:         case 21:
 3199:           buf.append("\u00bd");
 3200:           break; // onehalf
 3201:         case 22:
 3202:           buf.append("\u00bc");
 3203:           break; // onequarter
 3204:         case 23:
 3205:           buf.append("\u00b9");
 3206:           break; // onesuperior
 3207:         case 24:
 3208:           buf.append("\u00be");
 3209:           break; // threequarters
 3210:         case 25:
 3211:           buf.append("\u00b3");
 3212:           break; // threesuperior
 3213:         case 26:
 3214:           buf.append("\u00b2");
 3215:           break; // twosuperior
 3216:         case 27:
 3217:           buf.append("\u00a6");
 3218:           break; // brokenbar
 3219:         case 28:
 3220:           buf.append("-");
 3221:           break; // minus
 3222:         case 29:
 3223:           buf.append("\u00d7");
 3224:           break; // multiply
 3225:         case 39:
 3226:           buf.append("'");
 3227:           break; // quotesingle
 3228:         case 94:
 3229:           buf.append("\u0302");
 3230:           break; // circumflex
 3231:         case 96:
 3232:           buf.append("\u0300");
 3233:           break; // grave
 3234:         case 196:
 3235:           buf.append("\u00c4");
 3236:           break; // A dieresis
 3237:         case 197:
 3238:           buf.append("\u00c5");
 3239:           break; // A ring
 3240:         case 201:
 3241:           buf.append("\u00c9");
 3242:           break; // E acute
 3243:         case 209:
 3244:           buf.append("\u00d1");
 3245:           break; // N tilde
 3246:         case 214:
 3247:           buf.append("\u00d6");
 3248:           break; // O dieresis
 3249:         case 220:
 3250:           buf.append("\u00dc");
 3251:           break; // U dieresis
 3252:         case 225:
 3253:           buf.append("\u00e1");
 3254:           break; // a acute
 3255:         case 224:
 3256:           buf.append("\u00e0");
 3257:           break; // a grave
 3258:         case 226:
 3259:           buf.append("\u00e2");
 3260:           break; // a circumflex
 3261:         case 228:
 3262:           buf.append("\u00e4");
 3263:           break; // a dieresis
 3264:         case 227:
 3265:           buf.append("\u00e3");
 3266:           break; // a tilde
 3267:         case 229:
 3268:           buf.append("\u0101");
 3269:           break; // a macron
 3270:         case 231:
 3271:           buf.append("\u00e7");
 3272:           break; // c cedilla
 3273:         case 233:
 3274:           buf.append("\u00e9");
 3275:           break; // e acute
 3276:         case 232:
 3277:           buf.append("\u00e8");
 3278:           break; // e grave
 3279:         case 234:
 3280:           buf.append("\u00ea");
 3281:           break; // e circumflex
 3282:         case 235:
 3283:           buf.append("\u00eb");
 3284:           break; // e dieresis
 3285:         case 237:
 3286:           buf.append("\u00ed");
 3287:           break; // i acute
 3288:         case 236:
 3289:           buf.append("\u00ec");
 3290:           break; // i grave
 3291:         case 238:
 3292:           buf.append("\u00ee");
 3293:           break; // i circumflex
 3294:         case 239:
 3295:           buf.append("\u00ef");
 3296:           break; // i dieresis
 3297:         case 241:
 3298:           buf.append("\u00f1");
 3299:           break; // n tilde
 3300:         case 243:
 3301:           buf.append("\u00f3");
 3302:           break; // o acute
 3303:         case 242:
 3304:           buf.append("\u00f2");
 3305:           break; // o grave
 3306:         case 244:
 3307:           buf.append("\u00f4");
 3308:           break; // o circumflex
 3309:         case 246:
 3310:           buf.append("\u00f6");
 3311:           break; // o dieresis
 3312:         case 245:
 3313:           buf.append("\u00f5");
 3314:           break; // o tilde
 3315:         case 250:
 3316:           buf.append("\u00fa");
 3317:           break; // u acute
 3318:         case 249:
 3319:           buf.append("\u00f9");
 3320:           break; // u grave
 3321:         case 251:
 3322:           buf.append("\u00fb");
 3323:           break; // u circumflex
 3324:         case 252:
 3325:           buf.append("\u00fc");
 3326:           break; // u dieresis
 3327:         case 8224:
 3328:           buf.append("\u1e6d");
 3329:           break; // t underdot
 3330:         case 176:
 3331:           buf.append("\u00b0");
 3332:           break; // degree
 3333:         case 162:
 3334:           buf.append("\u1ebd");
 3335:           break; // e tilde
 3336:         case 163:
 3337:           buf.append("\u00a3");
 3338:           break; // sterling
 3339:         case 167:
 3340:           buf.append("\u00a7");
 3341:           break; // section
 3342:         case 182:
 3343:           buf.append("\u00b6");
 3344:           break; // paragraph
 3345:         case 223:
 3346:           buf.append("\u015b");
 3347:           break; // s acute
 3348:         case 174:
 3349:           buf.append("\u1e5b");
 3350:           break; // r underdot
 3351:         case 169:
 3352:           buf.append("\u1e45");
 3353:           break; // n overdot
 3354:         case 180:
 3355:           buf.append("\u0301");
 3356:           break; // acute
 3357:         case 168:
 3358:           buf.append("\u0308");
 3359:           break; // dieresis
 3360:         case 8800:
 3361:           buf.append("\u1e6d");
 3362:           break; // t underdot
 3363:         case 198:
 3364:           buf.append("\u00c6");
 3365:           break; // AE
 3366:         case 216:
 3367:           buf.append("\u014d");
 3368:           break; // o macron
 3369:         case 8734:
 3370:           buf.append("\u0129");
 3371:           break; // i tilde
 3372:         case 177:
 3373:           buf.append("\u00b1");
 3374:           break; // plusminus
 3375:         case 165:
 3376:           buf.append("\u012b");
 3377:           break; // i macron
 3378:         case 181:
 3379:           buf.append("\u1e43");
 3380:           break; // m underdot
 3381:         case 8706:
 3382:           buf.append("\u1e0d");
 3383:           break; // d underdot
 3384:         case 8721:
 3385:           buf.append("\u1e63");
 3386:           break; // s underdot
 3387:         case 960:
 3388:           buf.append("\u017a");
 3389:           break; // z acute
 3390:         case 8747:
 3391:           buf.append("\u1e45");
 3392:           break; // n overdot
 3393:         case 937:
 3394:           buf.append("\u0169");
 3395:           break; // u tilde
 3396:         case 230:
 3397:           buf.append("\u00e6");
 3398:           break; // ae
 3399:         case 248:
 3400:           buf.append("\u00f8");
 3401:           break; // oslash
 3402:         case 191:
 3403:           buf.append("\u0304\u0306");
 3404:           break; // macron breve
 3405:         case 172:
 3406:           buf.append("\u1e37");
 3407:           break; // 
 3408:         case 8730:
 3409:           buf.append("j\u0305");
 3410:           break; // j macron [does a single char exist?]
 3411:         case 402:
 3412:           buf.append("\u0103");
 3413:           break; // a breve
 3414:         case 8776:
 3415:           buf.append("\u016d");
 3416:           break; // u breve
 3417:         case 187:
 3418:           buf.append("\u1e42");
 3419:           break; // M underdot
 3420:         case 8230:
 3421:           buf.append("\u2026");
 3422:           break; // ellipsis
 3423:         case 192:
 3424:           buf.append("\u00c0");
 3425:           break; // A grave
 3426:         case 195:
 3427:           buf.append("\u00c3");
 3428:           break; // A tilde
 3429:         case 213:
 3430:           buf.append("\u00d5");
 3431:           break; // O tilde
 3432:         case 338:
 3433:           buf.append("m\u0306");
 3434:           break; // m breve
 3435:         case 339:
 3436:           buf.append("\u0153");
 3437:           break; // oe
 3438:         case 8211:
 3439:           buf.append("\u2013");
 3440:           break; // endash
 3441:         case 8212:
 3442:           buf.append("\u2014");
 3443:           break; // emdash
 3444:         case 8220:
 3445:           buf.append("\u201c");
 3446:           break; // quotedblleft
 3447:         case 8221:
 3448:           buf.append("\u201d");
 3449:           break; // quotedblright
 3450:         case 8216:
 3451:           buf.append("\u2018");
 3452:           break; // quoteleft
 3453:         case 8217:
 3454:           buf.append("\u2019");
 3455:           break; // quoteright
 3456:         case 247:
 3457:           buf.append("\u1e37");
 3458:           break; // l underring [actually underdot]
 3459:         case 9674:
 3460:           buf.append("\u1e41");
 3461:           break; // m overdot
 3462:         case 255:
 3463:           buf.append("n\u0306");
 3464:           break; // n breve
 3465:         case 376:
 3466:           buf.append("\u00d7");
 3467:           break; // multiply
 3468:         case 8364:
 3469:           buf.append("\u1e5b");
 3470:           break; // r underring [actually underdot]
 3471:         case 8249:
 3472:           buf.append("\u1e44");
 3473:           break; // N overdot
 3474:         case 8250:
 3475:           buf.append("\u1e62");
 3476:           break; // S underdot
 3477:         case 64257:
 3478:           buf.append("\u1e24");
 3479:           break; // H underdot
 3480:         case 64258:
 3481:           buf.append("\u1e0c");
 3482:           break; // D underdot
 3483:         case 8225:
 3484:           buf.append("\u2021");
 3485:           break; // daggerdbl
 3486:         case 8218:
 3487:           buf.append("\u1e36");
 3488:           break; // L underdot
 3489:         case 8222:
 3490:           buf.append("\u0113");
 3491:           break; // e macron
 3492:         case 194:
 3493:           buf.append("\u1e5f");
 3494:           break; // r underbar
 3495:         case 202:
 3496:           buf.append("r\u0324");
 3497:           break; // r underdieresis
 3498:         case 193:
 3499:           buf.append("\u012a");
 3500:           break; // I macron
 3501:         case 203:
 3502:           buf.append("\u016b");
 3503:           break; // u macron
 3504:         case 200:
 3505:           buf.append("\u1e6c");
 3506:           break; // T underdot
 3507:         case 205:
 3508:           buf.append("\u1e64");
 3509:           break; // S acute
 3510:         case 206:
 3511:           buf.append("\u2020");
 3512:           break; // dagger
 3513:         case 207:
 3514:           buf.append("\u0115");
 3515:           break; // e breve
 3516:         case 204:
 3517:           buf.append("\u014f");
 3518:           break; // o breve
 3519:         case 211:
 3520:           buf.append("\u0100");
 3521:           break; // A macron
 3522:         case 212:
 3523:           buf.append("\u1e46");
 3524:           break; // N underdot
 3525:         case 210:
 3526:           buf.append("\u1e3b");
 3527:           break; // l underbar
 3528:         case 218:
 3529:           buf.append("\u016a");
 3530:           break; // U macron
 3531:         case 219:
 3532:           buf.append("\u0179");
 3533:           break; // Z acute
 3534:         case 217:
 3535:           buf.append("\u1e5a");
 3536:           break; // R underdot
 3537:         case 305:
 3538:           buf.append("\u0131");
 3539:           break; // dotlessi
 3540:         case 710:
 3541:           buf.append("\u1e47");
 3542:           break; // n underdot
 3543:         case 732:
 3544:           buf.append("\u1e49");
 3545:           break; // n underbar
 3546:         case 175:
 3547:           buf.append("\u0304");
 3548:           break; // macron
 3549:         case 728:
 3550:           buf.append("\u0306");
 3551:           break; // breve
 3552:         case 729:
 3553:           buf.append("\u1e25");
 3554:           break; // h underdot
 3555:         case 730:
 3556:           buf.append("\u012d");
 3557:           break; // i breve
 3558:         case 184:
 3559:           buf.append("\u0327");
 3560:           break; // cedilla
 3561:         case 733:
 3562:           buf.append("\u030b");
 3563:           break; // hungarumlaut
 3564:         case 731:
 3565:           buf.append("\u0328");
 3566:           break; // ogonek
 3567:         case 711:
 3568:           buf.append("\u030c");
 3569:           break; // caron
 3570:         case 199:
 3571:           buf.append("\u012b\u0303");
 3572:           break; // imacron tilde
 3573:         case 8226:
 3574:           buf.append("\u1e5d");
 3575:           break; // runderdot macron
 3576:         case 8482:
 3577:           buf.append("\u016b\0306");
 3578:           break; // umacron breve
 3579:         case 8804:
 3580:           buf.append("\u0101\u0301");
 3581:           break; // amacron acute
 3582:         case 8805:
 3583:           buf.append("\u016b\u0301");
 3584:           break; // umacron acute
 3585:         case 8719:
 3586:           buf.append("\u0113\u0301");
 3587:           break; // emacron acute
 3588:         case 170:
 3589:           buf.append("\u0113\u0300");
 3590:           break; // emacron breve
 3591:         case 186:
 3592:           buf.append("\u014d\u0300");
 3593:           break; // omacron breve
 3594:         case 161:
 3595:           buf.append("\u0101\u0306");
 3596:           break; // amacron breve
 3597:         case 8710:
 3598:           buf.append("\u0101\u0303");
 3599:           break; // amacron tilde
 3600:         case 171:
 3601:           buf.append("\u012b\u0301");
 3602:           break; // imacron acute
 3603:         case 8260:
 3604:           buf.append("\u1e00");
 3605:           break; // runderdotmacron acute
 3606:         case 183:
 3607:           buf.append("\u1e5b\u0301");
 3608:           break; // runderdot acute
 3609:         case 8240:
 3610:           buf.append("\u012b\u0306");
 3611:           break; // imacron breve
 3612:         case 63743:
 3613:           buf.append("\u016b\u0303");
 3614:           break; // umacron tilde
 3615:         default:
 3616:           buf.append(c);
 3617:           break;
 3618:       }
 3619:     }
 3620:     return buf.toString();
 3621:   }
 3622: 
 3623:   public static ConversionProperties getFieldNamesAndDestTableName(String create, String query,
 3624:       String tableName)
 3625:   {
 3626:     String[] fieldNames = null;
 3627:     String destTableName = null;
 3628:     // determine destTableName from createStatement or from source table
 3629:     // name
 3630:     if (!create.equals(""))
 3631:     {
 3632:       int fromIndex = create.toLowerCase().indexOf("table") + 5;
 3633:       int toIndex = create.indexOf("(");
 3634:       int endIndex = create.indexOf(")", toIndex);
 3635: 
 3636:       destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "").trim();
 3637:       System.out.println("destTable " + destTableName);
 3638:       // retrieve field_names from select statement
 3639:       // TODO problem with different fieldNames in create statement will
 3640:       // overwrite them
 3641:       if (query.indexOf("*") < 0)
 3642:       {
 3643:         int selectEndIndex = query.indexOf("from");
 3644:         StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");
 3645:         int numFields = tokenizer.countTokens();
 3646:         fieldNames = new String[numFields];
 3647:         int fieldIndex = 0;
 3648:         while (tokenizer.hasMoreTokens())
 3649:         {
 3650:           String fieldName = tokenizer.nextToken().trim();
 3651:           fieldNames[fieldIndex] = convertText(fieldName);
 3652:           System.out.println(fieldNames[fieldIndex]);
 3653:           fieldIndex++;
 3654:         }
 3655: 
 3656:       } else
 3657:       {
 3658:         // use create statement for field names
 3659:         StringTokenizer tokenizer = new StringTokenizer(create.substring(toIndex + 1, endIndex),
 3660:             ",");
 3661:         int numFields = tokenizer.countTokens();
 3662:         fieldNames = new String[numFields];
 3663:         int fieldIndex = 0;
 3664:         while (tokenizer.hasMoreTokens())
 3665:         {
 3666:           String fieldName = tokenizer.nextToken().trim();
 3667:           int index = fieldName.lastIndexOf(" ");
 3668:           fieldNames[fieldIndex] = fieldName.substring(0, index);
 3669:           System.out.println(fieldNames[fieldIndex]);
 3670:           fieldIndex++;
 3671:         }
 3672:       }
 3673:     } else
 3674:     {
 3675:       destTableName = convertText(tableName);
 3676: 
 3677:       // retrieve field_names from select statement
 3678:       if (query.indexOf("*") < 0)
 3679:       {
 3680:         int selectEndIndex = query.indexOf("from");
 3681:         StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");
 3682:         int numFields = tokenizer.countTokens();
 3683:         fieldNames = new String[numFields];
 3684:         int fieldIndex = 0;
 3685:         while (tokenizer.hasMoreTokens())
 3686:         {
 3687:           String fieldName = tokenizer.nextToken().trim();
 3688:           fieldNames[fieldIndex] = beanDest.getQC() + convertText(fieldName) + beanDest.getQC();
 3689:           // System.out.println("field "+ fieldNames[fieldIndex]);
 3690:           fieldIndex++;
 3691:         }
 3692: 
 3693:       } else
 3694:       {
 3695:         Vector fieldNamesVec = bean.getColumnNames();
 3696:         fieldNames = new String[fieldNamesVec.size()];
 3697:         int fieldIndex = -1;
 3698:         for (Iterator iter = fieldNamesVec.iterator(); iter.hasNext();)
 3699:         {
 3700:           String element = (String) iter.next();
 3701:           fieldNames[++fieldIndex] = beanDest.getQC() + convertText(element) + beanDest.getQC();
 3702:           // System.out.println("field " + fieldNames[fieldIndex]);
 3703:         }
 3704:       }
 3705:     }
 3706:     return new ConversionProperties(destTableName, fieldNames);
 3707:   }
 3708: 
 3709:   /**
 3710:    * creates an insert into statement for the specified table and given field
 3711:    * names
 3712:    * 
 3713:    * @param destTableName
 3714:    * @param fieldNames
 3715:    * @return
 3716:    */
 3717:   public static StringBuffer createInsertCommand(String destTableName, String[] fieldNames)
 3718:   {
 3719:     StringBuffer command = new StringBuffer();
 3720:     command.append("INSERT  INTO ");
 3721:     command.append(beanDest.getQC());
 3722:     command.append(destTableName); // convertText((String)
 3723:     // names.get(tbIndex)));
 3724:     command.append(beanDest.getQC());
 3725:     command.append(" (");
 3726:     for (int i = 0; i < fieldNames.length; i++)
 3727:     {
 3728:       command.append(fieldNames[i]);
 3729:       if (i < fieldNames.length - 1)
 3730:         command.append(",");
 3731:     }
 3732:     command.append(") ");
 3733: 
 3734:     command.append(" values ( ");
 3735:     // add a question marks for every field
 3736:     for (int i = 0; i < fieldNames.length - 1; ++i)
 3737:       command.append("?,");
 3738:     command.append("?)");
 3739:     return command;
 3740:   }
 3741: 
 3742:   public static StringBuffer createUpdateCommand(String destTableName, String[] fieldNames,
 3743:       String id)
 3744:   {
 3745:     StringBuffer command = new StringBuffer();
 3746: 
 3747:     command.append("UPDATE ");
 3748:     command.append(beanDest.getQC());
 3749:     command.append(destTableName);
 3750:     command.append(beanDest.getQC());
 3751:     command.append(" SET  ");
 3752: 
 3753:     int size = bean.getColumnNames().size();
 3754:     for (int i = 0; i < size - 1; ++i)
 3755:       command.append(fieldNames[i] + " = ? ,");
 3756:     command.append(fieldNames[size - 1] + " = ? ");
 3757:     command.append("WHERE " + id + " =  ?");
 3758:     return command;
 3759:   }
 3760: 
 3761:   public static StringBuffer createDeleteCommand(String destTableName, String idField)
 3762:   {
 3763:     StringBuffer command = new StringBuffer();
 3764: 
 3765:     command.append("DELETE FROM");
 3766:     command.append(beanDest.getQC());
 3767:     command.append(destTableName);
 3768:     // command.append(convertText((String) names.get(tbIndex)));
 3769:     command.append(beanDest.getQC());
 3770:     command.append("WHERE " + idField + " =  ?");
 3771:     return command;
 3772:   }
 3773: 
 3774:   public void makeTest(String table, String idField, String tempQuery) throws Exception
 3775:   {
 3776:     int counter = 0;
 3777: 
 3778:     // ****** test code *****
 3779: 
 3780:     bean.getConnection();
 3781:     ResultSet resultSet = null;
 3782:     String lastResult = "P227634.11";// "P227625.79554";//"P227625.77391";//"P116034.970998";
 3783:     String myQuery = "select " + bean.getQC() + idField + bean.getQC() + ",serial " + " from "
 3784:         + bean.getQC() + table + bean.getQC();
 3785:     System.out.println("Query is now " + myQuery);
 3786:     JDialog statusDialog = new JDialog();
 3787:     statusDialog.setTitle("Status Information");
 3788:     JLabel status = new JLabel("actual DataSet : ");
 3789:     JLabel status2 = new JLabel(Integer.toString(++counter));
 3790:     JLabel status3 = new JLabel(lastResult);
 3791: 
 3792:     JPanel statusPanel = new JPanel();
 3793:     JPanel statusPanel2 = new JPanel();
 3794:     statusPanel.add(status);
 3795:     statusPanel.add(status2);
 3796:     statusPanel2.add(status3);
 3797:     statusDialog.getContentPane().add(statusPanel, "North");
 3798:     statusDialog.getContentPane().add(statusPanel2, "Center");
 3799:     statusDialog.setLocation(400, 500);
 3800:     statusDialog.setSize(300, 150);
 3801:     statusDialog.setVisible(true);
 3802:     while (true)
 3803:     {
 3804:       if (!statusDialog.isVisible())
 3805:         statusDialog.setVisible(true);
 3806:       tempQuery = myQuery + " where " + bean.getQC() + idField + bean.getQC() + ">'" + lastResult
 3807:           + "'";
 3808:       resultSet = bean.makeQuery(tempQuery, 1);
 3809:       if (resultSet == null)
 3810:       {
 3811:         System.out.println("lastResult was " + lastResult + " counter was " + counter);
 3812:         break;
 3813:       } else
 3814:       {
 3815:         resultSet.next();
 3816:         lastResult = resultSet.getString(1);
 3817:         counter++;
 3818:         status2.setText(Integer.toString(counter));
 3819:         status3.setText(lastResult + " " + resultSet.getString(2));
 3820:         if (counter % 100 == 0)
 3821:         {
 3822:           System.out.println("actual Result was " + lastResult + " counter was " + counter);
 3823:           // break;
 3824:         }
 3825:       }
 3826:       resultSet = null;
 3827:     }
 3828:     System.exit(0);
 3829: 
 3830:     // ****** end Test ******
 3831: 
 3832:   }
 3833: }

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