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 AspectSharp
.Lang
.AST
.Visitors
19 using System
.Collections
;
22 /// Summary description for PrintTreeVisitor.
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
)
38 Current
.AppendChild( node
);
42 _document
.AppendChild(node
);
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
);
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
);
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
);
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
);
100 base.OnAspectDefinition (aspect
);
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
);
117 public override void OnInterceptorDefinition(InterceptorDefinition interceptor
)
119 Push( Document
.CreateNode(XmlNodeType
.Element
, "interceptor", null) );
120 SerializeTypeReference(interceptor
.TypeReference
);
124 public override void OnMixinDefinition(MixinDefinition mixin
)
126 Push( Document
.CreateNode(XmlNodeType
.Element
, "mixin", null) );
127 SerializeTypeReference(mixin
.TypeReference
);
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
);
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
);
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();
163 base.OnPointCutDefinition (pointcut
);
167 private void SerializeTargetType( TargetTypeDefinition def
)
169 Push( Document
.CreateNode(XmlNodeType
.Element
, "singletype", null) );
170 SerializeTypeReference( def
.SingleType
);
174 private void SerializeTypeReference(TypeReference def
)
176 XmlAttribute att
= Document
.CreateAttribute("type");
178 if (def
.TargetType
== TargetTypeEnum
.Link
)
180 att
.Value
= def
.LinkRef
;
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
);