Annotation of FM2SQL/Convert.java, revision 1.17
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.17 ! rogo 1074: if(mode!=DataBase.UPDATE_MODE)
1.9 rogo 1075: convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates,mode);
1.17 ! rogo 1076: else
! 1077: update(db.bean.url,database.url,db.tables,db.layouts,db.selects,db.creates,db.ids,mode);
! 1078:
1.1 rogo 1079: }
1080: // printContents(node3);
1081: // FM2SQL.fmInstance=new FM2SQL();
1082: } catch (Exception e)
1083: {
1084: // TODO Auto-generated catch block
1085: e.printStackTrace();
1086: }
1087: /*
1088: Node tempNode=root.find(rootNode,new int[] {1});
1089: if(tempNode==null) return rtTag;
1090: int count=1;
1091: /*
1092: for(int i=0;i<tempNode.contents.v.size();++i)
1093: {
1094: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1095: }*/
1096: /*
1097: if(tempNode.contents.v.size()==0) return rtTag;
1098: Node notNull=null;
1099: Node rtNode=root.find(rootNode+"/rt",new int[] {1,1});
1100: rtTag=(rtNode==null) ? "finished":rtNode.getCharacters();
1101: System.out.println("rt tag is "+ rtTag);
1102: int length=0;//(tempNode.contents.length()-1)/2;
1103: for(int i=0;i<tempNode.contents.v.size();++i) {
1104: Node node=(Node)tempNode.contents.v.elementAt(i);
1105: if(node.type.equals("element"))
1106: if(node.name.equals("doc"))length++;
1107: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1108: }
1109: if(length==0) rtTag="finished";
1110: while(count<=length)
1111: {
1112: //System.out.println(count+" "+length);
1113: tempNode=root.find(rootNode+"/doc",new int[] {1,count});
1114: if(tempNode==null)
1115: {
1116: rtTag="finished";
1117: break;
1118: }
1119: Bundle.Document doc = new Bundle.Document();
1120: Hashtable attributes=tempNode.attributes;
1121: int docID=0;
1122: int ver=0;
1123: if(attributes != null)
1124: {
1125: docID=Integer.parseInt((String)attributes.get("id"));
1126: ver=Integer.parseInt((String)attributes.get("ver"));
1127: doc.addData("docID",(String)attributes.get("id"));
1128: doc.addData("ver",(String)attributes.get("ver"));
1129: }
1130: bundle.docs.put(new Integer(docID),doc);
1131: bundle.documents.addElement(doc);
1132: // System.out.println("id " +docID+" ver "+ver);
1133: Node fileNode=root.find(rootNode+"/doc/file",new int[] {1,count,1});
1134: attributes=(fileNode==null)? null:fileNode.attributes;
1135: int fileID=0;
1136: int fileVer=0;
1137: if(attributes != null)
1138: {
1139: fileID=Integer.parseInt((String)attributes.get("id"));
1140: fileVer=Integer.parseInt((String)attributes.get("ver"));
1141: doc.addData("fileID",(String)attributes.get("id"));
1142: doc.addData("fileVer",(String)attributes.get("ver"));
1143: // System.out.println("fileid " +fileID+" ver "+fileVer);
1144: }
1145: // add all Document tags to actual Document
1146: for(int i=0;i<tempNode.contents.v.size();++i)
1147: {
1148: Node node=(Node)tempNode.contents.v.elementAt(i);
1149: if(node.type.equals("element"))
1150: {
1151: //System.out.println(node.name+" "+node.getCharacters()+" "+node.contents.v.size());
1152: if(node.contents.v.size()>1)
1153: {
1154: for(int j=0;j<node.contents.v.size();++j)
1155: {
1156: Node node2=(Node)node.contents.v.elementAt(j);
1157: if(node2.type.equals("element"))
1158: {
1159: doc.addData(node2.name,node2.getCharacters());
1160: //System.out.println(node2.name+" "+node2.getCharacters()+" "+node2.contents.v.size());
1161: }
1162: }
1163: } else doc.addData(node.name,node.getCharacters());
1164: }
1165: }
1166: bundle.docsLocator.put(doc.locator,doc);
1167:
1168: bundle.documents.addElement(doc);
1169: count++;
1170: } */
1171:
1172: // System.out.println(bundle.docs.size());
1173: // return rtTag;
1174: }
1175: public static Vector getXMLConfig(String xmlFile)
1176: {
1177: StringBuffer sb=null;
1178: try
1179: {
1180: // read XML Metadata from a file
1181: FileInputStream fi= new FileInputStream(xmlFile);
1182: InputStreamReader isr= new InputStreamReader(fi,"UTF-8");
1183: BufferedReader buffr = new BufferedReader(isr);
1184: sb=new StringBuffer();
1185: int c=0;
1186: while ((c = buffr.read()) != -1)
1187: {
1188: char ch=(char)c;
1189: sb.append(ch);
1190: // System.out.print((char)c);
1191: }
1192:
1193: } catch(Exception e) { e.printStackTrace();}
1194:
1195: boolean finished = false;
1196: // parse string and build document tree
1.4 rogo 1197: Xparse parser =new Xparse();
1198: parser.changeEntities = true;
1199: Node root = parser.parse(sb.toString());
1200: // printContents(root);
1.1 rogo 1201: Vector databases= new Vector();
1202: Vector tables = new Vector();
1203: Vector layouts = new Vector();
1204: Vector selects = new Vector();
1205: Vector creates = new Vector();
1.10 rogo 1206: Vector ids = new Vector();
1207:
1.5 rogo 1208: int mode = -1;
1.1 rogo 1209: try
1210: {
1211: Node tempNode = root.find("convert/source", new int[] { 1, 1 });
1212: if(tempNode==null) throw new Error("parse error source tag missing");
1213: System.out.println(tempNode.name);
1214: int length =countNodes(tempNode);
1215: for (int i = 1; i <= length; i++)
1216: {
1217:
1218: DBBean database = new DBBean();
1219: tables = new Vector();
1220: layouts = new Vector();
1221: selects = new Vector();
1222: creates = new Vector();
1.10 rogo 1223: ids = new Vector();
1.1 rogo 1224: // parse dataBase
1225: Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
1226: Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
1227: Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
1228: Node node3 = root.find("convert/source/database", new int[] { 1, 1, i});
1.5 rogo 1229: Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 });
1230:
1.1 rogo 1231: if(node3== null) throw new Error("parse error database tag missing");
1232: if(node==null) throw new Error("parse error url tag missing");
1233: if(node1==null) throw new Error("parse error user tag missing");
1234: if(node2==null) throw new Error("parse error password tag missing");
1235: String url=node.getCharacters();
1236: String user=node1.getCharacters();
1237: String password=node2.getCharacters();
1238: database.setURL(url.trim());
1239: database.setUserAndPasswd(user.trim(),password.trim());
1240: System.out.println(node.name + " " + node.getCharacters());
1241: System.out.println(node1.name + " " + node1.getCharacters());
1242: System.out.println(node2.name + " " + node2.getCharacters());
1.5 rogo 1243: String modeString = "";
1244: if (nodeMode == null)
1245: modeString = "convert";
1246: else
1247: modeString = nodeMode.getCharacters();
1248: if (modeString.equals("convert"))
1249: mode = DataBase.CONVERT_MODE;
1250: else
1251: if (modeString.equals("append"))
1252: mode = DataBase.APPEND_MODE;
1253: else
1254: if (modeString.equals("update"))
1255: mode = DataBase.UPDATE_MODE;
1256:
1257:
1.1 rogo 1258: // if(node3!=null)
1259: // System.out.println(node3.name);
1260:
1261: int length2= countNodes(node3);
1262:
1263: System.out.println("number of tables " +length2);
1264:
1265: for(int j=1;j<=length2;++j)
1266: {
1267: Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
1268: Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
1269: Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
1270: if(node4!=null)
1271: System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
1272: if(node5!=null)
1273: System.out.println(node5.name + " " + node5.getCharacters());
1274: if(node6!=null)
1275: System.out.println(node6.name + " " + node6.getCharacters());
1276: if(node4==null) throw new Error("parse error table tag missing");
1277: // if(node5==null) throw new Error("parse error select tag missing");
1278: // if(node6==null) throw new Error("parse error create tag missing");
1279: String name = (String)node4.attributes.get("name");
1280: String layout = (String)node4.attributes.get("layout");
1.10 rogo 1281: String id = (String)node4.attributes.get("id");
1282: System.out.println("id was "+id);
1283:
1.1 rogo 1284: if(name==null) throw new Error("parse error required table tag attribute name missing");
1285: if(layout==null) layout ="";
1.10 rogo 1286: if(id == null) id="";
1.1 rogo 1287: if(name.equals("")) throw new Error("parse error table tag attribute must not be empty");
1288: tables.add(name);
1289: layouts.add(layout);
1.10 rogo 1290: ids.add(id);
1.1 rogo 1291: String query = (node5==null) ? "":node5.getCharacters();
1292: if(query.equals("")) System.err.println("Warning empty select tag or select tag missing !!");
1293: query = (query.equals("")) ? "select * from "+database.getQC()+name+database.getQC():query;
1294: selects.add(query);
1295: if(node6!=null) creates.add(node6.getCharacters().trim());
1296: else
1297: creates.add("");
1298:
1299: }
1.10 rogo 1300: databases.add(new DataBase(database, tables, layouts, selects,creates,ids,mode));
1.1 rogo 1301: }
1302: DBBean database = new DBBean();
1303: // parse dataBase
1304: Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
1305: Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
1306: Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
1307: String url = node.getCharacters();
1308: String user = node1.getCharacters();
1309: String password = node2.getCharacters();
1310: System.out.println(url);
1311: database.setURL(url.trim());
1312: database.setUserAndPasswd(user.trim(), password.trim());
1.10 rogo 1313: databases.add(new DataBase(database,null,null,null,null,null,0));
1.1 rogo 1314: //databases.add(database);
1315: /* for (Iterator iter = databases.iterator(); iter.hasNext();)
1316: {
1317: DataBase db = (DataBase) iter.next();
1318: convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates);
1319:
1320: }*/
1321: // printContents(node3);
1322: // FM2SQL.fmInstance=new FM2SQL();
1323: } catch (Exception e)
1324: {
1325: // TODO Auto-generated catch block
1326: e.printStackTrace();
1327: }
1328: return databases;
1329: }
1330:
1331:
1332: private static int countNodes(Node tempNode)
1333: {
1334: int length = 0;
1335: for(int i=0;i<tempNode.contents.v.size();++i) {
1336: Node node=(Node)tempNode.contents.v.elementAt(i);
1337: if(node.type.equals("element"))
1338: {
1339: if(node.name.equals("database"))length++;
1340: if(node.name.equals("table"))length++;
1341: }
1342:
1343: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1344: }
1345: return length;
1346: }
1347: private static void printContents(Node root)
1348: {
1349:
1350:
1351: Vector contents=(root.index==null)? root.contents.v:root.index.v;
1352: for (int i=0;i<contents.size();++i)
1353: {
1354: Node n=(Node)contents.elementAt(i);
1355: if(n.type.equals("element"))
1356: {
1357: System.out.println("tag "+n.name);
1358: System.out.println(n.getCharacters());
1359: //contents=n.contents.v i=0;
1360: }
1361: // System.out.println(n.type);
1362:
1363:
1364:
1365: }
1366: }
1367: public static void readXMLFile(String xmlFile)
1368: {
1369: try
1370: {
1371: // read XML Metadata from a file
1372: FileInputStream fi= new FileInputStream(xmlFile);
1373: InputStreamReader isr= new InputStreamReader(fi,"UTF-8");
1374: BufferedReader buffr = new BufferedReader(isr);
1375: StringBuffer sb=new StringBuffer();
1376: int c=0;
1377: while ((c = buffr.read()) != -1)
1378: {
1379: char ch=(char)c;
1380: sb.append(ch);
1381: // System.out.print((char)c);
1382: }
1383: parseXMLConfig(sb);
1384: } catch(Exception e) { e.printStackTrace();}
1385: }
1386: public static class DataBase
1387: {
1388: DBBean bean;
1389: Vector creates;
1390: Vector selects;
1391: Vector layouts;
1392: Vector tables;
1.10 rogo 1393: Vector ids;
1.5 rogo 1394: final static int CONVERT_MODE = 1;
1395: final static int APPEND_MODE = 2;
1396: final static int UPDATE_MODE = 3;
1397: int mode = -1;
1398:
1.10 rogo 1399: public DataBase(DBBean bean,Vector tables,Vector layouts,Vector selects,Vector creates,Vector ids,int mode)
1.1 rogo 1400: {
1401: this.bean = bean;
1402: this.tables = tables;
1403: this.layouts = layouts;
1404: this.selects = selects;
1405: this.creates = creates;
1.10 rogo 1406: this.ids = ids;
1.5 rogo 1407: this.mode = mode;
1.14 rogo 1408: this.bean.setIDVector(ids);
1.1 rogo 1409: }
1410: public void exportToXML(BufferedWriter buffr) throws Exception
1411: {
1.14 rogo 1412: // ids=bean.getIDVector();
1.1 rogo 1413: buffr.write(" <database>\n");
1414: buffr.write(" <url>"+bean.url+"</url>\n");
1415: buffr.write(" <user>"+bean.user+"</user>\n");
1416: buffr.write(" <password>"+bean.passwd+"</password>\n");
1.5 rogo 1417: String modeString ="";
1418: if (mode == CONVERT_MODE)
1419: modeString = "convert";
1420: else
1421: if (mode == APPEND_MODE)
1422: modeString = "append";
1423: else
1424: if (mode == UPDATE_MODE)
1425: modeString = "update";
1426:
1427: buffr.write(" <mode>"+ modeString+"</mode>\n");
1.1 rogo 1428: int index = 0;
1429: while(index<tables.size())
1430: {
1431: String table=(String)tables.get(index);
1432: String layout=(String)layouts.get(index);
1433: String select=(String)selects.get(index);
1434: String create=(String)creates.get(index);
1.10 rogo 1435: String id=(String)ids.get(index);
1.1 rogo 1436:
1.14 rogo 1437: buffr.write(" <table name = \""+table+"\" layout = \""+layout+"\" id = \""+id+"\" >\n");
1.4 rogo 1438: buffr.write(" <select>"+ convertToEntities(select)+"</select>\n");
1.1 rogo 1439: if(!create.equals(""))
1440: buffr.write(" <create>"+create+" </create>\n");
1441: buffr.write(" </table>\n");
1442: index++;
1443: }
1444: buffr.write(" </database>\n");
1445: }
1446: public String toString() {return bean.url+" "+tables;}
1447:
1448: }
1449: public static String convertToUTF8(Object command)
1450: {
1451: String str = null;
1452: try
1453: {
1454: str = new String(command.toString().getBytes("UTF-8"));
1455: } catch (UnsupportedEncodingException e)
1456: {
1457: // TODO Auto-generated catch block
1458: e.printStackTrace();
1459: }
1460: return str;
1461: }
1462: public static void writeConfig(String file, DataBase source, DataBase destination) throws Exception
1463: {
1464: if(!file.toLowerCase().endsWith(".xml")) file+=".xml";
1465: File f = new File(file);
1466:
1467: FileOutputStream fout= new FileOutputStream(f);
1468: OutputStreamWriter outsw = new OutputStreamWriter(fout,"UTF-8");
1469: BufferedWriter buffw = new BufferedWriter(outsw);
1470: buffw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1471: buffw.newLine();
1472: buffw.write("<convert>\n");
1473: buffw.write(" <source>\n");
1474: source.exportToXML(buffw);
1475: buffw.write(" </source>\n");
1476: buffw.write("\n <destination>\n");
1477: destination.exportToXML(buffw);
1478: buffw.write(" </destination>\n");
1479: buffw.write("</convert>\n");
1480: buffw.close();
1481: }
1482: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>