1: /*
2: * Convert.java -- Converter class - Filemaker to SQL Converter
3: * Copyright (C) 2003 Robert Gordesch (rogo@mpiwg-berlin.mpg.de)
4: * This program is free software; you can redistribute it and/or modify it
5: * under the terms of the GNU General Public License as published by the Free
6: * Software Foundation; either version 2 of the License, or (at your option)
7: * any later version. Please read license.txt for the full details. A copy of
8: * the GPL may be found at http://www.gnu.org/copyleft/lgpl.html You should
9: * have received a copy of the GNU General Public License along with this
10: * program; if not, write to the Free Software Foundation, Inc., 59 Temple
11: * Place, Suite 330, Boston, MA 02111-1307 USA Created on 15.09.2003 by
12: * rogo
13: */
14:
15: import java.awt.Cursor;
16: import java.io.BufferedReader;
17: import java.io.BufferedWriter;
18: import java.io.File;
19: import java.io.FileInputStream;
20: import java.io.FileNotFoundException;
21: import java.io.FileOutputStream;
22: import java.io.InputStreamReader;
23: import java.io.OutputStreamWriter;
24: import java.io.PrintStream;
25: import java.io.UnsupportedEncodingException;
26: import java.sql.PreparedStatement;
27: import java.sql.SQLException;
28: import java.sql.Statement;
29: import java.sql.Types;
30: import java.util.ArrayList;
31: import java.util.Iterator;
32: import java.util.List;
33: import java.util.StringTokenizer;
34: import java.util.TreeSet;
35: import java.util.Vector;
36:
37: import com.exploringxml.xml.Node;
38: import com.exploringxml.xml.Xparse;
39:
40: class Convert
41: {
42: static DBBean bean = new DBBean();
43: static DBBean beanDest = new DBBean();
44:
45: static String user = "", passwd = "e1nste1n";
46: static String userDest = "postgres", passwdDest = "rogo";
47: static boolean batchRun = false;
48: static Vector databases = new Vector();
49: final static int numHits = 5000;
50: final static int numIntervalls = 2;
51: public static void main(String args[])
52: {
53: /* try
54: {
55: //byte[] b = "ö".getBytes("UTF-8");
56: // System.out.println("QueryString " +b[0]+" "+b[1]+(new String(b).getBytes()[0])+" "+new String(b).getBytes()[1]);
57: //System.out.println(new String(b,"UTF-8"));
58: } catch (UnsupportedEncodingException e)
59: {
60: e.printStackTrace();
61: }*/
62: FileOutputStream file = null;
63: if (args.length != 1)
64: {
65: System.out.println("Usage: java Convert <xml config file>");
66: System.exit(-1);
67: }
68: if (!(new File(args[0]).exists()))
69: System.exit(0);
70: try
71: {
72: file = new FileOutputStream("./log.txt");
73: } catch (FileNotFoundException e1)
74: {
75: e1.printStackTrace();
76: }
77: PrintStream stream = new PrintStream(file);
78: System.setOut(stream);
79: System.setErr(stream);
80: readXMLFile(args[0]);
81: System.out.println("Finished!");
82: //convert("jdbc:fmpro:http://141.14.237.74:8050","jdbc:postgresql://erebos/test",null,null);
83: }
84: public static void convertBatch(DBBean source, DBBean destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception
85: {
86: bean = source;
87: beanDest = destination;
88: convert(null, null, names, layouts, selects, creates, ids, mode, delimiter);
89: if (true)
90: return;
91: StringBuffer command = null;
92: try
93: {
94: bean.setConnection(source.url);
95: if (names == null)
96: names = bean.getTableNames();
97: //Collections.sort(names);
98: int tbIndex = 1;
99:
100: for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
101: {
102: Vector[] result = null;
103: try
104: {
105: String query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
106: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
107: query = (selects != null) ? selects.get(tbIndex).toString() : query;
108: //if vectors[1].get(i) != null)
109: if (!layout.equals(""))
110: {
111: System.out.println("before " + query + " table" + names.get(tbIndex));
112: layout = " layout " + bean.getQC() + layout + bean.getQC();
113: String name = names.get(tbIndex).toString();
114: StringBuffer queryLayout = new StringBuffer(query);
115: queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
116: query = queryLayout.toString();
117: System.out.println("added layout " + query);
118:
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 (mode == Convert.DataBase.CONVERT_MODE)
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: } else 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: } else
185: command = new StringBuffer().append(creates.get(tbIndex).toString());
186:
187: System.out.println(command);
188: // System.exit(0);
189: //command.append(DBBean.getQC());
190: stm.executeUpdate(command.toString());
191:
192: }
193: }
194: Vector row = null;
195: command = new StringBuffer();
196:
197: command.append("INSERT INTO ");
198: command.append(beanDest.getQC());
199: command.append(convertText((String) names.get(tbIndex)));
200: command.append(beanDest.getQC());
201: command.append(" values ( ");
202:
203: for (int i = 0; i < bean.getColumnNames().size() - 1; ++i)
204: command.append("?,");
205: command.append("?)");
206: PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
207: System.out.println(command);
208: while ((row = bean.getNextRow()) != null)
209: {
210: //print rows
211: Object obj = null;
212: for (int k = 0; k < row.size(); ++k)
213: {
214: obj = row.get(k);
215: if (obj instanceof ArrayList)
216: obj = formatFileMakerArray((List) obj, "\n");
217: String str = (obj == null) ? "NULL" : obj.toString();
218: if (!str.equals("NULL"))
219: pstm.setString(k + 1, str);
220: else
221: pstm.setNull(k + 1, Types.NULL);
222: }
223: pstm.execute();
224:
225: } // to for loop
226:
227: }
228: } catch (Exception e)
229: {
230: System.out.println("Error while connecting to database " + e);
231: //dialog.setVisible(false);
232: //dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
233: //FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
234: java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
235: java.io.PrintStream stream = new java.io.PrintStream(b);
236: stream.print(command + "\n\n");
237: e.printStackTrace(stream);
238: System.err.println(b);
239: //FM2SQL.showErrorDialog(b.toString(), "Error occured !");
240:
241: }
242: // dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
243: //FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
244:
245: // dialog.setVisible(false);
246: }
247: public static String formatFileMakerArray(List list, String delimiter)
248: {
249: StringBuffer formattedString = new StringBuffer();
250: for (int i = 0; i < list.size(); ++i)
251: {
252: formattedString.append(list.get(i).toString());
253: if (i < list.size() - 1)
254: formattedString.append(delimiter);
255: }
256: return formattedString.toString();
257: }
258: /**
259: * Method for SQL UPDATE
260: * @param source
261: * @param destination
262: * @param names
263: * @param layouts
264: * @param selects
265: * @param creates
266: * @param ids
267: * @param mode
268: * @throws Exception
269: */
270: public static void update(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode) throws Exception
271: {
272: FM2SQL.ProgressDialog dialog = null;
273: if (FM2SQL.fmInstance != null)
274: {
275: dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance, bean);
276: dialog.setTitle("Conversion running ...");
277: dialog.title.setText("Getting table data ...");
278: dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y + (FM2SQL.fmInstance.getHeight() - 250) / 2);
279: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
280: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
281: dialog.thread = Thread.currentThread();
282: }
283: // setting user and passwd
284: bean.setUserAndPasswd(user, passwd);
285: // setting user and passwd
286: beanDest.setUserAndPasswd(userDest, passwdDest);
287: if (dialog != null)
288: dialog.setSize(400, 250);
289: StringBuffer command = null;
290: String query = null;
291: try
292: {
293: //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");
294: //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
295: bean.setConnection(source);
296: if (names == null)
297: names = bean.getTableNames();
298: // Collections.sort(names);
299: int tbIndex = 1;
300:
301: // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
302: for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
303: {
304: Vector[] result = null;
305: String destTableName = "";
306: try
307: {
308: query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
309: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
310: query = (selects != null) ? selects.get(tbIndex).toString() : query;
311: //if vectors[1].get(i) != null)
312: if (layout != "")
313: {
314: layout = " layout " + bean.getQC() + layout + bean.getQC();
315: String name = names.get(tbIndex).toString();
316: StringBuffer queryLayout = new StringBuffer(query);
317: queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
318: query = queryLayout.toString();
319: System.out.println("added layout " + query);
320:
321: }
322: dialog.title.setText("Getting table data ...");
323: dialog.table.setText(names.get(tbIndex).toString());
324: dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
325: dialog.show();
326: bean.getConnection();
327: bean.makeQuery(query, 0);
328: } catch (Exception e)
329: {
330: continue;
331: }
332: // determine destTableName from createStatement or from source table name
333: if (!creates.get(tbIndex).equals(""))
334: {
335: String create = creates.get(tbIndex).toString().toLowerCase();
336: int fromIndex = create.indexOf("table") + 5;
337: int toIndex = create.indexOf("(");
338: destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "").trim();
339: System.out.println("destTable " + destTableName);
340:
341: } else
342: destTableName = convertText(names.get(tbIndex).toString());
343:
344: //beanDest.setConnection("jdbc:postgresql://erebos/test3");
345: beanDest.setConnection(destination);
346:
347: Statement stm = beanDest.getConnection().createStatement();
348:
349: Vector tables = beanDest.getTableNames();
350: // Collections.sort(tables);
351: System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
352: tables = beanDest.getTableNames();
353: // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
354: stm = beanDest.getConnection().createStatement();
355: // System.exit(0);
356:
357: if (dialog != null)
358: dialog.title.setText("Updating table data ...");
359:
360: int j = -1;
361:
362: Vector row = null;
363: command = new StringBuffer();
364:
365: command.append("UPDATE ");
366: command.append(beanDest.getQC());
367: command.append(destTableName);
368: //command.append(convertText((String) names.get(tbIndex)));
369: command.append(beanDest.getQC());
370: command.append(" SET ");
371:
372: int size = bean.getColumnNames().size();
373: for (int i = 0; i < size - 1; ++i)
374: command.append(beanDest.getQC() + convertText((String) bean.getColumnNames().get(i)) + beanDest.getQC() + " = ? ,");
375: command.append(convertText((String) bean.getColumnNames().get(size - 1)) + " = ? ");
376: command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " = ?");
377: PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
378: System.out.println(command + " " + tbIndex);
379: int rowCount = bean.getRowCount(query);
380: int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex));
381: while ((row = bean.getNextRow()) != null)
382: {
383: j++;
384: //print rows
385: Object obj = null;
386: /* for(int k=0;k<row.size()-1;++k)
387: {
388: obj = row.get(k);
389: //System.out.println("row "+obj+" "+k);
390: if(obj!=null&&!(obj instanceof ArrayList))
391: command.append("'"+convertUml(obj.toString())+"',");
392: else if(obj!=null&& obj instanceof ArrayList)
393: command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',");
394: else command.append("NULL,");
395: }
396: obj = row.get(row.size() - 1);
397: if (obj != null && !(obj instanceof ArrayList))
398: command.append("'"+convertUml(obj.toString())+"')");
399: else
400: if(obj!=null&& obj instanceof ArrayList)
401: command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')"); //command.append(obj.toString()+")");
402: else command.append("NULL)");
403: */
404: //command.append("'"+row.get(row.size()-1)+"')");
405: //command.append(" )");
406: // for(int k=0;k<row.size();++k)
407:
408: // System.out.println();
409: // System.out.println(command+" "+j+" "+row.size()+" "+ ((Vector)result2[0].get(j)).size());
410: // System.out.println(command);
411: for (int k = 0; k < row.size(); ++k)
412: {
413: obj = row.get(k);
414: if (obj instanceof ArrayList)
415: obj = ((List) obj).get(0);
416: String str = (obj == null) ? "NULL" : obj.toString();
417: if (!str.equals("NULL"))
418: pstm.setString(k + 1, str);
419: else
420: pstm.setNull(k + 1, Types.NULL);
421: }
422: pstm.setString(row.size() + 1, row.get(idIndex).toString());
423: //System.out.println(pstm.toString());
424: // System.exit(0);
425: pstm.execute();
426: //stm.executeUpdate(command.toString());
427: if (dialog != null)
428: dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
429: // System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j);
430: command = null;
431: } // to for loop
432:
433: }
434: } catch (Exception e)
435: {
436: System.out.println("Error while connecting to database " + e);
437: if (dialog != null)
438: {
439: dialog.setVisible(false);
440: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
441: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
442: }
443: java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
444: java.io.PrintStream stream = new java.io.PrintStream(b);
445: stream.print(command + "\n\n");
446: e.printStackTrace(stream);
447: FM2SQL.showErrorDialog(b.toString(), "Error occured !");
448:
449: }
450: if (dialog != null)
451: {
452: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
453: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
454:
455: dialog.setVisible(false);
456: }
457:
458: }
459: /**
460: * transfers the specified array of tables to the destination database
461: and creates the table if it does not exist if it exists and mode is not append the table is dropped
462:
463: * @param source
464: * @param destination
465: * @param names
466: * @param layouts
467: * @param selects
468: * @param creates
469: * @param ids
470: * @param mode
471: * @throws Exception
472: */
473:
474: public static void convert(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception
475: {
476:
477: FM2SQL.ProgressDialog dialog = null;
478:
479: if (FM2SQL.fmInstance != null)
480: {
481: dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance, bean);
482: dialog.setTitle("Conversion running ...");
483: dialog.title.setText("Getting table data ...");
484: dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y + (FM2SQL.fmInstance.getHeight() - 250) / 2);
485: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
486: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
487: dialog.thread = Thread.currentThread();
488: dialog.setSize(400, 250);
489: }
490: java.util.TreeSet myIds = new TreeSet();
491: int deltaID = 1;
492: String idField = "";
493: String destTableName = "";
494: String[] fieldNames = null;
495: if (source != null && destination != null)
496: {
497: // setting user and passwd
498: bean.setUserAndPasswd(user, passwd);
499: // setting user and passwd
500: beanDest.setUserAndPasswd(userDest, passwdDest);
501: }
502: StringBuffer command = null;
503: String query = null;
504: try
505: {
506: if (source != null)
507: bean.setConnection(source);
508: else
509: bean.setConnection(bean.url);
510:
511: if (names == null)
512: names = bean.getTableNames();
513: // Collections.sort(names);
514: int tbIndex = 1;
515:
516: // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
517: for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
518: {
519: Vector[] result = null;
520: try
521: {
522: query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
523: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
524: query = (selects != null) ? selects.get(tbIndex).toString() : query;
525: //if vectors[1].get(i) != null)
526: if (layout != "")
527: {
528: layout = " layout " + bean.getQC() + layout + bean.getQC();
529: String name = names.get(tbIndex).toString();
530: StringBuffer queryLayout = new StringBuffer(query);
531: queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
532: query = queryLayout.toString();
533: System.out.println("added layout " + query);
534:
535: }
536: // if ( layout!= "")
537: // query += " layout " + bean.getQC() + layout + bean.getQC();
538: if (dialog != null)
539: {
540: dialog.title.setText("Reading table data ...");
541: dialog.table.setText(names.get(tbIndex).toString());
542: dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
543: dialog.show();
544: }
545: //result = bean.getQueryData(query, dialog, 0);
546: bean.getConnection();
547: bean.makeQuery(query, 50);
548: idField = ids.get(tbIndex).toString();
549:
550: } catch (Exception e)
551: {
552: System.out.println(e);
553: continue;
554: }
555: if (destination != null)
556: beanDest.setConnection(destination);
557: else
558: beanDest.setConnection(beanDest.url);
559: Statement stm = beanDest.getConnection().createStatement();
560:
561: Vector tables = beanDest.getTableNames();
562: // Collections.sort(tables);
563: System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
564: tables = beanDest.getTableNames();
565: // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
566: stm = beanDest.getConnection().createStatement();
567: // System.exit(0);
568:
569: // determine destTableName from createStatement or from source table name
570: if (!creates.get(tbIndex).equals(""))
571: {
572: String create = creates.get(tbIndex).toString().toLowerCase();
573: int fromIndex = create.indexOf("table") + 5;
574: int toIndex = create.indexOf("(");
575: int endIndex = create.indexOf(")", toIndex);
576:
577: destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "").trim();
578: System.out.println("destTable " + destTableName);
579: // retrieve field_names from select statement
580: if (query.indexOf("*") < 0)
581: {
582: int selectEndIndex = query.indexOf("from");
583: StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");
584: int numFields = tokenizer.countTokens();
585: fieldNames = new String[numFields];
586: int fieldIndex = 0;
587: while (tokenizer.hasMoreTokens())
588: {
589: String fieldName = tokenizer.nextToken().trim();
590: fieldNames[fieldIndex] = convertText(fieldName);
591: System.out.println(fieldNames[fieldIndex]);
592: fieldIndex++;
593: }
594:
595: } else
596: {
597: // use create statement for field names
598: StringTokenizer tokenizer = new StringTokenizer(create.substring(toIndex + 1, endIndex), ",");
599: int numFields = tokenizer.countTokens();
600: fieldNames = new String[numFields];
601: int fieldIndex = 0;
602: while (tokenizer.hasMoreTokens())
603: {
604: String fieldName = tokenizer.nextToken().trim();
605: int index = fieldName.lastIndexOf(" ");
606: fieldNames[fieldIndex] = fieldName.substring(0, index);
607: System.out.println(fieldNames[fieldIndex]);
608: fieldIndex++;
609: }
610: }
611: } else
612: {
613: destTableName = convertText(names.get(tbIndex).toString());
614:
615: // retrieve field_names from select statement
616: if (query.indexOf("*") < 0)
617: {
618: int selectEndIndex = query.indexOf("from");
619: StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");
620: int numFields = tokenizer.countTokens();
621: fieldNames = new String[numFields];
622: int fieldIndex = 0;
623: while (tokenizer.hasMoreTokens())
624: {
625: String fieldName = tokenizer.nextToken().trim();
626: fieldNames[fieldIndex] = convertText(fieldName);
627: // System.out.println("field "+ fieldNames[fieldIndex]);
628: fieldIndex++;
629: }
630:
631: } else
632: {
633: Vector fieldNamesVec = bean.getColumnNames();
634: fieldNames = new String[fieldNamesVec.size()];
635: int fieldIndex = -1;
636: for (Iterator iter = fieldNamesVec.iterator(); iter.hasNext();)
637: {
638: String element = (String) iter.next();
639: fieldNames[++fieldIndex] = bean.getQC() + convertText(element) + bean.getQC();
640: // System.out.println("field " + fieldNames[fieldIndex]);
641: }
642: }
643: }
644: if (mode == Convert.DataBase.CONVERT_MODE)
645: {
646:
647: if (tables.indexOf(destTableName) >= 0)
648: {
649: stm.executeUpdate("drop table " + beanDest.getQC() + destTableName + beanDest.getQC());
650: tables.remove(destTableName);
651: System.out.println("dropped table" + destTableName);
652:
653: }
654: /*
655: if(destTableName.equals(""))
656: if (tables.indexOf(names.get(tbIndex)) >= 0)
657: {
658: stm.executeUpdate("drop table " + beanDest.getQC() + names.get(tbIndex) + beanDest.getQC());
659: tables.remove((String) names.get(tbIndex));
660: System.out.println("dropped table" + names.get(tbIndex));
661: } else if (tables.indexOf(convertText(names.get(tbIndex).toString())) >= 0)
662: {
663: stm.executeUpdate("drop table " + beanDest.getQC() + convertText((String) names.get(tbIndex)) + beanDest.getQC());
664: tables.remove(convertText((String) names.get(tbIndex)));
665: System.out.println("dropped table" + names.get(tbIndex));
666: }
667: */
668: if ((tables.indexOf(destTableName) < 0)) //&& tables.indexOf(names.get(tbIndex)) < 0 && tables.indexOf(convertText(names.get(tbIndex).toString())) < 0 )
669: {
670:
671: if (creates.get(tbIndex).equals("") || creates.get(tbIndex).toString().toLowerCase().indexOf("create") < 0)
672: {
673: System.out.println("Warning empty or invalid create statement - creating one for you\n");
674:
675: command = new StringBuffer(50);
676: command.append("CREATE TABLE ");
677: command.append(beanDest.getQC());
678: command.append(convertText((String) names.get(tbIndex)));
679: command.append(beanDest.getQC());
680: command.append("(");
681: String type = null;
682: Vector columnNames = bean.getColumnNames();
683: for (int i = 0; i < columnNames.size() - 1; ++i)
684: {
685: type = bean.metaData.getColumnTypeName(i + 1);
686: // System.out.println(i+" "+result[1].get(i)+" "+type);
687: type = (type.equals("NUMBER")) ? "INT4" : type;
688: type = (type.equals("CONTAINER")) ? "TEXT" : type;
689:
690: command.append(beanDest.getQC() + convertText((String) columnNames.get(i)) + beanDest.getQC() + " " + type + ", ");
691: }
692: type = bean.metaData.getColumnTypeName(columnNames.size());
693: type = (type.equals("NUMBER")) ? "INT4" : type;
694: type = (type.equals("CONTAINER")) ? "TEXT" : type;
695: command.append(beanDest.getQC() + convertText((String) columnNames.get(columnNames.size() - 1)) + beanDest.getQC() + " " + type);
696: command.append(" )");
697:
698: // System.out.println(command);
699: // System.exit(0);
700: //command.append(DBBean.getQC());
701: } else
702: command = new StringBuffer().append(creates.get(tbIndex).toString().toLowerCase());
703: stm.executeUpdate(command.toString());
704:
705: }
706: }
707: if (dialog != null)
708: dialog.title.setText("Writing table data ...");
709:
710: // prepare the insert statement
711: int j = -1;
712: Vector row = null;
713: command = new StringBuffer();
714:
715: command.append("INSERT INTO ");
716: command.append(beanDest.getQC());
717: command.append(destTableName); //convertText((String) names.get(tbIndex)));
718: command.append(beanDest.getQC());
719: command.append(" (");
720: for (int i = 0; i < fieldNames.length; i++)
721: {
722: command.append(fieldNames[i]);
723: if (i < fieldNames.length - 1)
724: command.append(",");
725: }
726: command.append(") ");
727:
728: command.append(" values ( ");
729:
730: // add a question marks for every field
731: for (int i = 0; i < bean.getColumnNames().size() - 1; ++i)
732: command.append("?,");
733: command.append("?)");
734: PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
735: System.out.println(command);
736: int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
737: Vector vec = new Vector(myIds);
738: int endIndex = -1;
739: String tempQuery = query;
740: String tempID = bean.getQC() + idField + bean.getQC();
741: // if id_field not do incremental conversion else do it all at once
742: if (!idField.equals(""))
743: {
744: long startTime = System.currentTimeMillis();
745: int counter = -1;
746: while (true)
747: {
748: ++counter;
749: if (counter == 0 && dialog != null)
750: dialog.title.setText("Check if data is available");
751: else if (dialog != null)
752: dialog.title.setText("Check if more data is available");
753: myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex), tempQuery, numHits);
754: if (myIds.isEmpty())
755: break;
756: vec = new Vector(myIds);
757: rowCount = vec.size();
758: System.out.println("ID LIST SIZE " + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIds.size());
759: deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
760: if (vec.size() <= numIntervalls)
761: {
762: endIndex = 0;
763: deltaID = vec.size();
764: }
765: for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)
766: {
767: System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());
768: if (query.indexOf("where") > 0)
769: tempQuery = query + " and " + tempID + ">='" + vec.get(k) + "' and " + tempID + "<='" + vec.get(k + deltaID) + "'";
770: else
771: tempQuery = query + " where " + tempID + ">='" + vec.get(k) + "' and " + tempID + "<='" + vec.get(k + deltaID) + "'";
772: System.out.println(tempQuery);
773: if (dialog != null)
774: dialog.title.setText("Reading table data ...");
775:
776: bean.makeQuery(tempQuery, deltaID);
777: if (dialog != null)
778: dialog.title.setText("Writing table data ...");
779:
780: command = writeDatainDestTable(dialog, command, k, pstm, rowCount, delimiter);
781: endIndex = k + deltaID;
782: }
783: System.out.println(endIndex);
784: //all data written ? if not write last chunk of data
785: if (endIndex == vec.size() - 1)
786: System.out.println("fits");
787: else
788: {
789: System.out.println(" last intervall from " + vec.get(endIndex) + " " + vec.lastElement());
790:
791: if (query.indexOf("where") > 0)
792: tempQuery = query + " and " + tempID + ">='" + vec.get(endIndex) + "' and " + tempID + "<='" + vec.lastElement() + "'";
793: else
794: tempQuery = query + " where " + tempID + ">='" + vec.get(endIndex) + "' and " + tempID + "<='" + vec.lastElement() + "'";
795: System.out.println(tempQuery);
796: if (dialog != null)
797: dialog.title.setText("Reading table data ...");
798: bean.makeQuery(tempQuery, 0);
799: if (dialog != null)
800: dialog.title.setText("Writing table data ...");
801: command = writeDatainDestTable(dialog, command, endIndex, pstm, rowCount, delimiter);
802: }
803: // prepare new query for next chunk
804: if (query.indexOf("where") > 0)
805: tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
806: else
807: tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
808:
809: }
810: long endTime = System.currentTimeMillis();
811: System.out.println("Time for incremental convert elapsed " + (endTime - startTime));
812: } else
813: {
814: // read and write all in one big chunk
815: long startTime = System.currentTimeMillis();
816:
817: bean.makeQuery(query, 0);
818: command = writeDatainDestTable(dialog, command, j, pstm, rowCount, delimiter);
819: long endTime = System.currentTimeMillis();
820: System.out.println("Time for old convert elapsed " + (endTime - startTime));
821:
822: }
823: }
824: } catch (Exception e)
825: {
826: System.out.println("Error while connecting to database " + e);
827: if (dialog != null)
828: {
829: dialog.setVisible(false);
830: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
831: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
832: java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
833: java.io.PrintStream stream = new java.io.PrintStream(b);
834: stream.print(command + "\n\n");
835: e.printStackTrace(stream);
836: FM2SQL.showErrorDialog(b.toString(), "Error occured !");
837: } else
838: {
839: e.printStackTrace();
840:
841: }
842: }
843: if (dialog != null)
844: {
845: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
846: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
847: dialog.setVisible(false);
848: }
849: }
850: /**
851: * Writes data to the destination table
852: * @param dialog progress dialog
853: * @param command
854: * @param j data index for progress bar
855: * @param pstm prepared statement
856: * @param rowCount number of datasets
857: * @return command
858: * @throws Exception
859: * @throws SQLException
860: */
861: private static StringBuffer writeDatainDestTable(FM2SQL.ProgressDialog dialog, StringBuffer command, int j, PreparedStatement pstm, int rowCount, String delimiter) throws Exception, SQLException
862: {
863: Vector row;
864: while ((row = bean.getNextRow()) != null)
865: {
866: j++;
867: // row = (Vector) result[0].get(j);
868: /* command = new StringBuffer();
869:
870: command.append("INSERT INTO ");
871: command.append(beanDest.getQC());
872: command.append(convertText((String) names.get(tbIndex)));
873: command.append(beanDest.getQC());
874: command.append(" values ( ");
875: */
876: //print rows
877: Object obj = null;
878: /* for(int k=0;k<row.size()-1;++k)
879: {
880: obj = row.get(k);
881: //System.out.println("row "+obj+" "+k);
882: if(obj!=null&&!(obj instanceof ArrayList))
883: command.append("'"+convertUml(obj.toString())+"',");
884: else if(obj!=null&& obj instanceof ArrayList)
885: command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',");
886: else command.append("NULL,");
887: }
888: obj = row.get(row.size() - 1);
889: if (obj != null && !(obj instanceof ArrayList))
890: command.append("'"+convertUml(obj.toString())+"')");
891: else
892: if(obj!=null&& obj instanceof ArrayList)
893: command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')"); //command.append(obj.toString()+")");
894: else command.append("NULL)");
895: */
896: //command.append("'"+row.get(row.size()-1)+"')");
897: //command.append(" )");
898: // for(int k=0;k<row.size();++k)
899:
900: // System.out.println();
901: // System.out.println(command+" "+j+" "+row.size()+" "+ ((Vector)result2[0].get(j)).size());
902: // System.out.println(command);
903: for (int k = 0; k < row.size(); ++k)
904: {
905: obj = row.get(k);
906:
907: if (obj instanceof ArrayList)
908: obj = formatFileMakerArray((List) obj, delimiter);
909:
910: String str = (obj == null) ? "NULL" : obj.toString();
911: if (obj instanceof Double)
912: {
913: pstm.setDouble(k + 1, ((Double) obj).doubleValue());
914: } else if (!str.equals("NULL"))
915: pstm.setString(k + 1, str);
916: else
917: pstm.setNull(k + 1, Types.NULL);
918: }
919: pstm.execute();
920: //stm.executeUpdate(command.toString());
921: if (dialog != null)
922: dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
923: // System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j);
924: command = null;
925: } // to while loop
926: return command;
927: }
928:
929: /**
930: * removes special characters from the input string as well as .fp5
931: * @param newName String to change
932: * @return
933: */
934: public static String convertText(String newName)
935: {
936: StringBuffer alterMe = new StringBuffer(newName.trim().toLowerCase());
937: int length = alterMe.length();
938: int j = 0;
939: int index = alterMe.indexOf(".fp5");
940: if (index >= 0)
941: {
942: alterMe.delete(index, index + 4);
943: length = length - 4;
944: }
945:
946: while (j < length)
947: {
948: if (alterMe.charAt(j) == ' ')
949: {
950: alterMe.setCharAt(j, '_');
951: // if(j<length-1) j=j+1;
952: } else if (alterMe.charAt(j) == '_')
953: {
954:
955: if (alterMe.charAt(j + 1) == '_')
956: alterMe.deleteCharAt(j);
957: length = length - 1;
958: // if(j<length-1) j=j+1;
959: } else if (alterMe.charAt(j) == 'ä')
960: {
961: alterMe.setCharAt(j, 'a');
962: alterMe.insert(j + 1, "e");
963: length = length + 1;
964: if (j < length - 1)
965: j = j + 1;
966: } else if (alterMe.charAt(j) == 'ö')
967: {
968: alterMe.setCharAt(j, 'o');
969: alterMe.insert(j + 1, "e");
970: length = length + 1;
971: if (j < length - 1)
972: j = j + 1;
973: } else if (alterMe.charAt(j) == 'ü')
974: {
975: alterMe.setCharAt(j, 'u');
976: alterMe.insert(j + 1, "e");
977: length = length + 1;
978: if (j < length - 1)
979: j = j + 1;
980: } else if (alterMe.charAt(j) == 'ß')
981: {
982: alterMe.setCharAt(j, 's');
983: alterMe.insert(j + 1, "s");
984: length = length + 1;
985: if (j < length - 1)
986: j = j + 1;
987: } else if (alterMe.charAt(j) == ':')
988: {
989: if (j < length - 1)
990: {
991: if (alterMe.charAt(j + 1) == ':')
992: {
993: alterMe.setCharAt(j, '_');
994: alterMe.delete(j + 1, j + 2);
995: length = length - 1;
996:
997: }
998:
999: if (j < length - 1)
1000: j = j + 1;
1001: }
1002: } else if (alterMe.charAt(j) == '-')
1003: {
1004: alterMe.setCharAt(j, '_');
1005:
1006: } else if (alterMe.charAt(j) == '?')
1007: {
1008: // changed ? to _ because of update statement
1009: alterMe.setCharAt(j, '_');
1010: // length = length + 1;
1011: // j=j+1;
1012: System.out.println(alterMe);
1013: } else if (alterMe.charAt(j) == '.')
1014: {
1015: if (j == length - 1)
1016: {
1017: alterMe.delete(j, j);
1018: length--;
1019: } else
1020: alterMe.setCharAt(j, '_');
1021: }
1022:
1023: ++j;
1024: }
1025: return alterMe.toString();
1026: }
1027: /**
1028: * Converts > and < in an entity (> or <)
1029: * @param newName
1030: * @return
1031: */
1032: public static String convertToEntities(String newName)
1033: {
1034: StringBuffer alterMe = new StringBuffer(newName.trim());
1035: int length = alterMe.length();
1036: int j = 0;
1037:
1038: while (j < length)
1039: {
1040:
1041: if (alterMe.charAt(j) == '>')
1042: {
1043: alterMe.setCharAt(j, '&');
1044: alterMe.insert(j + 1, "gt;");
1045: length = length + 2;
1046: if (j < length - 1)
1047: j = j + 1;
1048:
1049: } else if (alterMe.charAt(j) == '<')
1050: {
1051: alterMe.setCharAt(j, '&');
1052: alterMe.insert(j + 1, "lt;");
1053: length = length + 2;
1054: if (j < length - 1)
1055: j = j + 1;
1056:
1057: }
1058: ++j;
1059: }
1060: return alterMe.toString();
1061: }
1062: /**
1063: * Masks the single quote character '-->\'
1064: * @param newName
1065: * @return
1066: */
1067: public static String convertUml(String newName)
1068: {
1069: StringBuffer alterMe = new StringBuffer(newName.trim());
1070: int length = alterMe.length();
1071: int j = 0;
1072:
1073: while (j < length)
1074: {
1075:
1076: if (alterMe.charAt(j) == '\'')
1077: {
1078: alterMe.setCharAt(j, '\\');
1079: alterMe.insert(j + 1, "'");
1080: length = length + 1;
1081: if (j < length - 1)
1082: j = j + 1;
1083: }
1084: /* else
1085: if (alterMe.charAt(j) == '"')
1086: {
1087: alterMe.setCharAt(j, '\\');
1088: alterMe.insert(j + 1, "\"");
1089: length = length + 1;
1090: if(j<length-1) j=j+1;
1091: }
1092: else
1093: if (alterMe.charAt(j) == '>')
1094: {
1095: alterMe.setCharAt(j, '\\');
1096: alterMe.insert(j + 1, ">");
1097: length = length + 1;
1098: if(j<length-1) j=j+1;
1099: }
1100: else
1101: if (alterMe.charAt(j) == '<')
1102: {
1103: alterMe.setCharAt(j, '\\');
1104: alterMe.insert(j + 1, "<");
1105: length = length + 1;
1106: if(j<length-1) j=j+1;
1107: }
1108: else
1109: if (alterMe.charAt(j) == '?')
1110: {
1111: alterMe.setCharAt(j, '\\');
1112: alterMe.insert(j + 1, "?");
1113: length = length + 1;
1114: if(j<length-1) j=j+1;
1115: }
1116: else
1117: if (alterMe.charAt(j) == '&')
1118: {
1119: alterMe.setCharAt(j, '\\');
1120: alterMe.insert(j + 1, "&");
1121: length = length + 1;
1122: if(j<length-1) j=j+1;
1123: }
1124: else
1125: if (alterMe.charAt(j) == '=')
1126: {
1127: alterMe.setCharAt(j, '\\');
1128: alterMe.insert(j + 1, "=");
1129: length = length + 1;
1130: if(j<length-1) j=j+1;
1131: }
1132: else
1133: if (alterMe.charAt(j) == ',')
1134: {
1135: alterMe.setCharAt(j, '\\');
1136: alterMe.insert(j + 1, ",");
1137: length = length + 1;
1138: if(j<length-1) j=j+1;
1139: }
1140: else
1141: if (alterMe.charAt(j) == '.')
1142: {
1143: alterMe.setCharAt(j, '\\');
1144: alterMe.insert(j + 1, ".");
1145: length = length + 1;
1146: if(j<length-1) j=j+1;
1147: }
1148: else
1149: if (alterMe.charAt(j) == '[')
1150: {
1151: alterMe.setCharAt(j, '\\');
1152: alterMe.insert(j + 1, ".");
1153: length = length + 1;
1154: if(j<length-1) j=j+1;
1155: }
1156: else
1157: if (alterMe.charAt(j) == ']')
1158: {
1159: alterMe.setCharAt(j, '\\');
1160: alterMe.insert(j + 1, ".");
1161: length = length + 1;
1162: if(j<length-1) j=j+1;
1163: }
1164: else
1165: if (alterMe.charAt(j) == '%')
1166: {
1167: alterMe.setCharAt(j, '\\');
1168: alterMe.insert(j + 1, "%");
1169: length = length + 1;
1170: if(j<length-1) j=j+1;
1171: }*/
1172: ++j;
1173: }
1174: return alterMe.toString();
1175: }
1176: /**
1177: * parses the input xml file for batch conversion
1178: * called from readXMLFile
1179: * * @param sb
1180: */
1181: public static void parseXMLConfig(StringBuffer sb)
1182: {
1183: boolean finished = false;
1184: // parse string and build document tree
1185: Xparse parser = new Xparse();
1186: parser.changeEntities = true;
1187: Node root = parser.parse(sb.toString());
1188: // printContents(root);
1189: Vector databases = new Vector();
1190: Vector tables = new Vector();
1191: Vector layouts = new Vector();
1192: Vector selects = new Vector();
1193: Vector creates = new Vector();
1194: Vector ids = new Vector();
1195: String delimiter = "|";
1196: int mode = -1;
1197:
1198: try
1199: {
1200: Node tempNode = root.find("convert/source", new int[] { 1, 1 });
1201: if (tempNode == null)
1202: throw new Error("parse error source tag missing");
1203: System.out.println(tempNode.name);
1204: int length = countNodes(tempNode);
1205: for (int i = 1; i <= length; i++)
1206: {
1207:
1208: DBBean database = new DBBean();
1209: tables = new Vector();
1210: layouts = new Vector();
1211: selects = new Vector();
1212: creates = new Vector();
1213: ids = new Vector();
1214: // parse dataBase
1215: Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
1216: Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
1217: Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
1218: Node node3 = root.find("convert/source/database", new int[] { 1, 1, i });
1219: Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 });
1220: Node delimiterNode = root.find("convert/source/database/delimiter", new int[] { 1, 1, i, 1, 1 });
1221: if (node3 == null)
1222: throw new Error("parse error database tag missing");
1223: if (node == null)
1224: throw new Error("parse error url tag missing");
1225: if (node1 == null)
1226: throw new Error("parse error user tag missing");
1227: if (node2 == null)
1228: throw new Error("parse error password tag missing");
1229: if (delimiterNode != null)
1230: delimiter = delimiterNode.getCharacters();
1231: String url = node.getCharacters();
1232: String user = node1.getCharacters();
1233: String password = node2.getCharacters();
1234: database.setURL(url.trim());
1235: database.setUserAndPasswd(user.trim(), password.trim());
1236: System.out.println(node.name + " " + node.getCharacters());
1237: System.out.println(node1.name + " " + node1.getCharacters());
1238: System.out.println(node2.name + " " + node2.getCharacters());
1239: String modeString = "";
1240: if (nodeMode == null)
1241: modeString = "convert";
1242: else
1243: modeString = nodeMode.getCharacters();
1244: if (modeString.equals("convert"))
1245: mode = DataBase.CONVERT_MODE;
1246: else if (modeString.equals("append"))
1247: mode = DataBase.APPEND_MODE;
1248: else if (modeString.equals("update"))
1249: mode = DataBase.UPDATE_MODE;
1250: else if (modeString.equals("delete"))
1251: mode = DataBase.DELETE_MODE;
1252:
1253: // if(node3!=null)
1254: // System.out.println(node3.name);
1255:
1256: int length2 = countNodes(node3);
1257:
1258: System.out.println("number of tables " + length2);
1259:
1260: for (int j = 1; j <= length2; ++j)
1261: {
1262: Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
1263: Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
1264: Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
1265: if (node4 != null)
1266: System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
1267: if (node5 != null)
1268: System.out.println(node5.name + " " + node5.getCharacters());
1269: if (node6 != null)
1270: System.out.println(node6.name + " " + node6.getCharacters());
1271: if (node4 == null)
1272: throw new Error("parse error table tag missing");
1273: // if(node5==null) throw new Error("parse error select tag missing");
1274: // if(node6==null) throw new Error("parse error create tag missing");
1275: String name = (String) node4.attributes.get("name");
1276: String layout = (String) node4.attributes.get("layout");
1277: String id = (String) node4.attributes.get("id");
1278: System.out.println("id was " + id);
1279: if (name == null)
1280: throw new Error("parse error required table tag attribute name missing");
1281: if (layout == null)
1282: layout = "";
1283: if (id == null)
1284: id = "";
1285: if (name.equals(""))
1286: throw new Error("parse error table tag attribute must not be empty");
1287: tables.add(name);
1288: layouts.add(layout);
1289: ids.add(id);
1290: String query = (node5 == null) ? "" : node5.getCharacters();
1291: if (query.equals(""))
1292: 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)
1296: creates.add(node6.getCharacters().trim());
1297: else
1298: creates.add("");
1299:
1300: }
1301: DataBase dataBase = new DataBase(database, tables, layouts, selects, creates, ids, mode);
1302: dataBase.delimiter = delimiter;
1303: databases.add(dataBase);
1304: }
1305: DBBean database = new DBBean();
1306: // parse dataBase
1307: Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
1308: Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
1309: Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
1310: String url = node.getCharacters();
1311: String user = node1.getCharacters();
1312: String password = node2.getCharacters();
1313: System.out.println(url);
1314: database.setURL(url.trim());
1315: database.setUserAndPasswd(user.trim(), password.trim());
1316: //databases.add(database);
1317: for (Iterator iter = databases.iterator(); iter.hasNext();)
1318: {
1319: DataBase db = (DataBase) iter.next();
1320: if (mode != DataBase.UPDATE_MODE)
1321: convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids, mode, db.delimiter);
1322: else
1323: update(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates, db.ids, mode);
1324:
1325: }
1326: // printContents(node3);
1327: // FM2SQL.fmInstance=new FM2SQL();
1328: } catch (Exception e)
1329: {
1330:
1331: e.printStackTrace();
1332: }
1333: }
1334: public static Vector getXMLConfig(String xmlFile)
1335: {
1336: StringBuffer sb = null;
1337: try
1338: {
1339: // read XML Metadata from a file
1340: FileInputStream fi = new FileInputStream(xmlFile);
1341: InputStreamReader isr = new InputStreamReader(fi, "UTF-8");
1342: BufferedReader buffr = new BufferedReader(isr);
1343: sb = new StringBuffer();
1344: int c = 0;
1345: while ((c = buffr.read()) != -1)
1346: {
1347: char ch = (char) c;
1348: sb.append(ch);
1349: // System.out.print((char)c);
1350: }
1351:
1352: } catch (Exception e)
1353: {
1354: e.printStackTrace();
1355: }
1356:
1357: boolean finished = false;
1358: // parse string and build document tree
1359: Xparse parser = new Xparse();
1360: parser.changeEntities = true;
1361: Node root = parser.parse(sb.toString());
1362: // printContents(root);
1363: Vector databases = new Vector();
1364: Vector tables = new Vector();
1365: Vector layouts = new Vector();
1366: Vector selects = new Vector();
1367: Vector creates = new Vector();
1368: Vector ids = new Vector();
1369: String delimiter = "|";
1370: int mode = -1;
1371: try
1372: {
1373: Node tempNode = root.find("convert/source", new int[] { 1, 1 });
1374: if (tempNode == null)
1375: throw new Error("parse error source tag missing");
1376: System.out.println(tempNode.name);
1377: int length = countNodes(tempNode);
1378: for (int i = 1; i <= length; i++)
1379: {
1380:
1381: DBBean database = new DBBean();
1382: tables = new Vector();
1383: layouts = new Vector();
1384: selects = new Vector();
1385: creates = new Vector();
1386: ids = new Vector();
1387: // parse dataBase
1388: Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
1389: Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
1390: Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
1391: Node node3 = root.find("convert/source/database", new int[] { 1, 1, i });
1392: Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 });
1393: Node delimiterNode = root.find("convert/source/database/delimiter", new int[] { 1, 1, i, 1, 1 });
1394:
1395: if (delimiterNode != null)
1396: delimiter = delimiterNode.getCharacters();
1397: if (node3 == null)
1398: throw new Error("parse error database tag missing");
1399: if (node == null)
1400: throw new Error("parse error url tag missing");
1401: if (node1 == null)
1402: throw new Error("parse error user tag missing");
1403: if (node2 == null)
1404: throw new Error("parse error password tag missing");
1405: String url = node.getCharacters();
1406: String user = node1.getCharacters();
1407: String password = node2.getCharacters();
1408: database.setURL(url.trim());
1409: database.setUserAndPasswd(user.trim(), password.trim());
1410: System.out.println(node.name + " " + node.getCharacters());
1411: System.out.println(node1.name + " " + node1.getCharacters());
1412: System.out.println(node2.name + " " + node2.getCharacters());
1413: String modeString = "";
1414: if (nodeMode == null)
1415: modeString = "convert";
1416: else
1417: modeString = nodeMode.getCharacters();
1418: if (modeString.equals("convert"))
1419: mode = DataBase.CONVERT_MODE;
1420: else if (modeString.equals("append"))
1421: mode = DataBase.APPEND_MODE;
1422: else if (modeString.equals("update"))
1423: mode = DataBase.UPDATE_MODE;
1424: else if (modeString.equals("delete"))
1425: mode = DataBase.DELETE_MODE;
1426:
1427: // if(node3!=null)
1428: // System.out.println(node3.name);
1429:
1430: int length2 = countNodes(node3);
1431:
1432: System.out.println("number of tables " + length2);
1433:
1434: for (int j = 1; j <= length2; ++j)
1435: {
1436: Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
1437: Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
1438: Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
1439: if (node4 != null)
1440: System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
1441: if (node5 != null)
1442: System.out.println(node5.name + " " + node5.getCharacters());
1443: if (node6 != null)
1444: System.out.println(node6.name + " " + node6.getCharacters());
1445: if (node4 == null)
1446: throw new Error("parse error table tag missing");
1447: // if(node5==null) throw new Error("parse error select tag missing");
1448: // if(node6==null) throw new Error("parse error create tag missing");
1449: String name = (String) node4.attributes.get("name");
1450: String layout = (String) node4.attributes.get("layout");
1451: String id = (String) node4.attributes.get("id");
1452: System.out.println("id was " + id);
1453:
1454: if (name == null)
1455: throw new Error("parse error required table tag attribute name missing");
1456: if (layout == null)
1457: layout = "";
1458: if (id == null)
1459: id = "";
1460: if (name.equals(""))
1461: throw new Error("parse error table tag attribute must not be empty");
1462: tables.add(name);
1463: layouts.add(layout);
1464: ids.add(id);
1465: String query = (node5 == null) ? "" : node5.getCharacters();
1466: if (query.equals(""))
1467: System.err.println("Warning empty select tag or select tag missing !!");
1468: query = (query.equals("")) ? "select * from " + database.getQC() + name + database.getQC() : query;
1469: selects.add(query);
1470: if (node6 != null)
1471: creates.add(node6.getCharacters().trim());
1472: else
1473: creates.add("");
1474:
1475: }
1476: DataBase dataBase = new DataBase(database, tables, layouts, selects, creates, ids, mode);
1477: dataBase.delimiter = delimiter;
1478: databases.add(dataBase);
1479: }
1480: DBBean database = new DBBean();
1481: // parse dataBase
1482: Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
1483: Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
1484: Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
1485: String url = node.getCharacters();
1486: String user = node1.getCharacters();
1487: String password = node2.getCharacters();
1488: System.out.println(url);
1489: database.setURL(url.trim());
1490: database.setUserAndPasswd(user.trim(), password.trim());
1491: databases.add(new DataBase(database, null, null, null, null, null, 0));
1492: //databases.add(database);
1493: /* for (Iterator iter = databases.iterator(); iter.hasNext();)
1494: {
1495: DataBase db = (DataBase) iter.next();
1496: convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates);
1497:
1498: }*/
1499: // printContents(node3);
1500: // FM2SQL.fmInstance=new FM2SQL();
1501: } catch (Exception e)
1502: {
1503: // TODO Auto-generated catch block
1504: e.printStackTrace();
1505: }
1506: return databases;
1507: }
1508:
1509: private static int countNodes(Node tempNode)
1510: {
1511: int length = 0;
1512: for (int i = 0; i < tempNode.contents.v.size(); ++i)
1513: {
1514: Node node = (Node) tempNode.contents.v.elementAt(i);
1515: if (node.type.equals("element"))
1516: {
1517: if (node.name.equals("database"))
1518: length++;
1519: if (node.name.equals("table"))
1520: length++;
1521: }
1522:
1523: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1524: }
1525: return length;
1526: }
1527: private static void printContents(Node root)
1528: {
1529:
1530: Vector contents = (root.index == null) ? root.contents.v : root.index.v;
1531: for (int i = 0; i < contents.size(); ++i)
1532: {
1533: Node n = (Node) contents.elementAt(i);
1534: if (n.type.equals("element"))
1535: {
1536: System.out.println("tag " + n.name);
1537: System.out.println(n.getCharacters());
1538: //contents=n.contents.v i=0;
1539: }
1540: // System.out.println(n.type);
1541: }
1542: }
1543: /**
1544: * reads the specified xml file
1545: * @param xmlFile
1546: */
1547: public static void readXMLFile(String xmlFile)
1548: {
1549: try
1550: {
1551: // read XML Metadata from a file
1552: FileInputStream fi = new FileInputStream(xmlFile);
1553: InputStreamReader isr = new InputStreamReader(fi, "UTF-8");
1554: BufferedReader buffr = new BufferedReader(isr);
1555: StringBuffer sb = new StringBuffer();
1556: int c = 0;
1557: while ((c = buffr.read()) != -1)
1558: {
1559: char ch = (char) c;
1560: sb.append(ch);
1561: // System.out.print((char)c);
1562: }
1563: parseXMLConfig(sb);
1564: } catch (Exception e)
1565: {
1566: e.printStackTrace();
1567: }
1568: }
1569:
1570: /**
1571: * Helper class for XML-File parsing
1572: * Holds the parsed data
1573: * @author rogo
1574: *
1575: */
1576: public static class DataBase
1577: {
1578: DBBean bean;
1579: Vector creates;
1580: Vector selects;
1581: Vector layouts;
1582: Vector tables;
1583: Vector ids;
1584: String delimiter = "//";
1585: final static int CONVERT_MODE = 1;
1586: final static int APPEND_MODE = 2;
1587: final static int UPDATE_MODE = 3;
1588: final static int DELETE_MODE = 4;
1589:
1590: int mode = -1;
1591:
1592: public DataBase(DBBean bean, Vector tables, Vector layouts, Vector selects, Vector creates, Vector ids, int mode)
1593: {
1594: this.bean = bean;
1595: this.tables = tables;
1596: this.layouts = layouts;
1597: this.selects = selects;
1598: this.creates = creates;
1599: this.ids = ids;
1600: this.mode = mode;
1601: this.bean.setIDVector(ids);
1602: }
1603: /**
1604: * writes the data contained in this object to the buffered writer
1605: * * @param buffr
1606: * @throws Exception
1607: */
1608: public void exportToXML(BufferedWriter buffr) throws Exception
1609: {
1610: // ids=bean.getIDVector();
1611: buffr.write(" <database>\n");
1612: buffr.write(" <url>" + bean.url + "</url>\n");
1613: buffr.write(" <user>" + bean.user + "</user>\n");
1614: buffr.write(" <password>" + bean.passwd + "</password>\n");
1615: buffr.write(" <delimiter>" + delimiter + "</delimiter>\n");
1616: String modeString = "";
1617: if (mode == CONVERT_MODE)
1618: modeString = "convert";
1619: else if (mode == APPEND_MODE)
1620: modeString = "append";
1621: else if (mode == UPDATE_MODE)
1622: modeString = "update";
1623: else if (mode == DELETE_MODE)
1624: modeString = "delete";
1625:
1626: buffr.write(" <mode>" + modeString + "</mode>\n");
1627: int index = 0;
1628: while (index < tables.size())
1629: {
1630: String table = (String) tables.get(index);
1631: String layout = (String) layouts.get(index);
1632: String select = (String) selects.get(index);
1633: String create = (String) creates.get(index);
1634: String id = (String) ids.get(index);
1635:
1636: buffr.write(" <table name = \"" + table + "\" layout = \"" + layout + "\" id = \"" + id + "\" >\n");
1637: buffr.write(" <select>" + convertToEntities(select) + "</select>\n");
1638: if (!create.equals(""))
1639: buffr.write(" <create>" + create + " </create>\n");
1640: buffr.write(" </table>\n");
1641: index++;
1642: }
1643: buffr.write(" </database>\n");
1644: }
1645: public String toString()
1646: {
1647: return bean.url + " " + tables;
1648: }
1649:
1650: }
1651: public static String convertToUTF8(Object command)
1652: {
1653: String str = null;
1654: try
1655: {
1656: str = new String(command.toString().getBytes("UTF-8"));
1657: } catch (UnsupportedEncodingException e)
1658: {
1659: // TODO Auto-generated catch block
1660: e.printStackTrace();
1661: }
1662: return str;
1663: }
1664: public static void writeConfig(String file, DataBase source, DataBase destination) throws Exception
1665: {
1666: if (!file.toLowerCase().endsWith(".xml"))
1667: file += ".xml";
1668: File f = new File(file);
1669:
1670: FileOutputStream fout = new FileOutputStream(f);
1671: OutputStreamWriter outsw = new OutputStreamWriter(fout, "UTF-8");
1672: BufferedWriter buffw = new BufferedWriter(outsw);
1673: buffw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1674: buffw.newLine();
1675: buffw.write("<convert>\n");
1676: buffw.write(" <source>\n");
1677: source.exportToXML(buffw);
1678: buffw.write(" </source>\n");
1679: buffw.write("\n <destination>\n");
1680: destination.exportToXML(buffw);
1681: buffw.write(" </destination>\n");
1682: buffw.write("</convert>\n");
1683: buffw.close();
1684: }
1685: public static void delete(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode) throws Exception
1686: {
1687: FM2SQL.ProgressDialog dialog = null;
1688: if (FM2SQL.fmInstance != null)
1689: {
1690: dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance, bean);
1691: dialog.setTitle("Conversion running ...");
1692: dialog.title.setText("Getting table data ...");
1693: dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y + (FM2SQL.fmInstance.getHeight() - 250) / 2);
1694: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
1695: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
1696: dialog.thread = Thread.currentThread();
1697: }
1698: // setting user and passwd
1699: bean.setUserAndPasswd(user, passwd);
1700: // setting user and passwd
1701: beanDest.setUserAndPasswd(userDest, passwdDest);
1702: if (dialog != null)
1703: dialog.setSize(400, 250);
1704: StringBuffer command = null;
1705: String query = null;
1706: try
1707: {
1708: //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");
1709: //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
1710: bean.setConnection(source);
1711: if (names == null)
1712: names = bean.getTableNames();
1713: // Collections.sort(names);
1714: int tbIndex = 1;
1715:
1716: // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
1717: for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
1718: {
1719: Vector[] result = null;
1720: java.util.TreeSet myIds = new TreeSet();
1721: java.util.TreeSet myIdsDest = new TreeSet();
1722: int deltaID = 1;
1723: String idField = "";
1724: String destTableName = "";
1725:
1726: try
1727: {
1728: query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
1729: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
1730: query = (selects != null) ? selects.get(tbIndex).toString() : query;
1731: //if vectors[1].get(i) != null)
1732: if (layout != "")
1733: {
1734: layout = " layout " + bean.getQC() + layout + bean.getQC();
1735: String name = names.get(tbIndex).toString();
1736: StringBuffer queryLayout = new StringBuffer(query);
1737: queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
1738: query = queryLayout.toString();
1739: System.out.println("added layout " + query);
1740:
1741: }
1742: dialog.title.setText("Getting table data ...");
1743: dialog.table.setText(names.get(tbIndex).toString());
1744: dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
1745: dialog.show();
1746: bean.getConnection();
1747: bean.makeQuery(query, 50);
1748: idField = ids.get(tbIndex).toString();
1749:
1750: } catch (Exception e)
1751: {
1752: continue;
1753: }
1754: // determine destTableName from createStatement or from source table name
1755: if (!creates.get(tbIndex).equals(""))
1756: {
1757: String create = creates.get(tbIndex).toString().toLowerCase();
1758: int fromIndex = create.indexOf("table") + 5;
1759: int toIndex = create.indexOf("(");
1760: destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "").trim();
1761: System.out.println("destTable " + destTableName);
1762:
1763: } else
1764: destTableName = convertText(names.get(tbIndex).toString());
1765:
1766: // for id kram
1767: Vector vec = null;
1768: Vector vecDest = null;
1769: // tempo
1770: beanDest.setConnection(destination);
1771: int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
1772: String tempID = bean.getQC() + idField + bean.getQC();
1773: String tempIDdest = beanDest.getQC() + convertText(idField) + beanDest.getQC();
1774:
1775: int endIndex = -1;
1776: String tempQuery = query;
1777: String destQuery = query.replaceAll(names.get(tbIndex).toString(), destTableName);
1778: String tempQueryDest = destQuery;
1779: // remove extra query parts destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1);
1780: System.out.println("new Query " + tempQueryDest);
1781: if (!idField.equals(""))
1782: {
1783: long startTime = System.currentTimeMillis();
1784: int counter = -1;
1785: while (true)
1786: {
1787: ++counter;
1788: if (counter == 0 && dialog != null)
1789: dialog.title.setText("Check if data is available");
1790: else if (dialog != null)
1791: dialog.title.setText("Check if more data is available");
1792: myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex), tempQuery, numHits);
1793: myIdsDest = beanDest.getIDVector(convertText(idField), destTableName, tempQueryDest, numHits);
1794: if (myIds.isEmpty())
1795: break;
1796: vec = new Vector(myIds);
1797: vecDest = new Vector(myIdsDest);
1798: rowCount = vec.size();
1799: // Deletion will work this way
1800: Vector deleted = new Vector(vec);
1801: Vector linesToDelete = new Vector(vecDest);
1802: // remove all lines that should not be deleted
1803: linesToDelete.removeAll(deleted);
1804: // System.out.println("ID LIST SIZE " + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIdsDest.size());
1805: /// @TODO complete delete task remove query show lines to be deleted let user choose if he wants that
1806: System.out.println("number of lines to be deleted " + linesToDelete.size());
1807: deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
1808: beanDest.setConnection(destination);
1809:
1810: Statement stm = beanDest.getConnection().createStatement();
1811:
1812: Vector tables = beanDest.getTableNames();
1813: // Collections.sort(tables);
1814: System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
1815: tables = beanDest.getTableNames();
1816: // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
1817: stm = beanDest.getConnection().createStatement();
1818:
1819: if (dialog != null)
1820: dialog.title.setText(" Deleting table data ...");
1821:
1822: int j = -1;
1823:
1824: Vector row = null;
1825: command = new StringBuffer();
1826:
1827: command.append("DELETE FROM");
1828: command.append(beanDest.getQC());
1829: command.append(destTableName);
1830: //command.append(convertText((String) names.get(tbIndex)));
1831: command.append(beanDest.getQC());
1832: int size = bean.getColumnNames().size();
1833: command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " = ?");
1834: PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
1835: System.out.println(command + " " + tbIndex);
1836: //int rowCount = bean.getRowCount(query);
1837: // int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex));
1838: while (true)
1839: {
1840:
1841: ++j;
1842: if (j == linesToDelete.size())
1843: break;
1844: //print rows
1845: pstm.setString(1, linesToDelete.get(j).toString());
1846: System.out.println(pstm.toString());
1847: pstm.execute();
1848: if (dialog != null)
1849: dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
1850: command = null;
1851: }
1852: // prepare new query for next chunk
1853: if (query.indexOf("where") > 0)
1854: tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
1855: else
1856: tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
1857:
1858: } //to outer while
1859: } // to idfield if
1860: } // table loop
1861:
1862: } catch (Exception e)
1863: {
1864: System.out.println("Error while connecting to database " + e);
1865: if (dialog != null)
1866: {
1867: dialog.setVisible(false);
1868: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
1869: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
1870: }
1871: java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
1872: java.io.PrintStream stream = new java.io.PrintStream(b);
1873: stream.print(command + "\n\n");
1874: e.printStackTrace(stream);
1875: FM2SQL.showErrorDialog(b.toString(), "Error occured !");
1876:
1877: }
1878: if (dialog != null)
1879: {
1880: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
1881: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
1882:
1883: dialog.setVisible(false);
1884: }
1885: } // to method
1886:
1887: /**
1888: * Converts input String in norman encoding to unicode
1889: * @param inp
1890: * @return converted String
1891: */
1892: static public String normanToUnicode(String inp) {
1893: StringBuffer buf = new StringBuffer();
1894: for (int i = 0; i < inp.length(); i++) {
1895: char c = inp.charAt(i);
1896: // System.out.println("char "+c+" "+(int)c);
1897: switch (c) {
1898: case 1: buf.append("\u00d0"); break; // Eth
1899: case 2: buf.append("\u00f0"); break; // eth
1900: case 3: buf.append("\u0141"); break; // Lslash
1901: case 4: buf.append("\u0142"); break; // lslash
1902: case 5: buf.append("\u0160"); break; // S caron
1903: case 6: buf.append("\u0161"); break; // s caron
1904: case 7: buf.append("\u00dd"); break; // Y acute
1905: case 8: buf.append("\u00fd"); break; // y acute
1906: case 11: buf.append("\u00de"); break; // Thorn
1907: case 12: buf.append("\u00fe"); break; // thorn
1908: case 14: buf.append("\u017d"); break; // Z caron
1909: case 15: buf.append("\u017e"); break; // z caron
1910: case 17: buf.append("\u0073"); break; // asciitilde
1911: case 18: buf.append("j\u0305"); break; // j macron [does a single char exist?]
1912: case 19: buf.append("^"); break; // circumflex
1913: case 20: buf.append("\u0303"); break; // tilde
1914: case 21: buf.append("\u00bd"); break; // onehalf
1915: case 22: buf.append("\u00bc"); break; // onequarter
1916: case 23: buf.append("\u00b9"); break; // onesuperior
1917: case 24: buf.append("\u00be"); break; // threequarters
1918: case 25: buf.append("\u00b3"); break; // threesuperior
1919: case 26: buf.append("\u00b2"); break; // twosuperior
1920: case 27: buf.append("\u00a6"); break; // brokenbar
1921: case 28: buf.append("-"); break; // minus
1922: case 29: buf.append("\u00d7"); break; // multiply
1923: case 39: buf.append("'"); break; // quotesingle
1924: case 94: buf.append("\u0302"); break; // circumflex
1925: case 96: buf.append("\u0300"); break; // grave
1926: case 196: buf.append("\u00c4"); break; // A dieresis
1927: case 197: buf.append("\u00c5"); break; // A ring
1928: case 201: buf.append("\u00c9"); break; // E acute
1929: case 209: buf.append("\u00d1"); break; // N tilde
1930: case 214: buf.append("\u00d6"); break; // O dieresis
1931: case 220: buf.append("\u00dc"); break; // U dieresis
1932: case 225: buf.append("\u00e1"); break; // a acute
1933: case 224: buf.append("\u00e0"); break; // a grave
1934: case 226: buf.append("\u00e2"); break; // a circumflex
1935: case 228: buf.append("\u00e4"); break; // a dieresis
1936: case 227: buf.append("\u00e3"); break; // a tilde
1937: case 229: buf.append("\u0101"); break; // a macron
1938: case 231: buf.append("\u00e7"); break; // c cedilla
1939: case 233: buf.append("\u00e9"); break; // e acute
1940: case 232: buf.append("\u00e8"); break; // e grave
1941: case 234: buf.append("\u00ea"); break; // e circumflex
1942: case 235: buf.append("\u00eb"); break; // e dieresis
1943: case 237: buf.append("\u00ed"); break; // i acute
1944: case 236: buf.append("\u00ec"); break; // i grave
1945: case 238: buf.append("\u00ee"); break; // i circumflex
1946: case 239: buf.append("\u00ef"); break; // i dieresis
1947: case 241: buf.append("\u00f1"); break; // n tilde
1948: case 243: buf.append("\u00f3"); break; // o acute
1949: case 242: buf.append("\u00f2"); break; // o grave
1950: case 244: buf.append("\u00f4"); break; // o circumflex
1951: case 246: buf.append("\u00f6"); break; // o dieresis
1952: case 245: buf.append("\u00f5"); break; // o tilde
1953: case 250: buf.append("\u00fa"); break; // u acute
1954: case 249: buf.append("\u00f9"); break; // u grave
1955: case 251: buf.append("\u00fb"); break; // u circumflex
1956: case 252: buf.append("\u00fc"); break; // u dieresis
1957: case 8224: buf.append("\u1e6d"); break; // t underdot
1958: case 176: buf.append("\u00b0"); break; // degree
1959: case 162: buf.append("\u1ebd"); break; // e tilde
1960: case 163: buf.append("\u00a3"); break; // sterling
1961: case 167: buf.append("\u00a7"); break; // section
1962: case 182: buf.append("\u00b6"); break; // paragraph
1963: case 223: buf.append("\u015b"); break; // s acute
1964: case 174: buf.append("\u1e5b"); break; // r underdot
1965: case 169: buf.append("\u1e45"); break; // n overdot
1966: case 353: buf.append("\u1e45"); break; // n overdot
1967: case 180: buf.append("\u0301"); break; // acute
1968: case 168: buf.append("\u0308"); break; // dieresis
1969: case 8800: buf.append("\u1e6d"); break; // t underdot
1970: case 198: buf.append("\u00c6"); break; // AE
1971: case 216: buf.append("\u014d"); break; // o macron
1972: case 8734: buf.append("\u0129"); break; // i tilde
1973: case 177: buf.append("\u00b1"); break; // plusminus
1974: case 165: buf.append("\u012b"); break; // i macron
1975: case 181: buf.append("\u1e43"); break; // m underdot
1976: case 8706: buf.append("\u1e0d"); break; // d underdot
1977: case 240: buf.append("\u1e0d"); break; // d underdot
1978:
1979: case 8721: buf.append("\u1e63"); break; // s underdot
1980: case 960: buf.append("\u017a"); break; // z acute
1981: case 8747: buf.append("\u1e45"); break; // n overdot
1982: case 937: buf.append("\u0169"); break; // u tilde
1983: case 230: buf.append("\u00e6"); break; // ae
1984: case 248: buf.append("\u00f8"); break; // oslash
1985: case 191: buf.append("\u0304\u0306"); break; // macron breve
1986: case 172: buf.append("\u1e37"); break; //
1987: case 8730: buf.append("j\u0305"); break; // j macron [does a single char exist?]
1988: case 402: buf.append("\u0103"); break; // a breve
1989: case 8776: buf.append("\u016d"); break; // u breve
1990: case 187: buf.append("\u1e42"); break; // M underdot
1991: case 8230: buf.append("\u2026"); break; // ellipsis
1992: case 192: buf.append("\u00c0"); break; // A grave
1993: case 195: buf.append("\u00c3"); break; // A tilde
1994: case 213: buf.append("\u00d5"); break; // O tilde
1995: case 338: buf.append("m\u0306"); break; // m breve
1996: case 339: buf.append("\u0153"); break; // oe
1997: case 8211: buf.append("\u2013"); break; // endash
1998: case 8212: buf.append("\u2014"); break; // emdash
1999: case 8220: buf.append("\u201c"); break; // quotedblleft
2000: case 8221: buf.append("\u201d"); break; // quotedblright
2001: case 8216: buf.append("\u2018"); break; // quoteleft
2002: case 8217: buf.append("\u2019"); break; // quoteright
2003: case 247: buf.append("\u1e37"); break; // l underring [actually underdot]
2004: case 9674: buf.append("\u1e41"); break; // m overdot
2005: case 255: buf.append("n\u0306"); break; // n breve
2006: case 376: buf.append("\u00d7"); break; // multiply
2007: case 8364: buf.append("\u1e5b"); break; // r underring [actually underdot]
2008: case 8249: buf.append("\u1e44"); break; // N overdot
2009: case 8250: buf.append("\u1e62"); break; // S underdot
2010: case 64257: buf.append("\u1e24"); break; // H underdot
2011: case 64258: buf.append("\u1e0c"); break; // D underdot
2012: case 8225: buf.append("\u2021"); break; // daggerdbl
2013: case 8218: buf.append("\u1e36"); break; // L underdot
2014: case 8222: buf.append("\u0113"); break; // e macron
2015: case 194: buf.append("\u1e5f"); break; // r underbar
2016: case 202: buf.append("r\u0324"); break; // r underdieresis
2017: case 193: buf.append("\u012a"); break; // I macron
2018: case 8486:
2019: case 203: buf.append("\u016b"); break; // u macron
2020: case 200: buf.append("\u1e6c"); break; // T underdot
2021: case 205: buf.append("\u1e64"); break; // S acute
2022: case 206: buf.append("\u2020"); break; // dagger
2023: case 207: buf.append("\u0115"); break; // e breve
2024: case 204: buf.append("\u014f"); break; // o breve
2025: case 211: buf.append("\u0100"); break; // A macron
2026: case 212: buf.append("\u1e46"); break; // N underdot
2027: case 210: buf.append("\u1e3b"); break; // l underbar
2028: case 218: buf.append("\u016a"); break; // U macron
2029: case 219: buf.append("\u0179"); break; // Z acute
2030: case 217: buf.append("\u1e5a"); break; // R underdot
2031: case 305: buf.append("\u0131"); break; // dotlessi
2032: case 710: buf.append("\u1e47"); break; // n underdot
2033: case 732: buf.append("\u1e49"); break; // n underbar
2034: case 175: buf.append("\u0304"); break; // macron
2035: case 728: buf.append("\u0306"); break; // breve
2036: case 729:case 215: buf.append("\u1e25"); break; // h underdot
2037: case 730: buf.append("\u012d"); break; // i breve
2038: case 184: buf.append("\u0327"); break; // cedilla
2039: case 733: buf.append("\u030b"); break; // hungarumlaut
2040: case 731: buf.append("\u0328"); break; // ogonek
2041: case 711: buf.append("\u030c"); break; // caron
2042: case 199: buf.append("\u012b\u0303"); break; // imacron tilde
2043: case 8226: buf.append("\u1e5d"); break; // runderdot macron
2044: case 8482: buf.append("\u016b\0306"); break; // umacron breve
2045: case 8804: buf.append("\u0101\u0301"); break; // amacron acute
2046: case 8805: buf.append("\u016b\u0301"); break; // umacron acute
2047: case 8719: buf.append("\u0113\u0301"); break; // emacron acute
2048: case 170: buf.append("\u0113\u0300"); break; // emacron breve
2049: case 186: buf.append("\u014d\u0300"); break; // omacron breve
2050: case 161: buf.append("\u0101\u0306"); break; // amacron breve
2051: case 8710: buf.append("\u0101\u0303"); break; // amacron tilde
2052: case 171: buf.append("\u012b\u0301"); break; // imacron acute
2053: case 8260: buf.append("\u1e00"); break; // runderdotmacron acute
2054: case 183: buf.append("\u1e5b\u0301"); break; // runderdot acute
2055: case 8240: buf.append("\u012b\u0306"); break; // imacron breve
2056: case 63743: buf.append("\u016b\u0303"); break; // umacron tilde
2057: default: buf.append(c);
2058: if((int)c>127)
2059: System.out.println("char "+c+" "+(int)c);
2060: break;
2061: }
2062: }
2063: return buf.toString();
2064: }
2065:
2066: static public String normanToUnicodeOld(String inp) {
2067: StringBuffer buf = new StringBuffer();
2068: for (int i = 0; i < inp.length(); i++) {
2069: char c = inp.charAt(i);
2070: switch (c) {
2071: case 1: buf.append("\u00d0"); break; // Eth
2072: case 2: buf.append("\u00f0"); break; // eth
2073: case 3: buf.append("\u0141"); break; // Lslash
2074: case 4: buf.append("\u0142"); break; // lslash
2075: case 5: buf.append("\u0160"); break; // S caron
2076: case 6: buf.append("\u0161"); break; // s caron
2077: case 7: buf.append("\u00dd"); break; // Y acute
2078: case 8: buf.append("\u00fd"); break; // y acute
2079: case 11: buf.append("\u00de"); break; // Thorn
2080: case 12: buf.append("\u00fe"); break; // thorn
2081: case 14: buf.append("\u017d"); break; // Z caron
2082: case 15: buf.append("\u017e"); break; // z caron
2083: case 17: buf.append("\u0073"); break; // asciitilde
2084: case 18: buf.append("j\u0305"); break; // j macron [does a single char exist?]
2085: case 19: buf.append("^"); break; // circumflex
2086: case 20: buf.append("\u0303"); break; // tilde
2087: case 21: buf.append("\u00bd"); break; // onehalf
2088: case 22: buf.append("\u00bc"); break; // onequarter
2089: case 23: buf.append("\u00b9"); break; // onesuperior
2090: case 24: buf.append("\u00be"); break; // threequarters
2091: case 25: buf.append("\u00b3"); break; // threesuperior
2092: case 26: buf.append("\u00b2"); break; // twosuperior
2093: case 27: buf.append("\u00a6"); break; // brokenbar
2094: case 28: buf.append("-"); break; // minus
2095: case 29: buf.append("\u00d7"); break; // multiply
2096: case 39: buf.append("'"); break; // quotesingle
2097: case 94: buf.append("\u0302"); break; // circumflex
2098: case 96: buf.append("\u0300"); break; // grave
2099: case 128: buf.append("\u00c4"); break; // A dieresis
2100: case 129: buf.append("\u00c5"); break; // A ring
2101: case 131: buf.append("\u00c9"); break; // E acute
2102: case 132: buf.append("\u00d1"); break; // N tilde
2103: case 133: buf.append("\u00d6"); break; // O dieresis
2104: case 134: buf.append("\u00dc"); break; // U dieresis
2105: case 135: buf.append("\u00e1"); break; // a acute
2106: case 136: buf.append("\u00e0"); break; // a grave
2107: case 137: buf.append("\u00e2"); break; // a circumflex
2108: case 138: buf.append("\u00e4"); break; // a dieresis
2109: case 139: buf.append("\u00e3"); break; // a tilde
2110: case 140: buf.append("\u0101"); break; // a macron
2111: case 141: buf.append("\u00e7"); break; // c cedilla
2112: case 142: buf.append("\u00e9"); break; // e acute
2113: case 143: buf.append("\u00e8"); break; // e grave
2114: case 144: buf.append("\u00ea"); break; // e circumflex
2115: case 145: buf.append("\u00eb"); break; // e dieresis
2116: case 146: buf.append("\u00ed"); break; // i acute
2117: case 147: buf.append("\u00ec"); break; // i grave
2118: case 148: buf.append("\u00ee"); break; // i circumflex
2119: case 149: buf.append("\u00ef"); break; // i dieresis
2120: case 150: buf.append("\u00f1"); break; // n tilde
2121: case 151: buf.append("\u00f3"); break; // o acute
2122: case 152: buf.append("\u00f2"); break; // o grave
2123: case 153: buf.append("\u00f4"); break; // o circumflex
2124: case 154: buf.append("\u00f6"); break; // o dieresis
2125: case 155: buf.append("\u00f5"); break; // o tilde
2126: case 156: buf.append("\u00fa"); break; // u acute
2127: case 157: buf.append("\u00f9"); break; // u grave
2128: case 158: buf.append("\u00fb"); break; // u circumflex
2129: case 159: buf.append("\u00fc"); break; // u dieresis
2130: case 160: buf.append("\u1e6d"); break; // t underdot
2131: case 161: buf.append("\u00b0"); break; // degree
2132: case 162: buf.append("\u1ebd"); break; // e tilde
2133: case 163: buf.append("\u00a3"); break; // sterling
2134: case 164: buf.append("\u00a7"); break; // section
2135: case 166: buf.append("\u00b6"); break; // paragraph
2136: case 167: buf.append("\u015b"); break; // s acute
2137: case 168: buf.append("\u1e5b"); break; // r underdot
2138: case 169: buf.append("\u1e67"); break; // s caron
2139: case 171: buf.append("\u0301"); break; // acute
2140: case 172: buf.append("\u0308"); break; // dieresis
2141: case 173: buf.append("\u1e6d"); break; // t underdot
2142: case 174: buf.append("\u00c6"); break; // AE
2143: case 175: buf.append("\u014d"); break; // o macron
2144: case 176: buf.append("\u0129"); break; // i tilde
2145: case 177: buf.append("\u00b1"); break; // plusminus
2146: case 180: buf.append("\u012b"); break; // i macron
2147: case 181: buf.append("\u1e43"); break; // m underdot
2148: case 182: buf.append("\u1e0d"); break; // d underdot
2149: case 183: buf.append("\u1e63"); break; // s underdot
2150: case 185: buf.append("\u017a"); break; // z acute
2151: case 186: buf.append("\u1e45"); break; // n overdot
2152: case 189: buf.append("\u0169"); break; // u tilde
2153: case 190: buf.append("\u00e6"); break; // ae
2154: case 191: buf.append("\u00f8"); break; // oslash
2155: case 192: buf.append("\u0304\u0306"); break; // macron breve
2156: case 194: buf.append("\u1e37"); break; //
2157: case 195: buf.append("j\u0305"); break; // j macron [does a single char exist?]
2158: case 196: buf.append("\u0103"); break; // a breve
2159: case 197: buf.append("\u016d"); break; // u breve
2160: case 200: buf.append("\u1e42"); break; // M underdot
2161: case 201: buf.append("\u2026"); break; // ellipsis
2162: case 203: buf.append("\u00c0"); break; // A grave
2163: case 204: buf.append("\u00c3"); break; // A tilde
2164: case 205: buf.append("\u00d5"); break; // O tilde
2165: case 206: buf.append("m\u0306"); break; // m breve
2166: case 207: buf.append("\u0153"); break; // oe
2167: case 208: buf.append("\u2013"); break; // endash
2168: case 209: buf.append("\u2014"); break; // emdash
2169: case 210: buf.append("\u201c"); break; // quotedblleft
2170: case 211: buf.append("\u201d"); break; // quotedblright
2171: case 212: buf.append("\u2018"); break; // quoteleft
2172: case 213: buf.append("\u2019"); break; // quoteright
2173: case 214: buf.append("\u1e37"); break; // l underring [actually underdot]
2174: case 215: buf.append("\u1e41"); break; // m overdot
2175: case 216: buf.append("n\u0306"); break; // n breve
2176: case 217: buf.append("\u00d7"); break; // multiply
2177: case 219: buf.append("\u1e5b"); break; // r underring [actually underdot]
2178: case 220: buf.append("\u1e44"); break; // N overdot
2179: case 221: buf.append("\u1e62"); break; // S underdot
2180: case 222: buf.append("\u1e24"); break; // H underdot
2181: case 223: buf.append("\u1e0c"); break; // D underdot
2182: case 224: buf.append("\u2021"); break; // daggerdbl
2183: case 226: buf.append("\u1e36"); break; // L underdot
2184: case 227: buf.append("\u0113"); break; // e macron
2185: case 229: buf.append("\u1e5f"); break; // r underbar
2186: case 230: buf.append("r\u0324"); break; // r underdieresis
2187: case 231: buf.append("\u012a"); break; // I macron
2188: case 232: buf.append("\u016b"); break; // u macron
2189: case 233: buf.append("\u01e6c"); break; // T underdot
2190: case 234: buf.append("\u1e64"); break; // S acute
2191: case 235: buf.append("\u2020"); break; // dagger
2192: case 236: buf.append("\u0115"); break; // e breve
2193: case 237: buf.append("\u014f"); break; // o breve
2194: case 238: buf.append("\u0100"); break; // A macron
2195: case 239: buf.append("\u1e46"); break; // N underdot
2196: case 241: buf.append("\u1e3b"); break; // l underbar
2197: case 242: buf.append("\u016a"); break; // U macron
2198: case 243: buf.append("\u0179"); break; // Z acute
2199: case 244: buf.append("\u1e5a"); break; // R underdot
2200: case 245: buf.append("\u0131"); break; // dotlessi
2201: case 246: buf.append("\u1e47"); break; // n underdot
2202: case 247: buf.append("\u1e49"); break; // n underbar
2203: case 248: buf.append("\u0304"); break; // macron
2204: case 249: buf.append("\u0306"); break; // breve
2205: case 250: buf.append("\u1e25"); break; // h underdot
2206: case 251: buf.append("\u012d"); break; // i breve
2207: case 252: buf.append("\u0327"); break; // cedilla
2208: case 253: buf.append("\u030b"); break; // hungarumlaut
2209: case 254: buf.append("\u0328"); break; // ogonek
2210: case 255: buf.append("\u030c"); break; // caron
2211: case 130: buf.append("\u012b\u0303"); break; // imacron tilde
2212: case 165: buf.append("\u1e5d"); break; // runderdot macron
2213: case 170: buf.append("\u016b\0306"); break; // umacron breve
2214: case 178: buf.append("\u0101\u0301"); break; // amacron acute
2215: case 179: buf.append("\u016b\u0301"); break; // umacron acute
2216: case 184: buf.append("\u0113\u0301"); break; // emacron acute
2217: case 187: buf.append("\u0113\u0300"); break; // emacron breve
2218: case 188: buf.append("\u014d\u0300"); break; // omacron breve
2219: case 193: buf.append("\u0101\u0306"); break; // amacron breve
2220: case 198: buf.append("\u0101\u0303"); break; // amacron tilde
2221: case 199: buf.append("\u012b\u0301"); break; // imacron acute
2222: case 218: buf.append("\u1e00"); break; // runderdotmacron acute
2223: case 225: buf.append("\u1e5b\u0301"); break; // runderdot acute
2224: case 228: buf.append("\u012b\u0306"); break; // imacron breve
2225: case 240: buf.append("\u016b\u0303"); break; // umacron tilde
2226: default: buf.append(c); break;
2227: }
2228: }
2229: return buf.toString();
2230: }
2231: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>