Added RedirectUsingNamedRoute
[castle.git] / InversionOfControl / Castle.Windsor / Configuration / Interpreters / XmlProcessor / ElementProcessors / IncludeElementProcessor.cs
blobec76ac54cd4856d01650a74f37aa69c281b5c84a
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.ElementProcessors
17 using System;
18 using System.Xml;
20 using Castle.Core.Resource;
22 public class IncludeElementProcessor : AbstractXmlNodeProcessor
24 public IncludeElementProcessor()
28 public override String Name
30 get { return "include"; }
33 /// <summary>
34 /// Accepts the specified node.
35 /// Check if node has the same name as the processor and the node.NodeType
36 /// is in the AcceptNodeTypes List
37 /// NOTE: since the BatchRegistrationFacility already uses an include
38 /// element we will distringish between both by looking for the presence of an uri attribute
39 /// we should revisit this later by using xml-namespaces
40 /// </summary>
41 /// <param name="node">The node.</param>
42 /// <returns></returns>
43 public override bool Accept(XmlNode node)
45 return node.Attributes.GetNamedItem("uri") != null && base.Accept(node);
48 public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine)
50 XmlElement element = nodeList.Current as XmlElement;
52 XmlNode result = ProcessInclude(element, element.GetAttribute("uri"), engine);
54 ReplaceItself(result, element);
57 public XmlNode ProcessInclude(XmlElement element, String includeUri, IXmlProcessorEngine engine)
59 XmlDocumentFragment frag = null;
61 if (includeUri == null)
63 throw new ConfigurationProcessingException(
64 String.Format("Found an include node without an 'uri' attribute: {0}", element.BaseURI));
67 String[] uriList = includeUri.Split(',');
68 frag = CreateFragment(element);
70 foreach(String uri in uriList)
72 using(IResource resource = engine.GetResource(uri))
74 XmlDocument doc = new XmlDocument();
76 try
78 doc.Load(resource.GetStreamReader());
80 catch(Exception ex)
82 throw new ConfigurationProcessingException(
83 String.Format("Error processing include node: {0}", includeUri), ex);
86 engine.PushResource(resource);
88 engine.DispatchProcessAll(new DefaultXmlProcessorNodeList(doc.DocumentElement));
90 engine.PopResource();
92 if (element.GetAttribute("preserve-wrapper") == "yes")
94 AppendChild(frag, doc.DocumentElement);
96 else
98 AppendChild(frag, doc.DocumentElement.ChildNodes);
103 return frag;