Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / AspectSharp / AspectSharp.Lang / AST / Visitors / XmlTreeVisitor.cs
blob6c79f1000785a3ca11bcdce8c10f8bc0909346a7
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 AspectSharp.Lang.AST.Visitors
17 using System;
18 using System.Xml;
19 using System.Collections;
21 /// <summary>
22 /// Summary description for PrintTreeVisitor.
23 /// </summary>
24 public class XmlTreeVisitor : DepthFirstVisitor
26 private XmlDocument _document;
27 private Stack _nodes = new Stack();
29 public XmlTreeVisitor()
31 _document = new XmlDocument();
34 private void Push( XmlNode node )
36 if (Current != null)
38 Current.AppendChild( node );
40 else
42 _document.AppendChild(node);
45 _nodes.Push(node);
48 private void Pop()
50 XmlNode node = (XmlNode) _nodes.Pop();
53 private XmlNode Current
55 get { return _nodes.Count != 0 ? _nodes.Peek() as XmlNode : _document.DocumentElement; }
58 public XmlDocument Document
60 get { return _document; }
63 public override void OnEngineConfiguration(EngineConfiguration conf)
65 Push( Document.CreateNode(XmlNodeType.Element, "configuration", null) );
66 base.OnEngineConfiguration (conf);
67 Pop();
70 public override void OnGlobalInterceptorDeclaration(NodeCollectionBase dict)
72 if (dict.Count == 0) return;
74 Push( Document.CreateNode(XmlNodeType.Element, "interceptors", null) );
75 base.OnGlobalInterceptorDeclaration(dict);
76 Pop();
79 public override void OnGlobalMixinDeclaration(NodeCollectionBase dict)
81 if (dict.Count == 0) return;
83 Push( Document.CreateNode(XmlNodeType.Element, "mixins", null) );
84 base.OnGlobalMixinDeclaration(dict);
85 Pop();
88 public override void OnAspectDefinition(AspectDefinition aspect)
90 Push( Document.CreateNode(XmlNodeType.Element, "aspect", null) );
92 XmlAttribute att = Document.CreateAttribute("name");
93 att.Value = aspect.Name;
94 Current.Attributes.Append( att );
96 Push( Document.CreateNode(XmlNodeType.Element, "for", null) );
97 SerializeTargetType( aspect.TargetType );
98 Pop();
100 base.OnAspectDefinition (aspect);
101 Pop();
104 public override void OnImportDirective(ImportDirective import)
106 Push( Document.CreateNode(XmlNodeType.Element, "import", null) );
108 XmlAttribute att = Document.CreateAttribute("namespace");
109 att.Value = import.Namespace;
110 Current.Attributes.Append( att );
112 SerializeAssemblyReference( import.AssemblyReference );
114 Pop();
117 public override void OnInterceptorDefinition(InterceptorDefinition interceptor)
119 Push( Document.CreateNode(XmlNodeType.Element, "interceptor", null) );
120 SerializeTypeReference(interceptor.TypeReference);
121 Pop();
124 public override void OnMixinDefinition(MixinDefinition mixin)
126 Push( Document.CreateNode(XmlNodeType.Element, "mixin", null) );
127 SerializeTypeReference(mixin.TypeReference);
128 Pop();
131 public override void OnInterceptorEntryDefinition(InterceptorEntryDefinition interceptor)
133 Push( Document.CreateNode(XmlNodeType.Element, "interceptor", null) );
134 XmlAttribute att = Document.CreateAttribute("key");
135 att.Value = interceptor.Key;
136 Current.Attributes.Append( att );
137 SerializeTypeReference(interceptor.TypeReference);
138 Pop();
141 public override void OnMixinEntryDefinition(MixinEntryDefinition mixin)
143 Push( Document.CreateNode(XmlNodeType.Element, "mixin", null) );
144 XmlAttribute att = Document.CreateAttribute("key");
145 att.Value = mixin.Key;
146 Current.Attributes.Append( att );
147 SerializeTypeReference(mixin.TypeReference);
148 Pop();
151 public override void OnPointCutDefinition(PointCutDefinition pointcut)
153 Push( Document.CreateNode(XmlNodeType.Element, "pointcut", null) );
155 XmlAttribute att = Document.CreateAttribute("symbol");
156 att.Value = pointcut.Flags.ToString();
157 Current.Attributes.Append( att );
159 Push( Document.CreateNode(XmlNodeType.Element, "signature", null) );
160 Current.InnerText = pointcut.Method.ToString();
161 Pop();
163 base.OnPointCutDefinition (pointcut);
164 Pop();
167 private void SerializeTargetType( TargetTypeDefinition def )
169 Push( Document.CreateNode(XmlNodeType.Element, "singletype", null) );
170 SerializeTypeReference( def.SingleType );
171 Pop();
174 private void SerializeTypeReference(TypeReference def)
176 XmlAttribute att = Document.CreateAttribute("type");
178 if (def.TargetType == TargetTypeEnum.Link)
180 att.Value = def.LinkRef;
182 else
184 att.Value = def.TypeName;
186 Current.Attributes.Append( att );
188 att = Document.CreateAttribute("refTypeEnum");
189 att.Value = def.TargetType.ToString();
190 Current.Attributes.Append( att );
192 SerializeAssemblyReference(def.AssemblyReference);
195 private void SerializeAssemblyReference(AssemblyReference def)
197 if (def == null) return;
199 XmlAttribute att = Document.CreateAttribute("assembly");
200 att.Value = def.AssemblyName;
201 Current.Attributes.Append( att );