import java.util.*; import java.sql.*; import java.awt.Cursor; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.UnsupportedEncodingException; import com.exploringxml.xml.Node; import com.exploringxml.xml.Xparse; class Convert { static DBBean bean = new DBBean(); static DBBean beanDest = new DBBean(); static String user ="",passwd="e1nste1n"; static String userDest="postgres",passwdDest="rogo"; static boolean batchRun=false; static Vector databases = new Vector(); public static void main(String args[]) { /* try { //byte[] b = "ö".getBytes("UTF-8"); // System.out.println("QueryString " +b[0]+" "+b[1]+(new String(b).getBytes()[0])+" "+new String(b).getBytes()[1]); //System.out.println(new String(b,"UTF-8")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ FileOutputStream file = null; if(args.length!=1) { System.out.println("Usage: java Convert "); System.exit(-1); } if(!(new File(args[0]).exists())) System.exit(0); try { file = new FileOutputStream("./log.txt"); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } PrintStream stream= new PrintStream(file); System.setOut(stream); System.setErr(stream); readXMLFile(args[0]); System.out.println("Finished!"); //convert("jdbc:fmpro:http://141.14.237.74:8050","jdbc:postgresql://erebos/test",null,null); } public static void convertBatch(DBBean source,DBBean destination,Vector names,Vector layouts,Vector selects,Vector creates,int mode) throws Exception { /*FM2SQL.ProgressDialog dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance); dialog.setTitle("Conversion running ..."); dialog.title.setText("Getting table data ..."); dialog.setLocation(500,500); dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); */ // FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); //dialog.thread=Thread.currentThread(); bean=source; beanDest = destination; /* // setting user and passwd bean.setUserAndPasswd(user,passwd); // setting user and passwd beanDest.setUserAndPasswd(userDest,passwdDest); */ //dialog.setSize(400,250); StringBuffer command= null; try { //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050"); //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo"); bean.setConnection(source.url); if(names==null) names=bean.getTableNames(); //Collections.sort(names); int tbIndex = 1; // System.out.println("Start at "+names.indexOf("archimedes_facsimiles")); for(tbIndex=0;tbIndex=0) { stm.executeUpdate("drop table "+beanDest.getQC()+names.get(tbIndex)+beanDest.getQC()); tables.remove((String)names.get(tbIndex)); System.out.println("dropped table "+ names.get(tbIndex)); } else if(tables.indexOf(convertText(names.get(tbIndex).toString()))>=0) { stm.executeUpdate("drop table "+beanDest.getQC()+convertText((String)names.get(tbIndex))+beanDest.getQC()); tables.remove(convertText((String)names.get(tbIndex))); System.out.println("dropped table "+ names.get(tbIndex)); } if(tables.indexOf(names.get(tbIndex))<0&&tables.indexOf(convertText(names.get(tbIndex).toString()))<0) { if(creates.get(tbIndex).equals("")||creates.get(tbIndex).toString().toLowerCase().indexOf("create")<0) { System.out.println("Warning empty or invalid create statement - creating one for you\n"); command = new StringBuffer(50); command.append("CREATE TABLE "); command.append(beanDest.getQC()); command.append(convertText((String)names.get(tbIndex))); command.append(beanDest.getQC()); command.append("("); String type = null; Vector columnNames=bean.getColumnNames(); for(int i=0;i=0) { stm.executeUpdate("drop table "+beanDest.getQC()+names.get(tbIndex)+beanDest.getQC()); tables.remove((String)names.get(tbIndex)); System.out.println("dropped table"+ names.get(tbIndex)); } else if(tables.indexOf(convertText(names.get(tbIndex).toString()))>=0) { stm.executeUpdate("drop table "+beanDest.getQC()+convertText((String)names.get(tbIndex))+beanDest.getQC()); tables.remove(convertText((String)names.get(tbIndex))); System.out.println("dropped table"+ names.get(tbIndex)); } if(tables.indexOf(names.get(tbIndex))<0&&tables.indexOf(convertText(names.get(tbIndex).toString()))<0) { if(creates.get(tbIndex).equals("")||creates.get(tbIndex).toString().toLowerCase().indexOf("create")<0) { System.out.println("Warning empty or invalid create statement - creating one for you\n"); command = new StringBuffer(50); command.append("CREATE TABLE "); command.append(beanDest.getQC()); command.append(convertText((String)names.get(tbIndex))); command.append(beanDest.getQC()); command.append("("); String type = null; Vector columnNames = bean.getColumnNames(); for(int i=0;i= 0 ) { alterMe.delete(index, index + 4); length = length - 4; } while (j < length) { if (alterMe.charAt(j) == ' ') { alterMe.setCharAt(j, '_'); // if(j') { alterMe.setCharAt(j, '&'); alterMe.insert(j + 1, "gt;"); length = length + 2; if (j < length - 1) j = j + 1; } else if (alterMe.charAt(j) == '<') { alterMe.setCharAt(j, '&'); alterMe.insert(j + 1, "lt;"); length = length + 2; if (j < length - 1) j = j + 1; } ++j; } return alterMe.toString(); } public static String convertUml(String newName) { StringBuffer alterMe = new StringBuffer(newName.trim()); int length = alterMe.length(); int j = 0; while (j < length) { if (alterMe.charAt(j) == '\'') { alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "'"); length = length + 1; if(j') { alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, ">"); length = length + 1; if(j1) { for(int j=0;j\n"); buffr.write(" "+bean.url+"\n"); buffr.write(" "+bean.user+"\n"); buffr.write(" "+bean.passwd+"\n"); String modeString =""; if (mode == CONVERT_MODE) modeString = "convert"; else if (mode == APPEND_MODE) modeString = "append"; else if (mode == UPDATE_MODE) modeString = "update"; buffr.write(" "+ modeString+"\n"); int index = 0; while(index\n"); buffr.write(" \n"); if(!create.equals("")) buffr.write(" "+create+" \n"); buffr.write(" \n"); index++; } buffr.write(" \n"); } public String toString() {return bean.url+" "+tables;} } public static String convertToUTF8(Object command) { String str = null; try { str = new String(command.toString().getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return str; } public static void writeConfig(String file, DataBase source, DataBase destination) throws Exception { if(!file.toLowerCase().endsWith(".xml")) file+=".xml"; File f = new File(file); FileOutputStream fout= new FileOutputStream(f); OutputStreamWriter outsw = new OutputStreamWriter(fout,"UTF-8"); BufferedWriter buffw = new BufferedWriter(outsw); buffw.write(""); buffw.newLine(); buffw.write("\n"); buffw.write(" \n"); source.exportToXML(buffw); buffw.write(" \n"); buffw.write("\n \n"); destination.exportToXML(buffw); buffw.write(" \n"); buffw.write("\n"); buffw.close(); } }