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