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