Added RedirectUsingNamedRoute
[castle.git] / InversionOfControl / Castle.Windsor / Configuration / Interpreters / AbstractInterpreter.cs
blobfd2d6f5705dc936da529a98d2a5f6bc563c11041
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.Windsor.Configuration.Interpreters
17 using System;
18 using System.Collections;
19 using System.Configuration;
21 using Castle.Core.Resource;
22 using Castle.Core.Configuration;
23 using Castle.MicroKernel;
25 /// <summary>
26 /// Provides common methods for those who wants
27 /// to implement <see cref="IConfigurationInterpreter"/>
28 /// </summary>
29 public abstract class AbstractInterpreter : IConfigurationInterpreter
31 #region Fields
33 protected static readonly string ContainersNodeName = "containers";
34 protected static readonly string ContainerNodeName = "container";
35 protected static readonly string FacilitiesNodeName = "facilities";
36 protected static readonly string FacilityNodeName = "facility";
37 protected static readonly string ComponentsNodeName = "components";
38 protected static readonly string BootstrapNodeName = "bootstrap";
39 protected static readonly string ComponentNodeName = "component";
40 protected static readonly string IncludeNodeName = "include";
41 protected static readonly string PropertiesNodeName = "properties";
43 // private ImportDirectiveCollection imports = new ImportDirectiveCollection();
44 private IResource source;
45 private Stack resourceStack = new Stack();
46 private string environmentName;
48 #endregion
50 #region Constructors
52 public AbstractInterpreter(IResource source)
54 if (source == null) throw new ArgumentNullException("source", "IResource is null");
56 this.source = source;
58 PushResource(source);
61 public AbstractInterpreter(string filename) : this(new FileResource(filename))
65 public AbstractInterpreter() : this(new ConfigResource())
69 #endregion
71 /// <summary>
72 /// Should obtain the contents from the resource,
73 /// interpret it and populate the <see cref="IConfigurationStore"/>
74 /// accordingly.
75 /// </summary>
76 /// <param name="resource"></param>
77 /// <param name="store"></param>
78 public abstract void ProcessResource(IResource resource, IConfigurationStore store);
80 #region Support for Resource stack
82 protected void PushResource(IResource resource)
84 resourceStack.Push(resource);
87 protected void PopResource()
89 resourceStack.Pop();
92 protected IResource CurrentResource
94 get
96 if (resourceStack.Count == 0) return null;
98 return resourceStack.Peek() as IResource;
102 #endregion
104 #region Properties
106 /// <summary>
107 /// Exposes the reference to <see cref="IResource"/>
108 /// which the interpreter is likely to hold
109 /// </summary>
110 /// <value></value>
111 public IResource Source
113 get { return source; }
116 /// <summary>
117 /// Gets or sets the name of the environment.
118 /// </summary>
119 /// <value>The name of the environment.</value>
120 public string EnvironmentName
122 get { return environmentName; }
123 set { environmentName = value; }
126 #endregion
128 #region Helpers to populate IConfigurationStore
130 protected void AddFacilityConfig(IConfiguration facility, IConfigurationStore store)
132 AddFacilityConfig( facility.Attributes["id"], facility, store );
135 protected void AddComponentConfig(IConfiguration component, IConfigurationStore store)
137 AddComponentConfig( component.Attributes["id"], component, store );
140 protected void AddChildContainerConfig(string name, IConfiguration childContainer, IConfigurationStore store)
142 AssertValidId(name);
144 // TODO: Use import collection on type attribute (if it exists)
146 store.AddChildContainerConfiguration(name, childContainer);
149 protected void AddFacilityConfig(string id, IConfiguration facility, IConfigurationStore store)
151 AssertValidId(id);
153 // TODO: Use import collection on type attribute (if it exists)
155 store.AddFacilityConfiguration( id, facility );
158 protected void AddComponentConfig(string id, IConfiguration component, IConfigurationStore store)
160 AssertValidId(id);
162 // TODO: Use import collection on type and service attribute (if they exist)
164 store.AddComponentConfiguration( id, component );
167 protected void AddBootstrapComponentConfig(string id, IConfiguration component, IConfigurationStore store)
169 AssertValidId(id);
171 // TODO: Use import collection on type and service attribute (if they exist)
173 store.AddBootstrapComponentConfiguration(id, component);
176 private void AssertValidId(string id)
178 if (id == null || id.Length == 0)
180 string message = "Component or Facility was declared without a proper 'id' attribute";
182 throw new ConfigurationErrorsException(message);
186 #endregion
188 protected void ProcessInclude(string uri, IConfigurationStore store)
190 IResource resource = store.GetResource(uri, CurrentResource);
192 if (resource == null)
194 // TODO: Proper Exception
197 PushResource(resource);
199 ProcessResource(resource, store);
201 PopResource();