crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/Settings/XmlSettingsValueConverter.cs
changeset 0 818e61de6cd1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/Settings/XmlSettingsValueConverter.cs	Thu Feb 11 15:50:58 2010 +0200
@@ -0,0 +1,93 @@
+/*
+* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
+* All rights reserved.
+* This component and the accompanying materials are made available
+* under the terms of "Eclipse Public License v1.0"
+* which accompanies this distribution, and is available
+* at the URL "http://www.eclipse.org/legal/epl-v10.html".
+*
+* Initial Contributors:
+* Nokia Corporation - initial contribution.
+*
+* Contributors:
+* 
+* Description:
+*
+*/
+using System;
+using System.Xml;
+using System.Text;
+using System.IO;
+using System.ComponentModel;
+using System.Collections.Generic;
+
+namespace SymbianUtils.Settings
+{
+    internal static class XmlSettingsValueConverter<T>
+	{
+        public static T Convert( XmlSettingsValue aValue, T aDefault )
+        {
+            bool defaultApplied = false;
+            return Convert( aValue, aDefault, out defaultApplied );
+        }
+
+        public static T Convert( XmlSettingsValue aValue, T aDefault, out bool aDefaultApplied )
+        {
+            T ret = aDefault;
+            aDefaultApplied = true;
+            //
+            Type srcType = aValue.Value.GetType();
+            Type destType = aDefault.GetType();
+            //
+            try
+            {
+                // Don't need to convert if source and destination types are
+                // identical.
+                if ( srcType == destType )
+                {
+                    ret = (T) aValue.Value;
+                }
+                else
+                {
+                    // Don't try to convert empty strings.
+                    if ( aValue.Value is string && destType != typeof( string ) )
+                    {
+                        string val = ( (string) aValue.Value ).Trim();
+                        if ( val == string.Empty )
+                        {
+                            return aDefault;
+                        }
+                    }
+
+                    // Try the type converter instead
+                    TypeConverter converter = TypeDescriptor.GetConverter( destType );
+                    if ( converter.CanConvertFrom( srcType ) )
+                    {
+                        if ( aValue.Value is string )
+                        {
+                            string text = (string) aValue.Value;
+                            ret = (T) converter.ConvertFromInvariantString( text );
+                        }
+                        else
+                        {
+                            ret = (T) converter.ConvertFrom( aValue.Value );
+                        }
+                        aDefaultApplied = false;
+                    }
+                    else
+                    {
+                        SymbianUtils.SymDebug.SymDebugger.Break();
+                    }
+                }
+            }
+            catch ( System.FormatException )
+            {
+                SymbianUtils.SymDebug.SymDebugger.Break();
+            }
+            //
+            return ret;
+        }
+	}
+}
+
+