crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/Settings/XmlSettingsCategory.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.Xml;
       
    19 using System.Text;
       
    20 using System.IO;
       
    21 using System.Collections.Generic;
       
    22 using Microsoft.Win32;
       
    23 using System.Diagnostics;
       
    24 using System.Collections;
       
    25 using System.Windows.Forms;
       
    26 
       
    27 namespace SymbianUtils.Settings
       
    28 {
       
    29     public class XmlSettingCategory
       
    30 	{
       
    31 		#region Constructors
       
    32 		public XmlSettingCategory( string aName )
       
    33 		{
       
    34 			iName = aName;
       
    35 			iVersion = (Version) XmlSettings.CurrentSettingsVersion.Clone();
       
    36 		}
       
    37 
       
    38 		public XmlSettingCategory( XmlReader aReader )
       
    39 		{
       
    40             XmlUtils.ReadAttributeValue( aReader, "name", out iName );
       
    41             //
       
    42             string versionAsString;
       
    43 			XmlUtils.ReadAttributeValue( aReader, "version", out versionAsString );
       
    44             iVersion = new Version( versionAsString );
       
    45 		}
       
    46 		#endregion
       
    47 
       
    48 		#region Store & Restore API
       
    49 		internal void WriteSettings( XmlWriter aWriter )
       
    50 		{
       
    51 			aWriter.WriteStartElement( Name, null );
       
    52 		    //
       
    53             foreach ( KeyValuePair<string, XmlSettingsValue> pair in iSettings )
       
    54             {
       
    55                 // Write overall item encapsulation header
       
    56                 aWriter.WriteStartElement( "item", null );
       
    57                 
       
    58                     // Write key
       
    59                     aWriter.WriteAttributeString( "key", pair.Key );
       
    60                 
       
    61                     // Get value object to write itself out
       
    62                     pair.Value.Write( aWriter );
       
    63 
       
    64                 // End item
       
    65                 aWriter.WriteEndElement();
       
    66             }
       
    67             //
       
    68             aWriter.WriteEndElement();
       
    69 		}
       
    70 
       
    71         internal void ReadSettings( XmlReader aReader )
       
    72 		{
       
    73 			while ( !aReader.EOF && aReader.Name == "item" )
       
    74 			{
       
    75 				if ( aReader.NodeType != XmlNodeType.EndElement )
       
    76 				{
       
    77                     string key = XmlUtils.ReadAttributeValue( aReader, "key" );
       
    78                     //
       
    79                     XmlSettingsValue value = new XmlSettingsValue( aReader );
       
    80                     iSettings.Add( key, value );
       
    81 				}
       
    82 				aReader.Read();
       
    83 			}
       
    84             //
       
    85 			SymbianUtils.SymDebug.SymDebugger.Assert(aReader.Name == Name);
       
    86 		}
       
    87 		#endregion
       
    88 
       
    89         #region Misc. API
       
    90         internal void Add( string aKey, string aValue )
       
    91 		{
       
    92             XmlSettingsValue value = new XmlSettingsValue( aValue );
       
    93             iSettings.Add( aKey, value );
       
    94 		}
       
    95 
       
    96         internal void Remove( string aKey )
       
    97 		{
       
    98             if ( iSettings.ContainsKey( aKey ) )
       
    99             {
       
   100                 iSettings.Remove( aKey );
       
   101 			}
       
   102 		}
       
   103 
       
   104         public bool Contains( string aKey )
       
   105         {
       
   106             return iSettings.ContainsKey( aKey );
       
   107         }
       
   108         
       
   109 		public bool Exists( string aKey )
       
   110 		{
       
   111             return Contains( aKey );
       
   112 		}
       
   113 		#endregion
       
   114 
       
   115 		#region Properties
       
   116 		public string Name
       
   117 		{
       
   118 			get { return iName; }
       
   119 		}
       
   120 
       
   121 		public Version Version
       
   122 		{
       
   123 			get { return iVersion; }
       
   124 		}
       
   125 		#endregion
       
   126 
       
   127 		#region Indexers
       
   128         public XmlSettingsValue this[ string aKey ]
       
   129 		{
       
   130 			get
       
   131 			{
       
   132                 XmlSettingsValue ret = new XmlSettingsValue( string.Empty );
       
   133                 //
       
   134                 if ( Exists( aKey ) )
       
   135                 {
       
   136                     ret = iSettings[ aKey ];
       
   137                 }
       
   138                 else
       
   139                 {
       
   140                     iSettings.Add( aKey, ret );
       
   141                 }
       
   142                 //
       
   143                 return ret;
       
   144 			}
       
   145 			set
       
   146 			{
       
   147                 if ( !Exists( aKey ) )
       
   148                 {
       
   149                     iSettings.Add( aKey, value );
       
   150                 }
       
   151                 else
       
   152                 {
       
   153                     // Item already exists
       
   154                     iSettings[ aKey ] = value;
       
   155                 }
       
   156             }
       
   157 		}
       
   158 		#endregion
       
   159 		
       
   160 		#region Internal methods
       
   161 		#endregion
       
   162 
       
   163 		#region Data members
       
   164 		private readonly string iName;
       
   165 		private readonly Version iVersion;
       
   166         private SortedList<string, XmlSettingsValue> iSettings = new SortedList<string, XmlSettingsValue>();
       
   167 		#endregion
       
   168 	}
       
   169 }