Annotation of FM2SQL/Convert.java, revision 1.85

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

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