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 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; }
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
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
;
57 /// Convert and return child parameter into an XmlElement
58 /// An exception will be throw in case the child node cannot be converted
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
;
69 throw new XmlProcessorException("{0} expects XmlElement found {1}", element
.Name
, child
.NodeType
);
75 protected String
GetRequiredAttribute(XmlElement element
, String attribute
)
77 String attValue
= element
.GetAttribute(attribute
).Trim();
81 throw new XmlProcessorException("'{0}' requires a non empty '{1}' attribute", element
.Name
, attribute
);
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]);