File: ConfigSourceBase.cs

package info (click to toggle)
nini 1.1.0%2Bdfsg.3-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 880 kB
  • sloc: cs: 7,649; xml: 2,945; makefile: 28; ansic: 7
file content (219 lines) | stat: -rw-r--r-- 5,690 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#region Copyright
//
// Nini Configuration Project.
// Copyright (C) 2006 Brent R. Matzelle.  All rights reserved.
//
// This software is published under the terms of the MIT X11 license, a copy of 
// which has been included with this distribution in the LICENSE.txt file.
// 
#endregion

using System;
using System.Text;
using System.Collections;

namespace Nini.Config
{
	/// <include file='IConfigSource.xml' path='//Interface[@name="IConfigSource"]/docs/*' />
	public abstract class ConfigSourceBase : IConfigSource
	{
		#region Private variables
		ArrayList sourceList = new ArrayList ();
		ConfigCollection configList = null;
		bool autoSave = false;
		AliasText alias = new AliasText ();
		#endregion

		#region Constructors
		/// <include file='ConfigSourceBase.xml' path='//Constructor[@name="Constructor"]/docs/*' />
		public ConfigSourceBase ()
		{
			configList = new ConfigCollection (this);
		}
		#endregion
		
		#region Public properties
		/// <include file='IConfigSource.xml' path='//Property[@name="Configs"]/docs/*' />
		public ConfigCollection Configs
		{
			get { return configList; }
		}
		
		/// <include file='IConfigSource.xml' path='//Property[@name="AutoSave"]/docs/*' />
		public bool AutoSave
		{
			get { return autoSave; }
			set { autoSave = value; }
		}
		
		/// <include file='IConfigSource.xml' path='//Property[@name="Alias"]/docs/*' />
		public AliasText Alias
		{
			get { return alias; }
		}
		#endregion
		
		#region Public methods
		/// <include file='IConfigSource.xml' path='//Method[@name="Merge"]/docs/*' />
		public void Merge (IConfigSource source)
		{
			if (!sourceList.Contains (source))  {
				sourceList.Add (source);
			}
			
			foreach (IConfig config in source.Configs)
			{
				this.Configs.Add (config);
			}
		}
		
		/// <include file='IConfigSource.xml' path='//Method[@name="AddConfig"]/docs/*' />
		public virtual IConfig AddConfig (string name)
		{
			return configList.Add (name);
		}

		/// <include file='IConfigSource.xml' path='//Method[@name="GetExpanded"]/docs/*' />
		public string GetExpanded (IConfig config, string key)
		{
			return Expand (config, key, false);
		}
		
		/// <include file='IConfigSource.xml' path='//Method[@name="Save"]/docs/*' />
		public virtual void Save ()
		{
			OnSaved (new EventArgs ());
		}

		/// <include file='IConfigSource.xml' path='//Method[@name="Reload"]/docs/*' />
		public virtual void Reload ()
		{
			OnReloaded (new EventArgs ());
		}
		
		/// <include file='IConfigSource.xml' path='//Method[@name="ExpandKeyValues"]/docs/*' />
		public void ExpandKeyValues ()
		{
			string[] keys = null;

			foreach (IConfig config in configList)
			{
				keys = config.GetKeys ();
				for (int i = 0; i < keys.Length; i++)
				{
					Expand (config, keys[i], true);
				}
			}
		}

		/// <include file='IConfigSource.xml' path='//Method[@name="ReplaceKeyValues"]/docs/*' />
		public void ReplaceKeyValues ()
		{
			ExpandKeyValues ();
		}
		#endregion

		#region Public events
		/// <include file='IConfigSource.xml' path='//Event[@name="Reloaded"]/docs/*' />
		public event EventHandler Reloaded;

		/// <include file='IConfigSource.xml' path='//Event[@name="Saved"]/docs/*' />
		public event EventHandler Saved;
		#endregion

		#region Protected methods
		/// <include file='ConfigSourceBase.xml' path='//Method[@name="OnReloaded"]/docs/*' />
		protected void OnReloaded (EventArgs e)
		{
			if (Reloaded != null) {
				Reloaded (this, e);
			}
		}

		/// <include file='ConfigSourceBase.xml' path='//Method[@name="OnSaved"]/docs/*' />
		protected void OnSaved (EventArgs e)
		{
			if (Saved != null) {
				Saved (this, e);
			}
		}
		#endregion

		#region Private methods	
		/// <summary>
		/// Expands key values from the given IConfig.
		/// </summary>
		private string Expand (IConfig config, string key, bool setValue)
		{
			string result = config.Get (key);
			if (result == null) {
				throw new ArgumentException (String.Format ("[{0}] not found in [{1}]",
										key, config.Name));
			}

			while (true)
			{
				int startIndex = result.IndexOf ("${", 0);
				if (startIndex == -1) {
					break;
				}

				int endIndex = result.IndexOf ("}", startIndex + 2);
				if (endIndex == -1) {
					break;
				}

				string search = result.Substring (startIndex + 2, 
												  endIndex - (startIndex + 2));

				if (search == key) {
					// Prevent infinite recursion
					throw new ArgumentException 
						("Key cannot have a expand value of itself: " + key);
				}

				string replace = ExpandValue (config, search);

				result = result.Replace("${" + search + "}", replace);
			}

			if (setValue) {
				config.Set(key, result);
			}

			return result;
		}
		
		/// <summary>
		/// Returns the replacement value of a config.
		/// </summary>
		private string ExpandValue (IConfig config, string search)
		{
			string result = null;
			
			string[] replaces = search.Split ('|');
			
			if (replaces.Length > 1) {
				IConfig newConfig = this.Configs[replaces[0]];
				if (newConfig == null) {
					throw new ArgumentException ("Expand config not found: "
												 + replaces[0]);
				}
				result = newConfig.Get (replaces[1]);
				if (result == null) {
					throw new ArgumentException ("Expand key not found: "
												 + replaces[1]);
				}
			} else {
				result = config.Get (search);
				
				if (result == null) {
				    throw new ArgumentException ("Key not found: " + search);
				}
			}
			
			return result;
		}
		#endregion
	}
}