AbstractInterpreter
Provides common methods for those who wants
to implement IConfigurationInterpreter
using Castle.Core.Configuration;
using Castle.Core.Resource;
using Castle.MicroKernel;
using Castle.MicroKernel.SubSystems.Configuration;
using System;
using System.Collections.Generic;
namespace Castle.Windsor.Configuration.Interpreters
{
public abstract class AbstractInterpreter : IConfigurationInterpreter
{
protected static readonly string ContainersNodeName = "containers";
protected static readonly string ContainerNodeName = "container";
protected static readonly string FacilitiesNodeName = "facilities";
protected static readonly string FacilityNodeName = "facility";
protected static readonly string ComponentsNodeName = "components";
protected static readonly string ComponentNodeName = "component";
protected static readonly string InstallersNodeName = "installers";
protected static readonly string InstallNodeName = "install";
private readonly IResource source;
private readonly Stack<IResource> resourceStack = new Stack<IResource>();
protected IResource CurrentResource {
get {
if (resourceStack.Count == 0)
return null;
return resourceStack.Peek();
}
}
public IResource Source => source;
public string EnvironmentName { get; set; }
protected AbstractInterpreter(IResource source)
{
if (source == null)
throw new ArgumentNullException("source", "IResource is null");
this.source = source;
PushResource(source);
}
public AbstractInterpreter(string filename)
: this(new FileResource(filename))
{
}
public abstract void ProcessResource(IResource resource, IConfigurationStore store, IKernel kernel);
protected void PushResource(IResource resource)
{
resourceStack.Push(resource);
}
protected void PopResource()
{
resourceStack.Pop();
}
protected static void AddChildContainerConfig(string name, IConfiguration childContainer, IConfigurationStore store)
{
AssertValidId(name);
store.AddChildContainerConfiguration(name, childContainer);
}
protected static void AddFacilityConfig(string id, IConfiguration facility, IConfigurationStore store)
{
AssertValidId(id);
store.AddFacilityConfiguration(id, facility);
}
protected static void AddComponentConfig(string id, IConfiguration component, IConfigurationStore store)
{
AssertValidId(id);
store.AddComponentConfiguration(id, component);
}
protected static void AddInstallerConfig(IConfiguration installer, IConfigurationStore store)
{
store.AddInstallerConfiguration(installer);
}
private static void AssertValidId(string id)
{
if (string.IsNullOrEmpty(id))
throw new ConfigurationProcessingException("Component or Facility was declared without a proper 'id' or 'type' attribute.");
}
}
}