File:  [Repository] / FM2SQL / Attic / Convert.java
Revision 1.11: download - view: text, annotated - select for diffs - revision graph
Thu Jan 15 13:32:09 2004 UTC (20 years, 5 months ago) by rogo
Branches: MAIN
CVS tags: HEAD
*** empty log message ***

    1: 
    2: 
    3: import java.util.*;
    4: import java.sql.*;
    5: import java.awt.Cursor;
    6: import java.io.BufferedReader;
    7: import java.io.BufferedWriter;
    8: import java.io.File;
    9: import java.io.FileInputStream;
   10: import java.io.FileNotFoundException;
   11: import java.io.FileOutputStream;
   12: import java.io.InputStreamReader;
   13: import java.io.OutputStreamWriter;
   14: import java.io.PrintStream;
   15: import java.io.UnsupportedEncodingException;
   16: 
   17: import com.exploringxml.xml.Node;
   18: import com.exploringxml.xml.Xparse;
   19: 
   20: class Convert 
   21: {
   22: 	static DBBean bean = new DBBean();
   23: 	static DBBean beanDest = new DBBean();
   24: 
   25:   static String user ="",passwd="e1nste1n";
   26:   static String userDest="postgres",passwdDest="rogo";
   27:   static boolean batchRun=false;
   28:   static Vector databases = new Vector();
   29:   
   30:   public static void main(String args[])
   31:   {
   32: /*    try
   33:     {
   34:       //byte[] b = "ö".getBytes("UTF-8");
   35:     //  System.out.println("QueryString " +b[0]+" "+b[1]+(new String(b).getBytes()[0])+" "+new String(b).getBytes()[1]);
   36:     //System.out.println(new String(b,"UTF-8"));
   37:     } catch (UnsupportedEncodingException e)
   38:     {
   39:       e.printStackTrace();
   40:     }*/
   41:     FileOutputStream file = null;
   42:     if(args.length!=1)
   43:     {
   44:      System.out.println("Usage: java Convert <xml config file>");
   45:      System.exit(-1);
   46:     }
   47:     if(!(new File(args[0]).exists())) System.exit(0);
   48:     try
   49:     {
   50:       file = new FileOutputStream("./log.txt");
   51:     } catch (FileNotFoundException e1)
   52:     {
   53:       e1.printStackTrace();
   54:     } 
   55:    PrintStream stream= new PrintStream(file);
   56:    System.setOut(stream);
   57:    System.setErr(stream);
   58:    readXMLFile(args[0]);
   59:    System.out.println("Finished!");
   60:     //convert("jdbc:fmpro:http://141.14.237.74:8050","jdbc:postgresql://erebos/test",null,null);
   61:   }
   62:   public static void  convertBatch(DBBean source,DBBean destination,Vector names,Vector layouts,Vector selects,Vector creates,int mode) throws Exception
   63:   {
   64:    /*FM2SQL.ProgressDialog dialog = new  FM2SQL.ProgressDialog(FM2SQL.fmInstance);
   65:    dialog.setTitle("Conversion running ...");
   66:    dialog.title.setText("Getting table data ...");
   67:    dialog.setLocation(500,500); 
   68:    dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
   69:   */
   70:   // FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
   71:    //dialog.thread=Thread.currentThread();
   72:    bean=source;
   73:    beanDest = destination;
   74:  /*  // setting user and passwd 
   75:    bean.setUserAndPasswd(user,passwd);
   76:   // setting user and passwd 
   77:   beanDest.setUserAndPasswd(userDest,passwdDest);
   78:   */
   79:   //dialog.setSize(400,250);
   80:    StringBuffer command= null;
   81:     try
   82:     {
   83:       //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");    
   84:     //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
   85:       bean.setConnection(source.url);
   86:       if(names==null)
   87:        names=bean.getTableNames();
   88:       //Collections.sort(names);
   89:         int tbIndex = 1;
   90:      
   91:      // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
   92:       for(tbIndex=0;tbIndex<names.size();++tbIndex)
   93:       {
   94:         Vector[] result = null;
   95:        try {
   96:      String query = "select * from " + bean.getQC() +names.get(tbIndex).toString() + bean.getQC();
   97:      String layout=(layouts.isEmpty()) ? "":layouts.get(tbIndex).toString(); 
   98:      query = (selects!=null) ? selects.get(tbIndex).toString():query;
   99:         //if  vectors[1].get(i) != null)
  100:         if (!layout.equals(""))
  101:         {
  102:           System.out.println("before "+query+" table"+names.get(tbIndex));
  103:           layout = " layout " + bean.getQC() + layout + bean.getQC();
  104:           String name = names.get(tbIndex).toString();
  105:           StringBuffer queryLayout = new StringBuffer(query);
  106:           queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
  107:           query = queryLayout.toString();
  108:           System.out.println("added layout " + query);
  109:         
  110:         }
  111:         //  if ( layout!= "")
  112:         //   query += " layout " + bean.getQC() + layout + bean.getQC();
  113:      /*   dialog.title.setText("Getting table data ...");
  114:         dialog.table.setText(names.get(tbIndex).toString());
  115:         dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
  116:         dialog.show();
  117:       */
  118:           System.out.println(" performing query "+query);
  119:           //result = bean.getQueryData(query, null, 0);
  120:         bean.getConnection();
  121:         bean.makeQuery(query,0);
  122:       } catch (Exception e)
  123:         {
  124:           System.out.println(e.getMessage());
  125:           e.printStackTrace();
  126:           continue;
  127:         }
  128:         //beanDest.setConnection("jdbc:postgresql://erebos/test3");
  129:         beanDest.setConnection(destination.url);
  130: 
  131:         Statement stm = beanDest.getConnection().createStatement();
  132: 
  133:         Vector tables = beanDest.getTableNames();
  134:      //   Collections.sort(tables);
  135:         System.out.println("converting table "+names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames()); 
  136:         tables = beanDest.getTableNames();
  137:         // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
  138:         stm = beanDest.getConnection().createStatement();
  139:         // System.exit(0);
  140:       if(mode==Convert.DataBase.CONVERT_MODE) 
  141:       {
  142:         if(tables.indexOf(names.get(tbIndex))>=0) 
  143:         {
  144:          stm.executeUpdate("drop table "+beanDest.getQC()+names.get(tbIndex)+beanDest.getQC());
  145:         tables.remove((String)names.get(tbIndex));
  146:         System.out.println("dropped table "+ names.get(tbIndex));
  147:         }
  148:         else
  149:         if(tables.indexOf(convertText(names.get(tbIndex).toString()))>=0) 
  150:         {
  151:          stm.executeUpdate("drop table "+beanDest.getQC()+convertText((String)names.get(tbIndex))+beanDest.getQC());
  152:         tables.remove(convertText((String)names.get(tbIndex)));
  153:         System.out.println("dropped table "+ names.get(tbIndex));
  154:         }
  155:  
  156:         if(tables.indexOf(names.get(tbIndex))<0&&tables.indexOf(convertText(names.get(tbIndex).toString()))<0) 
  157:         {
  158:           if(creates.get(tbIndex).equals("")||creates.get(tbIndex).toString().toLowerCase().indexOf("create")<0)
  159:           {
  160:             System.out.println("Warning empty or invalid create statement - creating one for you\n");
  161:         
  162:           command = new StringBuffer(50);
  163:           command.append("CREATE TABLE ");
  164:           command.append(beanDest.getQC());
  165:           command.append(convertText((String)names.get(tbIndex)));
  166:           command.append(beanDest.getQC());
  167:           command.append("(");
  168:            String type = null;
  169:            Vector columnNames=bean.getColumnNames();
  170:           for(int i=0;i<columnNames.size()-1;++i)
  171:           {
  172:            type = bean.metaData.getColumnTypeName(i+1);
  173:          //   System.out.println(i+" "+result[1].get(i)+" "+type);
  174:           type = (type.equals("NUMBER")) ?"INT4":type;
  175:           type = (type.equals("CONTAINER")) ?"TEXT":type;
  176: 
  177:           command.append(beanDest.getQC() + convertText((String) columnNames.get(i)) + beanDest.getQC() + " " + type + ", ");
  178:         }
  179:           type = bean.metaData.getColumnTypeName(columnNames.size());
  180:           type = (type.equals("NUMBER")) ? "INT4" : type;
  181:           type = (type.equals("CONTAINER")) ? "TEXT" : type;
  182:           command.append(beanDest.getQC() + convertText((String) columnNames.get(columnNames.size() - 1)) + beanDest.getQC() + " " + type);
  183:           command.append(" )");
  184:         }
  185:          else
  186:          command=new StringBuffer().append(creates.get(tbIndex).toString());
  187:          
  188:            System.out.println(command);
  189:           //  System.exit(0);
  190:           //command.append(DBBean.getQC());   
  191:           stm.executeUpdate(command.toString());
  192:         
  193:       }
  194:     }
  195:     // System.out.println(names);
  196:     /*    
  197:     if (idVal == "")
  198:           stm.executeUpdate("INSERT  INTO " + DBBean.getQC() + box.getSelectedItem() + DBBean.getQC() + " (" + DBBean.getQC() + columnName + DBBean.getQC() + ")  VALUES ('" + value + "') ");
  199:         else
  200:           stm.executeUpdate("UPDATE   " + DBBean.getQC() + box.getSelectedItem() + DBBean.getQC() + " SET  " + DBBean.getQC() + columnName + DBBean.getQC() + "='" + value + "' WHERE ID='" + idVal + "' ");
  201:       */
  202: 
  203:     // Vector[] result2=beanDest.getQueryData(names.get(tbIndex).toString());
  204:     // print results to screen
  205:   /*  for (int i = 0; i < result[1].size(); ++i)
  206:     {
  207:       //print Header
  208:       // System.out.println(" "+result[1].get(i)+" "+result2[1].get(i)+" "+i);
  209:     }*/
  210:     // System.out.println();
  211:     //dialog.title.setText("Writing table data ...");
  212:     Vector row = null;
  213:     //for (int j = 0; j < result[0].size(); ++j)
  214:     command = new StringBuffer();
  215: 
  216:     command.append("INSERT  INTO ");
  217:     command.append(beanDest.getQC());
  218:     command.append(convertText((String) names.get(tbIndex)));
  219:     command.append(beanDest.getQC());
  220:     command.append(" values ( ");
  221: 
  222:     for (int i = 0; i < bean.getColumnNames().size() - 1; ++i)
  223:       command.append("?,");
  224:     command.append("?)");
  225:     PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
  226:     System.out.println(command);
  227:     while((row=bean.getNextRow())!=null)
  228:     {
  229: /*      //Vector row = (Vector) result[0].get(j);
  230:       command = new StringBuffer();
  231: 
  232:         command.append("INSERT  INTO ");
  233:         command.append(beanDest.getQC());
  234:         command.append(convertText((String) names.get(tbIndex)));
  235:         command.append(beanDest.getQC());
  236:         command.append(" values ( ");
  237:    */
  238:              //print rows
  239:      Object obj = null;
  240:      /*
  241:      for(int k=0;k<row.size()-1;++k)
  242:      {
  243:         obj = row.get(k);
  244:         //System.out.println("row "+obj+" "+k);
  245:        if(obj!=null&&!(obj instanceof ArrayList))
  246:        command.append("'"+convertUml(new String(obj.toString().getBytes("UTF-8"),"UTF-8"))+"',"); 
  247:        else if(obj!=null&&   obj instanceof ArrayList)
  248:        command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',"); 
  249:        else command.append("NULL,");
  250:      }
  251:      obj = row.get(row.size() - 1);
  252:      if (obj != null && !(obj instanceof ArrayList))
  253:      command.append("'"+convertUml(new String(obj.toString().getBytes("UTF-8"),"UTF-8"))+"')"); 
  254:       else
  255:       if(obj!=null&&   obj instanceof ArrayList)
  256:        command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')");         //command.append(obj.toString()+")");
  257:        else command.append("NULL)");
  258:        //command.append("'"+row.get(row.size()-1)+"')"); 
  259:      //command.append(" )");
  260:      //  for(int k=0;k<row.size();++k)
  261:         
  262:     // System.out.println();
  263:        //   System.out.println(command+" "+j+" "+row.size()+" "+  ((Vector)result2[0].get(j)).size());
  264:            // System.out.println(command);
  265:             stm.executeUpdate(command.toString());
  266:   // dialog.progress.setValue((int)(((double)(j+1)/(double)result[0].size())*100.0));
  267:   // System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j);
  268:    command = null;
  269: */
  270:     for (int k = 0; k < row.size(); ++k)
  271:     {
  272:       obj = row.get(k);
  273:       if (obj instanceof ArrayList)
  274:         obj = ((List) obj).get(0);
  275:       String str = (obj == null) ? "NULL" : obj.toString();
  276:       if (!str.equals("NULL"))
  277:         pstm.setString(k + 1, str);
  278:       else
  279:         pstm.setNull(k + 1, Types.NULL);
  280:     }
  281:     pstm.execute();
  282: 
  283: }// to for loop    
  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: 
  307: 
  308:   public static void  convert(String source,String destination,Vector names,Vector layouts,Vector selects,Vector creates,int mode) throws Exception
  309:   {
  310:    FM2SQL.ProgressDialog dialog = new  FM2SQL.ProgressDialog(FM2SQL.fmInstance);
  311:    dialog.setTitle("Conversion running ...");
  312:    dialog.title.setText("Getting table data ...");
  313:    dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x+(FM2SQL.fmInstance.getWidth()-400)/2,FM2SQL.fmInstance.getLocationOnScreen().y+(FM2SQL.fmInstance.getHeight()-250)/2); 
  314: 	 dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  315: 	 FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  316:    dialog.thread=Thread.currentThread();
  317:    // setting user and passwd 
  318:    bean.setUserAndPasswd(user,passwd);
  319:   // setting user and passwd 
  320:   beanDest.setUserAndPasswd(userDest,passwdDest);
  321:   dialog.setSize(400,250);
  322:    StringBuffer command= null;
  323:    String query = null;
  324:     try
  325:     {
  326:       //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");    
  327: 	  //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
  328:       bean.setConnection(source);
  329:       if(names==null)
  330:        names=bean.getTableNames();
  331:      // Collections.sort(names);
  332:         int tbIndex = 1;
  333:      
  334:      // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
  335:       for(tbIndex=0;tbIndex<names.size();++tbIndex)
  336:       {
  337:         Vector[] result = null;
  338:        try {
  339: 	    query = "select * from " + bean.getQC() +names.get(tbIndex).toString() + bean.getQC();
  340: 	   String layout=(layouts.isEmpty()) ? "":layouts.get(tbIndex).toString(); 
  341:      query = (selects!=null) ? selects.get(tbIndex).toString():query;
  342:         //if  vectors[1].get(i) != null)
  343:         if (layout != "")
  344:         {
  345:           layout = " layout " + bean.getQC() + layout + bean.getQC();
  346:           String name = names.get(tbIndex).toString();
  347:           StringBuffer queryLayout = new StringBuffer(query);
  348:           queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
  349:           query = queryLayout.toString();
  350:           System.out.println("added layout  "+ query);
  351:         
  352:         }
  353:         //  if ( layout!= "")
  354:         //	 query += " layout " + bean.getQC() + layout + bean.getQC();
  355:         dialog.title.setText("Getting table data ...");
  356:         dialog.table.setText(names.get(tbIndex).toString());
  357:         dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
  358:         dialog.show();
  359:         //result = bean.getQueryData(query, dialog, 0);
  360:         bean.getConnection();
  361:         bean.makeQuery(query,0);
  362:       } catch (Exception e)
  363:         {
  364:           continue;
  365:         }
  366:         //beanDest.setConnection("jdbc:postgresql://erebos/test3");
  367:         beanDest.setConnection(destination);
  368: 
  369:         Statement stm = beanDest.getConnection().createStatement();
  370: 
  371:         Vector tables = beanDest.getTableNames();
  372:        // Collections.sort(tables);
  373:         System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames()); 
  374:         tables = beanDest.getTableNames();
  375:         // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
  376:         stm = beanDest.getConnection().createStatement();
  377:         // System.exit(0);
  378:       if(mode==Convert.DataBase.CONVERT_MODE) 
  379:       {
  380:         if(tables.indexOf(names.get(tbIndex))>=0) 
  381:         {
  382:          stm.executeUpdate("drop table "+beanDest.getQC()+names.get(tbIndex)+beanDest.getQC());
  383:         tables.remove((String)names.get(tbIndex));
  384:         System.out.println("dropped table"+ names.get(tbIndex));
  385:         }
  386:         else
  387:         if(tables.indexOf(convertText(names.get(tbIndex).toString()))>=0) 
  388:         {
  389:          stm.executeUpdate("drop table "+beanDest.getQC()+convertText((String)names.get(tbIndex))+beanDest.getQC());
  390:         tables.remove(convertText((String)names.get(tbIndex)));
  391:         System.out.println("dropped table"+ names.get(tbIndex));
  392:         }
  393:  
  394: 				if(tables.indexOf(names.get(tbIndex))<0&&tables.indexOf(convertText(names.get(tbIndex).toString()))<0) 
  395:         {
  396:           if(creates.get(tbIndex).equals("")||creates.get(tbIndex).toString().toLowerCase().indexOf("create")<0)
  397:           {
  398:             System.out.println("Warning empty or invalid create statement - creating one for you\n");
  399:         
  400:           command = new StringBuffer(50);
  401:           command.append("CREATE TABLE ");
  402:           command.append(beanDest.getQC());
  403:           command.append(convertText((String)names.get(tbIndex)));
  404:           command.append(beanDest.getQC());
  405:           command.append("(");
  406:            String type = null;
  407:           Vector columnNames = bean.getColumnNames();
  408:           for(int i=0;i<columnNames.size()-1;++i)
  409:           {
  410:            type = bean.metaData.getColumnTypeName(i+1);
  411:          //   System.out.println(i+" "+result[1].get(i)+" "+type);
  412:           type = (type.equals("NUMBER")) ?"INT4":type;
  413:           type = (type.equals("CONTAINER")) ?"TEXT":type;
  414: 
  415:           command.append(beanDest.getQC() + convertText((String) columnNames.get(i)) + beanDest.getQC() + " " + type + ", ");
  416:         }
  417:           type = bean.metaData.getColumnTypeName(columnNames.size());
  418:           type = (type.equals("NUMBER")) ? "INT4" : type;
  419:           type = (type.equals("CONTAINER")) ? "TEXT" : type;
  420:           command.append(beanDest.getQC() + convertText((String) columnNames.get(columnNames.size() - 1)) + beanDest.getQC() + " " + type);
  421:           command.append(" )");
  422: 
  423:           // System.out.println(command);
  424:           //  System.exit(0);
  425:           //command.append(DBBean.getQC());   
  426:           } else
  427:           command=new StringBuffer().append(creates.get(tbIndex).toString());
  428:           stm.executeUpdate(command.toString());
  429:         
  430:       }
  431:     }
  432:     // System.out.println(names);
  433:     /*    
  434:     if (idVal == "")
  435:           stm.executeUpdate("INSERT  INTO " + DBBean.getQC() + box.getSelectedItem() + DBBean.getQC() + " (" + DBBean.getQC() + columnName + DBBean.getQC() + ")  VALUES ('" + value + "') ");
  436:         else
  437:           stm.executeUpdate("UPDATE   " + DBBean.getQC() + box.getSelectedItem() + DBBean.getQC() + " SET  " + DBBean.getQC() + columnName + DBBean.getQC() + "='" + value + "' WHERE ID='" + idVal + "' ");
  438:       */
  439: 
  440:     // Vector[] result2=beanDest.getQueryData(names.get(tbIndex).toString());
  441:     // print results to screen
  442:    /* for (int i = 0; i < result[1].size(); ++i)
  443:     {
  444:       //print Header
  445:       // System.out.println(" "+result[1].get(i)+" "+result2[1].get(i)+" "+i);
  446:     }*/
  447:     // System.out.println();
  448:     dialog.title.setText("Writing table data ...");
  449:     
  450:     //for (int j = 0; j < result[0].size(); ++j)
  451:     int j=-1;
  452:     Vector row = null;
  453:     command = new StringBuffer();
  454: 
  455:     command.append("INSERT  INTO ");
  456:     command.append(beanDest.getQC());
  457:     command.append(convertText((String) names.get(tbIndex)));
  458:     command.append(beanDest.getQC());
  459:     command.append(" values ( ");
  460:     
  461:     for(int i=0;i<bean.getColumnNames().size()-1;++i) command.append("?,");
  462:     command.append("?)");
  463:     PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
  464:     System.out.println(command);
  465:     int rowCount= bean.getRowCount(query);
  466:     while((row=bean.getNextRow())!=null)
  467:     {
  468:       j++;
  469:       // row = (Vector) result[0].get(j);
  470:    /*   command = new StringBuffer();
  471: 
  472:         command.append("INSERT  INTO ");
  473:         command.append(beanDest.getQC());
  474:         command.append(convertText((String) names.get(tbIndex)));
  475:         command.append(beanDest.getQC());
  476:         command.append(" values ( ");
  477:      */
  478:            //print rows
  479:      Object obj = null;
  480:     /* for(int k=0;k<row.size()-1;++k)
  481:      {
  482:         obj = row.get(k);
  483:         //System.out.println("row "+obj+" "+k);
  484:        if(obj!=null&&!(obj instanceof ArrayList))
  485:        command.append("'"+convertUml(obj.toString())+"',"); 
  486:        else if(obj!=null&&   obj instanceof ArrayList)
  487:        command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',"); 
  488:        else command.append("NULL,");
  489:      }
  490:      obj = row.get(row.size() - 1);
  491:      if (obj != null && !(obj instanceof ArrayList))
  492:      command.append("'"+convertUml(obj.toString())+"')"); 
  493:       else
  494:       if(obj!=null&&   obj instanceof ArrayList)
  495:        command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')");         //command.append(obj.toString()+")");
  496:        else command.append("NULL)");
  497:       */
  498:       //command.append("'"+row.get(row.size()-1)+"')"); 
  499:       //command.append(" )");
  500:       //  for(int k=0;k<row.size();++k)
  501: 
  502:       // System.out.println();
  503:       //   System.out.println(command+" "+j+" "+row.size()+" "+  ((Vector)result2[0].get(j)).size());
  504:       // System.out.println(command);
  505:       for(int k=0;k<row.size();++k) 
  506:       {
  507:         obj = row.get(k);
  508:         if(obj instanceof ArrayList) obj=((List)obj).get(0);
  509:         String str =(obj==null) ? "NULL":obj.toString();
  510:         if(!str.equals("NULL"))
  511:         pstm.setString(k+1,str); 
  512:         else pstm.setNull(k+1,Types.NULL);
  513:       }
  514:       pstm.execute();
  515:       //stm.executeUpdate(command.toString());
  516:       dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
  517: 	// System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j);
  518:    command = null;
  519: }// to for loop    
  520:    
  521:     }
  522:  } catch(Exception e) 
  523:    {
  524:      System.out.println("Error while connecting to database "+ e);
  525:      dialog.setVisible(false);
  526: 	   dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  527: 		 FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  528: 		 java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
  529: 		 java.io.PrintStream stream = new java.io.PrintStream(b);
  530: 		 stream.print(command+"\n\n");
  531: 		 e.printStackTrace(stream);
  532: 		 FM2SQL.showErrorDialog(b.toString(), "Error occured !");
  533: 	 
  534:    }
  535: 	 dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  536: 	 FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  537: 
  538: 	 dialog.setVisible(false); 
  539:   }
  540: 
  541:  public static String convertText(String newName)
  542:   {
  543:     StringBuffer alterMe = new StringBuffer(newName.trim().toLowerCase());
  544:     int length = alterMe.length();
  545:     int j = 0;
  546:     int index=alterMe.indexOf(".fp5");
  547:     if ( index >= 0 )
  548:      {
  549:        alterMe.delete(index, index + 4);
  550:        length = length - 4;
  551:      }
  552:  
  553:     while (j < length)
  554:     { 
  555:       if (alterMe.charAt(j) == ' ')
  556:       {
  557:         alterMe.setCharAt(j, '_');
  558:     //    if(j<length-1) j=j+1;
  559:       }
  560:       else
  561:       if (alterMe.charAt(j) == '_')
  562:       {
  563: 		
  564: 		if(alterMe.charAt(j+1)=='_')
  565: 		alterMe.deleteCharAt(j);
  566:         length = length-1;
  567:       //  if(j<length-1) j=j+1;
  568:       }
  569:      else
  570: 	 if (alterMe.charAt(j) == 'ä')
  571: 		  {
  572: 			alterMe.setCharAt(j, 'a');
  573: 			alterMe.insert(j+1,"e");
  574: 			length=length+1;
  575: 			if(j<length-1) j=j+1;
  576: 		  }
  577: 		else
  578: 		 if (alterMe.charAt(j) == 'ö')
  579: 			  {
  580: 				alterMe.setCharAt(j, 'o');
  581: 				alterMe.insert(j+1,"e");
  582: 				length=length+1;
  583: 				if(j<length-1) j=j+1;
  584: 			  }
  585: 		else
  586: 		 if (alterMe.charAt(j) == 'ü')
  587: 			  {
  588: 				alterMe.setCharAt(j, 'u');
  589: 				alterMe.insert(j+1,"e");
  590: 				length=length+1;
  591: 				if(j<length-1) j=j+1;
  592: 			  }
  593: 		else
  594:     if (alterMe.charAt(j) == 'ß')
  595:     {
  596:       alterMe.setCharAt(j, 's');
  597:       alterMe.insert(j + 1, "s");
  598:       length = length + 1;
  599:       if (j < length - 1)
  600:         j = j + 1;
  601:     } else if (alterMe.charAt(j) == ':')
  602:     {
  603:       if(j<length-1)
  604:       {
  605:       if (alterMe.charAt(j + 1) == ':')
  606:       {
  607:         alterMe.setCharAt(j,'_');
  608:         alterMe.delete(j+1,j+2);
  609:       length = length - 1;
  610:    
  611:       }
  612:  
  613:       if (j < length - 1)
  614:         j = j + 1;
  615:       }
  616:     } else if (alterMe.charAt(j) == '-')
  617:     {
  618:       alterMe.setCharAt(j, '_');
  619: 
  620:     }
  621:     else if (alterMe.charAt(j) == '.')
  622:     {
  623:       if(j==length-1)
  624:       {
  625:         alterMe.delete(j, j); 
  626:         length--;
  627:       }
  628: 			else
  629: 			alterMe.setCharAt(j,'_');
  630:     }
  631: 
  632:     ++j;
  633:   }
  634:     return alterMe.toString();
  635:   }
  636:   public static String convertToEntities(String newName)
  637:   {
  638:     StringBuffer alterMe = new StringBuffer(newName.trim());
  639:     int length = alterMe.length();
  640:     int j = 0;
  641: 
  642:     while (j < length)
  643:     {
  644: 
  645:       if (alterMe.charAt(j) == '>')
  646:       {
  647:         alterMe.setCharAt(j, '&');
  648:         alterMe.insert(j + 1, "gt;");
  649:         length = length + 2;
  650:         if (j < length - 1)
  651:           j = j + 1;
  652: 
  653:       } else if (alterMe.charAt(j) == '<')
  654:       {
  655:         alterMe.setCharAt(j, '&');
  656:         alterMe.insert(j + 1, "lt;");
  657:         length = length + 2;
  658:         if (j < length - 1)
  659:           j = j + 1;
  660: 
  661:       }
  662:       ++j;
  663:     }
  664:     return alterMe.toString();
  665:   }
  666:   public static String convertUml(String newName)
  667:    {
  668: 	 StringBuffer alterMe = new StringBuffer(newName.trim());
  669: 	 int length = alterMe.length();
  670: 	 int j = 0;
  671: 	
  672: 	
  673: 	 while (j < length)
  674: 	 { 
  675: 	
  676: 	   if (alterMe.charAt(j) == '\'')
  677: 	   {
  678: 		 alterMe.setCharAt(j, '\\');
  679: 		 alterMe.insert(j + 1, "'");
  680: 		 length = length + 1;
  681: 		 if(j<length-1) j=j+1;
  682: 	   }
  683: 	/*   else
  684: 	   if (alterMe.charAt(j) == '"')
  685: 	   {
  686: 		 alterMe.setCharAt(j, '\\');
  687: 		 alterMe.insert(j + 1, "\"");
  688: 		 length = length + 1;
  689: 		 if(j<length-1) j=j+1;
  690: 	   }
  691: 	  else
  692: 	  if (alterMe.charAt(j) == '>')
  693: 	   {
  694: 		 alterMe.setCharAt(j, '\\');
  695: 		 alterMe.insert(j + 1, ">");
  696: 		 length = length + 1;
  697: 		 if(j<length-1) j=j+1;
  698: 	   }
  699: 	  else
  700: 	  if (alterMe.charAt(j) == '<')
  701: 	   {
  702: 		 alterMe.setCharAt(j, '\\');
  703: 		 alterMe.insert(j + 1, "<");
  704: 		 length = length + 1;
  705: 		 if(j<length-1) j=j+1;
  706: 	   }
  707: 	 else
  708: 	 if (alterMe.charAt(j) == '?')
  709: 	   {
  710: 		 alterMe.setCharAt(j, '\\');
  711: 		 alterMe.insert(j + 1, "?");
  712: 		 length = length + 1;
  713: 		 if(j<length-1) j=j+1;
  714: 	   }
  715: 	 else
  716: 	 if (alterMe.charAt(j) == '&')
  717: 	   {
  718: 		 alterMe.setCharAt(j, '\\');
  719: 		 alterMe.insert(j + 1, "&");
  720: 		 length = length + 1;
  721: 		 if(j<length-1) j=j+1;
  722: 	   }
  723: 	 else
  724: 	 if (alterMe.charAt(j) == '=')
  725: 	   {
  726: 		 alterMe.setCharAt(j, '\\');
  727: 		 alterMe.insert(j + 1, "=");
  728: 		 length = length + 1;
  729: 		 if(j<length-1) j=j+1;
  730: 	   }
  731: 	 else
  732: 	 if (alterMe.charAt(j) == ',')
  733: 	   {
  734: 		 alterMe.setCharAt(j, '\\');
  735: 		 alterMe.insert(j + 1, ",");
  736: 		 length = length + 1;
  737: 		 if(j<length-1) j=j+1;
  738: 	   }
  739: 	 else
  740: 	 if (alterMe.charAt(j) == '.')
  741: 	   {
  742: 		 alterMe.setCharAt(j, '\\');
  743: 		 alterMe.insert(j + 1, ".");
  744: 		 length = length + 1;
  745: 		 if(j<length-1) j=j+1;
  746: 	   }
  747: 	 else
  748: 	 if (alterMe.charAt(j) == '[')
  749: 	   {
  750: 		 alterMe.setCharAt(j, '\\');
  751: 		 alterMe.insert(j + 1, ".");
  752: 		 length = length + 1;
  753: 		 if(j<length-1) j=j+1;
  754: 	   }
  755: 	else
  756: 	 if (alterMe.charAt(j) == ']')
  757: 	   {
  758: 		 alterMe.setCharAt(j, '\\');
  759: 		 alterMe.insert(j + 1, ".");
  760: 		 length = length + 1;
  761: 		 if(j<length-1) j=j+1;
  762: 	   }
  763: 	else
  764: 	 if (alterMe.charAt(j) == '%')
  765: 	   {
  766: 		 alterMe.setCharAt(j, '\\');
  767: 		 alterMe.insert(j + 1, "%");
  768: 		 length = length + 1;
  769: 		 if(j<length-1) j=j+1;
  770: 	   }*/
  771: 		++j;
  772: 	 }
  773: 	 return alterMe.toString();
  774:    }
  775:    
  776:    public static void parseXMLConfig(StringBuffer sb)
  777:     {
  778:       boolean finished = false;
  779:       // parse string and build document tree
  780:       Xparse parser =new Xparse();
  781:       parser.changeEntities = true;
  782:       Node  root = parser.parse(sb.toString());
  783:        // printContents(root);
  784:       Vector databases= new Vector();
  785:       Vector tables = new Vector();
  786:       Vector layouts = new Vector();
  787:       Vector selects = new Vector();
  788:       Vector creates = new Vector();
  789: 			Vector ids     = new Vector();
  790:       int mode = -1;
  791:    
  792:       try
  793:       {
  794:         Node tempNode = root.find("convert/source", new int[] { 1, 1 });
  795:         if(tempNode==null) throw new Error("parse error source tag missing");
  796:         System.out.println(tempNode.name);
  797:         int length =countNodes(tempNode);
  798:         for (int i = 1; i <= length; i++)
  799:         {
  800:           
  801:           DBBean database = new DBBean();
  802:           tables = new Vector();
  803:           layouts = new Vector();
  804:           selects = new Vector();
  805:           creates = new Vector();
  806:           ids     = new Vector();
  807:          // parse dataBase
  808:           Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
  809:           Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
  810:           Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
  811:           Node node3 = root.find("convert/source/database", new int[] { 1, 1, i});
  812:     			Node nodeMode = root.find("convert/source/database/mode/", new int[] {1, 1, i, 1,1});
  813: 
  814:           if(node3== null) throw new Error("parse error database tag missing");
  815:           if(node==null) throw new Error("parse error url tag missing");
  816:           if(node1==null) throw new Error("parse error user tag missing");
  817:           if(node2==null) throw new Error("parse error password tag missing");
  818:           String url=node.getCharacters();
  819:           String user=node1.getCharacters();
  820:           String password=node2.getCharacters();
  821:           database.setURL(url.trim());
  822:           database.setUserAndPasswd(user.trim(),password.trim());
  823:           System.out.println(node.name + " " + node.getCharacters());
  824:           System.out.println(node1.name + " " + node1.getCharacters());
  825:           System.out.println(node2.name + " " + node2.getCharacters());
  826:           String modeString = "";
  827:           if (nodeMode == null)
  828:             modeString = "convert";
  829:           else
  830:             modeString = nodeMode.getCharacters();
  831:           if (modeString.equals("convert"))
  832:             mode = DataBase.CONVERT_MODE;
  833:           else
  834:             if (modeString.equals("append"))
  835:               mode = DataBase.APPEND_MODE;
  836:             else
  837:               if (modeString.equals("update"))
  838:                 mode = DataBase.UPDATE_MODE;
  839:        //   if(node3!=null)
  840:          // System.out.println(node3.name);
  841:           
  842:           int length2= countNodes(node3);
  843:     
  844:          System.out.println("number of tables " +length2);
  845:     
  846:           for(int j=1;j<=length2;++j)
  847:           {
  848:             Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
  849:             Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
  850:             Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
  851:             if(node4!=null)
  852:             System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
  853:             if(node5!=null)
  854:             System.out.println(node5.name + " " + node5.getCharacters());
  855:             if(node6!=null)
  856:             System.out.println(node6.name + " " + node6.getCharacters());
  857:             if(node4==null) throw new Error("parse error table tag missing");
  858:             // if(node5==null) throw new Error("parse error select tag missing");
  859:             // if(node6==null) throw new Error("parse error create tag missing");
  860:             String name = (String)node4.attributes.get("name");
  861:             String layout = (String)node4.attributes.get("layout");
  862:             String id = (String)node4.attributes.get("id");
  863:             System.out.println("id was "+id);
  864:             if(name==null) throw new Error("parse error required table tag attribute name missing");
  865:             if(layout==null) layout ="";
  866:             if(id==null)  id = "";
  867:             if(name.equals("")) throw new Error("parse error table tag attribute must not be empty");
  868:             tables.add(name);
  869:             layouts.add(layout);
  870:             ids.add(id);
  871:             String query = (node5==null) ? "":node5.getCharacters(); 
  872:             if(query.equals("")) System.err.println("Warning empty select tag or  select tag missing !!");
  873:             query = (query.equals("")) ? "select * from "+database.getQC()+name+database.getQC():query;
  874:             selects.add(query);
  875:             if(node6!=null) creates.add(node6.getCharacters().trim());
  876:              else
  877:               creates.add("");
  878:             
  879:           }
  880:           databases.add(new DataBase(database, tables, layouts, selects,creates,ids,mode));
  881:         }
  882:         DBBean database = new DBBean();
  883:         // parse dataBase
  884:         Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
  885:         Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
  886:         Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
  887:         String url = node.getCharacters();
  888:         String user = node1.getCharacters();
  889:         String password = node2.getCharacters();
  890:         System.out.println(url);
  891:         database.setURL(url.trim());
  892:         database.setUserAndPasswd(user.trim(), password.trim());
  893:         //databases.add(database);
  894:        for (Iterator iter = databases.iterator(); iter.hasNext();)
  895:       {
  896:         DataBase db = (DataBase) iter.next();
  897:         convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates,mode);
  898:         
  899:       }
  900:         // printContents(node3);
  901:         //   FM2SQL.fmInstance=new FM2SQL();
  902:       } catch (Exception e)
  903:       {
  904:         // TODO Auto-generated catch block
  905:         e.printStackTrace();
  906:       }
  907:       /*
  908:       Node tempNode=root.find(rootNode,new int[] {1});
  909:       if(tempNode==null) return rtTag;
  910:       int count=1;
  911:       /* 
  912:       for(int i=0;i<tempNode.contents.v.size();++i)
  913:       {
  914:         // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
  915:       }*/
  916:    /*
  917:       if(tempNode.contents.v.size()==0) return rtTag;
  918:       Node notNull=null;
  919:       Node rtNode=root.find(rootNode+"/rt",new int[] {1,1});
  920:       rtTag=(rtNode==null) ? "finished":rtNode.getCharacters();
  921:       System.out.println("rt tag is "+ rtTag);
  922:       int length=0;//(tempNode.contents.length()-1)/2;
  923:       for(int i=0;i<tempNode.contents.v.size();++i)    {
  924:         Node node=(Node)tempNode.contents.v.elementAt(i);
  925:         if(node.type.equals("element"))
  926:         if(node.name.equals("doc"))length++;
  927:         // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
  928:       }
  929:      if(length==0) rtTag="finished";        
  930:       while(count<=length)
  931:       {
  932:          //System.out.println(count+" "+length);
  933:         tempNode=root.find(rootNode+"/doc",new int[] {1,count});
  934:         if(tempNode==null)
  935:         {
  936:           rtTag="finished";
  937:           break;
  938:         }    
  939:         Bundle.Document doc = new Bundle.Document();
  940:         Hashtable attributes=tempNode.attributes;
  941:         int docID=0;
  942:         int ver=0;
  943:          if(attributes != null)
  944:         {
  945:           docID=Integer.parseInt((String)attributes.get("id"));
  946:           ver=Integer.parseInt((String)attributes.get("ver"));
  947:           doc.addData("docID",(String)attributes.get("id"));
  948:           doc.addData("ver",(String)attributes.get("ver"));
  949:         }
  950:         bundle.docs.put(new Integer(docID),doc);
  951:         bundle.documents.addElement(doc);
  952:         // System.out.println("id " +docID+" ver "+ver);
  953:         Node fileNode=root.find(rootNode+"/doc/file",new int[] {1,count,1});
  954:         attributes=(fileNode==null)? null:fileNode.attributes;
  955:         int fileID=0;
  956:         int fileVer=0;
  957:         if(attributes != null)
  958:         {
  959:           fileID=Integer.parseInt((String)attributes.get("id"));
  960:           fileVer=Integer.parseInt((String)attributes.get("ver"));
  961:           doc.addData("fileID",(String)attributes.get("id"));
  962:           doc.addData("fileVer",(String)attributes.get("ver"));
  963:           // System.out.println("fileid " +fileID+" ver "+fileVer);
  964:         }
  965:      // add all Document tags to actual Document
  966:         for(int i=0;i<tempNode.contents.v.size();++i)
  967:         {   
  968:           Node node=(Node)tempNode.contents.v.elementAt(i);
  969:           if(node.type.equals("element"))
  970:            {
  971:              //System.out.println(node.name+" "+node.getCharacters()+" "+node.contents.v.size());
  972:              if(node.contents.v.size()>1) 
  973:              {
  974:                for(int j=0;j<node.contents.v.size();++j)
  975:                {  
  976:                  Node node2=(Node)node.contents.v.elementAt(j);
  977:                  if(node2.type.equals("element"))
  978:                   {
  979:                     doc.addData(node2.name,node2.getCharacters());
  980:                     //System.out.println(node2.name+" "+node2.getCharacters()+" "+node2.contents.v.size());
  981:                   }
  982:                }
  983:              } else doc.addData(node.name,node.getCharacters());
  984:           }
  985:         }
  986:         bundle.docsLocator.put(doc.locator,doc); 
  987:    
  988:       bundle.documents.addElement(doc);
  989:         count++;
  990:       }      */
  991:   
  992:       // System.out.println(bundle.docs.size());
  993:    // return rtTag;
  994:     }
  995:     public static Vector getXMLConfig(String xmlFile)
  996:      {
  997:        StringBuffer sb=null;
  998:        try 
  999:              {
 1000:                 // read XML Metadata from a file
 1001:                 FileInputStream fi= new FileInputStream(xmlFile);
 1002:                 InputStreamReader isr= new InputStreamReader(fi,"UTF-8");
 1003:                 BufferedReader buffr = new BufferedReader(isr);
 1004:                 sb=new StringBuffer();
 1005:                 int c=0;
 1006:                 while ((c = buffr.read()) != -1) 
 1007:                 {
 1008:                   char ch=(char)c;
 1009:                   sb.append(ch);
 1010:                   // System.out.print((char)c);
 1011:                 }
 1012:                
 1013:             } catch(Exception e) { e.printStackTrace();} 
 1014:   
 1015:        boolean finished = false;
 1016:        // parse string and build document tree
 1017:        Xparse parser =new Xparse();
 1018:        parser.changeEntities = true;
 1019:        Node  root = parser.parse(sb.toString());
 1020:    // printContents(root);
 1021:        Vector databases= new Vector();
 1022:        Vector tables = new Vector();
 1023:        Vector layouts = new Vector();
 1024:        Vector selects = new Vector();
 1025:        Vector creates = new Vector();
 1026:        Vector ids     = new Vector();
 1027:        
 1028:        int mode = -1;
 1029:        try
 1030:        {
 1031:          Node tempNode = root.find("convert/source", new int[] { 1, 1 });
 1032:          if(tempNode==null) throw new Error("parse error source tag missing");
 1033:          System.out.println(tempNode.name);
 1034:          int length =countNodes(tempNode);
 1035:          for (int i = 1; i <= length; i++)
 1036:          {
 1037:           
 1038:            DBBean database = new DBBean();
 1039:            tables = new Vector();
 1040:            layouts = new Vector();
 1041:            selects = new Vector();
 1042:            creates = new Vector();
 1043:            ids     = new Vector();
 1044:           // parse dataBase
 1045:            Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
 1046:            Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
 1047:            Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
 1048:            Node node3 = root.find("convert/source/database", new int[] { 1, 1, i});
 1049:            Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 });
 1050:            
 1051:            if(node3== null) throw new Error("parse error database tag missing");
 1052:            if(node==null) throw new Error("parse error url tag missing");
 1053:            if(node1==null) throw new Error("parse error user tag missing");
 1054:            if(node2==null) throw new Error("parse error password tag missing");
 1055:            String url=node.getCharacters();
 1056:            String user=node1.getCharacters();
 1057:            String password=node2.getCharacters();
 1058:            database.setURL(url.trim());
 1059:            database.setUserAndPasswd(user.trim(),password.trim());
 1060:            System.out.println(node.name + " " + node.getCharacters());
 1061:            System.out.println(node1.name + " " + node1.getCharacters());
 1062:            System.out.println(node2.name + " " + node2.getCharacters());
 1063:            String modeString = "";  
 1064:           if (nodeMode == null)
 1065:             modeString = "convert";
 1066:           else
 1067:             modeString = nodeMode.getCharacters();
 1068:           if (modeString.equals("convert"))
 1069:             mode = DataBase.CONVERT_MODE;
 1070:           else
 1071:             if (modeString.equals("append"))
 1072:               mode = DataBase.APPEND_MODE;
 1073:             else
 1074:               if (modeString.equals("update"))
 1075:                 mode = DataBase.UPDATE_MODE;
 1076: 			 
 1077:         
 1078:         //   if(node3!=null)
 1079:           // System.out.println(node3.name);
 1080:           
 1081:            int length2= countNodes(node3);
 1082:     
 1083:           System.out.println("number of tables " +length2);
 1084:     
 1085:            for(int j=1;j<=length2;++j)
 1086:            {
 1087:              Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
 1088:              Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
 1089:              Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
 1090:              if(node4!=null)
 1091:              System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
 1092:              if(node5!=null)
 1093:              System.out.println(node5.name + " " + node5.getCharacters());
 1094:              if(node6!=null)
 1095:              System.out.println(node6.name + " " + node6.getCharacters());
 1096:              if(node4==null) throw new Error("parse error table tag missing");
 1097:              // if(node5==null) throw new Error("parse error select tag missing");
 1098:              // if(node6==null) throw new Error("parse error create tag missing");
 1099:              String name = (String)node4.attributes.get("name");
 1100:              String layout = (String)node4.attributes.get("layout");
 1101:              String id = (String)node4.attributes.get("id");
 1102:              System.out.println("id was "+id);
 1103:          
 1104:              if(name==null) throw new Error("parse error required table tag attribute name missing");
 1105:              if(layout==null) layout ="";
 1106:              if(id == null)  id="";
 1107:              if(name.equals("")) throw new Error("parse error table tag attribute must not be empty");
 1108:              tables.add(name);
 1109:              layouts.add(layout);
 1110:              ids.add(id);
 1111:              String query = (node5==null) ? "":node5.getCharacters(); 
 1112:              if(query.equals("")) System.err.println("Warning empty select tag or  select tag missing !!");
 1113:              query = (query.equals("")) ? "select * from "+database.getQC()+name+database.getQC():query;
 1114:              selects.add(query);
 1115:              if(node6!=null) creates.add(node6.getCharacters().trim());
 1116:               else
 1117:                creates.add("");
 1118:             
 1119:            }
 1120:            databases.add(new DataBase(database, tables, layouts, selects,creates,ids,mode));
 1121:          }
 1122:          DBBean database = new DBBean();
 1123:          // parse dataBase
 1124:          Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
 1125:          Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
 1126:          Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
 1127:          String url = node.getCharacters();
 1128:          String user = node1.getCharacters();
 1129:          String password = node2.getCharacters();
 1130:          System.out.println(url);
 1131:          database.setURL(url.trim());
 1132:          database.setUserAndPasswd(user.trim(), password.trim());
 1133:          databases.add(new DataBase(database,null,null,null,null,null,0));
 1134:          //databases.add(database);
 1135:     /*    for (Iterator iter = databases.iterator(); iter.hasNext();)
 1136:        {
 1137:          DataBase db = (DataBase) iter.next();
 1138:          convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates);
 1139:         
 1140:        }*/
 1141:          // printContents(node3);
 1142:          //   FM2SQL.fmInstance=new FM2SQL();
 1143:        } catch (Exception e)
 1144:        {
 1145:          // TODO Auto-generated catch block
 1146:          e.printStackTrace();
 1147:        }
 1148:        return databases;
 1149:       }
 1150: 
 1151: 
 1152:     private static int countNodes(Node tempNode)
 1153:     {
 1154:       int length = 0;
 1155:       for(int i=0;i<tempNode.contents.v.size();++i)    {
 1156:       Node node=(Node)tempNode.contents.v.elementAt(i);
 1157:       if(node.type.equals("element"))
 1158:       {
 1159:         if(node.name.equals("database"))length++;
 1160:         if(node.name.equals("table"))length++;
 1161:       }
 1162:       
 1163:             // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
 1164:        }
 1165:      return length;
 1166:     }
 1167:     private static void printContents(Node root)
 1168:     {
 1169:         
 1170:       
 1171:       Vector contents=(root.index==null)? root.contents.v:root.index.v;
 1172:       for (int i=0;i<contents.size();++i)
 1173:       {
 1174:         Node n=(Node)contents.elementAt(i);
 1175:         if(n.type.equals("element"))
 1176:         {
 1177:            System.out.println("tag "+n.name);
 1178:            System.out.println(n.getCharacters());
 1179:           //contents=n.contents.v i=0;
 1180:         }
 1181:         // System.out.println(n.type);
 1182:       
 1183:         
 1184:       
 1185:       }
 1186:     }
 1187:     public static void readXMLFile(String xmlFile)
 1188:     {
 1189:     try 
 1190:         {
 1191:            // read XML Metadata from a file
 1192:            FileInputStream fi= new FileInputStream(xmlFile);
 1193:            InputStreamReader isr= new InputStreamReader(fi,"UTF-8");
 1194:            BufferedReader buffr = new BufferedReader(isr);
 1195:            StringBuffer sb=new StringBuffer();
 1196:            int c=0;
 1197:            while ((c = buffr.read()) != -1) 
 1198:            {
 1199:              char ch=(char)c;
 1200:              sb.append(ch);
 1201:              // System.out.print((char)c);
 1202:            }
 1203:            parseXMLConfig(sb);
 1204:        } catch(Exception e) { e.printStackTrace();} 
 1205:     }
 1206: public static class DataBase 
 1207: {
 1208:   DBBean bean;
 1209:   Vector creates;
 1210:   Vector selects;
 1211:   Vector layouts;
 1212:   Vector tables;
 1213:   Vector ids;
 1214:   final static int CONVERT_MODE = 1;
 1215: 	final static int APPEND_MODE  = 2;
 1216: 	final static int UPDATE_MODE  = 3;
 1217:   int mode = -1;
 1218:   
 1219:   public DataBase(DBBean bean,Vector tables,Vector layouts,Vector selects,Vector creates,Vector ids,int mode)
 1220:   {
 1221:     this.bean = bean;
 1222:     this.tables = tables;
 1223:     this.layouts = layouts;
 1224:     this.selects = selects;
 1225:     this.creates = creates;
 1226:     this.ids     = ids;
 1227:     this.mode = mode;
 1228:   }
 1229:   public void exportToXML(BufferedWriter buffr) throws Exception
 1230:    {
 1231:      buffr.write("    <database>\n");
 1232:      buffr.write("      <url>"+bean.url+"</url>\n");
 1233:      buffr.write("      <user>"+bean.user+"</user>\n");
 1234:      buffr.write("      <password>"+bean.passwd+"</password>\n");
 1235:      String modeString ="";
 1236:     if (mode == CONVERT_MODE)
 1237:       modeString = "convert";
 1238:     else
 1239:       if (mode == APPEND_MODE)
 1240:         modeString = "append";
 1241:       else
 1242:         if (mode == UPDATE_MODE)
 1243:           modeString = "update";
 1244:   
 1245:      buffr.write("      <mode>"+ modeString+"</mode>\n");
 1246:      int index = 0;
 1247:      while(index<tables.size())
 1248:      {
 1249:        String table=(String)tables.get(index);
 1250:        String layout=(String)layouts.get(index);
 1251:        String select=(String)selects.get(index);
 1252:        String create=(String)creates.get(index);
 1253:        String id=(String)ids.get(index);
 1254:    
 1255:        buffr.write("      <table name = \""+table+"\" layout = \""+layout+"\" id = \""+id+"\" +>\n");
 1256:        buffr.write("         <select>"+ convertToEntities(select)+"</select>\n");
 1257:        if(!create.equals(""))
 1258:        buffr.write("         <create>"+create+"         </create>\n"); 
 1259:        buffr.write("      </table>\n");
 1260:        index++;
 1261:      }
 1262:      buffr.write("    </database>\n"); 
 1263:    }
 1264: public String toString() {return bean.url+" "+tables;}
 1265: 
 1266: }
 1267: public static String convertToUTF8(Object command)
 1268: {
 1269:   String str = null;
 1270:   try
 1271:   {
 1272:     str = new String(command.toString().getBytes("UTF-8"));
 1273:   } catch (UnsupportedEncodingException e)
 1274:   {
 1275:     // TODO Auto-generated catch block
 1276:     e.printStackTrace();
 1277:   }
 1278:   return str;
 1279: }
 1280:   public static void writeConfig(String file, DataBase source, DataBase destination) throws Exception
 1281:   {
 1282:     if(!file.toLowerCase().endsWith(".xml")) file+=".xml"; 
 1283:     File f = new File(file);
 1284:   
 1285:     FileOutputStream fout= new FileOutputStream(f);
 1286:     OutputStreamWriter outsw = new OutputStreamWriter(fout,"UTF-8");
 1287:     BufferedWriter buffw = new BufferedWriter(outsw);
 1288:     buffw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
 1289:     buffw.newLine();
 1290:     buffw.write("<convert>\n");
 1291:     buffw.write("  <source>\n");
 1292:     source.exportToXML(buffw);
 1293:     buffw.write("  </source>\n");
 1294:     buffw.write("\n  <destination>\n");
 1295:     destination.exportToXML(buffw);
 1296:     buffw.write("  </destination>\n");
 1297:     buffw.write("</convert>\n");
 1298:     buffw.close();
 1299:   } 
 1300: }  

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