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
.XmlProcessor
.ElementProcessors
20 using Castle
.Core
.Resource
;
22 public class IncludeElementProcessor
: AbstractXmlNodeProcessor
24 public IncludeElementProcessor()
28 public override String Name
30 get { return "include"; }
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
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();
78 doc
.Load(resource
.GetStreamReader());
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
));
92 if (element
.GetAttribute("preserve-wrapper") == "yes")
94 AppendChild(frag
, doc
.DocumentElement
);
98 AppendChild(frag
, doc
.DocumentElement
.ChildNodes
);