Annotation of FM2SQL/Convert.java, revision 1.18
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: 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: }
1.9 rogo 62: public static void convertBatch(DBBean source,DBBean destination,Vector names,Vector layouts,Vector selects,Vector creates,int mode) throws Exception
1.1 rogo 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);
1.9 rogo 140: if(mode==Convert.DataBase.CONVERT_MODE)
1.1 rogo 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)
1.3 rogo 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);
1.1 rogo 227: while((row=bean.getNextRow())!=null)
228: {
1.3 rogo 229: /* //Vector row = (Vector) result[0].get(j);
1.1 rogo 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 ( ");
1.3 rogo 237: */
238: //print rows
1.1 rogo 239: Object obj = null;
1.3 rogo 240: /*
1.1 rogo 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;
1.3 rogo 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:
1.1 rogo 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:
1.13 rogo 307: public static void update(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids,int mode) throws Exception
1.12 rogo 308: {
309: FM2SQL.ProgressDialog dialog = null;
310: if (FM2SQL.fmInstance != null)
311: {
312: 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: }
320: // setting user and passwd
321: bean.setUserAndPasswd(user, passwd);
322: // setting user and passwd
323: beanDest.setUserAndPasswd(userDest, passwdDest);
324: if (dialog != null)
325: dialog.setSize(400, 250);
326: StringBuffer command = null;
327: String query = null;
328: try
329: {
330: //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");
331: //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
332: bean.setConnection(source);
333: if (names == null)
334: names = bean.getTableNames();
335: // Collections.sort(names);
336: int tbIndex = 1;
337:
338: // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
339: for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
340: {
341: Vector[] result = null;
342: try
343: {
344: query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
345: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
346: query = (selects != null) ? selects.get(tbIndex).toString() : query;
347: //if vectors[1].get(i) != null)
348: if (layout != "")
349: {
350: layout = " layout " + bean.getQC() + layout + bean.getQC();
351: String name = names.get(tbIndex).toString();
352: StringBuffer queryLayout = new StringBuffer(query);
353: queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
354: query = queryLayout.toString();
355: System.out.println("added layout " + query);
356:
357: }
358: dialog.title.setText("Getting table data ...");
359: dialog.table.setText(names.get(tbIndex).toString());
360: dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
361: dialog.show();
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:
381: if (dialog != null)
382: dialog.title.setText("Writing table data ...");
383:
384: int j = -1;
385: Vector row = null;
386: command = new StringBuffer();
387:
388: command.append("UPDATE ");
389: command.append(beanDest.getQC());
390: command.append(convertText((String) names.get(tbIndex)));
391: command.append(beanDest.getQC());
392: command.append(" SET ");
393:
394: int size = bean.getColumnNames().size();
395: for (int i = 0; i < size - 1; ++i)
1.16 rogo 396: command.append(beanDest.getQC()+convertText((String)bean.getColumnNames().get(i))+beanDest.getQC()+" = ? ,");
1.13 rogo 397: command.append(convertText((String)bean.getColumnNames().get(size-1))+" = ? ");
1.16 rogo 398: command.append("WHERE "+convertText(ids.get(tbIndex).toString())+ " = ?");
1.12 rogo 399: PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
1.14 rogo 400: System.out.println(command+" "+tbIndex);
1.12 rogo 401: int rowCount = bean.getRowCount(query);
1.14 rogo 402: int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex) );
1.12 rogo 403: while ((row = bean.getNextRow()) != null)
404: {
405: j++;
406: //print rows
407: Object obj = null;
408: /* for(int k=0;k<row.size()-1;++k)
409: {
410: obj = row.get(k);
411: //System.out.println("row "+obj+" "+k);
412: if(obj!=null&&!(obj instanceof ArrayList))
413: command.append("'"+convertUml(obj.toString())+"',");
414: else if(obj!=null&& obj instanceof ArrayList)
415: command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',");
416: else command.append("NULL,");
417: }
418: obj = row.get(row.size() - 1);
419: if (obj != null && !(obj instanceof ArrayList))
420: command.append("'"+convertUml(obj.toString())+"')");
421: else
422: if(obj!=null&& obj instanceof ArrayList)
423: command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')"); //command.append(obj.toString()+")");
424: else command.append("NULL)");
425: */
426: //command.append("'"+row.get(row.size()-1)+"')");
427: //command.append(" )");
428: // for(int k=0;k<row.size();++k)
429:
430: // System.out.println();
431: // System.out.println(command+" "+j+" "+row.size()+" "+ ((Vector)result2[0].get(j)).size());
432: // System.out.println(command);
433: for (int k = 0; k < row.size(); ++k)
434: {
435: obj = row.get(k);
436: if (obj instanceof ArrayList)
437: obj = ((List) obj).get(0);
438: String str = (obj == null) ? "NULL" : obj.toString();
439: if (!str.equals("NULL"))
440: pstm.setString(k + 1, str);
441: else
442: pstm.setNull(k + 1, Types.NULL);
443: }
1.14 rogo 444: pstm.setString(row.size()+1,row.get(idIndex).toString());
1.15 rogo 445: //System.out.println(pstm.toString());
1.14 rogo 446: // System.exit(0);
1.12 rogo 447: pstm.execute();
448: //stm.executeUpdate(command.toString());
449: if(dialog!=null)
450: dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
451: // System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j);
452: command = null;
453: } // to for loop
454:
455: }
456: } catch (Exception e)
457: {
458: System.out.println("Error while connecting to database " + e);
1.18 ! rogo 459: if (dialog != null)
! 460: {
! 461: dialog.setVisible(false);
! 462: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
! 463: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
! 464: }
1.12 rogo 465: java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
466: java.io.PrintStream stream = new java.io.PrintStream(b);
467: stream.print(command + "\n\n");
468: e.printStackTrace(stream);
469: FM2SQL.showErrorDialog(b.toString(), "Error occured !");
470:
471: }
1.18 ! rogo 472: if (dialog != null)
! 473: {
! 474: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
! 475: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
! 476:
! 477: dialog.setVisible(false);
! 478: }
1.12 rogo 479:
480: }
481:
482:
1.1 rogo 483:
1.6 rogo 484: public static void convert(String source,String destination,Vector names,Vector layouts,Vector selects,Vector creates,int mode) throws Exception
1.1 rogo 485: {
486: FM2SQL.ProgressDialog dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance);
487: dialog.setTitle("Conversion running ...");
488: dialog.title.setText("Getting table data ...");
489: dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x+(FM2SQL.fmInstance.getWidth()-400)/2,FM2SQL.fmInstance.getLocationOnScreen().y+(FM2SQL.fmInstance.getHeight()-250)/2);
490: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
491: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
492: dialog.thread=Thread.currentThread();
493: // setting user and passwd
494: bean.setUserAndPasswd(user,passwd);
495: // setting user and passwd
496: beanDest.setUserAndPasswd(userDest,passwdDest);
497: dialog.setSize(400,250);
498: StringBuffer command= null;
499: String query = null;
500: try
501: {
502: //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");
503: //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
504: bean.setConnection(source);
505: if(names==null)
506: names=bean.getTableNames();
507: // Collections.sort(names);
508: int tbIndex = 1;
509:
510: // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
511: for(tbIndex=0;tbIndex<names.size();++tbIndex)
512: {
513: Vector[] result = null;
514: try {
515: query = "select * from " + bean.getQC() +names.get(tbIndex).toString() + bean.getQC();
516: String layout=(layouts.isEmpty()) ? "":layouts.get(tbIndex).toString();
517: query = (selects!=null) ? selects.get(tbIndex).toString():query;
518: //if vectors[1].get(i) != null)
519: if (layout != "")
520: {
521: layout = " layout " + bean.getQC() + layout + bean.getQC();
522: String name = names.get(tbIndex).toString();
523: StringBuffer queryLayout = new StringBuffer(query);
524: queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
525: query = queryLayout.toString();
526: System.out.println("added layout "+ query);
527:
528: }
529: // if ( layout!= "")
530: // query += " layout " + bean.getQC() + layout + bean.getQC();
531: dialog.title.setText("Getting table data ...");
532: dialog.table.setText(names.get(tbIndex).toString());
533: dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
534: dialog.show();
535: //result = bean.getQueryData(query, dialog, 0);
536: bean.getConnection();
537: bean.makeQuery(query,0);
538: } catch (Exception e)
539: {
540: continue;
541: }
542: //beanDest.setConnection("jdbc:postgresql://erebos/test3");
543: beanDest.setConnection(destination);
544:
545: Statement stm = beanDest.getConnection().createStatement();
546:
547: Vector tables = beanDest.getTableNames();
548: // Collections.sort(tables);
549: System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
550: tables = beanDest.getTableNames();
551: // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
552: stm = beanDest.getConnection().createStatement();
553: // System.exit(0);
1.7 rogo 554: if(mode==Convert.DataBase.CONVERT_MODE)
1.1 rogo 555: {
556: if(tables.indexOf(names.get(tbIndex))>=0)
557: {
558: stm.executeUpdate("drop table "+beanDest.getQC()+names.get(tbIndex)+beanDest.getQC());
559: tables.remove((String)names.get(tbIndex));
560: System.out.println("dropped table"+ names.get(tbIndex));
561: }
562: else
563: if(tables.indexOf(convertText(names.get(tbIndex).toString()))>=0)
564: {
565: stm.executeUpdate("drop table "+beanDest.getQC()+convertText((String)names.get(tbIndex))+beanDest.getQC());
566: tables.remove(convertText((String)names.get(tbIndex)));
567: System.out.println("dropped table"+ names.get(tbIndex));
568: }
569:
570: if(tables.indexOf(names.get(tbIndex))<0&&tables.indexOf(convertText(names.get(tbIndex).toString()))<0)
571: {
572: if(creates.get(tbIndex).equals("")||creates.get(tbIndex).toString().toLowerCase().indexOf("create")<0)
573: {
574: System.out.println("Warning empty or invalid create statement - creating one for you\n");
575:
576: command = new StringBuffer(50);
577: command.append("CREATE TABLE ");
578: command.append(beanDest.getQC());
579: command.append(convertText((String)names.get(tbIndex)));
580: command.append(beanDest.getQC());
581: command.append("(");
582: String type = null;
583: Vector columnNames = bean.getColumnNames();
584: for(int i=0;i<columnNames.size()-1;++i)
585: {
586: type = bean.metaData.getColumnTypeName(i+1);
587: // System.out.println(i+" "+result[1].get(i)+" "+type);
588: type = (type.equals("NUMBER")) ?"INT4":type;
589: type = (type.equals("CONTAINER")) ?"TEXT":type;
590:
591: command.append(beanDest.getQC() + convertText((String) columnNames.get(i)) + beanDest.getQC() + " " + type + ", ");
592: }
593: type = bean.metaData.getColumnTypeName(columnNames.size());
594: type = (type.equals("NUMBER")) ? "INT4" : type;
595: type = (type.equals("CONTAINER")) ? "TEXT" : type;
596: command.append(beanDest.getQC() + convertText((String) columnNames.get(columnNames.size() - 1)) + beanDest.getQC() + " " + type);
597: command.append(" )");
598:
599: // System.out.println(command);
600: // System.exit(0);
601: //command.append(DBBean.getQC());
602: } else
603: command=new StringBuffer().append(creates.get(tbIndex).toString());
604: stm.executeUpdate(command.toString());
605:
606: }
607: }
608: // System.out.println(names);
609: /*
610: if (idVal == "")
611: stm.executeUpdate("INSERT INTO " + DBBean.getQC() + box.getSelectedItem() + DBBean.getQC() + " (" + DBBean.getQC() + columnName + DBBean.getQC() + ") VALUES ('" + value + "') ");
612: else
613: stm.executeUpdate("UPDATE " + DBBean.getQC() + box.getSelectedItem() + DBBean.getQC() + " SET " + DBBean.getQC() + columnName + DBBean.getQC() + "='" + value + "' WHERE ID='" + idVal + "' ");
614: */
615:
616: // Vector[] result2=beanDest.getQueryData(names.get(tbIndex).toString());
617: // print results to screen
618: /* for (int i = 0; i < result[1].size(); ++i)
619: {
620: //print Header
621: // System.out.println(" "+result[1].get(i)+" "+result2[1].get(i)+" "+i);
622: }*/
623: // System.out.println();
624: dialog.title.setText("Writing table data ...");
625:
626: //for (int j = 0; j < result[0].size(); ++j)
627: int j=-1;
628: Vector row = null;
1.3 rogo 629: command = new StringBuffer();
630:
631: command.append("INSERT INTO ");
632: command.append(beanDest.getQC());
633: command.append(convertText((String) names.get(tbIndex)));
634: command.append(beanDest.getQC());
635: command.append(" values ( ");
636:
637: for(int i=0;i<bean.getColumnNames().size()-1;++i) command.append("?,");
638: command.append("?)");
639: PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
640: System.out.println(command);
1.1 rogo 641: int rowCount= bean.getRowCount(query);
642: while((row=bean.getNextRow())!=null)
643: {
644: j++;
645: // row = (Vector) result[0].get(j);
1.3 rogo 646: /* command = new StringBuffer();
1.1 rogo 647:
648: command.append("INSERT INTO ");
649: command.append(beanDest.getQC());
650: command.append(convertText((String) names.get(tbIndex)));
651: command.append(beanDest.getQC());
652: command.append(" values ( ");
1.3 rogo 653: */
654: //print rows
1.1 rogo 655: Object obj = null;
1.3 rogo 656: /* for(int k=0;k<row.size()-1;++k)
1.1 rogo 657: {
658: obj = row.get(k);
659: //System.out.println("row "+obj+" "+k);
660: if(obj!=null&&!(obj instanceof ArrayList))
1.3 rogo 661: command.append("'"+convertUml(obj.toString())+"',");
1.1 rogo 662: else if(obj!=null&& obj instanceof ArrayList)
663: command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',");
664: else command.append("NULL,");
665: }
666: obj = row.get(row.size() - 1);
667: if (obj != null && !(obj instanceof ArrayList))
1.3 rogo 668: command.append("'"+convertUml(obj.toString())+"')");
1.1 rogo 669: else
670: if(obj!=null&& obj instanceof ArrayList)
671: command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')"); //command.append(obj.toString()+")");
672: else command.append("NULL)");
1.3 rogo 673: */
1.1 rogo 674: //command.append("'"+row.get(row.size()-1)+"')");
675: //command.append(" )");
676: // for(int k=0;k<row.size();++k)
677:
678: // System.out.println();
679: // System.out.println(command+" "+j+" "+row.size()+" "+ ((Vector)result2[0].get(j)).size());
680: // System.out.println(command);
1.3 rogo 681: for(int k=0;k<row.size();++k)
682: {
683: obj = row.get(k);
684: if(obj instanceof ArrayList) obj=((List)obj).get(0);
685: String str =(obj==null) ? "NULL":obj.toString();
686: if(!str.equals("NULL"))
687: pstm.setString(k+1,str);
688: else pstm.setNull(k+1,Types.NULL);
689: }
690: pstm.execute();
691: //stm.executeUpdate(command.toString());
1.1 rogo 692: dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
693: // System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j);
694: command = null;
695: }// to for loop
696:
697: }
698: } catch(Exception e)
699: {
700: System.out.println("Error while connecting to database "+ e);
701: dialog.setVisible(false);
702: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
703: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
704: java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
705: java.io.PrintStream stream = new java.io.PrintStream(b);
706: stream.print(command+"\n\n");
707: e.printStackTrace(stream);
708: FM2SQL.showErrorDialog(b.toString(), "Error occured !");
709:
710: }
711: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
712: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
713:
714: dialog.setVisible(false);
715: }
716:
717: public static String convertText(String newName)
718: {
719: StringBuffer alterMe = new StringBuffer(newName.trim().toLowerCase());
720: int length = alterMe.length();
721: int j = 0;
1.3 rogo 722: int index=alterMe.indexOf(".fp5");
723: if ( index >= 0 )
724: {
725: alterMe.delete(index, index + 4);
726: length = length - 4;
727: }
728:
1.1 rogo 729: while (j < length)
730: {
731: if (alterMe.charAt(j) == ' ')
732: {
733: alterMe.setCharAt(j, '_');
734: // if(j<length-1) j=j+1;
735: }
736: else
737: if (alterMe.charAt(j) == '_')
738: {
739:
740: if(alterMe.charAt(j+1)=='_')
741: alterMe.deleteCharAt(j);
742: length = length-1;
743: // if(j<length-1) j=j+1;
744: }
745: else
746: if (alterMe.charAt(j) == 'ä')
747: {
748: alterMe.setCharAt(j, 'a');
749: alterMe.insert(j+1,"e");
750: length=length+1;
751: if(j<length-1) j=j+1;
752: }
753: else
754: if (alterMe.charAt(j) == 'ö')
755: {
756: alterMe.setCharAt(j, 'o');
757: alterMe.insert(j+1,"e");
758: length=length+1;
759: if(j<length-1) j=j+1;
760: }
761: else
762: if (alterMe.charAt(j) == 'ü')
763: {
764: alterMe.setCharAt(j, 'u');
765: alterMe.insert(j+1,"e");
766: length=length+1;
767: if(j<length-1) j=j+1;
768: }
769: else
770: if (alterMe.charAt(j) == 'ß')
771: {
772: alterMe.setCharAt(j, 's');
773: alterMe.insert(j + 1, "s");
774: length = length + 1;
775: if (j < length - 1)
776: j = j + 1;
777: } else if (alterMe.charAt(j) == ':')
778: {
779: if(j<length-1)
780: {
781: if (alterMe.charAt(j + 1) == ':')
782: {
783: alterMe.setCharAt(j,'_');
784: alterMe.delete(j+1,j+2);
785: length = length - 1;
786:
787: }
788:
789: if (j < length - 1)
790: j = j + 1;
791: }
792: } else if (alterMe.charAt(j) == '-')
793: {
794: alterMe.setCharAt(j, '_');
795:
1.16 rogo 796: } else if (alterMe.charAt(j) == '?')
797: {
798: // changed ? to _ because of update statement
799: alterMe.setCharAt(j,'_');
800: // length = length + 1;
801: // j=j+1;
802: System.out.println(alterMe);
803: }
804:
1.3 rogo 805: else if (alterMe.charAt(j) == '.')
806: {
807: if(j==length-1)
808: {
809: alterMe.delete(j, j);
810: length--;
811: }
1.8 rogo 812: else
813: alterMe.setCharAt(j,'_');
1.1 rogo 814: }
815:
816: ++j;
817: }
818: return alterMe.toString();
819: }
1.4 rogo 820: public static String convertToEntities(String newName)
821: {
822: StringBuffer alterMe = new StringBuffer(newName.trim());
823: int length = alterMe.length();
824: int j = 0;
825:
826: while (j < length)
827: {
828:
829: if (alterMe.charAt(j) == '>')
830: {
831: alterMe.setCharAt(j, '&');
832: alterMe.insert(j + 1, "gt;");
833: length = length + 2;
834: if (j < length - 1)
835: j = j + 1;
836:
837: } else if (alterMe.charAt(j) == '<')
838: {
839: alterMe.setCharAt(j, '&');
840: alterMe.insert(j + 1, "lt;");
841: length = length + 2;
842: if (j < length - 1)
843: j = j + 1;
844:
845: }
846: ++j;
847: }
848: return alterMe.toString();
849: }
1.1 rogo 850: public static String convertUml(String newName)
851: {
852: StringBuffer alterMe = new StringBuffer(newName.trim());
853: int length = alterMe.length();
854: int j = 0;
855:
856:
857: while (j < length)
858: {
859:
860: if (alterMe.charAt(j) == '\'')
861: {
862: alterMe.setCharAt(j, '\\');
863: alterMe.insert(j + 1, "'");
864: length = length + 1;
865: if(j<length-1) j=j+1;
866: }
867: /* else
868: if (alterMe.charAt(j) == '"')
869: {
870: alterMe.setCharAt(j, '\\');
871: alterMe.insert(j + 1, "\"");
872: length = length + 1;
873: if(j<length-1) j=j+1;
874: }
875: else
876: if (alterMe.charAt(j) == '>')
877: {
878: alterMe.setCharAt(j, '\\');
879: alterMe.insert(j + 1, ">");
880: length = length + 1;
881: if(j<length-1) j=j+1;
882: }
883: else
884: if (alterMe.charAt(j) == '<')
885: {
886: alterMe.setCharAt(j, '\\');
887: alterMe.insert(j + 1, "<");
888: length = length + 1;
889: if(j<length-1) j=j+1;
890: }
891: else
892: if (alterMe.charAt(j) == '?')
893: {
894: alterMe.setCharAt(j, '\\');
895: alterMe.insert(j + 1, "?");
896: length = length + 1;
897: if(j<length-1) j=j+1;
898: }
899: else
900: if (alterMe.charAt(j) == '&')
901: {
902: alterMe.setCharAt(j, '\\');
903: alterMe.insert(j + 1, "&");
904: length = length + 1;
905: if(j<length-1) j=j+1;
906: }
907: else
908: if (alterMe.charAt(j) == '=')
909: {
910: alterMe.setCharAt(j, '\\');
911: alterMe.insert(j + 1, "=");
912: length = length + 1;
913: if(j<length-1) j=j+1;
914: }
915: else
916: if (alterMe.charAt(j) == ',')
917: {
918: alterMe.setCharAt(j, '\\');
919: alterMe.insert(j + 1, ",");
920: length = length + 1;
921: if(j<length-1) j=j+1;
922: }
923: else
924: if (alterMe.charAt(j) == '.')
925: {
926: alterMe.setCharAt(j, '\\');
927: alterMe.insert(j + 1, ".");
928: length = length + 1;
929: if(j<length-1) j=j+1;
930: }
931: else
932: if (alterMe.charAt(j) == '[')
933: {
934: alterMe.setCharAt(j, '\\');
935: alterMe.insert(j + 1, ".");
936: length = length + 1;
937: if(j<length-1) j=j+1;
938: }
939: else
940: if (alterMe.charAt(j) == ']')
941: {
942: alterMe.setCharAt(j, '\\');
943: alterMe.insert(j + 1, ".");
944: length = length + 1;
945: if(j<length-1) j=j+1;
946: }
947: else
948: if (alterMe.charAt(j) == '%')
949: {
950: alterMe.setCharAt(j, '\\');
951: alterMe.insert(j + 1, "%");
952: length = length + 1;
953: if(j<length-1) j=j+1;
954: }*/
955: ++j;
956: }
957: return alterMe.toString();
958: }
959:
960: public static void parseXMLConfig(StringBuffer sb)
961: {
962: boolean finished = false;
963: // parse string and build document tree
1.4 rogo 964: Xparse parser =new Xparse();
965: parser.changeEntities = true;
966: Node root = parser.parse(sb.toString());
1.1 rogo 967: // printContents(root);
968: Vector databases= new Vector();
969: Vector tables = new Vector();
970: Vector layouts = new Vector();
971: Vector selects = new Vector();
972: Vector creates = new Vector();
1.10 rogo 973: Vector ids = new Vector();
974: int mode = -1;
1.5 rogo 975:
1.1 rogo 976: try
977: {
978: Node tempNode = root.find("convert/source", new int[] { 1, 1 });
979: if(tempNode==null) throw new Error("parse error source tag missing");
980: System.out.println(tempNode.name);
981: int length =countNodes(tempNode);
982: for (int i = 1; i <= length; i++)
983: {
984:
985: DBBean database = new DBBean();
986: tables = new Vector();
987: layouts = new Vector();
988: selects = new Vector();
989: creates = new Vector();
1.10 rogo 990: ids = new Vector();
1.1 rogo 991: // parse dataBase
992: Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
993: Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
994: Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
995: Node node3 = root.find("convert/source/database", new int[] { 1, 1, i});
1.18 ! rogo 996: Node nodeMode = root.find("convert/source/database/mode", new int[] {1, 1, i, 1,1});
1.5 rogo 997:
1.1 rogo 998: if(node3== null) throw new Error("parse error database tag missing");
999: if(node==null) throw new Error("parse error url tag missing");
1000: if(node1==null) throw new Error("parse error user tag missing");
1001: if(node2==null) throw new Error("parse error password tag missing");
1002: String url=node.getCharacters();
1003: String user=node1.getCharacters();
1004: String password=node2.getCharacters();
1005: database.setURL(url.trim());
1006: database.setUserAndPasswd(user.trim(),password.trim());
1007: System.out.println(node.name + " " + node.getCharacters());
1008: System.out.println(node1.name + " " + node1.getCharacters());
1009: System.out.println(node2.name + " " + node2.getCharacters());
1.5 rogo 1010: String modeString = "";
1011: if (nodeMode == null)
1012: modeString = "convert";
1013: else
1014: modeString = nodeMode.getCharacters();
1015: if (modeString.equals("convert"))
1016: mode = DataBase.CONVERT_MODE;
1017: else
1018: if (modeString.equals("append"))
1019: mode = DataBase.APPEND_MODE;
1020: else
1021: if (modeString.equals("update"))
1022: mode = DataBase.UPDATE_MODE;
1.1 rogo 1023: // if(node3!=null)
1024: // System.out.println(node3.name);
1025:
1026: int length2= countNodes(node3);
1027:
1028: System.out.println("number of tables " +length2);
1029:
1030: for(int j=1;j<=length2;++j)
1031: {
1032: Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
1033: Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
1034: Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
1035: if(node4!=null)
1036: System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
1037: if(node5!=null)
1038: System.out.println(node5.name + " " + node5.getCharacters());
1039: if(node6!=null)
1040: System.out.println(node6.name + " " + node6.getCharacters());
1041: if(node4==null) throw new Error("parse error table tag missing");
1042: // if(node5==null) throw new Error("parse error select tag missing");
1043: // if(node6==null) throw new Error("parse error create tag missing");
1044: String name = (String)node4.attributes.get("name");
1045: String layout = (String)node4.attributes.get("layout");
1.10 rogo 1046: String id = (String)node4.attributes.get("id");
1047: System.out.println("id was "+id);
1.1 rogo 1048: if(name==null) throw new Error("parse error required table tag attribute name missing");
1049: if(layout==null) layout ="";
1.10 rogo 1050: if(id==null) id = "";
1.1 rogo 1051: if(name.equals("")) throw new Error("parse error table tag attribute must not be empty");
1052: tables.add(name);
1053: layouts.add(layout);
1.10 rogo 1054: ids.add(id);
1.1 rogo 1055: String query = (node5==null) ? "":node5.getCharacters();
1056: if(query.equals("")) System.err.println("Warning empty select tag or select tag missing !!");
1057: query = (query.equals("")) ? "select * from "+database.getQC()+name+database.getQC():query;
1058: selects.add(query);
1059: if(node6!=null) creates.add(node6.getCharacters().trim());
1060: else
1061: creates.add("");
1062:
1063: }
1.10 rogo 1064: databases.add(new DataBase(database, tables, layouts, selects,creates,ids,mode));
1.1 rogo 1065: }
1066: DBBean database = new DBBean();
1067: // parse dataBase
1068: Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
1069: Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
1070: Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
1071: String url = node.getCharacters();
1072: String user = node1.getCharacters();
1073: String password = node2.getCharacters();
1074: System.out.println(url);
1075: database.setURL(url.trim());
1076: database.setUserAndPasswd(user.trim(), password.trim());
1077: //databases.add(database);
1078: for (Iterator iter = databases.iterator(); iter.hasNext();)
1079: {
1080: DataBase db = (DataBase) iter.next();
1.17 rogo 1081: if(mode!=DataBase.UPDATE_MODE)
1.9 rogo 1082: convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates,mode);
1.17 rogo 1083: else
1084: update(db.bean.url,database.url,db.tables,db.layouts,db.selects,db.creates,db.ids,mode);
1085:
1.1 rogo 1086: }
1087: // printContents(node3);
1088: // FM2SQL.fmInstance=new FM2SQL();
1089: } catch (Exception e)
1090: {
1091: // TODO Auto-generated catch block
1092: e.printStackTrace();
1093: }
1094: /*
1095: Node tempNode=root.find(rootNode,new int[] {1});
1096: if(tempNode==null) return rtTag;
1097: int count=1;
1098: /*
1099: for(int i=0;i<tempNode.contents.v.size();++i)
1100: {
1101: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1102: }*/
1103: /*
1104: if(tempNode.contents.v.size()==0) return rtTag;
1105: Node notNull=null;
1106: Node rtNode=root.find(rootNode+"/rt",new int[] {1,1});
1107: rtTag=(rtNode==null) ? "finished":rtNode.getCharacters();
1108: System.out.println("rt tag is "+ rtTag);
1109: int length=0;//(tempNode.contents.length()-1)/2;
1110: for(int i=0;i<tempNode.contents.v.size();++i) {
1111: Node node=(Node)tempNode.contents.v.elementAt(i);
1112: if(node.type.equals("element"))
1113: if(node.name.equals("doc"))length++;
1114: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1115: }
1116: if(length==0) rtTag="finished";
1117: while(count<=length)
1118: {
1119: //System.out.println(count+" "+length);
1120: tempNode=root.find(rootNode+"/doc",new int[] {1,count});
1121: if(tempNode==null)
1122: {
1123: rtTag="finished";
1124: break;
1125: }
1126: Bundle.Document doc = new Bundle.Document();
1127: Hashtable attributes=tempNode.attributes;
1128: int docID=0;
1129: int ver=0;
1130: if(attributes != null)
1131: {
1132: docID=Integer.parseInt((String)attributes.get("id"));
1133: ver=Integer.parseInt((String)attributes.get("ver"));
1134: doc.addData("docID",(String)attributes.get("id"));
1135: doc.addData("ver",(String)attributes.get("ver"));
1136: }
1137: bundle.docs.put(new Integer(docID),doc);
1138: bundle.documents.addElement(doc);
1139: // System.out.println("id " +docID+" ver "+ver);
1140: Node fileNode=root.find(rootNode+"/doc/file",new int[] {1,count,1});
1141: attributes=(fileNode==null)? null:fileNode.attributes;
1142: int fileID=0;
1143: int fileVer=0;
1144: if(attributes != null)
1145: {
1146: fileID=Integer.parseInt((String)attributes.get("id"));
1147: fileVer=Integer.parseInt((String)attributes.get("ver"));
1148: doc.addData("fileID",(String)attributes.get("id"));
1149: doc.addData("fileVer",(String)attributes.get("ver"));
1150: // System.out.println("fileid " +fileID+" ver "+fileVer);
1151: }
1152: // add all Document tags to actual Document
1153: for(int i=0;i<tempNode.contents.v.size();++i)
1154: {
1155: Node node=(Node)tempNode.contents.v.elementAt(i);
1156: if(node.type.equals("element"))
1157: {
1158: //System.out.println(node.name+" "+node.getCharacters()+" "+node.contents.v.size());
1159: if(node.contents.v.size()>1)
1160: {
1161: for(int j=0;j<node.contents.v.size();++j)
1162: {
1163: Node node2=(Node)node.contents.v.elementAt(j);
1164: if(node2.type.equals("element"))
1165: {
1166: doc.addData(node2.name,node2.getCharacters());
1167: //System.out.println(node2.name+" "+node2.getCharacters()+" "+node2.contents.v.size());
1168: }
1169: }
1170: } else doc.addData(node.name,node.getCharacters());
1171: }
1172: }
1173: bundle.docsLocator.put(doc.locator,doc);
1174:
1175: bundle.documents.addElement(doc);
1176: count++;
1177: } */
1178:
1179: // System.out.println(bundle.docs.size());
1180: // return rtTag;
1181: }
1182: public static Vector getXMLConfig(String xmlFile)
1183: {
1184: StringBuffer sb=null;
1185: try
1186: {
1187: // read XML Metadata from a file
1188: FileInputStream fi= new FileInputStream(xmlFile);
1189: InputStreamReader isr= new InputStreamReader(fi,"UTF-8");
1190: BufferedReader buffr = new BufferedReader(isr);
1191: sb=new StringBuffer();
1192: int c=0;
1193: while ((c = buffr.read()) != -1)
1194: {
1195: char ch=(char)c;
1196: sb.append(ch);
1197: // System.out.print((char)c);
1198: }
1199:
1200: } catch(Exception e) { e.printStackTrace();}
1201:
1202: boolean finished = false;
1203: // parse string and build document tree
1.4 rogo 1204: Xparse parser =new Xparse();
1205: parser.changeEntities = true;
1206: Node root = parser.parse(sb.toString());
1207: // printContents(root);
1.1 rogo 1208: Vector databases= new Vector();
1209: Vector tables = new Vector();
1210: Vector layouts = new Vector();
1211: Vector selects = new Vector();
1212: Vector creates = new Vector();
1.10 rogo 1213: Vector ids = new Vector();
1214:
1.5 rogo 1215: int mode = -1;
1.1 rogo 1216: try
1217: {
1218: Node tempNode = root.find("convert/source", new int[] { 1, 1 });
1219: if(tempNode==null) throw new Error("parse error source tag missing");
1220: System.out.println(tempNode.name);
1221: int length =countNodes(tempNode);
1222: for (int i = 1; i <= length; i++)
1223: {
1224:
1225: DBBean database = new DBBean();
1226: tables = new Vector();
1227: layouts = new Vector();
1228: selects = new Vector();
1229: creates = new Vector();
1.10 rogo 1230: ids = new Vector();
1.1 rogo 1231: // parse dataBase
1232: Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
1233: Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
1234: Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
1235: Node node3 = root.find("convert/source/database", new int[] { 1, 1, i});
1.5 rogo 1236: Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 });
1237:
1.1 rogo 1238: if(node3== null) throw new Error("parse error database tag missing");
1239: if(node==null) throw new Error("parse error url tag missing");
1240: if(node1==null) throw new Error("parse error user tag missing");
1241: if(node2==null) throw new Error("parse error password tag missing");
1242: String url=node.getCharacters();
1243: String user=node1.getCharacters();
1244: String password=node2.getCharacters();
1245: database.setURL(url.trim());
1246: database.setUserAndPasswd(user.trim(),password.trim());
1247: System.out.println(node.name + " " + node.getCharacters());
1248: System.out.println(node1.name + " " + node1.getCharacters());
1249: System.out.println(node2.name + " " + node2.getCharacters());
1.5 rogo 1250: String modeString = "";
1251: if (nodeMode == null)
1252: modeString = "convert";
1253: else
1254: modeString = nodeMode.getCharacters();
1255: if (modeString.equals("convert"))
1256: mode = DataBase.CONVERT_MODE;
1257: else
1258: if (modeString.equals("append"))
1259: mode = DataBase.APPEND_MODE;
1260: else
1261: if (modeString.equals("update"))
1262: mode = DataBase.UPDATE_MODE;
1263:
1264:
1.1 rogo 1265: // if(node3!=null)
1266: // System.out.println(node3.name);
1267:
1268: int length2= countNodes(node3);
1269:
1270: System.out.println("number of tables " +length2);
1271:
1272: for(int j=1;j<=length2;++j)
1273: {
1274: Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
1275: Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
1276: Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
1277: if(node4!=null)
1278: System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
1279: if(node5!=null)
1280: System.out.println(node5.name + " " + node5.getCharacters());
1281: if(node6!=null)
1282: System.out.println(node6.name + " " + node6.getCharacters());
1283: if(node4==null) throw new Error("parse error table tag missing");
1284: // if(node5==null) throw new Error("parse error select tag missing");
1285: // if(node6==null) throw new Error("parse error create tag missing");
1286: String name = (String)node4.attributes.get("name");
1287: String layout = (String)node4.attributes.get("layout");
1.10 rogo 1288: String id = (String)node4.attributes.get("id");
1289: System.out.println("id was "+id);
1290:
1.1 rogo 1291: if(name==null) throw new Error("parse error required table tag attribute name missing");
1292: if(layout==null) layout ="";
1.10 rogo 1293: if(id == null) id="";
1.1 rogo 1294: if(name.equals("")) throw new Error("parse error table tag attribute must not be empty");
1295: tables.add(name);
1296: layouts.add(layout);
1.10 rogo 1297: ids.add(id);
1.1 rogo 1298: String query = (node5==null) ? "":node5.getCharacters();
1299: if(query.equals("")) System.err.println("Warning empty select tag or select tag missing !!");
1300: query = (query.equals("")) ? "select * from "+database.getQC()+name+database.getQC():query;
1301: selects.add(query);
1302: if(node6!=null) creates.add(node6.getCharacters().trim());
1303: else
1304: creates.add("");
1305:
1306: }
1.10 rogo 1307: databases.add(new DataBase(database, tables, layouts, selects,creates,ids,mode));
1.1 rogo 1308: }
1309: DBBean database = new DBBean();
1310: // parse dataBase
1311: Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
1312: Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
1313: Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
1314: String url = node.getCharacters();
1315: String user = node1.getCharacters();
1316: String password = node2.getCharacters();
1317: System.out.println(url);
1318: database.setURL(url.trim());
1319: database.setUserAndPasswd(user.trim(), password.trim());
1.10 rogo 1320: databases.add(new DataBase(database,null,null,null,null,null,0));
1.1 rogo 1321: //databases.add(database);
1322: /* for (Iterator iter = databases.iterator(); iter.hasNext();)
1323: {
1324: DataBase db = (DataBase) iter.next();
1325: convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates);
1326:
1327: }*/
1328: // printContents(node3);
1329: // FM2SQL.fmInstance=new FM2SQL();
1330: } catch (Exception e)
1331: {
1332: // TODO Auto-generated catch block
1333: e.printStackTrace();
1334: }
1335: return databases;
1336: }
1337:
1338:
1339: private static int countNodes(Node tempNode)
1340: {
1341: int length = 0;
1342: for(int i=0;i<tempNode.contents.v.size();++i) {
1343: Node node=(Node)tempNode.contents.v.elementAt(i);
1344: if(node.type.equals("element"))
1345: {
1346: if(node.name.equals("database"))length++;
1347: if(node.name.equals("table"))length++;
1348: }
1349:
1350: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1351: }
1352: return length;
1353: }
1354: private static void printContents(Node root)
1355: {
1356:
1357:
1358: Vector contents=(root.index==null)? root.contents.v:root.index.v;
1359: for (int i=0;i<contents.size();++i)
1360: {
1361: Node n=(Node)contents.elementAt(i);
1362: if(n.type.equals("element"))
1363: {
1364: System.out.println("tag "+n.name);
1365: System.out.println(n.getCharacters());
1366: //contents=n.contents.v i=0;
1367: }
1368: // System.out.println(n.type);
1369:
1370:
1371:
1372: }
1373: }
1374: public static void readXMLFile(String xmlFile)
1375: {
1376: try
1377: {
1378: // read XML Metadata from a file
1379: FileInputStream fi= new FileInputStream(xmlFile);
1380: InputStreamReader isr= new InputStreamReader(fi,"UTF-8");
1381: BufferedReader buffr = new BufferedReader(isr);
1382: StringBuffer sb=new StringBuffer();
1383: int c=0;
1384: while ((c = buffr.read()) != -1)
1385: {
1386: char ch=(char)c;
1387: sb.append(ch);
1388: // System.out.print((char)c);
1389: }
1390: parseXMLConfig(sb);
1391: } catch(Exception e) { e.printStackTrace();}
1392: }
1393: public static class DataBase
1394: {
1395: DBBean bean;
1396: Vector creates;
1397: Vector selects;
1398: Vector layouts;
1399: Vector tables;
1.10 rogo 1400: Vector ids;
1.5 rogo 1401: final static int CONVERT_MODE = 1;
1402: final static int APPEND_MODE = 2;
1403: final static int UPDATE_MODE = 3;
1404: int mode = -1;
1405:
1.10 rogo 1406: public DataBase(DBBean bean,Vector tables,Vector layouts,Vector selects,Vector creates,Vector ids,int mode)
1.1 rogo 1407: {
1408: this.bean = bean;
1409: this.tables = tables;
1410: this.layouts = layouts;
1411: this.selects = selects;
1412: this.creates = creates;
1.10 rogo 1413: this.ids = ids;
1.5 rogo 1414: this.mode = mode;
1.14 rogo 1415: this.bean.setIDVector(ids);
1.1 rogo 1416: }
1417: public void exportToXML(BufferedWriter buffr) throws Exception
1418: {
1.14 rogo 1419: // ids=bean.getIDVector();
1.1 rogo 1420: buffr.write(" <database>\n");
1421: buffr.write(" <url>"+bean.url+"</url>\n");
1422: buffr.write(" <user>"+bean.user+"</user>\n");
1423: buffr.write(" <password>"+bean.passwd+"</password>\n");
1.5 rogo 1424: String modeString ="";
1425: if (mode == CONVERT_MODE)
1426: modeString = "convert";
1427: else
1428: if (mode == APPEND_MODE)
1429: modeString = "append";
1430: else
1431: if (mode == UPDATE_MODE)
1432: modeString = "update";
1433:
1434: buffr.write(" <mode>"+ modeString+"</mode>\n");
1.1 rogo 1435: int index = 0;
1436: while(index<tables.size())
1437: {
1438: String table=(String)tables.get(index);
1439: String layout=(String)layouts.get(index);
1440: String select=(String)selects.get(index);
1441: String create=(String)creates.get(index);
1.10 rogo 1442: String id=(String)ids.get(index);
1.1 rogo 1443:
1.14 rogo 1444: buffr.write(" <table name = \""+table+"\" layout = \""+layout+"\" id = \""+id+"\" >\n");
1.4 rogo 1445: buffr.write(" <select>"+ convertToEntities(select)+"</select>\n");
1.1 rogo 1446: if(!create.equals(""))
1447: buffr.write(" <create>"+create+" </create>\n");
1448: buffr.write(" </table>\n");
1449: index++;
1450: }
1451: buffr.write(" </database>\n");
1452: }
1453: public String toString() {return bean.url+" "+tables;}
1454:
1455: }
1456: public static String convertToUTF8(Object command)
1457: {
1458: String str = null;
1459: try
1460: {
1461: str = new String(command.toString().getBytes("UTF-8"));
1462: } catch (UnsupportedEncodingException e)
1463: {
1464: // TODO Auto-generated catch block
1465: e.printStackTrace();
1466: }
1467: return str;
1468: }
1469: public static void writeConfig(String file, DataBase source, DataBase destination) throws Exception
1470: {
1471: if(!file.toLowerCase().endsWith(".xml")) file+=".xml";
1472: File f = new File(file);
1473:
1474: FileOutputStream fout= new FileOutputStream(f);
1475: OutputStreamWriter outsw = new OutputStreamWriter(fout,"UTF-8");
1476: BufferedWriter buffw = new BufferedWriter(outsw);
1477: buffw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1478: buffw.newLine();
1479: buffw.write("<convert>\n");
1480: buffw.write(" <source>\n");
1481: source.exportToXML(buffw);
1482: buffw.write(" </source>\n");
1483: buffw.write("\n <destination>\n");
1484: destination.exportToXML(buffw);
1485: buffw.write(" </destination>\n");
1486: buffw.write("</convert>\n");
1487: buffw.close();
1488: }
1489: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>