⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 properties.cs

📁 C#开发的运行于windows mobile PDA上的游戏
💻 CS
字号:
////////////////////////////////////////////////
// 
// Project: Lines.NET
// Version: 1.1
// Author:  Vladimir L.
// 
// homepage: http://www.boomsoft.org
// e-mail:   support@boomsoft.org
// 
// Copyright (c) 2003-2004, Boomsoft.org
// 

using System;
using System.Collections;
using System.Xml;
using System.IO;
using Lines.Utils;

namespace Lines.Utils
{
	/// <summary>
	/// This class helps to deal with properties stored in XML format.
	/// </summary>
	/// 
	/// <remarks>
	/// <p>It supports the following data types: <c>int</c>, <c>bool</c>, <c>string</c> and 
	/// <c>DateTime</c>.</p>
	/// 
	/// <p>The values of unsupported types will be serrialized as a <c>string</c> type using 
	/// <see cref="object.ToString()"/> method and later during loading will be treated as a 
	/// <c>string</c> property.</p>
	/// </remarks>
	public class Properties
	{
		/// <summary>
		/// Defines string that describes property of type <c>int</c>
		/// </summary>
		public const string TYPE_INTEGER	= "int";
		/// <summary>
		/// Defines string that describes property of type <c>DateTime</c>
		/// </summary>
		public const string TYPE_DATE		= "date";
		/// <summary>
		/// Defines string that describes property of type <c>bool</c>
		/// </summary>
		public const string TYPE_BOOLEAN	= "bool";
		/// <summary>
		/// Defines string that describes property of type <c>string</c>
		/// </summary>
		public const string TYPE_STRING		= "str";
		
		/// <summary>
		/// Holds the properties in key-value pairs of <c>Hashtable</c> entries.
		/// </summary>
		protected Hashtable properties = new Hashtable();
		
		/// <summary>
		/// Creates an instance of <c>Properties</c> class
		/// </summary>
		public Properties()
		{
		}
		
		/// <summary>
		/// Loads properties from file in XML format.
		/// <seealso cref="Properties.Save(string)"/>
		/// </summary>
		/// 
		/// <param name="filename">Refers to the XML file with properties to be load.</param>
		/// 
		/// <remarks>
		/// <p>The properties that were already defined
		/// will not be removed. The new properties will be appended to existing set of properties.
		/// In case when a property from the file was already defined by this instance the value
		/// of this property will be replaced with value from the file.</p>
		/// 
		/// <p>The properties file may look like:
		/// <code>
		///	&lt;configuration&gt;
		///		&lt;properties&gt;
		///			&lt;property name="logFile" value="application.log" type="str" /&gt;
		///			&lt;property name="source" value="import.txt" type="str" /&gt;
		///			&lt;property name="exitDialog" value="True" type="bool" /&gt;
		///			&lt;property name="recentFiles" value="5" type="int" /&gt;
		///		&lt;/properties&gt;
		///	&lt;/configuration&gt;
		/// </code>
		/// </p>
		/// </remarks>
		public void Load(string filename)
		{
			if (File.Exists(filename))
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(filename);
				XmlNode root = doc.DocumentElement;
				XmlNode node = root.FirstChild.FirstChild;
				while (node != null)
				{
					string name = node.Attributes["name"].Value;
					string type = node.Attributes["type"].Value;
					string stringValue = node.Attributes["value"].Value;
					object objectValue = null;
					switch (type)
					{
						case TYPE_INTEGER:
							objectValue = int.Parse(stringValue);
							break;
						case TYPE_DATE:
							objectValue = DateTime.Parse(stringValue);
							break;
						case TYPE_BOOLEAN:
							objectValue = bool.Parse(stringValue);
							break;
						case TYPE_STRING:
						default:
							objectValue = stringValue;
							break;
					}

					properties.Add(name, objectValue);

					node = node.NextSibling;
				}
			}
		}

		/// <summary>
		/// Saves properties into a file in XML format.
		/// <seealso cref="Properties.Load(string)"/>
		/// </summary>
		/// 
		/// <param name="filename">Defines the file name where the properties will be stored into</param>
		/// 
		/// <remarks>
		/// <p>The output of this method looks similar to an example in the description of 
		/// <see cref="Properties.Load(string)"/> method. If the file with such name already exists 
		/// it will be overwritten.</p>
		/// 
		/// <p>The values of unsupported types will be serrialized as a <c>string</c> 
		/// type using <see cref="object.ToString()"/> method and later during loading will be 
		/// treated as a <c>string</c> property.</p>
		/// </remarks>
		public void Save(string filename)
		{
			XmlDocument doc = new XmlDocument();
			XmlNode root = doc.CreateNode(XmlNodeType.Element, "configuration", "");
			XmlNode node;

			doc.AppendChild(root);
			node = doc.CreateNode(XmlNodeType.Element, "properties", "");
			root.AppendChild(node);
			root = node;

			XmlAttribute attribute;
			
			foreach (object key in properties.Keys)
			{
				object objValue = properties[key];
				node = doc.CreateNode(XmlNodeType.Element, "property", "");
				
				// Name
				attribute = doc.CreateAttribute("name");
				attribute.Value = key.ToString();
				node.Attributes.Append(attribute);

				// Value
				attribute = doc.CreateAttribute("value");
				attribute.Value = objValue.ToString();
				node.Attributes.Append(attribute);

				// Type
				attribute = doc.CreateAttribute("type");
				if (objValue is int)
				{
					attribute.Value = TYPE_INTEGER;
				} 
				else if (objValue is DateTime)
				{
					attribute.Value = TYPE_DATE;
				}
				else if (objValue is bool)
				{
					attribute.Value = TYPE_BOOLEAN;
				}
				else if (objValue is string)
				{
					attribute.Value = TYPE_STRING;
				}
				else
				{
					attribute.Value = TYPE_STRING;
				}
				node.Attributes.Append(attribute);

				root.AppendChild(node);
			}

			doc.Save(filename);
		}

