using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
using Green.Utility.Utils;
namespace SilverlightApplication2
{
public delegate void LoadComplete();
public class SLConfigManager
{
private XDocument document = null;
private static readonly SLConfigManager _SLConfigManager = new SLConfigManager();
private Dictionary<string, object> dict = new Dictionary<string, object>();
private Dictionary<string, string> appSettings = new Dictionary<string, string>();
private string _ConfigPath = null;
public static SLConfigManager Current
{
get
{
return _SLConfigManager;
}
}
public string ConfigPath
{
get { return _ConfigPath; }
set
{
if (_ConfigPath != value)
{
_ConfigPath = value;
LoadResource();
}
}
}
public bool IsLoaded
{
get;
private set;
}
public event LoadComplete LoadComplete;
public void EnsureLoadResource()
{
if (document == null)
{
LoadResource();
}
}
protected virtual void LoadResource()
{
if (string.IsNullOrEmpty(ConfigPath))
{
throw new Exception("ConfigPath is required!");
}
dict.Clear();
Uri url = new Uri(Application.Current.Host.Source, ConfigPath);
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(url, client);
}
protected virtual void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error != null)
{
throw e.Error;
}
var sw = new System.IO.StreamReader(e.Result);
var xml = sw.ReadToEnd();
document = XDocument.Parse(xml);
sw.Close();
e.Result.Close();
sw = null;
GetappSettings();
IsLoaded = true;
if (LoadComplete != null)
{
LoadComplete();
}
}
protected virtual void GetappSettings()
{
if (document != null)
{
var appSettingsel = document.Root.Element("appSettings");
if (appSettings != null)
{
appSettingsel.Elements("add").ToList().ForEach(e =>
{
var key = e.Attribute("key").Value;
var value = e.Attribute("value").Value;
if (!string.IsNullOrEmpty(key)&&!this.appSettings.ContainsKey(key))
{
this.appSettings.Add(key, value);
}
});
}
}
}
public T GetSection<T>(string section) where T : class ,new()
{
if (document == null)
{
// throw new Exception("Config Document is null!");
}
if (!dict.ContainsKey(section))
{
var el = document.Root.Descendants().SingleOrDefault(t => t.Name == section);
var xml = el.ToString();
dict.Add(section, XmlSerializer<T>(xml));
}
return dict[section] as T;
}
protected virtual T XmlSerializer<T>(string xml) where T : class ,new()
{
System.Xml.Serialization.XmlSerializer serialzer = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml));
var obj = serialzer.Deserialize(ms) as T;
ms.Flush();
ms.Close();
ms = null;
return obj;
}
public T GetAppSettings<T>(string key) where T : IConvertible
{
if (appSettings.ContainsKey(key))
{
var val = this.appSettings[key];
if (string.IsNullOrEmpty(val))
return default(T);
if (typeof(T).IsEnum)
{
return (T)Enum.Parse(typeof(T), val, true);
}
return (T)Convert.ChangeType(val, typeof(T), null);
}
return default(T);
}
public string GetAppSettings(string key)
{
return GetAppSettings<string>(key);
}
}
}