Added RedirectUsingNamedRoute
[castle.git] / InversionOfControl / Castle.Windsor / Configuration / Interpreters / XmlProcessor / XmlProcessor.cs
blob2222cba9a5846f105d92a133b61b3fa0d917251d
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.XmlProcessor
17 using System;
18 using System.Xml;
20 using Castle.MicroKernel.SubSystems.Resource;
21 using Castle.Core.Resource;
23 using ElementProcessors;
25 /// <summary>
26 /// Pendent
27 /// </summary>
28 public class XmlProcessor
30 private IXmlProcessorEngine engine;
32 /// <summary>
33 /// Initializes a new instance of the <see cref="XmlProcessor"/> class.
34 /// </summary>
35 public XmlProcessor() : this(null)
39 /// <summary>
40 /// Initializes a new instance of the <see cref="XmlProcessor"/> class.
41 /// </summary>
42 /// <param name="environmentName">Name of the environment.</param>
43 /// <param name="resourceSubSystem">The resource sub system.</param>
44 public XmlProcessor(string environmentName, IResourceSubSystem resourceSubSystem)
46 engine = new DefaultXmlProcessorEngine(environmentName, resourceSubSystem);
47 RegisterProcessors();
50 /// <summary>
51 /// Initializes a new instance of the <see cref="XmlProcessor"/> class.
52 /// </summary>
53 public XmlProcessor(string environmentName)
55 engine = new DefaultXmlProcessorEngine(environmentName);
56 RegisterProcessors();
59 protected virtual void RegisterProcessors()
61 AddElementProcessor(typeof(IfElementProcessor));
62 AddElementProcessor(typeof(DefineElementProcessor));
63 AddElementProcessor(typeof(UndefElementProcessor));
64 AddElementProcessor(typeof(ChooseElementProcessor));
65 AddElementProcessor(typeof(PropertiesElementProcessor));
66 AddElementProcessor(typeof(AttributesElementProcessor));
67 AddElementProcessor(typeof(IncludeElementProcessor));
68 AddElementProcessor(typeof(IfProcessingInstructionProcessor));
69 AddElementProcessor(typeof(DefinedProcessingInstructionProcessor));
70 AddElementProcessor(typeof(UndefProcessingInstructionProcessor));
71 AddElementProcessor(typeof(DefaultTextNodeProcessor));
72 AddElementProcessor(typeof(EvalProcessingInstructionProcessor));
75 protected void AddElementProcessor(Type t)
77 engine.AddNodeProcessor(t);
80 public XmlNode Process(XmlNode node)
82 try
84 if (node.NodeType == XmlNodeType.Document)
86 node = (node as XmlDocument).DocumentElement;
89 engine.DispatchProcessAll(new DefaultXmlProcessorNodeList(node));
91 return node;
93 catch(ConfigurationProcessingException)
95 throw;
97 catch(Exception ex)
99 String message = String.Format("Error processing node {0}, inner content {1}", node.Name, node.InnerXml);
101 throw new ConfigurationProcessingException(message, ex);
105 public XmlNode Process(IResource resource)
109 using(resource)
111 XmlDocument doc = new XmlDocument();
113 doc.Load(resource.GetStreamReader());
115 engine.PushResource(resource);
117 XmlNode element = Process(doc.DocumentElement);
119 engine.PopResource();
121 return element;
124 catch(ConfigurationProcessingException)
126 throw;
128 catch(Exception ex)
130 String message = String.Format("Error processing node resource {0}", resource);
132 throw new ConfigurationProcessingException(message, ex);