Author: jerome
Date: 2008-12-21 17:32:09 +0100 (Sun, 21 Dec 2008)
New Revision: 3278

Modified:
   
software_suite_v2/software/control_center/trunk/control_center/sources/com/tuxdroid/cc/CcCommons.java
   
software_suite_v2/software/control_center/trunk/control_center/sources/com/tuxdroid/cc/settings/Settings.java
   
software_suite_v2/software/control_center/trunk/control_center/sources/com/tuxdroid/cc/settings/XMLWriter.java
Log:
* Creating settings.xml file as utf-8 by default, but if settings exists and 
his encoding is 'latin' then continue writing in latin to the next settings 
removal.

Modified: 
software_suite_v2/software/control_center/trunk/control_center/sources/com/tuxdroid/cc/CcCommons.java
===================================================================
--- 
software_suite_v2/software/control_center/trunk/control_center/sources/com/tuxdroid/cc/CcCommons.java
       2008-12-21 15:05:32 UTC (rev 3277)
+++ 
software_suite_v2/software/control_center/trunk/control_center/sources/com/tuxdroid/cc/CcCommons.java
       2008-12-21 16:32:09 UTC (rev 3278)
@@ -50,6 +50,8 @@
        
        public static boolean isFrameworkBusy = false;
        
+       public static String settingsEncoding = "utf-8";
+       
        public static Languages language;
        public static Settings settings;
        public static CCRemoteActions remoteControl;

Modified: 
software_suite_v2/software/control_center/trunk/control_center/sources/com/tuxdroid/cc/settings/Settings.java
===================================================================
--- 
software_suite_v2/software/control_center/trunk/control_center/sources/com/tuxdroid/cc/settings/Settings.java
       2008-12-21 15:05:32 UTC (rev 3277)
+++ 
software_suite_v2/software/control_center/trunk/control_center/sources/com/tuxdroid/cc/settings/Settings.java
       2008-12-21 16:32:09 UTC (rev 3278)
