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