18 using System.Collections.Generic;
19 using System.Globalization;
23 namespace Deveel.Data.Configuration {
32 var keys = config.
GetKeys(level);
33 var values = keys.Select(x => config.
GetValue(x))
34 .Where(value => value != null)
37 return values.ToArray();
40 #region GetValue(string)
43 return GetValue(config, keyName, null);
47 var value = config.
GetValue(keyName);
55 return GetValue<T>(config, keyName,
default(T));
58 private static T ToType<T>(
object value) {
65 if (typeof(T) == typeof(
bool) &&
67 return (T)ConvertToBoolean((
string)value);
70 return ConvertToEnum<T>(value);
72 if (!(value is IConvertible))
73 throw new InvalidCastException();
75 return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
78 private static T ConvertToEnum<T>(
object value) {
88 var s = value.ToString();
89 return (T)Enum.Parse(typeof(T), s,
true);
93 if (String.Equals(value,
"true", StringComparison.OrdinalIgnoreCase) ||
94 String.Equals(value,
"enabled", StringComparison.OrdinalIgnoreCase) ||
95 String.Equals(value,
"1") ||
96 String.Equals(value,
"on", StringComparison.OrdinalIgnoreCase))
98 if (String.Equals(value,
"false", StringComparison.OrdinalIgnoreCase) ||
99 String.Equals(value,
"disabled", StringComparison.OrdinalIgnoreCase) ||
100 String.Equals(value,
"0") ||
101 String.Equals(value,
"off"))
104 throw new InvalidCastException();
107 public static T GetValue<T>(
this IConfiguration config,
string keyName, T defaultValue) {
108 var value = config.GetValue(keyName);
112 return ToType<T>(value);
116 return GetString(config, propertyKey, null);
120 return config.
GetValue(propertyKey, defaultValue);
124 return GetByte(config, propertyKey, 0);
128 return config.
GetValue(propertyKey, defaultValue);
131 [CLSCompliant(
false)]
133 return config.
GetValue(propertyKey, defaultValue);
137 return GetInt16(config, propertyKey, 0);
141 return config.
GetValue<
short>(propertyKey, defaultValue);
144 [CLSCompliant(
false)]
146 return GetUInt16(config, propertyKey, 0);
149 [CLSCompliant(
false)]
151 return config.
GetValue(propertyKey, defaultValue);
155 return GetInt32(config, propertyKey, 0);
159 return config.
GetValue(propertyKey, defaultValue);
162 [CLSCompliant(
false)]
164 return GetUInt32(config, propertyKey, 0);
167 [CLSCompliant(
false)]
169 return config.
GetValue(propertyKey, defaultValue);
173 return GetInt64(config, propertyKey, 0);
177 return config.
GetValue(propertyKey, defaultValue);
180 [CLSCompliant(
false)]
182 return GetUInt64(config, propertyKey, 0);
185 [CLSCompliant(
false)]
187 return config.
GetValue(propertyKey, defaultValue);
191 return GetBoolean(config, propertyKey,
false);
195 return config.
GetValue(propertyKey, defaultValue);
199 return GetSingle(config, propertyKey, 0);
203 return config.
GetValue(propertyKey, defaultValue);
207 return GetDouble(config, propertyKey, 0);
211 return config.
GetValue<
double>(propertyKey, defaultValue);
225 if (config.
Source == null)
226 throw new InvalidOperationException(
"Source was not configured");
228 config.Load(config.
Source, formatter);
233 if (source != null) {
235 if (!sourceStream.CanRead)
236 throw new ArgumentException(
"The input stream cannot be read.");
238 sourceStream.Seek(0, SeekOrigin.Begin);
239 formatter.
LoadInto(config, sourceStream);
242 }
catch (Exception ex) {
272 if (!outputStream.CanWrite)
273 throw new InvalidOperationException(
"The destination source cannot be written.");
275 outputStream.Seek(0, SeekOrigin.Begin);
276 formatter.
SaveFrom(config, level, outputStream);
277 outputStream.Flush();
279 }
catch (Exception ex) {
289 if (config.
Source == null)
292 config.Save(config.
Source, level, formatter);
340 foreach (var pair
in other) {
341 newConfig.SetValue(pair.Key, pair.Value);
static short GetInt16(this IConfiguration config, string propertyKey)
static void Load(this IConfiguration config, string fileName)
object GetValue(string key)
Gets a configuration setting for the given key.
static int GetInt32(this IConfiguration config, string propertyKey)
A source for stored configurations or destination to configurations to store.
static object ConvertToBoolean(string value)
static void Load(this IConfiguration config, IConfigSource source, IConfigFormatter formatter)
static short GetInt16(this IConfiguration config, string propertyKey, short defaultValue)
static void Load(this IConfiguration config, string fileName, IConfigFormatter formatter)
static IEnumerable< string > GetKeys(this IConfiguration config)
ConfigurationLevel
Defines the level of configuration settings to save or read
Stream InputStream
Gets a Stream that is used to load the configurations.
static byte GetByte(this IConfiguration config, string propertyKey, byte defaultValue)
An implementation of IConfigSource that handles a single Stream as source and destination of the conf...
static bool GetBoolean(this IConfiguration config, string propertyKey, bool defaultValue)
static void Save(this IConfiguration config, ConfigurationLevel level, Stream outputStream, IConfigFormatter formatter)
static float GetSingle(this IConfiguration config, string propertyKey)
static void Load(this IConfiguration config, Stream inputStream, IConfigFormatter formatter)
static ushort GetUInt16(this IConfiguration config, string propertyKey, ushort defaultValue)
static void Save(this IConfiguration config, Stream outputStream, IConfigFormatter formatter)
static void Save(this IConfiguration config, ConfigurationLevel level, string fileName, IConfigFormatter formatter)
static uint GetUInt32(this IConfiguration config, string propertyKey)
static long GetInt64(this IConfiguration config, string propertyKey, long defaultValue)
IConfigSource Source
Gets or sets an optional source of the configuration object
static string GetString(this IConfiguration config, string propertyKey)
static void Save(this IConfiguration config, ConfigurationLevel level)
static void Load(this IConfiguration config, IConfigSource source)
static void Save(this IConfiguration config, Stream outputStream)
static void Save(this IConfiguration config, string fileName, IConfigFormatter formatter)
static int GetInt32(this IConfiguration config, string propertyKey, int defaultValue)
static void Save(this IConfiguration config, ConfigurationLevel level, string fileName)
static void Save(this IConfiguration config, IConfigSource source, ConfigurationLevel level, IConfigFormatter formatter)
static long GetInt64(this IConfiguration config, string propertyKey)
static byte GetByte(this IConfiguration config, string propertyKey)
static void Save(this IConfiguration config, ConfigurationLevel level, IConfigFormatter formatter)
Stream OutputStream
Gets a Stream that can be writtern with the configurations from a IConfiguration. ...
static ulong GetUInt64(this IConfiguration config, string propertyKey)
static void Load(this IConfiguration config, IConfigFormatter formatter)
static uint GetUInt32(this IConfiguration config, string propertyKey, uint defaultValue)
static double GetDouble(this IConfiguration config, string propertyKey)
static void Load(this IConfiguration config, Stream inputStream)
Defines the contract for the configuration node of a component within the system or of the system its...
static IConfiguration MergeWith(this IConfiguration configuration, IConfiguration other)
static ulong GetUInt64(this IConfiguration config, string propertyKey, ulong defaultValue)
A channel used to read from and write to a given file in the underlying file-system.
static sbyte GetSByte(this IConfiguration config, string propertyKey, sbyte defaultValue)
static string GetString(this IConfiguration config, string propertyKey, string defaultValue)
static void Save(this IConfiguration config, ConfigurationLevel level, Stream outputStream)
static double GetDouble(this IConfiguration config, string propertyKey, double defaultValue)
static IEnumerable< object > GetValues(this IConfiguration config, ConfigurationLevel level)
static object GetValue(this IConfiguration config, string keyName)
static object GetValue(this IConfiguration config, string keyName, object defaultValue)
static void Save(this IConfiguration config, IConfigFormatter formatter)
static void Save(this IConfiguration config, string fileName)
static void Save(this IConfiguration config)
static void Save(this IConfiguration config, IConfigSource source, IConfigFormatter formatter)
static ushort GetUInt16(this IConfiguration config, string propertyKey)
IEnumerable< string > GetKeys(ConfigurationLevel level)
Enumerates the keys that can be obtained by the object, at the given ConfigurationLevel.
static bool GetBoolean(this IConfiguration config, string propertyKey)
static float GetSingle(this IConfiguration config, string propertyKey, float defaultValue)