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