1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
19 using System
.Configuration
;
21 using Castle
.Core
.Resource
;
22 using Castle
.Core
.Configuration
;
23 using Castle
.MicroKernel
;
26 /// Provides common methods for those who wants
27 /// to implement <see cref="IConfigurationInterpreter"/>
29 public abstract class AbstractInterpreter
: IConfigurationInterpreter
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
;
52 public AbstractInterpreter(IResource source
)
54 if (source
== null) throw new ArgumentNullException("source", "IResource is null");
61 public AbstractInterpreter(string filename
) : this(new FileResource(filename
))
65 public AbstractInterpreter() : this(new ConfigResource())
72 /// Should obtain the contents from the resource,
73 /// interpret it and populate the <see cref="IConfigurationStore"/>
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()
92 protected IResource CurrentResource
96 if (resourceStack
.Count
== 0) return null;
98 return resourceStack
.Peek() as IResource
;
107 /// Exposes the reference to <see cref="IResource"/>
108 /// which the interpreter is likely to hold
111 public IResource Source
113 get { return source; }
117 /// Gets or sets the name of the environment.
119 /// <value>The name of the environment.</value>
120 public string EnvironmentName
122 get { return environmentName; }
123 set { environmentName = value; }
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
)
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
)
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
)
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
)
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
);
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
);