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
20 using Castle
.MicroKernel
.SubSystems
.Resource
;
21 using Castle
.Core
.Resource
;
23 using ElementProcessors
;
28 public class XmlProcessor
30 private IXmlProcessorEngine engine
;
33 /// Initializes a new instance of the <see cref="XmlProcessor"/> class.
35 public XmlProcessor() : this(null)
40 /// Initializes a new instance of the <see cref="XmlProcessor"/> class.
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
);
51 /// Initializes a new instance of the <see cref="XmlProcessor"/> class.
53 public XmlProcessor(string environmentName
)
55 engine
= new DefaultXmlProcessorEngine(environmentName
);
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
)
84 if (node
.NodeType
== XmlNodeType
.Document
)
86 node
= (node
as XmlDocument
).DocumentElement
;
89 engine
.DispatchProcessAll(new DefaultXmlProcessorNodeList(node
));
93 catch(ConfigurationProcessingException
)
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
)
111 XmlDocument doc
= new XmlDocument();
113 doc
.Load(resource
.GetStreamReader());
115 engine
.PushResource(resource
);
117 XmlNode element
= Process(doc
.DocumentElement
);
119 engine
.PopResource();
124 catch(ConfigurationProcessingException
)
130 String message
= String
.Format("Error processing node resource {0}", resource
);
132 throw new ConfigurationProcessingException(message
, ex
);