DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
PropertiesConfigFormatter.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.Globalization;
21 using System.IO;
22 
23 using Deveel.Data.Util;
24 
25 namespace Deveel.Data.Configuration {
27  private void SetValue(IConfiguration config, string propKey, string value) {
28  config.SetValue(propKey, value);
29  }
30 
31  private object ConvertValueTo(string value, Type valueType) {
32  return Convert.ChangeType(value, valueType, CultureInfo.InvariantCulture);
33  }
34 
35  public void LoadInto(IConfiguration config, Stream inputStream) {
36  if (inputStream == null)
37  throw new ArgumentNullException("inputStream");
38 
39  try {
40  var properties = new Properties();
41  properties.Load(inputStream);
42 
43  foreach (var entry in properties) {
44  var propKey = (string) entry.Key;
45  SetValue(config, propKey, (string) entry.Value);
46  }
48  throw;
49  } catch (Exception ex) {
50  throw new DatabaseConfigurationException("Could not load properties from the given stream.", ex);
51  }
52  }
53 
54  public void SaveFrom(IConfiguration config, ConfigurationLevel level, Stream outputStream) {
55  try {
56  var keys = config.GetKeys(level);
57  var properties = new Properties();
58 
59  foreach (var configKey in keys) {
60  var configValue = config.GetValue(configKey);
61  if (configValue != null) {
62  var stringValue = Convert.ToString(configValue, CultureInfo.InvariantCulture);
63  properties.SetProperty(configKey, stringValue);
64  }
65  }
66 
67  properties.Store(outputStream, String.Empty);
69  throw;
70  } catch (Exception ex) {
71  throw new DatabaseConfigurationException("Could not save the configurations to the given stream.", ex);
72  }
73  }
74  }
75 }
object GetValue(string key)
Gets a configuration setting for the given key.
ConfigurationLevel
Defines the level of configuration settings to save or read
void SetValue(string key, object value)
Sets a given value for a key defined by this object.
void SetValue(IConfiguration config, string propKey, string value)
Defines the contract for the configuration node of a component within the system or of the system its...
void LoadInto(IConfiguration config, Stream inputStream)
Loads a stored configuration from the given stream into the configuration argument.
Provides the format to load and store configurations in and from a stream.
void SaveFrom(IConfiguration config, ConfigurationLevel level, Stream outputStream)
Stores the given level of configurations into the output stream provided, in the format handled by th...
IEnumerable< string > GetKeys(ConfigurationLevel level)
Enumerates the keys that can be obtained by the object, at the given ConfigurationLevel.