		/// <summary>
		/// Gets value of a certain property by it's name.
		/// <seealso cref="Properties.GetProperty(string, int)"/>
		/// <seealso cref="Properties.GetProperty(string, DateTime)"/>
		/// <seealso cref="Properties.GetProperty(string, bool)"/>
		/// <seealso cref="Properties.GetProperty(string, string)"/>
		/// </summary>
		/// 
		/// <param name="name">The property name</param>
		/// <returns>The property value, or <c>null</c> in case the one isn't defined.</returns>
		/// 
		/// <remarks>If a property with such name isn't specified then <c>null</c> is returned.</remarks>
		public object GetProperty(string name)
		{
			return properties[name];
		}
		
		/// <summary>
		/// Gets <c>int</c> value of a certain property.
		/// <seealso cref="Properties.GetProperty(string)"/>
		/// </summary>
		/// 
		/// <param name="name">The property name</param>
		/// <param name="defaultValue">The default property value (might be <c>null</c>)</param>
		/// <returns>The property value, or <c>defaultValue</c> in case the one isn't defined</returns>
		/// 
		/// <remarks>
		/// In case there is no property with such a name or the property isn't a type of 
		/// <c>int</c> the default value is returned and this value is automatically stored 
		/// in the properties under specified property name.
		/// </remarks>
		public int GetProperty(string name, int defaultValue)
		{
			int result = defaultValue;
			object obj = GetProperty(name);
			
			if ((obj != null) && (obj is int))
			{
				result = (int)obj;
			}
			else
			{
				properties[name] = defaultValue;
			}

			return result;
		}

		/// <summary>
		/// Gets <c>DateTime</c> value of a certain property.
		/// <seealso cref="Properties.GetProperty(string)"/>
		/// </summary>
		/// 
		/// <param name="name">The property name</param>
		/// <param name="defaultValue">The default property value (might be <c>null</c>)</param>
		/// <returns>The property value, or <c>defaultValue</c> in case the one isn't defined</returns>
		/// 
		/// <remarks>
		/// In case there is no property with such a name 
		/// or the property isn't a type of <c>DateTime</c> the default value is returned and this value 
		/// is automatically stored in the properties under specified property name.
		/// </remarks>
		public DateTime GetProperty(string name, DateTime defaultValue)
		{
			DateTime result = defaultValue;
			object obj = GetProperty(name);
			
			if ((obj != null) && (obj is DateTime))
			{
				result = (DateTime)obj;
			}
			else
			{
				properties[name] = defaultValue;
			}

			return result;
		}
		
		/// <summary>
		/// Gets <c>bool</c> value of a certain property.
		/// <seealso cref="Properties.GetProperty(string)"/>
		/// </summary>
		/// 
		/// <param name="name">The property name</param>
		/// <param name="defaultValue">The default property value (might be <c>null</c>)</param>
		/// <returns>The property value, or <c>defaultValue</c> in case the one isn't defined</returns>
		/// 
		/// <remarks>
		/// In case there is no property with such a name 
		/// or the property isn't a type of <c>bool</c> the default value is returned and this value 
		/// is automatically stored in the properties under specified property name.
		/// </remarks>
		public bool GetProperty(string name, bool defaultValue)
		{
			bool result = defaultValue;
			object obj = GetProperty(name);
			
			if ((obj != null) && (obj is bool))
			{
				result = (bool)obj;
			}
			else
			{
				properties[name] = defaultValue;
			}

			return result;
		}

		/// <summary>
		/// Gets <c>string</c> value of a certain property.
		/// <seealso cref="Properties.GetProperty(string)"/>
		/// </summary>
		/// 
		/// <param name="name">The property name</param>
		/// <param name="defaultValue">The default property value (might be <c>null</c>)</param>
		/// <returns>The property value, or <c>defaultValue</c> in case the one isn't defined</returns>
		/// 
		/// <remarks>
		/// In case there is no property with such a name 
		/// the default value is returned and this value is automatically stored in the properties under 
		/// specified property name. The value of a property is automatically transformed to string using
		/// <see cref="object.ToString()"/> method.
		/// </remarks>
		public string GetProperty(string name, string defaultValue)
		{
			string result = defaultValue;
			object obj = GetProperty(name);
			
			if (obj != null)
			{
				result = obj.ToString();
			}
			else
			{
				properties[name] = defaultValue;
			}

			return result;
		}

		/// <summary>
		/// Sets property's value.
		/// <seealso cref="Properties.GetProperty(string)"/>
		/// </summary>
		/// 
		/// <param name="name">The property name</param>
		/// <param name="propValue">The new value the property will hold</param>
		/// 
		/// <remarks>
		/// Although the only types that fully supported are <c>int</c>, <c>bool</c>, <c>string</c> 
		/// and <c>DateTime</c>, the property value might be of any type. All supported types have 
		/// corresponding methods to retrieve their values. The values of any other types may be 
		/// retrived via <see cref="Properties.GetProperty(string)"/> method and manually cast 
		/// to the actual type. The values of unsupported types will be serrialized as a <c>string</c> 
		/// type using <see cref="object.ToString()"/> method.
		/// </remarks>
		public void SetProperty(string name, object propValue)
		{
			properties[name] = propValue;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -