Annotation of FM2SQL/src/Convert.java, revision 1.11
1.1 rogo 1: /*
2: * Convert.java -- Converter class - Filemaker to SQL Converter Copyright (C)
3: * 2003 Robert Gordesch (rogo@mpiwg-berlin.mpg.de) This program is free
4: * software; you can redistribute it and/or modify it under the terms of the GNU
5: * General Public License as published by the Free Software Foundation; either
6: * version 2 of the License, or (at your option) any later version. Please read
7: * license.txt for the full details. A copy of the GPL may be found at
8: * http://www.gnu.org/copyleft/lgpl.html You should have received a copy of the
9: * GNU General Public License along with this program; if not, write to the Free
10: * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
11: * USA Created on 15.09.2003 by rogo
12: */
13:
14: import java.awt.Cursor;
15: import java.io.BufferedReader;
16: import java.io.BufferedWriter;
17: import java.io.File;
18: import java.io.FileInputStream;
19: import java.io.FileNotFoundException;
20: import java.io.FileOutputStream;
21: import java.io.IOException;
22: import java.io.InputStream;
23: import java.io.InputStreamReader;
24: import java.io.OutputStreamWriter;
25: import java.io.PrintStream;
26: import java.net.URL;
27: import java.sql.PreparedStatement;
28: import java.sql.ResultSet;
29: import java.sql.SQLException;
30: import java.sql.Statement;
31: import java.sql.Types;
1.11 ! rogo 32: import java.text.DateFormat;
1.1 rogo 33: import java.text.ParseException;
34: import java.util.ArrayList;
1.11 ! rogo 35: import java.util.Date;
1.1 rogo 36: import java.util.Hashtable;
37: import java.util.Iterator;
38: import java.util.List;
1.11 ! rogo 39: import java.util.Locale;
1.1 rogo 40: import java.util.StringTokenizer;
41: import java.util.TreeSet;
42: import java.util.Vector;
43:
44: import javax.swing.JDialog;
45: import javax.swing.JLabel;
46: import javax.swing.JPanel;
47:
48: import com.exploringxml.xml.Node;
49: import com.exploringxml.xml.Xparse;
50:
51: class Convert
52: {
1.11 ! rogo 53: private static final String _TEMP = "_temp";
! 54:
1.4 rogo 55: /**
56: * Helper class for index creation
57: *
58: * @author rogo
59: *
60: */
61: public static class IndexList extends Vector
62: {
63: public String toString()
64: {
65: StringBuffer buff = new StringBuffer(1000);
66: int count = 0;
67: for (Iterator iter = this.iterator(); iter.hasNext();)
68: {
69: String element = (String) iter.next();
70: if (count < elementCount - 1)
71: {
72: buff.append(element).append(", ");
73: count++;
74: } else
75: buff.append(element);
76:
77: }
78:
79: return buff.toString();
80: }
81: }
82:
83: /**
84: * Helper class for pre/or post execution of SQLCommands
85: * @author rogo
86: *
87: */
88: public static class SQLCommand
89: {
90: private DBBean bean;
91: private String command;
92:
93: public SQLCommand(DBBean bean, String command)
94: {
95: this.bean = bean;
96: this.command = command;
97: }
98:
99: public void executeCommand() throws SQLException, Exception
100: {
1.6 rogo 101: System.out.println("Executing command: \n");
102: System.out.println(command);
103:
1.4 rogo 104: java.sql.Connection con = bean.getConnection();
105: Statement stm = con.createStatement();
106: stm.execute(command);
1.6 rogo 107: stm.close();
1.4 rogo 108: }
109: }
110:
111: static DBBean bean = new DBBean();
112:
113: static DBBean beanDest = new DBBean();
114:
115: static String user = "", passwd = "e1nste1n";
116:
117: static String userDest = "postgres", passwdDest = "rogo";
118:
119: static boolean batchRun = false;
120:
121: static Vector databases = new Vector();
122:
123: final static int numHits = 5000;
124:
125: final static int numIntervalls = 4;
126:
1.10 rogo 127: static boolean debug = !false;
1.4 rogo 128:
129: static boolean isGUI = true;
130:
131: /**
132: * Vector for all SQLCommands to executed before any conversion action is performed
133: */
134: static Vector preSQLCommands = new Vector();
135: /**
136: * Vector for all SQLCommands to executed after any conversion action has been performed
137: */
138:
139: static Vector postSQLCommands = new Vector();
140:
1.11 ! rogo 141: static final String versionID = new String("FM2SQL Version 0.9.2b\n");
1.10 rogo 142:
143: private static boolean noError = false;
1.7 rogo 144:
1.4 rogo 145: public static void main(String args[]) throws IOException
146: {
1.11 ! rogo 147: DateFormat d = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,
! 148: Locale.GERMANY);
! 149: System.out.println(convertText("rogoistSuper" + "_" + d.format(new Date()).replace(':', '_')));
1.4 rogo 150: /*
151: * try { //byte[] b = "�".getBytes("UTF-8"); //
152: * System.out.println("QueryString " +b[0]+" "+b[1]+(new
153: * String(b).getBytes()[0])+" "+new String(b).getBytes()[1]);
154: * //System.out.println(new String(b,"UTF-8")); } catch
155: * (UnsupportedEncodingException e) { e.printStackTrace(); }
156: */
1.8 rogo 157: File tmpPath = new File(System.getProperty("java.io.tmpdir"));
1.4 rogo 158: isGUI = false;
159: FileOutputStream file = null;
160: if (args.length != 1)
161: {
1.7 rogo 162: System.out.println(versionID);
1.4 rogo 163: System.out.println("Usage: java Convert <xml config file>");
164: System.exit(-1);
165: }
1.8 rogo 166: File temp = null;
1.4 rogo 167: try
168: {
1.10 rogo 169: temp = new File(tmpPath, "fm2sql.txt");
1.8 rogo 170: int count = 1;
171: while (temp.exists())
172: {
173: temp = new File(tmpPath, "fm2sql" + generateSuffix(count++) + ".txt");
174: }
1.4 rogo 175: file = new FileOutputStream(temp);
176: } catch (FileNotFoundException e1)
177: {
178: e1.printStackTrace();
179: }
180: PrintStream stream = new PrintStream(file, true);
1.8 rogo 181: // write info for user to stdout
182: System.out.println(versionID);
1.10 rogo 183: System.out.println("Loading " + args[0] + "....");
184: System.out.println("Log will be written to " + temp.getCanonicalPath());
1.8 rogo 185:
1.4 rogo 186: if (!debug)
187: {
188: System.setOut(stream);
189: System.setErr(stream);
190: }
1.7 rogo 191: System.out.println(versionID);
1.10 rogo 192: System.out.println("Using config file : " + args[0] + "....");
193:
1.4 rogo 194: StringBuffer sb = readXMLFile(args[0]);
195: parseXMLConfig(sb);
196: if (!(new File(args[0]).exists()))
1.7 rogo 197: {
198:
1.4 rogo 199: System.exit(0);
1.7 rogo 200: }
1.4 rogo 201: System.out.println("Finished!");
202: // convert("jdbc:fmpro:http://141.14.237.74:8050","jdbc:postgresql://erebos/test",null,null);
203: }
204:
205: public static void convertBatch(DBBean source, DBBean destination, Vector names, Vector layouts,
206: Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception
207: {
208: bean = source;
209: beanDest = destination;
210: convert(null, null, names, layouts, selects, creates, ids, mode, delimiter);
211: }
212:
213: public static String formatFileMakerArray(List list, String delimiter)
214: {
215: StringBuffer formattedString = new StringBuffer();
216: for (int i = 0; i < list.size(); ++i)
217: {
218: formattedString.append(list.get(i).toString());
219: if (i < list.size() - 1)
220: formattedString.append(delimiter);
221: }
222: return formattedString.toString();
223: }
224:
225: /**
226: * Method for SQL UPDATE
227: *
228: * @param source
229: * @param destination
230: * @param names
231: * @param layouts
232: * @param selects
233: * @param creates
234: * @param ids
235: * @param mode
236: * @throws Exception
237: */
238: public static void update(String source, String destination, Vector names, Vector layouts,
239: Vector selects, Vector creates, Vector ids, int mode) throws Exception
240: {
241:
242: FM2SQL.ProgressDialog dialog = null;
243: if (isGUI)
244: {
245: dialog = initDialog();
246: }
247: // setting user and passwd
248: bean.setUserAndPasswd(user, passwd);
249: // setting user and passwd
250: beanDest.setUserAndPasswd(userDest, passwdDest);
251:
252: StringBuffer command = null;
253: String query = null;
254: try
255: {
256:
257: bean.setConnection(source);
258:
259: if (names == null)
260: names = bean.getTableNames();
261: // Collections.sort(names);
262: int tbIndex = 1;
263:
264: System.out.println("Start at table " + names.firstElement());
265: for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
266: {
267: Vector[] result = null;
268: String destTableName = "";
269: try
270: {
271: query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
272: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
273: query = (selects != null) ? selects.get(tbIndex).toString() : query;
274: // if vectors[1].get(i) != null)
1.10 rogo 275: if (!layout.equals(""))
1.4 rogo 276: {
1.11 ! rogo 277:
1.4 rogo 278: query = addLayoutToQuery(names, query, tbIndex, layout);
279:
280: }
281: if (dialog != null)
282: {
283: prepareDialogforUse(names, dialog, tbIndex);
284: }
285: bean.getConnection();
286: bean.makeQuery(query, 0);
287:
288: } catch (Exception e)
289: {
290: System.out.println("Warning exception occured \n " + e);
291:
292: continue;
293: }
294: // determine destTableName from createStatement or from source
295: // table name
296: if (!creates.get(tbIndex).equals(""))
297: {
298: String create = creates.get(tbIndex).toString().toLowerCase();
299: int fromIndex = create.indexOf("table") + 5;
300: int toIndex = create.indexOf("(");
301: destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "")
302: .trim();
303: System.out.println("destTable " + destTableName);
304:
305: } else
306: destTableName = convertText(names.get(tbIndex).toString());
307:
308: beanDest.setConnection(destination);
309:
310: Statement stm = beanDest.getConnection().createStatement();
311:
312: Vector tables = beanDest.getTableNames();
313:
314: System.out.println(names.get(tbIndex) + " "
315: + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
316: tables = beanDest.getTableNames();
317: stm = beanDest.getConnection().createStatement();
318:
319: if (dialog != null)
320: dialog.title.setText("Updating table data ...");
321: else
322: System.out.println("Updating table data ...");
323: int j = -1;
324:
325: Vector row = null;
326: command = new StringBuffer();
327:
328: command.append("UPDATE ");
329: command.append(beanDest.getQC());
330: command.append(destTableName);
331: // command.append(convertText((String) names.get(tbIndex)));
332: command.append(beanDest.getQC());
333: command.append(" SET ");
334:
335: int size = bean.getColumnNames().size();
336: for (int i = 0; i < size - 1; ++i)
337: command.append(beanDest.getQC() + convertText((String) bean.getColumnNames().get(i))
338: + beanDest.getQC() + " = ? ,");
339: command.append(convertText((String) bean.getColumnNames().get(size - 1)) + " = ? ");
340: command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " = ?");
341: PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
342: System.out.println(command + " " + tbIndex);
343: int rowCount = bean.getRowCount(query);
344: int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex));
345: while ((row = bean.getNextRow()) != null)
346: {
347: j++;
348: // print rows
349: Object obj = null;
350: for (int k = 0; k < row.size(); ++k)
351: {
352: obj = row.get(k);
353: if (obj instanceof ArrayList)
354: obj = ((List) obj).get(0);
355: String str = (obj == null) ? "NULL" : obj.toString();
356: if (!str.equals("NULL"))
357: pstm.setString(k + 1, str);
358: else
359: pstm.setNull(k + 1, Types.NULL);
360: }
361: pstm.setString(row.size() + 1, row.get(idIndex).toString());
362: pstm.execute();
363: if (dialog != null)
364: dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
365: command = null;
366: } // to for loop
367:
368: }
369: } catch (Exception e)
370: {
371: System.out.println("Error while connecting to database " + e);
372: if (isGUI)
373: {
374: showExceptionDialog(dialog, command, e);
375: } else
376: {
377: e.printStackTrace();
378:
379: }
380: } finally
381: {
382: if (isGUI)
383: {
384: resetGUI(dialog);
385: }
386: }
387: }
388:
389: /**
390: * @param dialog
391: */
392: private static void resetGUI(FM2SQL.ProgressDialog dialog)
393: {
394: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
395: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
396: dialog.setVisible(false);
397: }
398:
399: /**
400: * @param dialog
401: * @param command
402: * @param e
403: */
404: private static void showExceptionDialog(FM2SQL.ProgressDialog dialog, StringBuffer command,
405: Exception e)
406: {
407: dialog.setVisible(false);
408: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
409: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
410: java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
411: java.io.PrintStream stream = new java.io.PrintStream(b);
412: stream.print(command + "\n\n");
413: e.printStackTrace(stream);
414: FM2SQL.showErrorDialog(b.toString(), "Error occured !");
415: }
416:
417: /**
418: * @return
419: */
420: private static FM2SQL.ProgressDialog initDialog()
421: {
422: FM2SQL.ProgressDialog dialog;
423: dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance, bean);
424: dialog.setTitle("Conversion running ...");
425: dialog.title.setText("Getting table data ...");
426: dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x
427: + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y
428: + (FM2SQL.fmInstance.getHeight() - 250) / 2);
429: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
430: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
431: dialog.thread = Thread.currentThread();
432: dialog.setSize(420, 250);
433: return dialog;
434: }
435:
436: /**
437: * transfers the specified array of tables to the destination database and
438: * creates the table if it does not exist if it exists and mode is not append
439: * the table is dropped
440: *
441: * @param source
442: * @param destination
443: * @param names
444: * @param layouts
445: * @param selects
446: * @param creates
447: * @param ids
448: * @param mode
449: * @throws Exception
450: */
451:
452: public static void convert(String source, String destination, Vector names, Vector layouts,
453: Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception
454: {
1.7 rogo 455: // TODO implement convert_temp mode!!!
1.4 rogo 456: FM2SQL.ProgressDialog dialog = null;
457:
458: if (isGUI)
459: {
460: dialog = initDialog();
461: }
462:
463: System.out.println("connection established " + source + " " + bean.url);
464:
465: java.util.TreeSet myIds = new TreeSet();
466: int deltaID = 1;
467: String idField = "";
468: String destTableName = "";
469: String[] fieldNames = null;
470:
471: if (source != null && destination != null)
472: {
473: // setting user and passwd
474: bean.setUserAndPasswd(user, passwd);
475: // setting user and passwd
476: beanDest.setUserAndPasswd(userDest, passwdDest);
477: }
478:
479: StringBuffer command = null;
480: String query = null;
481: try
482: {
483:
484: if (source != null)
485: bean.setConnection(source);
486: else
487: bean.setConnection(bean.url);
488: if (names == null)
489: names = bean.getTableNames();
490: int tbIndex = 1;
491:
492: for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
493: {
494: Vector[] result = null;
495: try
496: {
497: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
498: query = (selects != null) ? selects.get(tbIndex).toString() : "select * from "
499: + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
1.10 rogo 500: if (layout.intern() != "")
1.4 rogo 501: {
502: query = addLayoutToQuery(names, query, tbIndex, layout);
503:
504: }
505: if (dialog != null)
506: {
507: prepareDialogforUse(names, dialog, tbIndex);
508: }
509:
510: bean.getConnection();
511: bean.makeQuery(query, 50);
512: idField = ids.get(tbIndex).toString();
513:
514: } catch (Exception e)
515: {
516: System.out.println("Warning exception occured \n " + e);
517:
518: continue;
519: }
520: if (destination != null)
521: beanDest.setConnection(destination);
522: else
523: beanDest.setConnection(beanDest.url);
524: Statement stm = beanDest.getConnection().createStatement();
525:
526: Vector tables = beanDest.getTableNames();
527: // Collections.sort(tables);
528: System.out.println(names.get(tbIndex) + " "
529: + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
530: tables = beanDest.getTableNames();
531: // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
532: stm = beanDest.getConnection().createStatement();
533: // System.exit(0);
534:
535: // determine destTableName from createStatement or from source
536: // table name
537: ConversionProperties prop = getFieldNamesAndDestTableName(creates.get(tbIndex).toString(),
538: query, names.get(tbIndex).toString());
539: destTableName = prop.destTableName;
1.7 rogo 540: if (mode == Convert.DataBase.CONVERT_TEMP_MODE)
541: {
1.11 ! rogo 542: destTableName += _TEMP;
1.7 rogo 543: }
1.4 rogo 544: fieldNames = prop.fieldNames;
1.7 rogo 545: if (mode == Convert.DataBase.CONVERT_MODE || mode == Convert.DataBase.CONVERT_TEMP_MODE)
1.4 rogo 546: {
547:
548: if (tables.indexOf(destTableName) >= 0)
549: {
550: stm.executeUpdate("drop table " + beanDest.getQC() + destTableName + beanDest.getQC());
551: tables.remove(destTableName);
552: System.out.println("dropped table" + destTableName);
553: }
554: if ((tables.indexOf(destTableName) < 0))
555: {
556:
557: if (creates.get(tbIndex).equals("")
558: || creates.get(tbIndex).toString().toLowerCase().indexOf("create") < 0)
559: {
560: System.out
561: .println("Warning empty or invalid create statement - creating one for you\n");
562:
563: command = new StringBuffer(50);
564: command.append("CREATE TABLE ");
565: command.append(beanDest.getQC());
1.7 rogo 566: command.append(destTableName);//convertText((String) names.get(tbIndex)));
1.4 rogo 567: command.append(beanDest.getQC());
568: command.append("(");
569: String type = null;
570: Vector columnNames = bean.getColumnNames();
571: for (int i = 0; i < columnNames.size() - 1; ++i)
572: {
573: type = bean.metaData.getColumnTypeName(i + 1);
574: // System.out.println(i+" "+result[1].get(i)+"
575: // "+type);
576: type = (type.equals("NUMBER")) ? "INT4" : type;
577: type = (type.equals("CONTAINER")) ? "TEXT" : type;
578:
579: command.append(beanDest.getQC() + convertText((String) columnNames.get(i))
580: + beanDest.getQC() + " " + type + ", ");
581: }
582: type = bean.metaData.getColumnTypeName(columnNames.size());
583: type = (type.equals("NUMBER")) ? "INT4" : type;
584: type = (type.equals("CONTAINER")) ? "TEXT" : type;
585: command.append(beanDest.getQC()
586: + convertText((String) columnNames.get(columnNames.size() - 1))
587: + beanDest.getQC() + " " + type);
588: command.append(" )");
589:
590: } else
591: command = new StringBuffer().append(creates.get(tbIndex).toString().toLowerCase());
592: stm.executeUpdate(command.toString());
593:
594: }
595: }
596: if (dialog != null)
597: dialog.title.setText("Writing table data ...");
598:
599: // prepare the insert statement
600: int j = -1;
601: Vector row = null;
602: command = new StringBuffer();
603:
604: command.append("INSERT INTO ");
605: command.append(beanDest.getQC());
606: command.append(destTableName);
607: command.append(beanDest.getQC());
608: command.append(" (");
609: for (int i = 0; i < fieldNames.length; i++)
610: {
611: command.append(fieldNames[i]);
612: if (i < fieldNames.length - 1)
613: command.append(",");
614: }
615: command.append(") ");
616:
617: command.append(" values ( ");
618: // add a question marks for every field
619: for (int i = 0; i < bean.getColumnNames().size() - 1; ++i)
620: command.append("?,");
621: command.append("?)");
622: PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
623: System.out.println(command);
624: int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
625: Vector vec = new Vector(myIds);
626: int endIndex = -1;
627: String tempQuery = query;
628: String tempID = bean.getQC() + idField + bean.getQC();
629: // if id_field not do incremental conversion else do it all at
630: // once
631: if (!idField.equals(""))
632: {
633: long startTime = System.currentTimeMillis();
634: int counter = -1;
635: while (true)
636: {
637: ++counter;
638: if (counter == 0 && dialog != null)
639: dialog.title.setText("Check if data is available");
640: else if (dialog != null)
641: dialog.title.setText("Check if more data is available");
642: myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex),
643: tempQuery, numHits);
644: if (myIds.isEmpty())
645: break;
646: vec = new Vector(myIds);
647: rowCount = vec.size();
648: System.out.println("ID LIST SIZE "
649: + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIds.size());
650: deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
651: if (vec.size() <= numIntervalls)
652: {
653: endIndex = 0;
654: deltaID = vec.size();
655: }
656: for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)
657: {
658: System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());
659: if (query.indexOf("where") > 0)
660: tempQuery = query + " and " + tempID + ">='" + vec.get(k) + "' and " + tempID
661: + "<='" + vec.get(k + deltaID) + "'";
662: else
663: tempQuery = query + " where " + tempID + ">='" + vec.get(k) + "' and " + tempID
664: + "<='" + vec.get(k + deltaID) + "'";
665: System.out.println(tempQuery);
666: if (dialog != null)
667: dialog.title.setText("Reading table data ...");
668:
669: bean.makeQuery(tempQuery, deltaID);
670: if (dialog != null)
671: dialog.title.setText("Writing table data ...");
672:
673: command = writeDatainDestTable(dialog, command, k, pstm, rowCount, delimiter);
674: endIndex = k + deltaID;
675: }
676: System.out.println(endIndex);
677: // all data written ? if not write last chunk of data
678: if (endIndex == vec.size() - 1)
679: System.out.println("fits");
680: else
681: {
682: System.out.println(" last intervall from " + vec.get(endIndex) + " "
683: + vec.lastElement());
684:
685: if (query.indexOf("where") > 0)
686: tempQuery = query + " and " + tempID + ">='" + vec.get(endIndex) + "' and "
687: + tempID + "<='" + vec.lastElement() + "'";
688: else
689: tempQuery = query + " where " + tempID + ">='" + vec.get(endIndex) + "' and "
690: + tempID + "<='" + vec.lastElement() + "'";
691: System.out.println(tempQuery);
692: if (dialog != null)
693: dialog.title.setText("Reading table data ...");
694: bean.makeQuery(tempQuery, 0);
695: if (dialog != null)
696: dialog.title.setText("Writing table data ...");
697: command = writeDatainDestTable(dialog, command, endIndex, pstm, rowCount, delimiter);
698: }
699: // prepare new query for next chunk
700: if (query.indexOf("where") > 0)
701: tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
702: else
703: tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
704:
705: }
706: long endTime = System.currentTimeMillis();
707: System.out.println("Time for incremental convert elapsed " + (endTime - startTime));
708: } else
709: {
710: // read and write all in one big chunk
711: long startTime = System.currentTimeMillis();
712:
713: bean.makeQuery(query, 0);
1.10 rogo 714: System.err.println("query for whole table done");
1.4 rogo 715: command = writeDatainDestTable(dialog, command, j, pstm, rowCount, delimiter);
716: long endTime = System.currentTimeMillis();
717: System.out.println("Time for old convert elapsed " + (endTime - startTime));
718:
719: }
720: if (isGUI)
721: resetGUI(dialog);
1.10 rogo 722: noError = true;
1.11 ! rogo 723: if (mode == Convert.DataBase.CONVERT_TEMP_MODE)
! 724: {
! 725: String originalName = destTableName.split(_TEMP)[0];
! 726: // TODO
! 727: if (beanDest.getTableNames().contains(originalName))
! 728: {
! 729: DateFormat d = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
! 730: d.setTimeZone(java.util.TimeZone.getTimeZone("CET"));
! 731:
! 732: String date = d.format(new Date());
! 733: date = date.replace(':', '_');
! 734: renameTable(beanDest, addQuotes(beanDest, originalName), addQuotes(beanDest,
! 735: convertText(originalName + "_" + date)));
! 736: }
! 737: renameTable(beanDest, addQuotes(beanDest, destTableName), addQuotes(beanDest,
! 738: originalName));
! 739: }
1.4 rogo 740: }
741: } catch (Exception e)
742: {
1.10 rogo 743: System.out.println("Error while connecting to database " + e.getMessage());
744: noError = false;
1.4 rogo 745: if (isGUI)
746: {
747: showExceptionDialog(dialog, command, e);
748: resetGUI(dialog);
749: } else
750: {
751: e.printStackTrace();
752:
753: }
1.10 rogo 754: } catch (Error e)
755: {
1.11 ! rogo 756: noError = false;
! 757:
1.10 rogo 758: System.out.println(e);
759: e.printStackTrace();
1.4 rogo 760: }
761:
762: }
763:
764: /**
1.11 ! rogo 765: *
! 766: * @param beanDest2
! 767: * @param originalName
! 768: * @return
! 769: */
! 770: private static String addQuotes(DBBean bean, String originalName)
! 771: {
! 772: return bean.getQC() + originalName + bean.getQC();
! 773: }
! 774:
! 775: /**
1.4 rogo 776: * @param names
777: * @param dialog
778: * @param tbIndex
779: */
780: private static void prepareDialogforUse(Vector names, FM2SQL.ProgressDialog dialog, int tbIndex)
781: {
782: dialog.title.setText("Reading table data ...");
783: dialog.table.setText(names.get(tbIndex).toString());
784: dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
785: dialog.setVisible(true);
786: }
787:
788: /**
789: * @param names
790: * @param query
791: * @param tbIndex
792: * @param layout
793: * @return
794: */
795: private static String addLayoutToQuery(Vector names, String query, int tbIndex, String layout)
796: {
797: layout = " layout " + bean.getQC() + layout + bean.getQC();
798: String name = names.get(tbIndex).toString();
799: StringBuffer queryLayout = new StringBuffer(query);
800: queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
801: query = queryLayout.toString();
802: System.out.println("added layout " + query);
803: return query;
804: }
805:
806: /**
807: * Writes data to the destination table
808: *
809: * @param dialog
810: * progress dialog
811: * @param command
812: * @param j
813: * data index for progress bar
814: * @param pstm
815: * prepared statement
816: * @param rowCount
817: * number of datasets
818: * @return command
819: * @throws Exception
820: * @throws SQLException
821: */
822: private static StringBuffer writeDatainDestTable(FM2SQL.ProgressDialog dialog,
823: StringBuffer command, int j, PreparedStatement pstm, int rowCount, String delimiter)
824: throws Exception, SQLException
825: {
826: Vector row;
827: while ((row = bean.getNextRow()) != null)
828: {
829: j++;
830: Object obj = null;
831: for (int k = 0; k < row.size(); ++k)
832: {
833: obj = row.get(k);
834:
835: if (obj instanceof ArrayList)
836: obj = formatFileMakerArray((List) obj, delimiter);
837:
838: String str = (obj == null) ? "NULL" : obj.toString();
839: if (obj instanceof Double)
840: {
841: pstm.setDouble(k + 1, ((Double) obj).doubleValue());
842: } else if (!str.equals("NULL"))
843: pstm.setString(k + 1, str);
844: else
845: pstm.setNull(k + 1, Types.NULL);
846: }
847: pstm.execute();
848: if (isGUI)
849: dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
850: command = null;
851: } // to while loop
852: return command;
853: }
854:
855: /**
856: * removes special characters from the input string as well as .fp5
857: *
858: * @param newName
859: * String to change
860: * @return
861: */
862: public static String convertText(String newName)
863: {
864: StringBuffer alterMe = new StringBuffer(newName.trim().toLowerCase());
865: int length = alterMe.length();
866: int j = 0;
867: int index = alterMe.indexOf(".fp5");
868: if (index >= 0)
869: {
870: alterMe.delete(index, index + 4);
871: length = length - 4;
872: }
873:
874: while (j < length)
875: {
876: if (alterMe.charAt(j) == ' ')
877: {
878: alterMe.setCharAt(j, '_');
879: // if(j<length-1) j=j+1;
880: } else if (alterMe.charAt(j) == '_')
881: {
882:
883: if (alterMe.charAt(j + 1) == '_')
884: alterMe.deleteCharAt(j);
885: length = length - 1;
886: // if(j<length-1) j=j+1;
887: } else if (alterMe.charAt(j) == 'ä')
888: {
889: alterMe.setCharAt(j, 'a');
890: alterMe.insert(j + 1, "e");
891: length = length + 1;
892: if (j < length - 1)
893: j = j + 1;
894: } else if (alterMe.charAt(j) == 'ö')
895: {
896: alterMe.setCharAt(j, 'o');
897: alterMe.insert(j + 1, "e");
898: length = length + 1;
899: if (j < length - 1)
900: j = j + 1;
901: } else if (alterMe.charAt(j) == 'ü')
902: {
903: alterMe.setCharAt(j, 'u');
904: alterMe.insert(j + 1, "e");
905: length = length + 1;
906: if (j < length - 1)
907: j = j + 1;
908: } else if (alterMe.charAt(j) == 'ß')
909: {
910: alterMe.setCharAt(j, 's');
911: alterMe.insert(j + 1, "s");
912: length = length + 1;
913: if (j < length - 1)
914: j = j + 1;
915: } else if (alterMe.charAt(j) == ':')
916: {
917: if (j < length - 1)
918: {
919: if (alterMe.charAt(j + 1) == ':')
920: {
921: alterMe.setCharAt(j, '_');
922: alterMe.delete(j + 1, j + 2);
923: length = length - 1;
924:
925: }
926:
927: if (j < length - 1)
928: j = j + 1;
929: }
930: } else if (alterMe.charAt(j) == '-')
931: {
932: alterMe.setCharAt(j, '_');
933:
934: } else if (alterMe.charAt(j) == '?')
935: {
936: // changed ? to _ because of update statement
937: alterMe.setCharAt(j, '_');
938: // length = length + 1;
939: // j=j+1;
940: System.out.println(alterMe);
941: } else if (alterMe.charAt(j) == '.')
942: {
943: if (j == length - 1)
944: {
945: alterMe.delete(j, j);
946: length--;
947: } else
948: alterMe.setCharAt(j, '_');
949: }
950:
951: ++j;
952: }
953: return alterMe.toString();
954: }
955:
956: /**
957: * Converts > and < in an entity (> or <)
958: *
959: * @param newName
960: * @return
961: */
962: public static String convertToEntities(String newName)
963: {
964: StringBuffer alterMe = new StringBuffer(newName.trim());
965: int length = alterMe.length();
966: int j = 0;
967:
968: while (j < length)
969: {
970:
971: if (alterMe.charAt(j) == '>')
972: {
973: alterMe.setCharAt(j, '&');
974: alterMe.insert(j + 1, "gt;");
975: length = length + 2;
976: if (j < length - 1)
977: j = j + 1;
978:
979: } else if (alterMe.charAt(j) == '<')
980: {
981: alterMe.setCharAt(j, '&');
982: alterMe.insert(j + 1, "lt;");
983: length = length + 2;
984: if (j < length - 1)
985: j = j + 1;
986:
987: }
988: ++j;
989: }
990: return alterMe.toString();
991: }
992:
993: /**
994: * Masks the single quote character '-->\'
995: *
996: * @param newName
997: * @return
998: */
999: public static String convertUml(String newName)
1000: {
1001: StringBuffer alterMe = new StringBuffer(newName.trim());
1002: int length = alterMe.length();
1003: int j = 0;
1004:
1005: while (j < length)
1006: {
1007:
1008: if (alterMe.charAt(j) == '\'')
1009: {
1010: alterMe.setCharAt(j, '\\');
1011: alterMe.insert(j + 1, "'");
1012: length = length + 1;
1013: if (j < length - 1)
1014: j = j + 1;
1015: }
1016: /*
1017: * else if (alterMe.charAt(j) == '"') { alterMe.setCharAt(j, '\\');
1018: * alterMe.insert(j + 1, "\""); length = length + 1; if(j <length-1)
1019: * j=j+1; } else if (alterMe.charAt(j) == '>') { alterMe.setCharAt(j,
1020: * '\\'); alterMe.insert(j + 1, ">"); length = length + 1; if(j <length-1)
1021: * j=j+1; } else if (alterMe.charAt(j) == ' <') { alterMe.setCharAt(j,
1022: * '\\'); alterMe.insert(j + 1, " <"); length = length + 1; if(j
1023: * <length-1) j=j+1; } else if (alterMe.charAt(j) == '?') {
1024: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "?"); length = length +
1025: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '&') {
1026: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "&"); length = length +
1027: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '=') {
1028: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "="); length = length +
1029: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == ',') {
1030: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, ","); length = length +
1031: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '.') {
1032: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "."); length = length +
1033: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '[') {
1034: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "."); length = length +
1035: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == ']') {
1036: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "."); length = length +
1037: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '%') {
1038: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "%"); length = length +
1039: * 1; if(j <length-1) j=j+1; }
1040: */
1041: ++j;
1042: }
1043: return alterMe.toString();
1044: }
1045:
1046: /**
1047: * parses the input xml file for batch conversion called from readXMLFile *
1048: *
1049: * @param sb
1050: */
1051: public static void parseXMLConfig(StringBuffer sb)
1052: {
1053: // boolean finished = false;
1054: Vector databases = new Vector();
1055: try
1056: {
1057: databases = getXMLConfig(sb);
1.7 rogo 1058:
1.6 rogo 1059: // destination DataBase object
1.7 rogo 1060: DataBase dbDest = ((DataBase) databases.lastElement());
1.6 rogo 1061:
1.4 rogo 1062: DBBean database = ((DataBase) databases.lastElement()).bean;
1063: databases.remove(databases.size() - 1);
1.7 rogo 1064:
1.6 rogo 1065: for (Iterator iterator = dbDest.preCommands.iterator(); iterator.hasNext();)
1066: {
1067: SQLCommand sqlCommand = (SQLCommand) iterator.next();
1068: sqlCommand.executeCommand();
1069: }
1070:
1.4 rogo 1071: // databases.add(database);
1072: for (Iterator iter = databases.iterator(); iter.hasNext();)
1073: {
1074: DataBase db = (DataBase) iter.next();
1075: for (Iterator iterator = db.preCommands.iterator(); iterator.hasNext();)
1076: {
1077: SQLCommand sqlCommand = (SQLCommand) iterator.next();
1078: sqlCommand.executeCommand();
1079: }
1080: int mode = db.mode;
1.10 rogo 1081: if (mode == DataBase.CONVERT_TEMP_MODE)
1082: {
1083: convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids,
1084: mode, db.delimiter);
1085: if (noError)
1086: {
1087: System.out.println("no Error occured ");
1088: // db.bean.setURL(database.url);
1089: // db.bean.setUserAndPasswd(database.user,database.passwd);
1090: //
1091: // Convert.user = db.bean.user;
1092: // Convert.passwd = db.bean.passwd;
1093: // userDest = database.user;
1094: // passwdDest = database.passwd;
1095: // synchronize(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates,
1096: // db.ids, mode, db.delimiter, new Vector(db.htIndex.values()));
1097: }
1098: }
1099: if (mode == DataBase.CONVERT_MODE || mode == DataBase.APPEND_MODE)
1.4 rogo 1100: convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids,
1101: mode, db.delimiter);
1102: else if (mode == DataBase.UPDATE_MODE)
1103: {
1104:
1105: Convert.user = db.bean.user;
1106: Convert.passwd = db.bean.passwd;
1107: userDest = database.user;
1108: passwdDest = database.passwd;
1109:
1110: update(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates, db.ids,
1111: mode);
1112: } else if (mode == DataBase.SYNCHRONIZE_MODE)
1113: {
1114: Convert.user = db.bean.user;
1115: Convert.passwd = db.bean.passwd;
1116: userDest = database.user;
1117: passwdDest = database.passwd;
1118:
1119: synchronize(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates,
1120: db.ids, mode, db.delimiter, new Vector(db.htIndex.values()));
1121: }
1122: for (Iterator iterator = db.postCommands.iterator(); iterator.hasNext();)
1123: {
1124: SQLCommand sqlCommand = (SQLCommand) iterator.next();
1125: sqlCommand.executeCommand();
1126: }
1127:
1.6 rogo 1128: }
1129: for (Iterator iterator = dbDest.postCommands.iterator(); iterator.hasNext();)
1130: {
1131: SQLCommand sqlCommand = (SQLCommand) iterator.next();
1132: sqlCommand.executeCommand();
1.4 rogo 1133: }
1134:
1135: } catch (Exception e)
1136: {
1137:
1138: e.printStackTrace();
1139:
1140: } finally
1141: {
1142: bean.closeAllConnections();
1143: beanDest.closeAllConnections();
1144: }
1.11 ! rogo 1145: }
! 1146:
! 1147: /**
! 1148: *
! 1149: * @param database
! 1150: * @param db
! 1151: * @throws Exception
! 1152: * @throws SQLException
! 1153: */
! 1154: private static void renameTable(DBBean database, String sourceTable, String destTable)
! 1155: throws SQLException, Exception
! 1156: {
! 1157: String command = "alter table " + sourceTable + " rename " + " to " + destTable;
! 1158: SQLCommand sqlCommand = new SQLCommand(database, command);
! 1159: sqlCommand.executeCommand();
1.4 rogo 1160: }
1161:
1162: public static Vector getXMLConfig(StringBuffer sb)
1163: {
1164:
1165: boolean finished = false;
1166: // parse string and build document tree
1167: Xparse parser = new Xparse();
1168: parser.changeEntities = true;
1169: Node root = parser.parse(sb.toString());
1170: // printContents(root);
1171: Vector databases = new Vector();
1172: Vector tables = new Vector();
1173: Vector layouts = new Vector();
1174: Vector selects = new Vector();
1175: Vector creates = new Vector();
1176: Vector ids = new Vector();
1177: Vector preSQLCommands = new Vector();
1178: Vector postSQLCommands = new Vector();
1179:
1180: String delimiter = "|";
1181: int mode = -1;
1182: try
1183: {
1184: Node tempNode = root.find("convert/source", new int[]
1185: { 1, 1 });
1186: if (tempNode == null)
1187: throw new Error("parse error source tag missing");
1188: System.out.println(tempNode.name);
1189: int length = countNodes(tempNode);
1190: for (int i = 1; i <= length; i++)
1191: {
1192:
1193: DBBean database = new DBBean();
1194: tables = new Vector();
1195: layouts = new Vector();
1196: selects = new Vector();
1197: creates = new Vector();
1198: ids = new Vector();
1199: preSQLCommands = new Vector();
1200: postSQLCommands = new Vector();
1201:
1202: // parse dataBase
1203: Node node = root.find("convert/source/database/url", new int[]
1204: { 1, 1, i, 1 });
1205: Node node1 = root.find("convert/source/database/user", new int[]
1206: { 1, 1, i, 1, 1 });
1207: Node node2 = root.find("convert/source/database/password", new int[]
1208: { 1, 1, i, 1, 1 });
1209: Node node3 = root.find("convert/source/database", new int[]
1210: { 1, 1, i });
1211: Node nodeMode = root.find("convert/source/database/mode", new int[]
1212: { 1, 1, i, 1, 1 });
1213: Node delimiterNode = root.find("convert/source/database/delimiter", new int[]
1214: { 1, 1, i, 1, 1 });
1215:
1216: Node commandNodes = root.find("convert/source/database/sqlcommands", new int[]
1217: { 1, 1, i, 1, 1 });
1218: if (commandNodes != null)
1219: {
1220: parseCommandNode(commandNodes, database, preSQLCommands, postSQLCommands);
1221: }
1222: Node useNormanToUnicodeMapper = root.find(
1223: "convert/source/database/usenormantounicodemapper", new int[]
1224: { 1, 1, i, 1, 1 });
1225:
1226: if (delimiterNode != null)
1227: delimiter = delimiterNode.getCharacters();
1228: if (useNormanToUnicodeMapper != null)
1229: {
1230: database.setUseNormanToUnicodeMapper(Boolean.valueOf(
1231: useNormanToUnicodeMapper.getCharacters()).booleanValue());
1232: System.out.println("useMapper "
1233: + Boolean.valueOf(useNormanToUnicodeMapper.getCharacters().trim()).booleanValue());
1234: }
1235:
1236: if (node3 == null)
1237: throw new Error("parse error database tag missing");
1238: if (node == null)
1239: throw new Error("parse error url tag missing");
1240: if (node1 == null)
1241: throw new Error("parse error user tag missing");
1242: if (node2 == null)
1243: throw new Error("parse error password tag missing");
1244: String url = node.getCharacters();
1245: String user = node1.getCharacters();
1246: String password = node2.getCharacters();
1247: database.setURL(url.trim());
1248: database.setUserAndPasswd(user.trim(), password.trim());
1249: System.out.println(node.name + " " + node.getCharacters());
1250: System.out.println(node1.name + " " + node1.getCharacters());
1251: System.out.println(node2.name + " " + node2.getCharacters());
1252: String modeString = "";
1253: if (nodeMode == null)
1254: modeString = "convert";
1255: else
1256: modeString = nodeMode.getCharacters();
1257: if (modeString.equals("convert"))
1258: mode = DataBase.CONVERT_MODE;
1259: else if (modeString.equals("append"))
1260: mode = DataBase.APPEND_MODE;
1261: else if (modeString.equals("update"))
1262: mode = DataBase.UPDATE_MODE;
1263: else if (modeString.equals("delete"))
1264: mode = DataBase.DELETE_MODE;
1265:
1266: else if (modeString.equals("synchronize"))
1267: mode = DataBase.SYNCHRONIZE_MODE;
1.7 rogo 1268: else if (modeString.equals("convert_temp"))
1269: mode = DataBase.CONVERT_TEMP_MODE;
1270:
1.4 rogo 1271: // if(node3!=null)
1272: // System.out.println(node3.name);
1273:
1274: int length2 = countNodes(node3);
1275:
1276: System.out.println("number of tables " + length2);
1277:
1278: for (int j = 1; j <= length2; ++j)
1279: {
1280: Node node4 = root.find("convert/source/database/table", new int[]
1281: { 1, 1, i, j });
1282: Node node5 = root.find("convert/source/database/table/select", new int[]
1283: { 1, 1, i, j, 1 });
1284: Node node6 = root.find("convert/source/database/table/create", new int[]
1285: { 1, 1, i, j, 1 });
1286:
1287: if (node4 != null)
1288: System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
1289: if (node5 != null)
1290: System.out.println(node5.name + " " + node5.getCharacters());
1291: if (node6 != null)
1292: System.out.println(node6.name + " " + node6.getCharacters());
1293: if (node4 == null)
1294: throw new Error("parse error table tag missing");
1295: // if(node5==null) throw new Error("parse error select tag
1296: // missing");
1297: // if(node6==null) throw new Error("parse error create tag
1298: // missing");
1299: String name = (String) node4.attributes.get("name");
1300: String layout = (String) node4.attributes.get("layout");
1301: String id = (String) node4.attributes.get("id");
1302: System.out.println("id was " + id);
1303:
1304: if (name == null)
1305: throw new Error("parse error required table tag attribute name missing");
1306: if (layout == null)
1307: layout = "";
1308: if (id == null)
1309: id = "";
1310: if (name.equals(""))
1311: throw new Error("parse error table tag attribute must not be empty");
1.10 rogo 1312: tables.add(name.intern());
1313: layouts.add(layout.intern());
1314: ids.add(id.intern());
1.4 rogo 1315: String query = (node5 == null) ? "" : node5.getCharacters();
1316: if (query.equals(""))
1317: System.err.println("Warning empty select tag or select tag missing !!");
1318: query = (query.equals("")) ? "select * from " + database.getQC() + name
1319: + database.getQC() : query;
1320: selects.add(query);
1321: if (node6 != null)
1322: creates.add(node6.getCharacters().trim());
1323: else
1324: creates.add("");
1325:
1326: }
1327: DataBase dataBase = new DataBase(database, tables, layouts, selects, creates, ids, mode);
1328: dataBase.delimiter = delimiter;
1329: dataBase.preCommands = new Vector(preSQLCommands);
1330: dataBase.postCommands = new Vector(postSQLCommands);
1331: databases.add(dataBase);
1332: }
1333: DBBean database = new DBBean();
1334:
1335: preSQLCommands.clear();
1336: postSQLCommands.clear();
1337:
1338: // parse dataBase
1339: Node node = root.find("convert/destination/database/url", new int[]
1340: { 1, 1, 1, 1 });
1341: Node node1 = root.find("convert/destination/database/user", new int[]
1342: { 1, 1, 1, 1, 1 });
1343: Node node2 = root.find("convert/destination/database/password", new int[]
1344: { 1, 1, 1, 1, 1 });
1345: Node commandNodes = root.find("convert/destination/database/sqlcommands", new int[]
1346: { 1, 1, 1, 1, 1 });
1347: if (commandNodes != null)
1348: {
1349: parseCommandNode(commandNodes, database, preSQLCommands, postSQLCommands);
1350: }
1351:
1352: String url = node.getCharacters();
1353: String user = node1.getCharacters();
1354: String password = node2.getCharacters();
1355: System.out.println(url);
1356: database.setURL(url.trim());
1357: database.setUserAndPasswd(user.trim(), password.trim());
1358: DataBase db = new DataBase(database, new Vector(), null, null, null, null, 0);
1359: databases.add(db);
1360: db.preCommands = new Vector(preSQLCommands);
1361: db.postCommands = new Vector(postSQLCommands);
1362:
1363: } catch (Exception e)
1364: {
1365: e.printStackTrace();
1366: }
1367: return databases;
1368: }
1369:
1370: /**
1371: *
1372: * @param commandNode
1373: * @param database
1374: * @param preSQLCommands
1375: * @param postSQLCommand
1376: */
1377: private static void parseCommandNode(Node commandNode, DBBean database,
1378: java.util.Vector preSQLCommands, java.util.Vector postSQLCommands)
1379: {
1380: // System.out.println(commandNode.name + " " + countNodes(commandNode));
1381: int numCommands = commandNode.contents.length();
1382: for (int j = 0; j < numCommands; ++j)
1383: {
1384: Node node = (Node) commandNode.contents.v.elementAt(j);
1385: if (node.type.equals("element"))
1386: {
1387: // System.out.println(node.name + " " + node.getCharacters() + database);
1388: String execute = node.attributes.get("execute").toString();
1389: if (execute.equals("before"))
1390: {
1391: preSQLCommands.add(new SQLCommand(database, node.getCharacters()));
1392: }
1393: if (execute.equals("after"))
1394: {
1395: postSQLCommands.add(new SQLCommand(database, node.getCharacters()));
1396: }
1397:
1398: }
1399:
1400: }
1401: }
1402:
1403: private static int countNodes(Node tempNode)
1404: {
1405: int length = 0;
1406: for (int i = 0; i < tempNode.contents.v.size(); ++i)
1407: {
1408: Node node = (Node) tempNode.contents.v.elementAt(i);
1409: if (node.type.equals("element"))
1410: {
1411: if (node.name.equals("database"))
1412: length++;
1413: if (node.name.equals("table"))
1414: length++;
1415: if (node.name.equals("sqlcommand"))
1416: length++;
1417:
1418: }
1419:
1420: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+"
1421: // "+i);
1422: }
1423: return length;
1424: }
1425:
1426: private static void printContents(Node root)
1427: {
1428:
1429: Vector contents = (root.index == null) ? root.contents.v : root.index.v;
1430: for (int i = 0; i < contents.size(); ++i)
1431: {
1432: Node n = (Node) contents.elementAt(i);
1433: if (n.type.equals("element"))
1434: {
1435: System.out.println("tag " + n.name);
1436: System.out.println(n.getCharacters());
1437: // contents=n.contents.v i=0;
1438: }
1439: // System.out.println(n.type);
1440: }
1441: }
1442:
1443: /**
1444: * reads the specified xml file
1445: *
1446: * @param xmlFile
1447: */
1448: public static StringBuffer readXMLFile(String xmlFile)
1449: {
1450: InputStream stream = null;
1451: StringBuffer sb = new StringBuffer();
1452:
1453: try
1454: {
1455:
1456: if (xmlFile.indexOf("file://") >= 0 || xmlFile.indexOf("http://") >= 0)
1457: {
1458: URL url = new URL(xmlFile);
1459: stream = url.openStream();
1460: } else
1461: // read XML Metadata from a file
1462: stream = new FileInputStream(xmlFile);
1463: InputStreamReader isr = new InputStreamReader(stream, "UTF-8");
1464: BufferedReader buffr = new BufferedReader(isr);
1465: int c = 0;
1466: while ((c = buffr.read()) != -1)
1467: {
1468: char ch = (char) c;
1469: sb.append(ch);
1470: // System.out.print((char)c);
1471: }
1472: } catch (Exception e)
1473: {
1474: e.printStackTrace();
1475: } finally
1476: {
1477:
1478: try
1479: {
1480: stream.close();
1481: } catch (IOException e1)
1482: {
1483: // TODO Auto-generated catch block
1484: e1.printStackTrace();
1485: }
1486: }
1487: return sb;
1488: }
1489:
1490: /**
1491: * Helper class for Conversion etc. Contains data needed for the conversion
1492: *
1493: * @author rogo
1494: *
1495: */
1496:
1497: public static class ConversionProperties
1498: {
1499: String destTableName;
1500:
1501: String[] fieldNames;
1502:
1503: public ConversionProperties()
1504: {
1505: }
1506:
1507: public ConversionProperties(String destTableName, String[] fieldNames)
1508: {
1509: this.destTableName = destTableName;
1510: this.fieldNames = fieldNames;
1511:
1512: }
1513:
1514: }
1515:
1516: /**
1517: * Helper class for XML-File parsing Holds the parsed data
1518: *
1519: * @author rogo
1520: *
1521: */
1522: public static class DataBase
1523: {
1524: DBBean bean;
1525:
1526: Vector creates;
1527:
1528: Vector selects;
1529:
1530: Vector layouts;
1531:
1.7 rogo 1532: Vector tables = new Vector();
1.4 rogo 1533:
1534: Vector ids;
1535:
1536: String delimiter = "//";
1537:
1538: Vector preCommands;
1539: Vector postCommands;
1540:
1541: /**
1542: * maps table name to index fields
1543: */
1544: Hashtable htIndex = new Hashtable();
1545:
1546: boolean useNormanToUnicodeMapper = false;
1547:
1548: final static int CONVERT_MODE = 1;
1549:
1550: final static int APPEND_MODE = 2;
1551:
1552: final static int UPDATE_MODE = 3;
1553:
1554: final static int DELETE_MODE = 4;
1555:
1556: final static int SYNCHRONIZE_MODE = 5;
1557:
1.7 rogo 1558: final static int CONVERT_TEMP_MODE = 6;
1559:
1.4 rogo 1560: int mode = -1;
1561:
1562: public DataBase(DBBean bean, Vector tables, Vector layouts, Vector selects, Vector creates,
1563: Vector ids, int mode)
1564: {
1565: this.bean = bean;
1566: this.tables = tables;
1567: this.layouts = layouts;
1568: this.selects = selects;
1569: this.creates = creates;
1570: this.ids = ids;
1571: this.mode = mode;
1572: this.bean.setIDVector(ids);
1573: }
1574:
1575: /**
1576: * @param indexListVec
1577: */
1578: public void buildIndexTable(Vector indexListVec)
1579: {
1580: for (int i = 0; i < tables.size(); i++)
1581: {
1582: fillIndexList((String) tables.get(i), (String) indexListVec.get(i));
1583: }
1584: }
1585:
1586: /**
1587: * writes the data contained in this object to the buffered writer *
1588: *
1589: * @param buffr
1590: * @throws Exception
1591: */
1592: public void exportToXML(BufferedWriter buffr) throws Exception
1593: {
1594: // ids=bean.getIDVector();
1595: buffr.write(" <database>\n");
1596: buffr.write(" <url>" + bean.url + "</url>\n");
1597: buffr.write(" <user>" + bean.user + "</user>\n");
1598: buffr.write(" <password>" + bean.passwd + "</password>\n");
1599: buffr.write(" <delimiter>" + delimiter + "</delimiter>\n");
1600: String modeString = "";
1601: if (mode == CONVERT_MODE)
1602: modeString = "convert";
1603: else if (mode == APPEND_MODE)
1604: modeString = "append";
1605: else if (mode == UPDATE_MODE)
1606: modeString = "update";
1607: else if (mode == DELETE_MODE)
1608: modeString = "delete";
1609: else if (mode == SYNCHRONIZE_MODE)
1610: modeString = "synchronize";
1611:
1612: buffr.write(" <mode>" + modeString + "</mode>\n");
1613: buffr.write(" <usenormantounicodemapper>" + useNormanToUnicodeMapper
1614: + "</usenormantounicodemapper>\n");
1615: if (preCommands != null || postCommands != null)
1616: {
1617: int count = 0;
1618:
1619: buffr.write(" <sqlcommands> \n");
1620:
1621: if (preCommands != null)
1622: {
1623: while (count < preCommands.size())
1624: {
1.7 rogo 1625: SQLCommand sqlcommand = (SQLCommand) preCommands.get(count);
1626: buffr.write(" <sqlcommand execute=\"before\">" + sqlcommand.command
1627: + "</sqlcommand>\n");
1.4 rogo 1628: count++;
1629: }
1630: }
1631: if (postCommands != null)
1632: {
1633: count = 0;
1634: while (count < postCommands.size())
1635: {
1.7 rogo 1636: SQLCommand sqlcommand = (SQLCommand) postCommands.get(count);
1637:
1638: buffr.write(" <sqlcommand execute=\"after\">" + sqlcommand.command
1639: + "</sqlcommand>\n");
1.4 rogo 1640: count++;
1641: }
1642: }
1643: buffr.write(" </sqlcommands> \n");
1644:
1645: }
1646: int index = 0;
1647: while (index < tables.size())
1648: {
1649: String table = (String) tables.get(index);
1650: String layout = (String) layouts.get(index);
1651: String select = (String) selects.get(index);
1652: String create = (String) creates.get(index);
1653: String id = (String) ids.get(index);
1654: IndexList indexList = (IndexList) htIndex.get(table);
1655: if (indexList == null)
1656: indexList = new IndexList();
1657: buffr.write(" <table name = \"" + table + "\" layout = \"" + layout + "\" id = \""
1658: + id + "\" indexList =\"" + indexList + "\">\n");
1659: buffr.write(" <select>" + convertToEntities(select) + "</select>\n");
1660: if (!create.equals(""))
1661: buffr.write(" <create>" + create + " </create>\n");
1662: buffr.write(" </table>\n");
1663: index++;
1664: }
1665: buffr.write(" </database>\n");
1666: }
1667:
1668: public void fillIndexList(String table, String list)
1669: {
1670: IndexList indexList = new IndexList();
1671: StringTokenizer tokenizer = new StringTokenizer(list, ",");
1672: while (tokenizer.hasMoreTokens())
1673: {
1674: indexList.add(tokenizer.nextToken());
1675: }
1676: System.out.println(indexList);
1677:
1678: htIndex.put(table, indexList);
1679: }
1680:
1681: public String toString()
1682: {
1683: return bean.url + " " + tables;
1684: }
1685:
1686: }
1687:
1688: public static void writeConfig(String file, DataBase source, DataBase destination)
1689: throws Exception
1690: {
1691: if (!file.toLowerCase().endsWith(".xml"))
1692: file += ".xml";
1693: File f = new File(file);
1694:
1695: FileOutputStream fout = new FileOutputStream(f);
1696: OutputStreamWriter outsw = new OutputStreamWriter(fout, "UTF-8");
1697: BufferedWriter buffw = new BufferedWriter(outsw);
1698: buffw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1699: buffw.newLine();
1700: buffw.write("<convert>\n");
1701: buffw.write(" <source>\n");
1702: source.exportToXML(buffw);
1703: buffw.write(" </source>\n");
1704: buffw.write("\n <destination>\n");
1705: destination.exportToXML(buffw);
1706: buffw.write(" </destination>\n");
1707: buffw.write("</convert>\n");
1708: buffw.close();
1709: }
1710:
1711: public static void delete(String source, String destination, Vector names, Vector layouts,
1712: Vector selects, Vector creates, Vector ids, int mode) throws Exception
1713: {
1714: FM2SQL.ProgressDialog dialog = null;
1715: if (isGUI)
1716: {
1717: dialog = initDialog();
1718: }
1719: // setting user and passwd
1720: bean.setUserAndPasswd(user, passwd);
1721: // setting user and passwd
1722: beanDest.setUserAndPasswd(userDest, passwdDest);
1723: StringBuffer command = null;
1724: String query = null;
1725: try
1726: {
1727: // bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");
1728: // bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
1729: bean.setConnection(source);
1730: if (names == null)
1731: names = bean.getTableNames();
1732: // Collections.sort(names);
1733: int tbIndex = 1;
1734:
1735: // System.out.println("Start at
1736: // "+names.indexOf("archimedes_facsimiles"));
1737: for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
1738: {
1739: Vector[] result = null;
1740: java.util.TreeSet myIds = new TreeSet();
1741: java.util.TreeSet myIdsDest = new TreeSet();
1742: int deltaID = 1;
1743: String idField = "";
1744: String destTableName = "";
1745:
1746: try
1747: {
1748: query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
1749: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
1750: query = (selects != null) ? selects.get(tbIndex).toString() : query;
1751: // if vectors[1].get(i) != null)
1752: if (layout != "")
1753: {
1754: layout = " layout " + bean.getQC() + layout + bean.getQC();
1755: String name = names.get(tbIndex).toString();
1756: StringBuffer queryLayout = new StringBuffer(query);
1757: queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
1758: query = queryLayout.toString();
1759: System.out.println("added layout " + query);
1760:
1761: }
1762: dialog.title.setText("Getting table data ...");
1763: dialog.table.setText(names.get(tbIndex).toString());
1764: dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
1765: dialog.setVisible(true);
1766: bean.getConnection();
1767: bean.makeQuery(query, 50);
1768: idField = ids.get(tbIndex).toString();
1769:
1770: } catch (Exception e)
1771: {
1772: continue;
1773: }
1774: // determine destTableName from createStatement or from source
1775: // table name
1776: if (!creates.get(tbIndex).equals(""))
1777: {
1778: String create = creates.get(tbIndex).toString().toLowerCase();
1779: int fromIndex = create.indexOf("table") + 5;
1780: int toIndex = create.indexOf("(");
1781: destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "")
1782: .trim();
1783: System.out.println("destTable " + destTableName);
1784:
1785: } else
1786: destTableName = convertText(names.get(tbIndex).toString());
1787:
1788: // for id kram
1789: Vector vec = null;
1790: Vector vecDest = null;
1791: // tempo
1792: beanDest.setConnection(destination);
1793: int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
1794: String tempID = bean.getQC() + idField + bean.getQC();
1795: String tempIDdest = beanDest.getQC() + convertText(idField) + beanDest.getQC();
1796:
1797: int endIndex = -1;
1798: String tempQuery = query;
1799: String destQuery = query.replaceAll(names.get(tbIndex).toString(), destTableName);
1800: String tempQueryDest = destQuery;
1801: // remove extra query parts
1802: // destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1);
1803: System.out.println("new Query " + tempQueryDest);
1804: if (!idField.equals(""))
1805: {
1806: long startTime = System.currentTimeMillis();
1807: int counter = -1;
1808: while (true)
1809: {
1810: ++counter;
1811: if (counter == 0 && dialog != null)
1812: dialog.title.setText("Check if data is available");
1813: else if (dialog != null)
1814: dialog.title.setText("Check if more data is available");
1815: myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex),
1816: tempQuery, numHits);
1817: myIdsDest = beanDest.getIDVector(convertText(idField), destTableName, tempQueryDest,
1818: numHits);
1819: if (myIds.isEmpty())
1820: break;
1821: vec = new Vector(myIds);
1822: vecDest = new Vector(myIdsDest);
1823: rowCount = vec.size();
1824: // Deletion will work this way
1825: Vector deleted = new Vector(vec);
1826: Vector linesToDelete = new Vector(vecDest);
1827: // remove all lines that should not be deleted
1828: linesToDelete.removeAll(deleted);
1829: // System.out.println("ID LIST SIZE " +
1830: // Math.round((double) myIds.size() / (double)
1831: // numIntervalls) + " " + myIdsDest.size());
1832: // / @TODO complete delete task remove query show lines
1833: // to be deleted let user choose if he wants that
1834: System.out.println("number of lines to be deleted " + linesToDelete.size());
1835: deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
1836: beanDest.setConnection(destination);
1837:
1838: Statement stm = beanDest.getConnection().createStatement();
1839:
1840: Vector tables = beanDest.getTableNames();
1841: // Collections.sort(tables);
1842: System.out.println(names.get(tbIndex) + " "
1843: + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
1844: tables = beanDest.getTableNames();
1845: // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
1846: stm = beanDest.getConnection().createStatement();
1847:
1848: if (dialog != null)
1849: dialog.title.setText(" Deleting table data ...");
1850:
1851: int j = -1;
1852:
1853: Vector row = null;
1854: command = new StringBuffer();
1855:
1856: command.append("DELETE FROM");
1857: command.append(beanDest.getQC());
1858: command.append(destTableName);
1859: command.append(beanDest.getQC());
1860: int size = bean.getColumnNames().size();
1861: command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " = ?");
1862: PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
1863: System.out.println(command + " " + tbIndex);
1864: while (true)
1865: {
1866:
1867: ++j;
1868: if (j == linesToDelete.size())
1869: break;
1870: // print rows
1871: pstm.setString(1, linesToDelete.get(j).toString());
1872: System.out.println(pstm.toString());
1873: pstm.execute();
1874: if (isGUI)
1875: dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
1876: command = null;
1877: }
1878: // prepare new query for next chunk
1879: if (query.indexOf("where") > 0)
1880: tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
1881: else
1882: tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
1883:
1884: } // to outer while
1885: } // to idfield if
1886: } // table loop
1887:
1888: } catch (Exception e)
1889: {
1890: System.out.println("Error while connecting to database " + e);
1891: if (isGUI)
1892: {
1893: showExceptionDialog(dialog, command, e);
1894: resetGUI(dialog);
1895: } else
1896: {
1897: e.printStackTrace();
1898: }
1899: }
1900: } // to method
1901:
1902: /**
1903: * synchronize method based on delete method code
1904: *
1905: * @param source
1906: * @param destination
1907: * @param names
1908: * @param layouts
1909: * @param selects
1910: * @param creates
1911: * @param ids
1912: * @param mode
1913: * @throws Exception
1914: */
1915: // TODO implement append,update and delete in one method
1916: // TODO using id based algorithm
1917: public static void synchronize(String source, String destination, Vector names, Vector layouts,
1918: Vector selects, Vector creates, Vector ids, int mode, String delimiter, Vector indexList)
1919: throws Exception
1920: {
1921: System.out.println(" bin in synchronize!!!");
1922: FM2SQL.ProgressDialog dialog = null;
1923: if (isGUI)
1924: {
1925: dialog = initDialog();
1926: dialog.setTitle("Synchronize running ...");
1927:
1928: }
1929: // setting user and passwd
1930: bean.setUserAndPasswd(user, passwd);
1931: // setting user and passwd
1932: beanDest.setUserAndPasswd(userDest, passwdDest);
1933: StringBuffer command = null;
1934: String query = null;
1935: try
1936: {
1937: bean.setConnection(source);
1938: if (names == null)
1939: names = bean.getTableNames();
1940: int tbIndex = 1;
1941:
1942: for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
1943: {
1944: Vector[] result = null;
1945: java.util.TreeSet myIds = new TreeSet();
1946: java.util.TreeSet myIdsDest = new TreeSet();
1947: int deltaID = 1;
1948: String idField = "";
1949: String destTableName = "";
1950:
1951: try
1952: {
1953: query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
1954: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
1955: query = (selects != null) ? selects.get(tbIndex).toString() : query;
1956: // if vectors[1].get(i) != null)
1957: if (!layout.equals(""))
1958: {
1959: query = addLayoutToQuery(names, query, tbIndex, layout);
1960:
1961: }
1962: if (dialog != null)
1963: {
1964: prepareDialogforUse(names, dialog, tbIndex);
1965: }
1966: bean.getConnection();
1967: bean.makeQuery(query, 50);
1968: idField = ids.get(tbIndex).toString();
1969:
1970: } catch (Exception e)
1971: {
1972: System.out.println("Warning exception occured \n " + e);
1973:
1974: continue;
1975: }
1976: // determine destTableName from createStatement or from source
1977: // table name
1978: if (!creates.get(tbIndex).equals(""))
1979: {
1980: String create = creates.get(tbIndex).toString().toLowerCase();
1981: int fromIndex = create.indexOf("table") + 5;
1982: int toIndex = create.indexOf("(");
1983: destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "")
1984: .trim();
1985: System.out.println("destTable " + destTableName);
1986:
1987: } else
1988: destTableName = convertText(names.get(tbIndex).toString());
1989:
1990: // for id kram
1991: Vector vec = null;
1992: Vector vecDest = null;
1993: // tempo
1994: beanDest.setConnection(destination);
1995: int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
1996: String tempID = bean.getQC() + idField + bean.getQC();
1997: String tempIDdest = beanDest.getQC() + convertText(idField) + beanDest.getQC();
1998:
1999: int endIndex = -1;
2000: String tempQuery = query;
2001: String destQuery = query.replaceAll(names.get(tbIndex).toString(), destTableName);
2002: destQuery = destQuery.replaceAll(bean.getQC(), beanDest.getQC());
2003: destQuery = removeLayoutPartFromQuery(destQuery, layouts.get(tbIndex).toString());
2004: // TODO remove layout part for destQuery
2005: String tempQueryDest = destQuery;
2006: // remove extra query parts
2007: // destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1);
2008: System.out.println("new Query " + tempQueryDest);
2009: System.out.println("idfield " + idField + " " + ids.get(tbIndex).toString());
2010: if (!idField.equals(""))
2011: {
2012: long startTime = System.currentTimeMillis();
2013: int counter = -1;
2014: TreeSet linesToDelete = null;
2015: PreparedStatement delPSt = null;
2016: while (true)
2017: {
2018: ++counter;
2019: if (counter == 0 && dialog != null)
2020: dialog.title.setText("Check if data is available");
2021: else if (dialog != null)
2022: dialog.title.setText("Check if more data is available");
2023:
2024: myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex),
2025: tempQuery, 0);
2026: myIdsDest = beanDest.getIDVector(convertText(idField), destTableName, tempQueryDest, 0);
2027: // System.out.println("status of remove
2028: // "+myIds.remove("b015892"));
2029: System.out.println("ids found for " + idField + " " + !myIds.isEmpty());
2030: if (myIds.isEmpty())
2031: break;
2032: vec = new Vector(myIds);
2033: vecDest = new Vector(myIdsDest);
2034: rowCount = vec.size();
2035: // Deletion will work this way
2036: Vector deleted = new Vector(vec);
2037:
2038: TreeSet linesToAppend = new TreeSet(vec);
2039: linesToAppend.addAll(vec);
2040: linesToDelete = new TreeSet(vecDest);
2041: // remove all lines that are already in dest database
2042: linesToAppend.removeAll(vecDest);
2043: // remove all lines that should not be deleted
2044: linesToDelete.removeAll(deleted);
2045: System.out.println("linesToAppend " + linesToAppend.size() + " " + destTableName);
2046: System.out.println("linesToDelete " + linesToDelete.size() + " " + destTableName);
2047: System.out.println("ID LIST SIZE "
2048: + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIds.size());
2049: deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
2050: ConversionProperties prop = getFieldNamesAndDestTableName(creates.get(tbIndex)
2051: .toString(), query, names.get(tbIndex).toString());
2052: StringBuffer insCommand = createInsertCommand(prop.destTableName, prop.fieldNames);
2053: StringBuffer updCommand = createUpdateCommand(prop.destTableName, prop.fieldNames,
2054: tempIDdest);
2055: StringBuffer delCommand = createDeleteCommand(destTableName, tempIDdest);
2056: PreparedStatement insPst = beanDest.getConnection().prepareStatement(
2057: insCommand.toString());
2058: PreparedStatement updPst = beanDest.getConnection().prepareStatement(
2059: updCommand.toString());
2060: delPSt = beanDest.getConnection().prepareStatement(delCommand.toString());
2061: // delPSt.setString(1,"b015892");
2062: // delPSt.execute();
2063: // if (true)
2064: // return;
2065: if (vec.size() <= numIntervalls)
2066: {
2067: endIndex = 0;
2068: deltaID = vec.size();
2069: }
2070: for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)
2071: {
2072: System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());
2073: if (query.indexOf("where") > 0)
2074: tempQuery = query + " and " + tempID + ">='" + vec.get(k) + "' and " + tempID
2075: + "<='" + vec.get(k + deltaID) + "'";
2076: else
2077: tempQuery = query + " where " + tempID + ">='" + vec.get(k) + "' and " + tempID
2078: + "<='" + vec.get(k + deltaID) + "'";
2079: System.out.println(tempQuery);
2080: if (dialog != null)
2081: dialog.title.setText("Reading table data ...");
2082:
2083: // bean.makeQuery(tempQuery, deltaID);
2084: if (dialog != null)
2085: dialog.title.setText("Writing table data ...");
2086:
2087: performSynchronize(idField, vec, tempQuery, linesToDelete, linesToAppend, insPst,
2088: updPst, delPSt, deltaID, delimiter, dialog);
2089: // System.out.println("ID LIST SIZE " +
2090: // Math.round((double) myIds.size() / (double)
2091: // numIntervalls) + " " + myIdsDest.size());
2092: endIndex = k + deltaID;
2093: }
2094: System.out.println(endIndex);
2095: // all data written ? if not write last chunk of data
2096: if (endIndex == vec.size() - 1)
2097: System.out.println("fits");
2098: else
2099: {
2100: System.out.println(" last intervall from " + vec.get(endIndex) + " "
2101: + vec.lastElement());
2102:
2103: if (query.indexOf("where") > 0)
2104: tempQuery = query + " and " + tempID + ">='" + vec.get(endIndex) + "' and "
2105: + tempID + "<='" + vec.lastElement() + "'";
2106: else
2107: tempQuery = query + " where " + tempID + ">='" + vec.get(endIndex) + "' and "
2108: + tempID + "<='" + vec.lastElement() + "'";
2109: System.out.println(tempQuery);
2110: if (dialog != null)
2111: dialog.title.setText("Reading table data ...");
2112: // bean.makeQuery(tempQuery, 0);
2113: if (dialog != null)
2114: dialog.title.setText("Writing table data ...");
2115: performSynchronize(idField, vec, tempQuery, linesToDelete, linesToAppend, insPst,
2116: updPst, delPSt, deltaID, delimiter, dialog);
2117: // System.out.println("ID LIST SIZE " +
2118: // Math.round((double) myIds.size() / (double)
2119: // numIntervalls) + " " + myIdsDest.size());
2120: }
2121: // prepare new query for next chunk
2122: if (query.indexOf("where") > 0)
2123: tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
2124: else
2125: tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
2126:
2127: }
2128: String tableName = names.get(tbIndex).toString();
2129: if (!indexList.isEmpty())
2130: {
2131: IndexList idList = (IndexList) indexList.get(0);
2132: System.out.println("found list " + idList);
2133: Statement stm = beanDest.getConnection().createStatement();
2134: Vector destTables = beanDest.getTableNames();
2135: System.out.println("tempQueryDest" + tempQueryDest);
2136: beanDest.makeQuery(tempQueryDest, 0);
2137: for (Iterator iter = idList.iterator(); iter.hasNext();)
2138: {
2139: String indexField = (String) iter.next();
2140: indexField = convertText(indexField);
2141: String indexName = destTableName + "_" + indexField;
2142: if (destTables.contains(indexName))
2143: {
2144: stm.execute("DROP INDEX " + destTableName + "_" + indexField);
2145: // continue;
2146: }
2147: // stm.execute("DROP INDEX
2148: // "+destTableName+"_"+indexField);
2149:
2150: String type = beanDest.getColumnType(indexField).toLowerCase();
2151: // System.out.println(indexField+" "+type+"
2152: // "+(type.indexOf("text") >= 0 ||
2153: // type.indexOf("varchar") >= 0 || type.indexOf("char")
2154: // >= 0));
2155: if (type.indexOf("text") >= 0 || type.indexOf("varchar") >= 0
2156: || type.indexOf("char") >= 0)
2157: {
2158: if (beanDest.url.indexOf("mysql") >= 0)
2159: {
2160: // System.out.println("CREATE INDEX " +
2161: // indexName + " ON " + destTableName + " (" +
2162: // indexField + "(10))");
2163: // TODO problem if index exist !!!
2164: stm.execute("CREATE INDEX " + indexName + " ON " + destTableName + " ("
2165: + indexField + "(10))");
2166: } else
2167: {
2168: stm.execute("CREATE INDEX " + indexName + " ON " + destTableName + " (lower( "
2169: + indexField + "))");
2170:
2171: }
2172:
2173: } else
2174: {
2175: stm.execute("CREATE INDEX " + destTableName + "_" + indexField + " ON "
2176: + destTableName + "(" + indexField + ")");
2177:
2178: }
2179:
2180: // stm.execute("DROP INDEX
2181: // "+destTableName+"_"+indexField);
2182:
2183: }
2184: }
2185: // CREATE UNIQUE INDEX title_idx ON films (title);
2186: for (Iterator iter = linesToDelete.iterator(); iter.hasNext();)
2187: {
2188: String id = (String) iter.next();
2189: delPSt.setString(1, id);
2190: delPSt.execute();
2191:
2192: }
2193:
2194: long endTime = System.currentTimeMillis();
2195: System.out.println("Time for incremental synchronize elapsed " + (endTime - startTime));
2196: } // to idfield if
2197: } // table loop
2198:
2199: } catch (Exception e)
2200: {
2201: System.out.println("Error while connecting to database " + e);
2202: e.printStackTrace();
2203: if (isGUI)
2204: showExceptionDialog(dialog, command, e);
2205: }
2206: if (isGUI)
2207: {
2208: resetGUI(dialog);
2209: }
2210: }
2211:
2212: /**
2213: * @param destQuery
2214: * @param string
2215: * @return
2216: */
2217: private static String removeLayoutPartFromQuery(String destQuery, String layoutName)
2218: {
2219: String removeString = "layout " + beanDest.getQC() + layoutName + beanDest.getQC();
2220: destQuery = destQuery.replaceFirst(removeString, "");
2221: System.out.println("destQuery change to " + destQuery);
2222: return destQuery;
2223: }
2224:
2225: private static void performSynchronize(String idField, Vector vec, String tempQuery,
2226: TreeSet linesToDelete, TreeSet linesToAppend, PreparedStatement insPst,
2227: PreparedStatement updPst, PreparedStatement delPSt, int deltaID, String delimiter,
2228: FM2SQL.ProgressDialog dialog) throws SQLException, ParseException, Exception
2229: {
2230: if (dialog != null)
2231: {
2232: dialog.progress.setValue(0);
2233: dialog.title.setText("Retrieving new data");
2234: }
2235:
2236: Vector[] vectors = bean.getQueryData(tempQuery, deltaID);
2237: int count = 0, size = vectors[0].size();
2238: int idIndex = vectors[1].indexOf(idField);
2239: // System.out.println(idIndex + " " + vectors[1] + " " + idField);
2240: // todo arraylist code has to be added
2241: if (dialog != null)
2242: dialog.title.setText("Synchronize with new data");
2243:
2244: for (Iterator iter = vectors[0].iterator(); iter.hasNext();)
2245: {
2246: Vector line = (Vector) iter.next();
2247: Object lineIDIndex = line.get(idIndex);
2248: if (linesToAppend.contains(lineIDIndex))
2249: System.out.println("line " + linesToAppend.contains(line.get(idIndex)) + " " + lineIDIndex);
2250: if (linesToAppend.contains(lineIDIndex))
2251: {
2252: for (int l = 0; l < line.size(); ++l)
2253: {
2254: Object obj = line.get(l);
2255: if (obj instanceof ArrayList)
2256: obj = formatFileMakerArray((List) obj, delimiter);
2257: if (obj != null)
2258: insPst.setString(l + 1, obj.toString());
2259: else
2260: insPst.setNull(l + 1, Types.NULL);
2261: }
2262: insPst.execute();
2263:
2264: } else
2265: // update
2266: {
2267: for (int l = 0; l < line.size(); ++l)
2268: {
2269: Object obj = line.get(l);
2270: if (obj instanceof ArrayList)
2271: obj = formatFileMakerArray((List) obj, delimiter);
2272: if (obj != null)
2273: updPst.setString(l + 1, obj.toString());
2274: else
2275: updPst.setNull(l + 1, Types.NULL);
2276: }
2277: updPst.setString(line.size() + 1, line.get(idIndex).toString());
2278: // updPst.addBatch();
2279: // updPst.execute();
2280: }
2281: if (dialog != null)
2282: {
2283: int value = (int) Math.round(((double) count / (double) size) * 100.0);
2284: dialog.progress.setValue(value);
2285: count++;
2286: }
2287: }
2288: // updPst.executeBatch();
2289: } // to method
2290:
2291: /**
2292: * Converts input String in norman encoding to unicode
2293: *
2294: * @param inp
2295: * @return converted String
2296: */
2297: static public String normanToUnicode(String inp)
2298: {
2299: StringBuffer buf = new StringBuffer();
2300: for (int i = 0; i < inp.length(); i++)
2301: {
2302: char c = inp.charAt(i);
2303: // System.out.println("char "+c+" "+(int)c);
2304: switch (c)
2305: {
2306: case 1:
2307: buf.append("\u00d0");
2308: break; // Eth
2309: case 2:
2310: buf.append("\u00f0");
2311: break; // eth
2312: case 3:
2313: buf.append("\u0141");
2314: break; // Lslash
2315: case 4:
2316: buf.append("\u0142");
2317: break; // lslash
2318: case 5:
2319: buf.append("\u0160");
2320: break; // S caron
2321: case 6:
2322: buf.append("\u0161");
2323: break; // s caron
2324: case 7:
2325: buf.append("\u00dd");
2326: break; // Y acute
2327: case 8:
2328: buf.append("\u00fd");
2329: break; // y acute
2330: case 11:
2331: buf.append("\u00de");
2332: break; // Thorn
2333: case 12:
2334: buf.append("\u00fe");
2335: break; // thorn
2336: case 14:
2337: buf.append("\u017d");
2338: break; // Z caron
2339: case 15:
2340: buf.append("\u017e");
2341: break; // z caron
2342: case 17:
2343: buf.append("\u0073");
2344: break; // asciitilde
2345: case 18:
2346: buf.append("j\u0305");
2347: break; // j macron [does a single char exist?]
2348: case 19:
2349: buf.append("^");
2350: break; // circumflex
2351: case 20:
2352: buf.append("\u0303");
2353: break; // tilde
2354: case 21:
2355: buf.append("\u00bd");
2356: break; // onehalf
2357: case 22:
2358: buf.append("\u00bc");
2359: break; // onequarter
2360: case 23:
2361: buf.append("\u00b9");
2362: break; // onesuperior
2363: case 24:
2364: buf.append("\u00be");
2365: break; // threequarters
2366: case 25:
2367: buf.append("\u00b3");
2368: break; // threesuperior
2369: case 26:
2370: buf.append("\u00b2");
2371: break; // twosuperior
2372: case 27:
2373: buf.append("\u00a6");
2374: break; // brokenbar
2375: case 28:
2376: buf.append("-");
2377: break; // minus
2378: case 29:
2379: buf.append("\u00d7");
2380: break; // multiply
2381: case 39:
2382: buf.append("'");
2383: break; // quotesingle
2384: case 94:
2385: buf.append("\u0302");
2386: break; // circumflex
2387: case 96:
2388: buf.append("\u0300");
2389: break; // grave
2390: case 196:
2391: buf.append("\u00c4");
2392: break; // A dieresis
2393: case 197:
2394: buf.append("\u00c5");
2395: break; // A ring
2396: case 201:
2397: buf.append("\u00c9");
2398: break; // E acute
2399: case 209:
2400: buf.append("\u00d1");
2401: break; // N tilde
2402: case 214:
2403: buf.append("\u00d6");
2404: break; // O dieresis
2405: case 220:
2406: buf.append("\u00dc");
2407: break; // U dieresis
2408: case 225:
2409: buf.append("\u00e1");
2410: break; // a acute
2411: case 224:
2412: buf.append("\u00e0");
2413: break; // a grave
2414: case 226:
2415: buf.append("\u00e2");
2416: break; // a circumflex
2417: case 228:
2418: buf.append("\u00e4");
2419: break; // a dieresis
2420: case 227:
2421: buf.append("\u00e3");
2422: break; // a tilde
2423: case 229:
2424: buf.append("\u0101");
2425: break; // a macron
2426: case 231:
2427: buf.append("\u00e7");
2428: break; // c cedilla
2429: case 233:
2430: buf.append("\u00e9");
2431: break; // e acute
2432: case 232:
2433: buf.append("\u00e8");
2434: break; // e grave
2435: case 234:
2436: buf.append("\u00ea");
2437: break; // e circumflex
2438: case 235:
2439: buf.append("\u00eb");
2440: break; // e dieresis
2441: case 237:
2442: buf.append("\u00ed");
2443: break; // i acute
2444: case 236:
2445: buf.append("\u00ec");
2446: break; // i grave
2447: case 238:
2448: buf.append("\u00ee");
2449: break; // i circumflex
2450: case 239:
2451: buf.append("\u00ef");
2452: break; // i dieresis
2453: case 241:
2454: buf.append("\u00f1");
2455: break; // n tilde
2456: case 243:
2457: buf.append("\u00f3");
2458: break; // o acute
2459: case 242:
2460: buf.append("\u00f2");
2461: break; // o grave
2462: case 244:
2463: buf.append("\u00f4");
2464: break; // o circumflex
2465: case 246:
2466: buf.append("\u00f6");
2467: break; // o dieresis
2468: case 245:
2469: buf.append("\u00f5");
2470: break; // o tilde
2471: case 250:
2472: buf.append("\u00fa");
2473: break; // u acute
2474: case 249:
2475: buf.append("\u00f9");
2476: break; // u grave
2477: case 251:
2478: buf.append("\u00fb");
2479: break; // u circumflex
2480: case 252:
2481: buf.append("\u00fc");
2482: break; // u dieresis
2483: case 8224:
2484: buf.append("\u1e6d");
2485: break; // t underdot
2486: case 176:
2487: buf.append("\u00b0");
2488: break; // degree
2489: case 162:
2490: buf.append("\u1ebd");
2491: break; // e tilde
2492: case 163:
2493: buf.append("\u00a3");
2494: break; // sterling
2495: case 167:
2496: buf.append("\u00a7");
2497: break; // section
2498: case 182:
2499: buf.append("\u00b6");
2500: break; // paragraph
2501: case 223:
2502: buf.append("\u015b");
2503: break; // s acute
2504: case 174:
2505: buf.append("\u1e5b");
2506: break; // r underdot
2507: case 169:
2508: buf.append("\u1e45");
2509: break; // n overdot
2510: case 353:
2511: buf.append("\u1e45");
2512: break; // n overdot
2513: case 180:
2514: buf.append("\u0301");
2515: break; // acute
2516: case 168:
2517: buf.append("\u0308");
2518: break; // dieresis
2519: case 8800:
2520: buf.append("\u1e6d");
2521: break; // t underdot
2522: case 198:
2523: buf.append("\u00c6");
2524: break; // AE
2525: case 216:
2526: buf.append("\u014d");
2527: break; // o macron
2528: case 8734:
2529: buf.append("\u0129");
2530: break; // i tilde
2531: case 177:
2532: buf.append("\u00b1");
2533: break; // plusminus
2534: case 165:
2535: buf.append("\u012b");
2536: break; // i macron
2537: case 181:
2538: buf.append("\u1e43");
2539: break; // m underdot
2540: case 8706:
2541: buf.append("\u1e0d");
2542: break; // d underdot
2543: case 240:
2544: buf.append("\u1e0d");
2545: break; // d underdot
2546:
2547: case 8721:
2548: buf.append("\u1e63");
2549: break; // s underdot
2550: case 960:
2551: buf.append("\u017a");
2552: break; // z acute
2553: case 8747:
2554: buf.append("\u1e45");
2555: break; // n overdot
2556: case 937:
2557: buf.append("\u0169");
2558: break; // u tilde
2559: case 230:
2560: buf.append("\u00e6");
2561: break; // ae
2562: case 248:
2563: buf.append("\u00f8");
2564: break; // oslash
2565: case 191:
2566: buf.append("\u0304\u0306");
2567: break; // macron breve
2568: case 172:
2569: buf.append("\u1e37");
2570: break; //
2571: case 8730:
2572: buf.append("j\u0305");
2573: break; // j macron [does a single char exist?]
2574: case 402:
2575: buf.append("\u0103");
2576: break; // a breve
2577: case 8776:
2578: buf.append("\u016d");
2579: break; // u breve
2580: case 187:
2581: buf.append("\u1e42");
2582: break; // M underdot
2583: case 8230:
2584: buf.append("\u2026");
2585: break; // ellipsis
2586: case 192:
2587: buf.append("\u00c0");
2588: break; // A grave
2589: case 195:
2590: buf.append("\u00c3");
2591: break; // A tilde
2592: case 213:
2593: buf.append("\u00d5");
2594: break; // O tilde
2595: case 338:
2596: buf.append("m\u0306");
2597: break; // m breve
2598: case 339:
2599: buf.append("\u0153");
2600: break; // oe
2601: case 8211:
2602: buf.append("\u2013");
2603: break; // endash
2604: case 8212:
2605: buf.append("\u2014");
2606: break; // emdash
2607: case 8220:
2608: buf.append("\u201c");
2609: break; // quotedblleft
2610: case 8221:
2611: buf.append("\u201d");
2612: break; // quotedblright
2613: case 8216:
2614: buf.append("\u2018");
2615: break; // quoteleft
2616: case 8217:
2617: buf.append("\u2019");
2618: break; // quoteright
2619: case 247:
2620: buf.append("\u1e37");
2621: break; // l underring [actually underdot]
2622: case 9674:
2623: buf.append("\u1e41");
2624: break; // m overdot
2625: case 255:
2626: buf.append("n\u0306");
2627: break; // n breve
2628: case 376:
2629: buf.append("\u00d7");
2630: break; // multiply
2631: case 8364:
2632: buf.append("\u1e5b");
2633: break; // r underring [actually underdot]
2634: case 8249:
2635: buf.append("\u1e44");
2636: break; // N overdot
2637: case 8250:
2638: buf.append("\u1e62");
2639: break; // S underdot
2640: case 64257:
2641: buf.append("\u1e24");
2642: break; // H underdot
2643: case 64258:
2644: buf.append("\u1e0c");
2645: break; // D underdot
2646: case 8225:
2647: buf.append("\u2021");
2648: break; // daggerdbl
2649: case 8218:
2650: buf.append("\u1e36");
2651: break; // L underdot
2652: case 8222:
2653: buf.append("\u0113");
2654: break; // e macron
2655: case 194:
2656: buf.append("\u1e5f");
2657: break; // r underbar
2658: case 202:
2659: buf.append("r\u0324");
2660: break; // r underdieresis
2661: case 193:
2662: buf.append("\u012a");
2663: break; // I macron
2664: case 8486:
2665: case 203:
2666: buf.append("\u016b");
2667: break; // u macron
2668: case 200:
2669: buf.append("\u1e6c");
2670: break; // T underdot
2671: case 205:
2672: buf.append("\u1e64");
2673: break; // S acute
2674: case 206:
2675: buf.append("\u2020");
2676: break; // dagger
2677: case 207:
2678: buf.append("\u0115");
2679: break; // e breve
2680: case 204:
2681: buf.append("\u014f");
2682: break; // o breve
2683: case 211:
2684: buf.append("\u0100");
2685: break; // A macron
2686: case 212:
2687: buf.append("\u1e46");
2688: break; // N underdot
2689: case 210:
2690: buf.append("\u1e3b");
2691: break; // l underbar
2692: case 218:
2693: buf.append("\u016a");
2694: break; // U macron
2695: case 219:
2696: buf.append("\u0179");
2697: break; // Z acute
2698: case 217:
2699: buf.append("\u1e5a");
2700: break; // R underdot
2701: case 305:
2702: buf.append("\u0131");
2703: break; // dotlessi
2704: case 710:
2705: buf.append("\u1e47");
2706: break; // n underdot
2707: case 732:
2708: buf.append("\u1e49");
2709: break; // n underbar
2710: case 175:
2711: buf.append("\u0304");
2712: break; // macron
2713: case 728:
2714: buf.append("\u0306");
2715: break; // breve
2716: case 729:
2717: case 215:
2718: buf.append("\u1e25");
2719: break; // h underdot
2720: case 730:
2721: buf.append("\u012d");
2722: break; // i breve
2723: case 184:
2724: buf.append("\u0327");
2725: break; // cedilla
2726: case 733:
2727: buf.append("\u030b");
2728: break; // hungarumlaut
2729: case 731:
2730: buf.append("\u0328");
2731: break; // ogonek
2732: case 711:
2733: buf.append("\u030c");
2734: break; // caron
2735: case 199:
2736: buf.append("\u012b\u0303");
2737: break; // imacron tilde
2738: case 8226:
2739: buf.append("\u1e5d");
2740: break; // runderdot macron
2741: case 8482:
2742: buf.append("\u016b\0306");
2743: break; // umacron breve
2744: case 8804:
2745: buf.append("\u0101\u0301");
2746: break; // amacron acute
2747: case 8805:
2748: buf.append("\u016b\u0301");
2749: break; // umacron acute
2750: case 8719:
2751: buf.append("\u0113\u0301");
2752: break; // emacron acute
2753: case 170:
2754: buf.append("\u0113\u0300");
2755: break; // emacron breve
2756: case 186:
2757: buf.append("\u014d\u0300");
2758: break; // omacron breve
2759: case 161:
2760: buf.append("\u0101\u0306");
2761: break; // amacron breve
2762: case 8710:
2763: buf.append("\u0101\u0303");
2764: break; // amacron tilde
2765: case 171:
2766: buf.append("\u012b\u0301");
2767: break; // imacron acute
2768: case 8260:
2769: buf.append("\u1e00");
2770: break; // runderdotmacron acute
2771: case 183:
2772: buf.append("\u1e5b\u0301");
2773: break; // runderdot acute
2774: case 8240:
2775: buf.append("\u012b\u0306");
2776: break; // imacron breve
2777: case 63743:
2778: buf.append("\u016b\u0303");
2779: break; // umacron tilde
2780: default:
2781: buf.append(c);
2782: if ((int) c > 127)
2783: System.out.println("char " + c + " " + (int) c);
2784: break;
2785: }
2786: }
2787: return buf.toString();
2788: }
2789:
2790: static public String normanToUnicodeOld(String inp)
2791: {
2792: StringBuffer buf = new StringBuffer();
2793: for (int i = 0; i < inp.length(); i++)
2794: {
2795: char c = inp.charAt(i);
2796: switch (c)
2797: {
2798: case 1:
2799: buf.append("\u00d0");
2800: break; // Eth
2801: case 2:
2802: buf.append("\u00f0");
2803: break; // eth
2804: case 3:
2805: buf.append("\u0141");
2806: break; // Lslash
2807: case 4:
2808: buf.append("\u0142");
2809: break; // lslash
2810: case 5:
2811: buf.append("\u0160");
2812: break; // S caron
2813: case 6:
2814: buf.append("\u0161");
2815: break; // s caron
2816: case 7:
2817: buf.append("\u00dd");
2818: break; // Y acute
2819: case 8:
2820: buf.append("\u00fd");
2821: break; // y acute
2822: case 11:
2823: buf.append("\u00de");
2824: break; // Thorn
2825: case 12:
2826: buf.append("\u00fe");
2827: break; // thorn
2828: case 14:
2829: buf.append("\u017d");
2830: break; // Z caron
2831: case 15:
2832: buf.append("\u017e");
2833: break; // z caron
2834: case 17:
2835: buf.append("\u0073");
2836: break; // asciitilde
2837: case 18:
2838: buf.append("j\u0305");
2839: break; // j macron [does a single char exist?]
2840: case 19:
2841: buf.append("^");
2842: break; // circumflex
2843: case 20:
2844: buf.append("\u0303");
2845: break; // tilde
2846: case 21:
2847: buf.append("\u00bd");
2848: break; // onehalf
2849: case 22:
2850: buf.append("\u00bc");
2851: break; // onequarter
2852: case 23:
2853: buf.append("\u00b9");
2854: break; // onesuperior
2855: case 24:
2856: buf.append("\u00be");
2857: break; // threequarters
2858: case 25:
2859: buf.append("\u00b3");
2860: break; // threesuperior
2861: case 26:
2862: buf.append("\u00b2");
2863: break; // twosuperior
2864: case 27:
2865: buf.append("\u00a6");
2866: break; // brokenbar
2867: case 28:
2868: buf.append("-");
2869: break; // minus
2870: case 29:
2871: buf.append("\u00d7");
2872: break; // multiply
2873: case 39:
2874: buf.append("'");
2875: break; // quotesingle
2876: case 94:
2877: buf.append("\u0302");
2878: break; // circumflex
2879: case 96:
2880: buf.append("\u0300");
2881: break; // grave
2882: case 128:
2883: buf.append("\u00c4");
2884: break; // A dieresis
2885: case 129:
2886: buf.append("\u00c5");
2887: break; // A ring
2888: case 131:
2889: buf.append("\u00c9");
2890: break; // E acute
2891: case 132:
2892: buf.append("\u00d1");
2893: break; // N tilde
2894: case 133:
2895: buf.append("\u00d6");
2896: break; // O dieresis
2897: case 134:
2898: buf.append("\u00dc");
2899: break; // U dieresis
2900: case 135:
2901: buf.append("\u00e1");
2902: break; // a acute
2903: case 136:
2904: buf.append("\u00e0");
2905: break; // a grave
2906: case 137:
2907: buf.append("\u00e2");
2908: break; // a circumflex
2909: case 138:
2910: buf.append("\u00e4");
2911: break; // a dieresis
2912: case 139:
2913: buf.append("\u00e3");
2914: break; // a tilde
2915: case 140:
2916: buf.append("\u0101");
2917: break; // a macron
2918: case 141:
2919: buf.append("\u00e7");
2920: break; // c cedilla
2921: case 142:
2922: buf.append("\u00e9");
2923: break; // e acute
2924: case 143:
2925: buf.append("\u00e8");
2926: break; // e grave
2927: case 144:
2928: buf.append("\u00ea");
2929: break; // e circumflex
2930: case 145:
2931: buf.append("\u00eb");
2932: break; // e dieresis
2933: case 146:
2934: buf.append("\u00ed");
2935: break; // i acute
2936: case 147:
2937: buf.append("\u00ec");
2938: break; // i grave
2939: case 148:
2940: buf.append("\u00ee");
2941: break; // i circumflex
2942: case 149:
2943: buf.append("\u00ef");
2944: break; // i dieresis
2945: case 150:
2946: buf.append("\u00f1");
2947: break; // n tilde
2948: case 151:
2949: buf.append("\u00f3");
2950: break; // o acute
2951: case 152:
2952: buf.append("\u00f2");
2953: break; // o grave
2954: case 153:
2955: buf.append("\u00f4");
2956: break; // o circumflex
2957: case 154:
2958: buf.append("\u00f6");
2959: break; // o dieresis
2960: case 155:
2961: buf.append("\u00f5");
2962: break; // o tilde
2963: case 156:
2964: buf.append("\u00fa");
2965: break; // u acute
2966: case 157:
2967: buf.append("\u00f9");
2968: break; // u grave
2969: case 158:
2970: buf.append("\u00fb");
2971: break; // u circumflex
2972: case 159:
2973: buf.append("\u00fc");
2974: break; // u dieresis
2975: case 160:
2976: buf.append("\u1e6d");
2977: break; // t underdot
2978: case 161:
2979: buf.append("\u00b0");
2980: break; // degree
2981: case 162:
2982: buf.append("\u1ebd");
2983: break; // e tilde
2984: case 163:
2985: buf.append("\u00a3");
2986: break; // sterling
2987: case 164:
2988: buf.append("\u00a7");
2989: break; // section
2990: case 166:
2991: buf.append("\u00b6");
2992: break; // paragraph
2993: case 167:
2994: buf.append("\u015b");
2995: break; // s acute
2996: case 168:
2997: buf.append("\u1e5b");
2998: break; // r underdot
2999: case 169:
3000: buf.append("\u1e67");
3001: break; // s caron
3002: case 171:
3003: buf.append("\u0301");
3004: break; // acute
3005: case 172:
3006: buf.append("\u0308");
3007: break; // dieresis
3008: case 173:
3009: buf.append("\u1e6d");
3010: break; // t underdot
3011: case 174:
3012: buf.append("\u00c6");
3013: break; // AE
3014: case 175:
3015: buf.append("\u014d");
3016: break; // o macron
3017: case 176:
3018: buf.append("\u0129");
3019: break; // i tilde
3020: case 177:
3021: buf.append("\u00b1");
3022: break; // plusminus
3023: case 180:
3024: buf.append("\u012b");
3025: break; // i macron
3026: case 181:
3027: buf.append("\u1e43");
3028: break; // m underdot
3029: case 182:
3030: buf.append("\u1e0d");
3031: break; // d underdot
3032: case 183:
3033: buf.append("\u1e63");
3034: break; // s underdot
3035: case 185:
3036: buf.append("\u017a");
3037: break; // z acute
3038: case 186:
3039: buf.append("\u1e45");
3040: break; // n overdot
3041: case 189:
3042: buf.append("\u0169");
3043: break; // u tilde
3044: case 190:
3045: buf.append("\u00e6");
3046: break; // ae
3047: case 191:
3048: buf.append("\u00f8");
3049: break; // oslash
3050: case 192:
3051: buf.append("\u0304\u0306");
3052: break; // macron breve
3053: case 194:
3054: buf.append("\u1e37");
3055: break; //
3056: case 195:
3057: buf.append("j\u0305");
3058: break; // j macron [does a single char exist?]
3059: case 196:
3060: buf.append("\u0103");
3061: break; // a breve
3062: case 197:
3063: buf.append("\u016d");
3064: break; // u breve
3065: case 200:
3066: buf.append("\u1e42");
3067: break; // M underdot
3068: case 201:
3069: buf.append("\u2026");
3070: break; // ellipsis
3071: case 203:
3072: buf.append("\u00c0");
3073: break; // A grave
3074: case 204:
3075: buf.append("\u00c3");
3076: break; // A tilde
3077: case 205:
3078: buf.append("\u00d5");
3079: break; // O tilde
3080: case 206:
3081: buf.append("m\u0306");
3082: break; // m breve
3083: case 207:
3084: buf.append("\u0153");
3085: break; // oe
3086: case 208:
3087: buf.append("\u2013");
3088: break; // endash
3089: case 209:
3090: buf.append("\u2014");
3091: break; // emdash
3092: case 210:
3093: buf.append("\u201c");
3094: break; // quotedblleft
3095: case 211:
3096: buf.append("\u201d");
3097: break; // quotedblright
3098: case 212:
3099: buf.append("\u2018");
3100: break; // quoteleft
3101: case 213:
3102: buf.append("\u2019");
3103: break; // quoteright
3104: case 214:
3105: buf.append("\u1e37");
3106: break; // l underring [actually underdot]
3107: case 215:
3108: buf.append("\u1e41");
3109: break; // m overdot
3110: case 216:
3111: buf.append("n\u0306");
3112: break; // n breve
3113: case 217:
3114: buf.append("\u00d7");
3115: break; // multiply
3116: case 219:
3117: buf.append("\u1e5b");
3118: break; // r underring [actually underdot]
3119: case 220:
3120: buf.append("\u1e44");
3121: break; // N overdot
3122: case 221:
3123: buf.append("\u1e62");
3124: break; // S underdot
3125: case 222:
3126: buf.append("\u1e24");
3127: break; // H underdot
3128: case 223:
3129: buf.append("\u1e0c");
3130: break; // D underdot
3131: case 224:
3132: buf.append("\u2021");
3133: break; // daggerdbl
3134: case 226:
3135: buf.append("\u1e36");
3136: break; // L underdot
3137: case 227:
3138: buf.append("\u0113");
3139: break; // e macron
3140: case 229:
3141: buf.append("\u1e5f");
3142: break; // r underbar
3143: case 230:
3144: buf.append("r\u0324");
3145: break; // r underdieresis
3146: case 231:
3147: buf.append("\u012a");
3148: break; // I macron
3149: case 232:
3150: buf.append("\u016b");
3151: break; // u macron
3152: case 233:
3153: buf.append("\u01e6c");
3154: break; // T underdot
3155: case 234:
3156: buf.append("\u1e64");
3157: break; // S acute
3158: case 235:
3159: buf.append("\u2020");
3160: break; // dagger
3161: case 236:
3162: buf.append("\u0115");
3163: break; // e breve
3164: case 237:
3165: buf.append("\u014f");
3166: break; // o breve
3167: case 238:
3168: buf.append("\u0100");
3169: break; // A macron
3170: case 239:
3171: buf.append("\u1e46");
3172: break; // N underdot
3173: case 241:
3174: buf.append("\u1e3b");
3175: break; // l underbar
3176: case 242:
3177: buf.append("\u016a");
3178: break; // U macron
3179: case 243:
3180: buf.append("\u0179");
3181: break; // Z acute
3182: case 244:
3183: buf.append("\u1e5a");
3184: break; // R underdot
3185: case 245:
3186: buf.append("\u0131");
3187: break; // dotlessi
3188: case 246:
3189: buf.append("\u1e47");
3190: break; // n underdot
3191: case 247:
3192: buf.append("\u1e49");
3193: break; // n underbar
3194: case 248:
3195: buf.append("\u0304");
3196: break; // macron
3197: case 249:
3198: buf.append("\u0306");
3199: break; // breve
3200: case 250:
3201: buf.append("\u1e25");
3202: break; // h underdot
3203: case 251:
3204: buf.append("\u012d");
3205: break; // i breve
3206: case 252:
3207: buf.append("\u0327");
3208: break; // cedilla
3209: case 253:
3210: buf.append("\u030b");
3211: break; // hungarumlaut
3212: case 254:
3213: buf.append("\u0328");
3214: break; // ogonek
3215: case 255:
3216: buf.append("\u030c");
3217: break; // caron
3218: case 130:
3219: buf.append("\u012b\u0303");
3220: break; // imacron tilde
3221: case 165:
3222: buf.append("\u1e5d");
3223: break; // runderdot macron
3224: case 170:
3225: buf.append("\u016b\0306");
3226: break; // umacron breve
3227: case 178:
3228: buf.append("\u0101\u0301");
3229: break; // amacron acute
3230: case 179:
3231: buf.append("\u016b\u0301");
3232: break; // umacron acute
3233: case 184:
3234: buf.append("\u0113\u0301");
3235: break; // emacron acute
3236: case 187:
3237: buf.append("\u0113\u0300");
3238: break; // emacron breve
3239: case 188:
3240: buf.append("\u014d\u0300");
3241: break; // omacron breve
3242: case 193:
3243: buf.append("\u0101\u0306");
3244: break; // amacron breve
3245: case 198:
3246: buf.append("\u0101\u0303");
3247: break; // amacron tilde
3248: case 199:
3249: buf.append("\u012b\u0301");
3250: break; // imacron acute
3251: case 218:
3252: buf.append("\u1e00");
3253: break; // runderdotmacron acute
3254: case 225:
3255: buf.append("\u1e5b\u0301");
3256: break; // runderdot acute
3257: case 228:
3258: buf.append("\u012b\u0306");
3259: break; // imacron breve
3260: case 240:
3261: buf.append("\u016b\u0303");
3262: break; // umacron tilde
3263: default:
3264: buf.append(c);
3265: break;
3266: }
3267: }
3268: return buf.toString();
3269: }
3270:
3271: static public String normanToUnicodeNew(String inp)
3272: {
3273: StringBuffer buf = new StringBuffer();
3274: for (int i = 0; i < inp.length(); i++)
3275: {
3276: char c = inp.charAt(i);
3277: switch (c)
3278: {
3279: case 1:
3280: buf.append("\u00d0");
3281: break; // Eth
3282: case 2:
3283: buf.append("\u00f0");
3284: break; // eth
3285: case 3:
3286: buf.append("\u0141");
3287: break; // Lslash
3288: case 4:
3289: buf.append("\u0142");
3290: break; // lslash
3291: case 5:
3292: buf.append("\u0160");
3293: break; // S caron
3294: case 6:
3295: buf.append("\u0161");
3296: break; // s caron
3297: case 7:
3298: buf.append("\u00dd");
3299: break; // Y acute
3300: case 8:
3301: buf.append("\u00fd");
3302: break; // y acute
3303: case 11:
3304: buf.append("\u00de");
3305: break; // Thorn
3306: case 12:
3307: buf.append("\u00fe");
3308: break; // thorn
3309: case 14:
3310: buf.append("\u017d");
3311: break; // Z caron
3312: case 15:
3313: buf.append("\u017e");
3314: break; // z caron
3315: case 17:
3316: buf.append("\u0073");
3317: break; // asciitilde
3318: case 18:
3319: buf.append("j\u0305");
3320: break; // j macron [does a single char exist?]
3321: case 19:
3322: buf.append("^");
3323: break; // circumflex
3324: case 20:
3325: buf.append("\u0303");
3326: break; // tilde
3327: case 21:
3328: buf.append("\u00bd");
3329: break; // onehalf
3330: case 22:
3331: buf.append("\u00bc");
3332: break; // onequarter
3333: case 23:
3334: buf.append("\u00b9");
3335: break; // onesuperior
3336: case 24:
3337: buf.append("\u00be");
3338: break; // threequarters
3339: case 25:
3340: buf.append("\u00b3");
3341: break; // threesuperior
3342: case 26:
3343: buf.append("\u00b2");
3344: break; // twosuperior
3345: case 27:
3346: buf.append("\u00a6");
3347: break; // brokenbar
3348: case 28:
3349: buf.append("-");
3350: break; // minus
3351: case 29:
3352: buf.append("\u00d7");
3353: break; // multiply
3354: case 39:
3355: buf.append("'");
3356: break; // quotesingle
3357: case 94:
3358: buf.append("\u0302");
3359: break; // circumflex
3360: case 96:
3361: buf.append("\u0300");
3362: break; // grave
3363: case 196:
3364: buf.append("\u00c4");
3365: break; // A dieresis
3366: case 197:
3367: buf.append("\u00c5");
3368: break; // A ring
3369: case 201:
3370: buf.append("\u00c9");
3371: break; // E acute
3372: case 209:
3373: buf.append("\u00d1");
3374: break; // N tilde
3375: case 214:
3376: buf.append("\u00d6");
3377: break; // O dieresis
3378: case 220:
3379: buf.append("\u00dc");
3380: break; // U dieresis
3381: case 225:
3382: buf.append("\u00e1");
3383: break; // a acute
3384: case 224:
3385: buf.append("\u00e0");
3386: break; // a grave
3387: case 226:
3388: buf.append("\u00e2");
3389: break; // a circumflex
3390: case 228:
3391: buf.append("\u00e4");
3392: break; // a dieresis
3393: case 227:
3394: buf.append("\u00e3");
3395: break; // a tilde
3396: case 229:
3397: buf.append("\u0101");
3398: break; // a macron
3399: case 231:
3400: buf.append("\u00e7");
3401: break; // c cedilla
3402: case 233:
3403: buf.append("\u00e9");
3404: break; // e acute
3405: case 232:
3406: buf.append("\u00e8");
3407: break; // e grave
3408: case 234:
3409: buf.append("\u00ea");
3410: break; // e circumflex
3411: case 235:
3412: buf.append("\u00eb");
3413: break; // e dieresis
3414: case 237:
3415: buf.append("\u00ed");
3416: break; // i acute
3417: case 236:
3418: buf.append("\u00ec");
3419: break; // i grave
3420: case 238:
3421: buf.append("\u00ee");
3422: break; // i circumflex
3423: case 239:
3424: buf.append("\u00ef");
3425: break; // i dieresis
3426: case 241:
3427: buf.append("\u00f1");
3428: break; // n tilde
3429: case 243:
3430: buf.append("\u00f3");
3431: break; // o acute
3432: case 242:
3433: buf.append("\u00f2");
3434: break; // o grave
3435: case 244:
3436: buf.append("\u00f4");
3437: break; // o circumflex
3438: case 246:
3439: buf.append("\u00f6");
3440: break; // o dieresis
3441: case 245:
3442: buf.append("\u00f5");
3443: break; // o tilde
3444: case 250:
3445: buf.append("\u00fa");
3446: break; // u acute
3447: case 249:
3448: buf.append("\u00f9");
3449: break; // u grave
3450: case 251:
3451: buf.append("\u00fb");
3452: break; // u circumflex
3453: case 252:
3454: buf.append("\u00fc");
3455: break; // u dieresis
3456: case 8224:
3457: buf.append("\u1e6d");
3458: break; // t underdot
3459: case 176:
3460: buf.append("\u00b0");
3461: break; // degree
3462: case 162:
3463: buf.append("\u1ebd");
3464: break; // e tilde
3465: case 163:
3466: buf.append("\u00a3");
3467: break; // sterling
3468: case 167:
3469: buf.append("\u00a7");
3470: break; // section
3471: case 182:
3472: buf.append("\u00b6");
3473: break; // paragraph
3474: case 223:
3475: buf.append("\u015b");
3476: break; // s acute
3477: case 174:
3478: buf.append("\u1e5b");
3479: break; // r underdot
3480: case 169:
3481: buf.append("\u1e45");
3482: break; // n overdot
3483: case 180:
3484: buf.append("\u0301");
3485: break; // acute
3486: case 168:
3487: buf.append("\u0308");
3488: break; // dieresis
3489: case 8800:
3490: buf.append("\u1e6d");
3491: break; // t underdot
3492: case 198:
3493: buf.append("\u00c6");
3494: break; // AE
3495: case 216:
3496: buf.append("\u014d");
3497: break; // o macron
3498: case 8734:
3499: buf.append("\u0129");
3500: break; // i tilde
3501: case 177:
3502: buf.append("\u00b1");
3503: break; // plusminus
3504: case 165:
3505: buf.append("\u012b");
3506: break; // i macron
3507: case 181:
3508: buf.append("\u1e43");
3509: break; // m underdot
3510: case 8706:
3511: buf.append("\u1e0d");
3512: break; // d underdot
3513: case 8721:
3514: buf.append("\u1e63");
3515: break; // s underdot
3516: case 960:
3517: buf.append("\u017a");
3518: break; // z acute
3519: case 8747:
3520: buf.append("\u1e45");
3521: break; // n overdot
3522: case 937:
3523: buf.append("\u0169");
3524: break; // u tilde
3525: case 230:
3526: buf.append("\u00e6");
3527: break; // ae
3528: case 248:
3529: buf.append("\u00f8");
3530: break; // oslash
3531: case 191:
3532: buf.append("\u0304\u0306");
3533: break; // macron breve
3534: case 172:
3535: buf.append("\u1e37");
3536: break; //
3537: case 8730:
3538: buf.append("j\u0305");
3539: break; // j macron [does a single char exist?]
3540: case 402:
3541: buf.append("\u0103");
3542: break; // a breve
3543: case 8776:
3544: buf.append("\u016d");
3545: break; // u breve
3546: case 187:
3547: buf.append("\u1e42");
3548: break; // M underdot
3549: case 8230:
3550: buf.append("\u2026");
3551: break; // ellipsis
3552: case 192:
3553: buf.append("\u00c0");
3554: break; // A grave
3555: case 195:
3556: buf.append("\u00c3");
3557: break; // A tilde
3558: case 213:
3559: buf.append("\u00d5");
3560: break; // O tilde
3561: case 338:
3562: buf.append("m\u0306");
3563: break; // m breve
3564: case 339:
3565: buf.append("\u0153");
3566: break; // oe
3567: case 8211:
3568: buf.append("\u2013");
3569: break; // endash
3570: case 8212:
3571: buf.append("\u2014");
3572: break; // emdash
3573: case 8220:
3574: buf.append("\u201c");
3575: break; // quotedblleft
3576: case 8221:
3577: buf.append("\u201d");
3578: break; // quotedblright
3579: case 8216:
3580: buf.append("\u2018");
3581: break; // quoteleft
3582: case 8217:
3583: buf.append("\u2019");
3584: break; // quoteright
3585: case 247:
3586: buf.append("\u1e37");
3587: break; // l underring [actually underdot]
3588: case 9674:
3589: buf.append("\u1e41");
3590: break; // m overdot
3591: case 255:
3592: buf.append("n\u0306");
3593: break; // n breve
3594: case 376:
3595: buf.append("\u00d7");
3596: break; // multiply
3597: case 8364:
3598: buf.append("\u1e5b");
3599: break; // r underring [actually underdot]
3600: case 8249:
3601: buf.append("\u1e44");
3602: break; // N overdot
3603: case 8250:
3604: buf.append("\u1e62");
3605: break; // S underdot
3606: case 64257:
3607: buf.append("\u1e24");
3608: break; // H underdot
3609: case 64258:
3610: buf.append("\u1e0c");
3611: break; // D underdot
3612: case 8225:
3613: buf.append("\u2021");
3614: break; // daggerdbl
3615: case 8218:
3616: buf.append("\u1e36");
3617: break; // L underdot
3618: case 8222:
3619: buf.append("\u0113");
3620: break; // e macron
3621: case 194:
3622: buf.append("\u1e5f");
3623: break; // r underbar
3624: case 202:
3625: buf.append("r\u0324");
3626: break; // r underdieresis
3627: case 193:
3628: buf.append("\u012a");
3629: break; // I macron
3630: case 203:
3631: buf.append("\u016b");
3632: break; // u macron
3633: case 200:
3634: buf.append("\u1e6c");
3635: break; // T underdot
3636: case 205:
3637: buf.append("\u1e64");
3638: break; // S acute
3639: case 206:
3640: buf.append("\u2020");
3641: break; // dagger
3642: case 207:
3643: buf.append("\u0115");
3644: break; // e breve
3645: case 204:
3646: buf.append("\u014f");
3647: break; // o breve
3648: case 211:
3649: buf.append("\u0100");
3650: break; // A macron
3651: case 212:
3652: buf.append("\u1e46");
3653: break; // N underdot
3654: case 210:
3655: buf.append("\u1e3b");
3656: break; // l underbar
3657: case 218:
3658: buf.append("\u016a");
3659: break; // U macron
3660: case 219:
3661: buf.append("\u0179");
3662: break; // Z acute
3663: case 217:
3664: buf.append("\u1e5a");
3665: break; // R underdot
3666: case 305:
3667: buf.append("\u0131");
3668: break; // dotlessi
3669: case 710:
3670: buf.append("\u1e47");
3671: break; // n underdot
3672: case 732:
3673: buf.append("\u1e49");
3674: break; // n underbar
3675: case 175:
3676: buf.append("\u0304");
3677: break; // macron
3678: case 728:
3679: buf.append("\u0306");
3680: break; // breve
3681: case 729:
3682: buf.append("\u1e25");
3683: break; // h underdot
3684: case 730:
3685: buf.append("\u012d");
3686: break; // i breve
3687: case 184:
3688: buf.append("\u0327");
3689: break; // cedilla
3690: case 733:
3691: buf.append("\u030b");
3692: break; // hungarumlaut
3693: case 731:
3694: buf.append("\u0328");
3695: break; // ogonek
3696: case 711:
3697: buf.append("\u030c");
3698: break; // caron
3699: case 199:
3700: buf.append("\u012b\u0303");
3701: break; // imacron tilde
3702: case 8226:
3703: buf.append("\u1e5d");
3704: break; // runderdot macron
3705: case 8482:
3706: buf.append("\u016b\0306");
3707: break; // umacron breve
3708: case 8804:
3709: buf.append("\u0101\u0301");
3710: break; // amacron acute
3711: case 8805:
3712: buf.append("\u016b\u0301");
3713: break; // umacron acute
3714: case 8719:
3715: buf.append("\u0113\u0301");
3716: break; // emacron acute
3717: case 170:
3718: buf.append("\u0113\u0300");
3719: break; // emacron breve
3720: case 186:
3721: buf.append("\u014d\u0300");
3722: break; // omacron breve
3723: case 161:
3724: buf.append("\u0101\u0306");
3725: break; // amacron breve
3726: case 8710:
3727: buf.append("\u0101\u0303");
3728: break; // amacron tilde
3729: case 171:
3730: buf.append("\u012b\u0301");
3731: break; // imacron acute
3732: case 8260:
3733: buf.append("\u1e00");
3734: break; // runderdotmacron acute
3735: case 183:
3736: buf.append("\u1e5b\u0301");
3737: break; // runderdot acute
3738: case 8240:
3739: buf.append("\u012b\u0306");
3740: break; // imacron breve
3741: case 63743:
3742: buf.append("\u016b\u0303");
3743: break; // umacron tilde
3744: default:
3745: buf.append(c);
3746: break;
3747: }
3748: }
3749: return buf.toString();
3750: }
3751:
3752: public static ConversionProperties getFieldNamesAndDestTableName(String create, String query,
3753: String tableName)
3754: {
3755: String[] fieldNames = null;
3756: String destTableName = null;
3757: // determine destTableName from createStatement or from source table
3758: // name
3759: if (!create.equals(""))
3760: {
3761: int fromIndex = create.toLowerCase().indexOf("table") + 5;
3762: int toIndex = create.indexOf("(");
3763: int endIndex = create.indexOf(")", toIndex);
3764:
3765: destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "").trim();
3766: System.out.println("destTable " + destTableName);
3767: // retrieve field_names from select statement
3768: // TODO problem with different fieldNames in create statement will
3769: // overwrite them
1.7 rogo 3770: if (query.indexOf("*") < 0 && create.equals(""))// quick hack for hartmut
1.4 rogo 3771: {
3772: int selectEndIndex = query.indexOf("from");
3773: StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");
3774: int numFields = tokenizer.countTokens();
3775: fieldNames = new String[numFields];
3776: int fieldIndex = 0;
3777: while (tokenizer.hasMoreTokens())
3778: {
3779: String fieldName = tokenizer.nextToken().trim();
3780: fieldNames[fieldIndex] = convertText(fieldName);
3781: System.out.println(fieldNames[fieldIndex]);
3782: fieldIndex++;
3783: }
3784:
3785: } else
3786: {
3787: // use create statement for field names
3788: StringTokenizer tokenizer = new StringTokenizer(create.substring(toIndex + 1, endIndex),
3789: ",");
3790: int numFields = tokenizer.countTokens();
3791: fieldNames = new String[numFields];
3792: int fieldIndex = 0;
3793: while (tokenizer.hasMoreTokens())
3794: {
3795: String fieldName = tokenizer.nextToken().trim();
3796: int index = fieldName.lastIndexOf(" ");
3797: fieldNames[fieldIndex] = fieldName.substring(0, index);
3798: System.out.println(fieldNames[fieldIndex]);
3799: fieldIndex++;
3800: }
3801: }
3802: } else
3803: {
3804: destTableName = convertText(tableName);
3805:
3806: // retrieve field_names from select statement
3807: if (query.indexOf("*") < 0)
3808: {
1.10 rogo 3809: int selectEndIndex = query.lastIndexOf("from");
1.4 rogo 3810: StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");
3811: int numFields = tokenizer.countTokens();
3812: fieldNames = new String[numFields];
3813: int fieldIndex = 0;
3814: while (tokenizer.hasMoreTokens())
3815: {
3816: String fieldName = tokenizer.nextToken().trim();
1.10 rogo 3817: String text = convertText(fieldName);
3818: if (text.indexOf("\"") >= 0)
3819: fieldNames[fieldIndex] = convertText(fieldName);
3820: else
3821: fieldNames[fieldIndex] = beanDest.getQC() + convertText(fieldName) + beanDest.getQC();
1.4 rogo 3822: // System.out.println("field "+ fieldNames[fieldIndex]);
3823: fieldIndex++;
3824: }
3825:
3826: } else
3827: {
3828: Vector fieldNamesVec = bean.getColumnNames();
3829: fieldNames = new String[fieldNamesVec.size()];
3830: int fieldIndex = -1;
3831: for (Iterator iter = fieldNamesVec.iterator(); iter.hasNext();)
3832: {
3833: String element = (String) iter.next();
3834: fieldNames[++fieldIndex] = beanDest.getQC() + convertText(element) + beanDest.getQC();
3835: // System.out.println("field " + fieldNames[fieldIndex]);
3836: }
3837: }
3838: }
3839: return new ConversionProperties(destTableName, fieldNames);
3840: }
3841:
3842: /**
3843: * creates an insert into statement for the specified table and given field
3844: * names
3845: *
3846: * @param destTableName
3847: * @param fieldNames
3848: * @return
3849: */
3850: public static StringBuffer createInsertCommand(String destTableName, String[] fieldNames)
3851: {
3852: StringBuffer command = new StringBuffer();
3853: command.append("INSERT INTO ");
3854: command.append(beanDest.getQC());
3855: command.append(destTableName); // convertText((String)
3856: // names.get(tbIndex)));
3857: command.append(beanDest.getQC());
3858: command.append(" (");
3859: for (int i = 0; i < fieldNames.length; i++)
3860: {
3861: command.append(fieldNames[i]);
3862: if (i < fieldNames.length - 1)
3863: command.append(",");
3864: }
3865: command.append(") ");
3866:
3867: command.append(" values ( ");
3868: // add a question marks for every field
3869: for (int i = 0; i < fieldNames.length - 1; ++i)
3870: command.append("?,");
3871: command.append("?)");
3872: return command;
3873: }
3874:
3875: public static StringBuffer createUpdateCommand(String destTableName, String[] fieldNames,
3876: String id)
3877: {
3878: StringBuffer command = new StringBuffer();
3879:
3880: command.append("UPDATE ");
3881: command.append(beanDest.getQC());
3882: command.append(destTableName);
3883: command.append(beanDest.getQC());
3884: command.append(" SET ");
3885:
3886: int size = bean.getColumnNames().size();
3887: for (int i = 0; i < size - 1; ++i)
3888: command.append(fieldNames[i] + " = ? ,");
3889: command.append(fieldNames[size - 1] + " = ? ");
3890: command.append("WHERE " + id + " = ?");
3891: return command;
3892: }
3893:
3894: public static StringBuffer createDeleteCommand(String destTableName, String idField)
3895: {
3896: StringBuffer command = new StringBuffer();
3897:
3898: command.append("DELETE FROM");
3899: command.append(beanDest.getQC());
3900: command.append(destTableName);
3901: // command.append(convertText((String) names.get(tbIndex)));
3902: command.append(beanDest.getQC());
3903: command.append("WHERE " + idField + " = ?");
3904: return command;
3905: }
3906:
3907: public void makeTest(String table, String idField, String tempQuery) throws Exception
3908: {
3909: int counter = 0;
3910:
3911: // ****** test code *****
3912:
3913: bean.getConnection();
3914: ResultSet resultSet = null;
3915: String lastResult = "P227634.11";// "P227625.79554";//"P227625.77391";//"P116034.970998";
3916: String myQuery = "select " + bean.getQC() + idField + bean.getQC() + ",serial " + " from "
3917: + bean.getQC() + table + bean.getQC();
3918: System.out.println("Query is now " + myQuery);
3919: JDialog statusDialog = new JDialog();
3920: statusDialog.setTitle("Status Information");
3921: JLabel status = new JLabel("actual DataSet : ");
3922: JLabel status2 = new JLabel(Integer.toString(++counter));
3923: JLabel status3 = new JLabel(lastResult);
3924:
3925: JPanel statusPanel = new JPanel();
3926: JPanel statusPanel2 = new JPanel();
3927: statusPanel.add(status);
3928: statusPanel.add(status2);
3929: statusPanel2.add(status3);
3930: statusDialog.getContentPane().add(statusPanel, "North");
3931: statusDialog.getContentPane().add(statusPanel2, "Center");
3932: statusDialog.setLocation(400, 500);
3933: statusDialog.setSize(300, 150);
3934: statusDialog.setVisible(true);
3935: while (true)
3936: {
3937: if (!statusDialog.isVisible())
3938: statusDialog.setVisible(true);
3939: tempQuery = myQuery + " where " + bean.getQC() + idField + bean.getQC() + ">'" + lastResult
3940: + "'";
3941: resultSet = bean.makeQuery(tempQuery, 1);
3942: if (resultSet == null)
3943: {
3944: System.out.println("lastResult was " + lastResult + " counter was " + counter);
3945: break;
3946: } else
3947: {
3948: resultSet.next();
3949: lastResult = resultSet.getString(1);
3950: counter++;
3951: status2.setText(Integer.toString(counter));
3952: status3.setText(lastResult + " " + resultSet.getString(2));
3953: if (counter % 100 == 0)
3954: {
3955: System.out.println("actual Result was " + lastResult + " counter was " + counter);
3956: // break;
3957: }
3958: }
3959: resultSet = null;
3960: }
3961: System.exit(0);
1.1 rogo 3962:
1.4 rogo 3963: // ****** end Test ******
1.1 rogo 3964:
1.4 rogo 3965: }
1.8 rogo 3966:
3967: public final static String generateSuffix(final int step)
3968: {
3969: String fileString = null;
3970: if (step < 10)
3971: fileString = "00" + step;
3972: else if (step < 100)
3973: fileString = "0" + step;
3974: else
3975: fileString = step + "";
3976: return fileString;
3977: }
3978:
1.1 rogo 3979: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>