DBImportFileCoder.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  * 
00024  */
00025 
00026 package org.openmobileis.synchro.openmsp.client.db;
00027 
00028 import java.io.*;
00029 
00030 import org.openmobileis.common.util.PropertiesManager;
00031 import org.openmobileis.common.util.codec.GeneralCoder;
00032 import org.openmobileis.common.util.collection.Array;
00033 import org.openmobileis.synchro.openmsp.OpenMSPException;
00034 
00042 public final class DBImportFileCoder {
00043   public static final char ADDAction ='A';
00044   public static final char REPLACEAction= 'R';
00045   public static final char DELETEAction= 'D';
00046   public static final char endLine = '\n';
00047   public static final char dataSeparator = ';';
00048   public static final char stringSeparator = '\'';
00049         public static final char antislashSeparator = '\\';
00050         public char guillemetannulator = '\\';
00051         private static DBImportFileCoder coder;
00052         
00053         private DBImportFileCoder()     {
00054                 String newGuillemetAnnulator = PropertiesManager.getManager().getProperty("server.common.util.string.format.sql.guillement");
00055                 if (newGuillemetAnnulator != null)      {
00056                         guillemetannulator = newGuillemetAnnulator.charAt(0);
00057                 }
00058         }
00059         
00060         public static DBImportFileCoder getCoder()      {
00061                 if (coder==null)        {
00062                         synchronized(DBImportFileCoder.class)   {
00063                                 if (coder == null)      {
00064                                         coder = new DBImportFileCoder();
00065                                 }
00066                         }
00067                 }
00068                 return coder;
00069         }
00070 
00075   public char[] encodeData(char action, String[] rowData) throws OpenMSPException {
00076     CharArrayWriter writer = new CharArrayWriter();
00077     try {
00078       writer.write(action);
00079       writer.write(dataSeparator);
00080       for (int i=0; i<rowData.length; i++) {
00081         if (rowData[i] == null) {
00082           writer.write("NULL");
00083         } else  {
00084           writer.write(rowData[i]);
00085         }
00086         if (i!=rowData.length -1)  {
00087           writer.write(dataSeparator);
00088         }
00089       }
00090       writer.write(endLine);
00091     } catch (Exception ex)  {
00092       throw new OpenMSPException("DBImportFileCoder encodeData Error ", ex);
00093     } finally {
00094       writer.flush();
00095       writer.close();
00096     }
00097     return writer.toCharArray();
00098   }
00099 
00100   public String formateString(String data) {
00101     StringBuffer str = new StringBuffer(stringSeparator);
00102     str.append(data).append(stringSeparator);
00103     return str.toString();
00104   }
00105 
00106   public String removeAntiSlash(String input)  {
00107     char[] chs = input.toCharArray();
00108     int size = chs.length;
00109     CharArrayWriter out = new CharArrayWriter(30);
00110     for (int i=0; i < size; i++)  {
00111       if (chs[i] == antislashSeparator) {
00112         continue;
00113       }
00114       out.write(chs[i]);
00115     }
00116     out.flush();
00117     out.close();
00118     return out.toString();
00119   }
00120 
00121 
00125   public void decodeFileData(String filename, DecodedRowListener listener) throws OpenMSPException {
00126     try {
00127       BufferedReader reader = new BufferedReader(new FileReader(filename));
00128       try {
00129         char read = 0;
00130         int input = 0;
00131         boolean insideString = false;
00132         StringBuffer str = new StringBuffer();
00133         Array dataList = new Array(10);
00134         boolean seeStringSeparator = false;
00135         boolean seeAntiSeparator = false;
00136         int line =0;
00137         char action =0;
00138         boolean rowBeginned = false;
00139         while ((input = reader.read()) != -1) {
00140           read=(char) input;
00141           if (!insideString)  {
00142             if (read == dataSeparator)  {
00143               if (rowBeginned)  {
00144                 String toAdd = str.toString();
00145                 if (toAdd.equals("NULL"))  {
00146                   dataList.add(null);
00147                 } else  {
00148                   dataList.add(toAdd);
00149                 }
00150                 str.setLength(0);
00151               } else  {
00152                 action = str.charAt(0);
00153                 rowBeginned = true;
00154                 str.setLength(0);
00155               }
00156               continue;
00157             } else if (read == stringSeparator)  {
00158               insideString = true;
00159               seeStringSeparator = false;
00160               seeAntiSeparator = false;
00161               continue;
00162             } else if (read == endLine) {
00163               line++;
00164               String toAdd = str.toString();
00165               if (toAdd.equals("NULL"))  {
00166                 dataList.add(null);
00167               } else  {
00168                 dataList.add(toAdd);
00169               }
00170               str.setLength(0);
00171               // notify row.
00172               String[] rowData = new String[dataList.size()];
00173               dataList.toArray(rowData);
00174               listener.notifyNewRowDecoded(action, rowData);
00175               dataList.clear();
00176               rowBeginned = false;
00177               continue;
00178             }
00179           } else  {
00180             if (read == antislashSeparator)  {
00181                 seeAntiSeparator = true;
00182             } else if (read == stringSeparator)  {
00183               if (seeAntiSeparator) {
00184                 seeAntiSeparator = false;
00185               } else  {
00186                 seeStringSeparator = true;
00187                 continue;
00188               }
00189             } else if (seeStringSeparator) {
00190               insideString = false;
00191               if (read == dataSeparator)  {
00192                 dataList.add(str.toString());
00193                 str.setLength(0);
00194               } else if (read == endLine) {
00195                 line++;
00196                 dataList.add(str.toString());
00197                 str.setLength(0);
00198                 // notify row.
00199                 String[] rowData = new String[dataList.size()];
00200                 dataList.toArray(rowData);
00201                 listener.notifyNewRowDecoded(action, rowData);
00202                 dataList.clear();
00203                 rowBeginned = false;
00204                 continue;
00205               } else  {
00206                 listener.notifyError(line, read);
00207               }
00208               continue;
00209             } else  if (seeAntiSeparator){
00210               seeAntiSeparator = false;
00211             }
00212           }
00213           str.append(read);
00214         }
00215       } finally {
00216         reader.close();
00217       }
00218     } catch (Exception ex) {
00219       throw new OpenMSPException("DBImportFileCoder decodeFileData Error ", ex);
00220     }
00221   }
00222 
00223   public String cleanAndFormatStringforDB(String toFormat)  {
00224     if (toFormat == null)
00225       return "NULL";
00226     return new String(cleanAndFormatStringforDB(toFormat.toCharArray()));
00227   }
00228 
00229   private char[] cleanAndFormatStringforDB(char[] toFormat)  {
00230         
00231     CharArrayWriter writer  = new CharArrayWriter();
00232     for (int i=0; i<toFormat.length; i++) {
00233                         if (toFormat[i] == '\'')  {
00234                                 writer.write(guillemetannulator);
00235                         } else if (toFormat[i] == antislashSeparator)  {
00236                                 writer.write(antislashSeparator);
00237                         } else if (toFormat[i] == 0)  {
00238                                 writer.write(' ');
00239                                 continue; //skip caractere 00
00240       }
00241       writer.write(toFormat[i]);
00242     }
00243     writer.flush();
00244     writer.close();
00245     return writer.toCharArray();
00246   }
00247   
00248   public String decodeString(String inputString) {
00249     if (inputString == null) {
00250       return ("NULL");
00251     }
00252     return DBImportFileCoder.getCoder().removeAntiSlash(inputString);
00253   }
00254 
00255   public String encodeInString(String inputString)  {
00256     if (inputString == null) {
00257       return ("NULL");
00258     }
00259     char[] toEncode = cleanAndFormatStringforDB(inputString.toCharArray());
00260     char[] encoded = new char[toEncode.length+2];
00261     encoded[0] = '\'';
00262     encoded[toEncode.length+1] = '\'';
00263     System.arraycopy(toEncode, 0, encoded, 1, toEncode.length);
00264     return new String(encoded);
00265   }
00266 
00272   public String[] decodeData(String data)  {
00273     char read = 0;
00274     boolean insideString = false;
00275     StringBuffer str = new StringBuffer();
00276     Array dataList = new Array(10);
00277     boolean seeStringSeparator = false;
00278     boolean seeAntiSeparator = false;
00279     for (int i=2; i<data.length(); i++) { // begin after the action
00280       read=data.charAt(i);
00281       if (!insideString)  {
00282         if (read == dataSeparator)  {
00283           String toAdd = str.toString();
00284           if (toAdd.equals("NULL"))  {
00285             dataList.add(null);
00286           } else  {
00287             dataList.add(toAdd);
00288           }
00289           str.setLength(0);
00290           continue;
00291         } else if (read == stringSeparator)  {
00292           insideString = true;
00293           seeStringSeparator = false;
00294           seeAntiSeparator = false;
00295           continue;
00296         } else if (read == endLine) {
00297           break;
00298         }
00299       } else  {
00300         if (read == antislashSeparator)  {
00301           seeAntiSeparator = true;
00302         } else if (read == stringSeparator)  {
00303           if (seeAntiSeparator) {
00304             seeAntiSeparator = false;
00305           } else  {
00306             seeStringSeparator = true;
00307             continue;
00308           }
00309         } else if (seeStringSeparator) {
00310           insideString = false;
00311           if (read == dataSeparator)  {
00312             dataList.add(str.toString());
00313             str.setLength(0);
00314           } else if (read == endLine) {
00315             break;
00316           } else  {
00317             return null;
00318           }
00319           continue;
00320         } else  if (seeAntiSeparator){
00321           seeAntiSeparator = false;
00322         }
00323       }
00324       str.append(read);
00325     }
00326     
00327 
00328     // add the current buffer
00329     String toAdd = str.toString();
00330     if (toAdd.equals("NULL"))  {
00331       dataList.add(null);
00332     } else  {
00333       dataList.add(toAdd);
00334     }
00335     String[] rowData = new String[dataList.size()];
00336     dataList.toArray(rowData);
00337     return rowData;
00338   }
00339   
00340   public String convertBooleanToDBData(boolean bool)    {
00341         if (bool) return "1";
00342         return "0";
00343   }
00344   
00345   public boolean convertDBDataToBoolean(byte data)      {
00346         if (data == 0) return false;
00347         return true;
00348   }
00349 
00350   public String[] serializeDBObject (String uid, Object obj) throws IOException {
00351     String[] data = new String[2];
00352     data[0] = uid;
00353     ByteArrayOutputStream bstr = new ByteArrayOutputStream();
00354     ObjectOutputStream ostr = new ObjectOutputStream(bstr);
00355     ostr.writeObject(obj);
00356     byte[] b = bstr.toByteArray();
00357     data[1] = new String(GeneralCoder.encodeBase64(b));
00358     return data;
00359   }
00360   
00361   public Object unserializeDBObject(String[] synchroData) throws IOException, ClassNotFoundException {
00362       ByteArrayInputStream bstr = new ByteArrayInputStream(GeneralCoder.decodeBase64(synchroData[1].getBytes()));
00363       ObjectInputStream ostr = new ObjectInputStream(bstr);
00364       return ostr.readObject();
00365   }
00366 
00367   public static void main(String[] args) {
00368     try {
00369             DecodedRowListener listener = new DecodedRowListener()  {
00370               public void notifyNewRowDecoded(char action, String[] rowData) throws OpenMSPException {
00371                 String str = new String(DBImportFileCoder.getCoder().encodeData(action, rowData));
00372                 System.out.println("Decoded row :"+str);
00373               }
00374         
00375               public void notifyError(int ligne, char errorCharacter) {
00376                 System.out.println("Error found line "+ligne+" char "+errorCharacter);
00377               }
00378             };
00379         
00380             String[] addData = {"1234", "'first String'", "'test inside \\'\\'inside\\'\\' fin'", "345"};
00381             String[] deleteData = {"'second  String line end \\n suite \\n'", "23", "'test inside \\'\\'inside\\'\\''"};
00382             String[] updateData = {"'thrid  separator ;;\\'\\';\\'\\''","'String'","''",null, "345"};
00383         
00384             System.out.println("try file encoding");
00385             try {
00386               FileWriter writer = new FileWriter("/home/pdelrieu/dev/temp/testimportfile.txt");
00387               char[] endoded = DBImportFileCoder.getCoder().encodeData(DBImportFileCoder.ADDAction, addData);
00388               System.out.println("Endoded :"+new String(endoded));
00389               writer.write(endoded);
00390         
00391               endoded = DBImportFileCoder.getCoder().encodeData(DBImportFileCoder.DELETEAction, deleteData);
00392               System.out.println("Endoded :"+new String(endoded));
00393               writer.write(endoded);
00394         
00395               endoded = DBImportFileCoder.getCoder().encodeData(DBImportFileCoder.REPLACEAction, updateData);
00396               System.out.println("Endoded :"+new String(endoded));
00397               writer.write(endoded);
00398         
00399               writer.flush();
00400               writer.close();
00401         
00402               DBImportFileCoder.getCoder().decodeFileData("/home/pdelrieu/dev/temp/testimportfile.txt", listener);
00403             } catch (Exception ex)  {
00404               ex.printStackTrace();
00405             }
00406         
00407             String encoded = new String(DBImportFileCoder.getCoder().encodeData(DBImportFileCoder.ADDAction, addData));
00408             String[] data = DBImportFileCoder.getCoder().decodeData(encoded);
00409             StringBuffer buff = new StringBuffer("Decoded Data :");
00410             for (int i=0; i<data.length; i++) {
00411               buff.append('#').append(data[i]);
00412             }
00413             System.out.println(buff.toString());
00414         
00415             encoded = new String(DBImportFileCoder.getCoder().encodeData(DBImportFileCoder.DELETEAction, deleteData));
00416             data = DBImportFileCoder.getCoder().decodeData(encoded);
00417             buff = new StringBuffer("Decoded Data :");
00418             for (int i=0; i<data.length; i++) {
00419               buff.append('#').append(data[i]);
00420             }
00421             System.out.println(buff.toString());
00422         
00423         
00424             encoded = new String(DBImportFileCoder.getCoder().encodeData(DBImportFileCoder.REPLACEAction, updateData));
00425             data = DBImportFileCoder.getCoder().decodeData(encoded);
00426             buff = new StringBuffer("Decoded Data :");
00427             for (int i=0; i<data.length; i++) {
00428               buff.append('#').append(data[i]);
00429             }
00430             System.out.println(buff.toString());
00431     } catch (Exception ex)      {
00432       ex.printStackTrace();
00433     }
00434 
00435 
00436   }
00437 }

Generated on Mon Dec 4 11:03:25 2006 for OpenMobileIS by  doxygen 1.5.1-p1