Added RedirectUsingNamedRoute
[castle.git] / InversionOfControl / Castle.Windsor / Configuration / Interpreters / XmlProcessor / ElementProcessors / AbstractXmlNodeProcessor.cs
blob1d006ac2213932aa332c7e56b70abe5de3a6a296
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 public abstract class AbstractXmlNodeProcessor : IXmlNodeProcessor
22 private static readonly XmlNodeType[] acceptNodes = new XmlNodeType[] {XmlNodeType.Element};
24 public AbstractXmlNodeProcessor()
28 public abstract String Name { get; }
30 public virtual XmlNodeType[] AcceptNodeTypes
32 get { return acceptNodes; }
35 /// <summary>
36 /// Accepts the specified node.
37 /// Check if node has the same name as the processor and the node.NodeType
38 /// is in the AcceptNodeTypes List
39 /// </summary>
40 /// <param name="node">The node.</param>
41 /// <returns></returns>
42 public virtual bool Accept(XmlNode node)
44 return node.Name == Name && Array.IndexOf(AcceptNodeTypes, node.NodeType) != -1;
47 public abstract void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine);
49 protected virtual bool IgnoreNode(XmlNode node)
51 return node.NodeType == XmlNodeType.Comment ||
52 node.NodeType == XmlNodeType.Entity ||
53 node.NodeType == XmlNodeType.EntityReference;
56 /// <summary>
57 /// Convert and return child parameter into an XmlElement
58 /// An exception will be throw in case the child node cannot be converted
59 /// </summary>
60 /// <param name="element">Parent node</param>
61 /// <param name="child">Node to be converted</param>
62 /// <returns>child node as XmlElement</returns>
63 protected XmlElement GetNodeAsElement(XmlElement element, XmlNode child)
65 XmlElement result = child as XmlElement;
67 if (result == null)
69 throw new XmlProcessorException("{0} expects XmlElement found {1}", element.Name, child.NodeType);
72 return result;
75 protected String GetRequiredAttribute(XmlElement element, String attribute)
77 String attValue = element.GetAttribute(attribute).Trim();
79 if (attValue == "")
81 throw new XmlProcessorException("'{0}' requires a non empty '{1}' attribute", element.Name, attribute);
84 return attValue;
87 protected XmlNode ImportNode(XmlNode targetElement, XmlNode node)
89 return targetElement.OwnerDocument == node.OwnerDocument ?
90 node : targetElement.OwnerDocument.ImportNode(node, true);
93 protected void AppendChild(XmlNode element, XmlNodeList nodes)
95 DefaultXmlProcessorNodeList childNodes = new DefaultXmlProcessorNodeList(nodes);
97 while(childNodes.MoveNext())
99 AppendChild(element, childNodes.Current);
103 protected void AppendChild(XmlNode element, string text)
105 AppendChild(element, CreateText(element, text));
108 protected void AppendChild(XmlNode element, XmlNode child)
110 element.AppendChild(ImportNode(element, child));
113 protected void ReplaceNode(XmlNode element, XmlNode newNode, XmlNode oldNode)
115 if (newNode == oldNode) return;
117 XmlNode importedNode = ImportNode(element, newNode);
119 element.ReplaceChild(importedNode, oldNode);
122 protected XmlText CreateText(XmlNode node, string content)
124 return node.OwnerDocument.CreateTextNode(content);
127 protected XmlDocumentFragment CreateFragment(XmlNode parentNode)
129 return parentNode.OwnerDocument.CreateDocumentFragment();
132 protected bool IsTextNode(XmlNode node)
134 return node.NodeType == XmlNodeType.Text || node.NodeType == XmlNodeType.CDATA;
137 protected void ReplaceItself(XmlNode newNode, XmlNode oldNode)
139 ReplaceNode(oldNode.ParentNode, newNode, oldNode);
142 protected void RemoveItSelf(XmlNode node)
144 node.ParentNode.RemoveChild(node);
147 protected void MoveChildNodes(XmlDocumentFragment fragment, XmlElement element)
149 while(element.ChildNodes.Count > 0)
151 fragment.AppendChild(element.ChildNodes[0]);