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