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