Added generalized configuration support to ComponentRegistration besides parameters...
[castle.git] / InversionOfControl / Castle.MicroKernel / Registration / Configuration.cs
blob0092e5309f1d3b5b2ddc02c74b15ba81c57cfdb3
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MicroKernel.Registration
17 using System;
18 using Castle.Core.Configuration;
20 #region Node
22 /// <summary>
23 /// Represents a configuration child.
24 /// </summary>
25 public abstract class Node
27 private readonly String name;
29 protected Node(String name)
31 this.name = name;
34 protected string Name
36 get { return name; }
39 /// <summary>
40 /// Applies the configuration node.
41 /// </summary>
42 /// <param name="configuration">The configuration.</param>
43 public abstract void ApplyTo(IConfiguration configuration);
46 #endregion
48 #region Attribute
50 /// <summary>
51 /// Represents a configuration attribute.
52 /// </summary>
53 public class Attribute : Node
55 private readonly String value;
57 internal Attribute(String name, String value)
58 : base(name)
60 this.value = value;
63 /// <summary>
64 /// Applies the configuration node.
65 /// </summary>
66 /// <param name="configuration">The configuration.</param>
67 public override void ApplyTo(IConfiguration configuration)
69 configuration.Attributes.Add(Name, value);
72 /// <summary>
73 /// Create a <see cref="NamedAttribute"/> with name.
74 /// </summary>
75 /// <param name="name">The attribute name.</param>
76 /// <returns>The new <see cref="NamedAttribute"/></returns>
77 public static NamedAttribute ForName(String name)
79 return new NamedAttribute(name);
83 #endregion
85 #region NamedChild
87 /// <summary>
88 /// Represents a named attribute.
89 /// </summary>
90 public class NamedAttribute
92 private readonly String name;
94 internal NamedAttribute(String name)
96 this.name = name;
99 /// <summary>
100 /// Builds the <see cref="Attribute"/> with name/value.
101 /// </summary>
102 /// <param name="value">The attribute value.</param>
103 /// <returns>The new <see cref="SimpleChild"/></returns>
104 public Attribute Eq(String value)
106 return new Attribute(name, value);
110 #endregion
112 #region Child
114 /// <summary>
115 /// Represents a configuration child.
116 /// </summary>
117 public abstract class Child
119 /// <summary>
120 /// Create a <see cref="NamedChild"/> with name.
121 /// </summary>
122 /// <param name="name">The child name.</param>
123 /// <returns>The new <see cref="NamedChild"/></returns>
124 public static NamedChild ForName(String name)
126 return new NamedChild(name);
130 #endregion
132 #region NamedChild
134 /// <summary>
135 /// Represents a named child.
136 /// </summary>
137 public class NamedChild : Node
139 internal NamedChild(String name)
140 : base(name)
144 /// <summary>
145 /// Builds the <see cref="SimpleChild"/> with name/value.
146 /// </summary>
147 /// <param name="value">The child value.</param>
148 /// <returns>The new <see cref="SimpleChild"/></returns>
149 public SimpleChild Eq(String value)
151 return new SimpleChild(Name, value);
154 /// <summary>
155 /// Builds the <see cref="ComplexChild"/> with name/config.
156 /// </summary>
157 /// <param name="configNode">The child configuration.</param>
158 /// <returns>The new <see cref="ComplexChild"/></returns>
159 public ComplexChild Eq(IConfiguration configNode)
161 return new ComplexChild(Name, configNode);
164 /// <summary>
165 /// Builds the <see cref="Child"/> with name/config.
166 /// </summary>
167 /// <param name="childNodes">The child nodes.</param>
168 /// <returns>The new <see cref="CompoundChild"/></returns>
169 public CompoundChild Eq(params Node[] childNodes)
171 return new CompoundChild(Name, childNodes);
174 /// <summary>
175 /// Applies the configuration node.
176 /// </summary>
177 /// <param name="configuration">The configuration.</param>
178 public override void ApplyTo(IConfiguration configuration)
180 MutableConfiguration node = new MutableConfiguration(Name);
181 configuration.Children.Add(node);
185 #endregion
187 #region SimpleChild
189 /// <summary>
190 /// Represents a simple child node.
191 /// </summary>
192 public class SimpleChild : Node
194 private readonly String value;
196 internal SimpleChild(String name, String value)
197 : base(name)
199 this.value = value;
202 /// <summary>
203 /// Applies the configuration node.
204 /// </summary>
205 /// <param name="configuration">The configuration.</param>
206 public override void ApplyTo(IConfiguration configuration)
208 MutableConfiguration node = new MutableConfiguration(Name, value);
209 configuration.Children.Add(node);
213 #endregion
215 #region ComplexChild
217 /// <summary>
218 /// Represents a complex child node.
219 /// </summary>
220 public class ComplexChild : Node
222 private readonly IConfiguration configNode;
224 internal ComplexChild(String name, IConfiguration configNode)
225 : base(name)
227 this.configNode = configNode;
230 /// <summary>
231 /// Applies the configuration node.
232 /// </summary>
233 /// <param name="configuration">The configuration.</param>
234 public override void ApplyTo(IConfiguration configuration)
236 MutableConfiguration node = new MutableConfiguration(Name);
237 node.Children.Add(configNode);
238 configuration.Children.Add(node);
242 #endregion
244 #region CompoundChild
246 /// <summary>
247 /// Represents a compound child node.
248 /// </summary>
249 public class CompoundChild : Node
251 private readonly Node[] childNodes;
253 internal CompoundChild(String name, Node[] childNodes)
254 : base(name)
256 this.childNodes = childNodes;
259 /// <summary>
260 /// Applies the configuration node.
261 /// </summary>
262 /// <param name="configuration">The configuration.</param>
263 public override void ApplyTo(IConfiguration configuration)
265 MutableConfiguration node = new MutableConfiguration(Name);
266 foreach (Node childNode in childNodes)
268 childNode.ApplyTo( node );
270 configuration.Children.Add(node);
274 #endregion