@@ -179,7 +179,7 @@
         */
        private void verifyEncoding()
        {
-               CcCommons.logger.append("Checking settings.xml encoding.", 
true);
+               CcCommons.logger.append("Detecting settings.xml encoding.", 
true);
                if(!CcCommons.TuxDroidSettingsFile.exists())
                {
                        return;
@@ -198,140 +198,21 @@
                        encoding = encoding.substring(encoding.indexOf("=") + 
1);
                        encoding = encoding.substring(0, encoding.indexOf(" 
")).replace("\"", "");
                        
-                       if(!encoding.equalsIgnoreCase("utf-8"))
+                       if(encoding.equalsIgnoreCase("UTF-8"))
                        {
-                               this.encodeUTF8(encoding);
+                               CcCommons.settingsEncoding = "UTF-8";
+                               CcCommons.logger.append("Encoding is: UTF-8", 
true);
                        }
                        
+                       else
+                       {
+                               CcCommons.settingsEncoding = "ISO-8859-1";
+                               CcCommons.logger.append("Encoding is: 
ISO-8859-1", true);
+                       }
+                       
                        br.close(); 
                }               
                catch (Exception e){}
-       }
-       
-       
-       /**
-        * Encode xml file into utf-8 format.
-        * @throws IOException 
-        */
-       private void encodeUTF8(String oldEncoding) throws IOException
-       {
-               CcCommons.logger.append("Re-encoding of settings.xml file in 
progress.", true);
-               
-               BufferedReader br = null;
-           PrintWriter pw = null;
-           File dest = null;
-           FileOutputStream ops = null;
-           
-           try
-           {
-               //Reading stream.
-               br = new BufferedReader(new InputStreamReader(new 
FileInputStream(CcCommons.TuxDroidSettingsFile), oldEncoding));
-               //writing stream.
-               dest = new 
File(CcCommons.TuxDroidSettingsDirectory.getAbsolutePath() + File.separator + 
"settings.xml.enc");
-               ops = new FileOutputStream(dest);
-               pw = new PrintWriter(ops);
-               
-               String ligne;
-               boolean first = true;
-               while((ligne = br.readLine())!=null){
-                  if(!first)
-                  {
-                          pw.println(new String(ligne.getBytes(), "utf-8"));
-                  }
-                  else
-                  {
-                          first = false;
-                          pw.println(new String("<?xml version=\"1.0\" 
encoding=\"UTF-8\" standalone=\"no\"?>".getBytes(), "utf-8"));
-                  }
-               }
-           }
-           catch(OutOfMemoryError tooBad)
-           {
-               
-               if(CcCommons.language == null)
-               {
-                       CcCommons.language = new Languages(Locale.getDefault());
-               }
-               
-               String message = 
String.format(CcCommons.language.messages.getCorruptedSettingsMessage(),
-                                                                          
CcCommons.TuxDroidSettingsFile.getAbsolutePath()
-                                                                         );
-               JOptionPane.showMessageDialog( null, message,
-                                                                          
CcCommons.language.getTitle(), 
-                                                                          
JOptionPane.ERROR_MESSAGE
-                                                                         );
-               pw.close();
-               br.close();
-               ops.close();
-               dest.delete();
-               CcCommons.logger.append("Settings file error: deleting faild", 
true);
-               CcCommons.logger.appendError(tooBad.getStackTrace());
-               
-               System.exit(0);
-               return;
-           }
-           
-           finally
-           {
-                try 
-                { 
-                        pw.close(); 
-                        dest.delete();
-                } 
-                catch( Throwable e ) 
-                {
-                        dest.deleteOnExit();
-                }
-                try 
-                { 
-                        br.close(); 
-                        dest.delete();
-                } 
-                catch( Throwable e ) 
-                {
-                        dest.deleteOnExit();
-                } 
-           }
-           
-           if((CcCommons.TuxDroidSettingsFile != null) && 
(CcCommons.TuxDroidSettingsFile.exists()))
-           {
-               CcCommons.TuxDroidSettingsFile.delete();
-               CcCommons.logger.append("Creating a new settings file", true);
-                       CcCommons.TuxDroidSettingsFile = new 
File(CcCommons.TuxDroidSettingsDirectory.getAbsolutePath()+
-                                                                               
                          File.separator + "settings.xml");
-                       try 
-                       {
-                               if(dest == null)
-                               {
-                                       return;
-                               }
-                               
-                               FileUtils.copy(dest, 
CcCommons.TuxDroidSettingsFile);
-                               //Now the settings file has changed, force to 
parse again the new settings file.
-                               XMLReader.edited = true;
-                               CcCommons.logger.append("Encoding completed.", 
true);
-                       } 
-                       
-                       catch (IOException e) 
-                       {
-                               //Cannot restor backup from file, so quit 
application, a new settings file will be
-                               //created at cc startup.
-                               CcCommons.logger.append("Cannot encode file.", 
true);
-                               if(CcCommons.language == null)
-                       {
-                               CcCommons.language = new 
Languages(Locale.getDefault());
-                       }
-                       
-                       String message = 
String.format(CcCommons.language.messages.getCorruptedSettingsMessage(),
-                                                                               
   CcCommons.TuxDroidSettingsFile.getAbsolutePath()
-                                                                               
  );
-                       JOptionPane.showMessageDialog( null, message,
-                                                                               
   CcCommons.language.getTitle(), 
-                                                                               
   JOptionPane.ERROR_MESSAGE
-                                                                               
  );
-                               System.exit(0);
-                       }
-           }
        }
        
        /**

Modified: 
software_suite_v2/software/control_center/trunk/control_center/sources/com/tuxdroid/cc/settings/XMLWriter.java
===================================================================
--- 
software_suite_v2/software/control_center/trunk/control_center/sources/com/tuxdroid/cc/settings/XMLWriter.java
      2008-12-21 15:05:32 UTC (rev 3277)
+++ 
software_suite_v2/software/control_center/trunk/control_center/sources/com/tuxdroid/cc/settings/XMLWriter.java
      2008-12-21 16:32:09 UTC (rev 3278)
@@ -170,7 +170,7 @@
             //Setting xml properties.
             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
             transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
-            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+            transformer.setOutputProperty(OutputKeys.ENCODING, 
CcCommons.settingsEncoding);
             
             // Registration.
             transformer.transform(source, resultat);
@@ -179,8 +179,7 @@
         }
                catch(Exception e)
                {
-                       CcCommons.logger.appendError(e.getStackTrace());
-            e.printStackTrace();
+                       CcCommons.logger.appendError(e.getStackTrace());
             CcCommons.logger.append(" -->> ERROR: An error occured trying to 
register changes.", true);
             CcCommons.logger.append("Trying to restore from a previous backup 
file.", true);
             Settings.restore();
@@ -197,12 +196,15 @@
         
         DocumentBuilder constructeur = null;
         
-               try {
+               try 
+               {
                        constructeur = fabrique.newDocumentBuilder();
-               } catch (ParserConfigurationException e) {
+               } 
+               catch (ParserConfigurationException e) 
+               {
                        CcCommons.logger.appendError(e.getStackTrace());
-                       e.printStackTrace();
-               }
+               }
+               
         Document document = constructeur.newDocument();
         // Create simple xml format.
         Element racine = document.createElement("settings");
@@ -257,9 +259,12 @@
         
         //Registering new xml file.
         this.registerChanges(document);
-        try {
+        try 
+        {
                        Thread.sleep(500);
-               } catch (InterruptedException e) {
+               } 
+        catch (InterruptedException e) 
+        {
                        CcCommons.logger.appendError(e.getStackTrace());
                        e.printStackTrace();
                }


------------------------------------------------------------------------------
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to