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