~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Core/MonoDevelop.Projects.Dom/DomOutputVisitor.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
Import upstream version 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// DomOutputVisitor.cs
 
3
//  
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@novell.com>
 
6
// 
 
7
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using System.Text;
 
28
using System.Linq;
 
29
 
 
30
namespace MonoDevelop.Projects.Dom
 
31
{
 
32
        /// <summary>
 
33
        /// DOM output visitor. Used for debug purposes to get a string out of a monodevelop dom object.
 
34
        /// </summary>
 
35
        public class DomOutputVisitor : AbstractDomVisitor<object, object>
 
36
        {
 
37
                StringBuilder output = new StringBuilder ();
 
38
                
 
39
                public string Output {
 
40
                        get {
 
41
                                return this.output.ToString ();
 
42
                        }
 
43
                }
 
44
                
 
45
                int indentLevel = 0;
 
46
                
 
47
                string CurIndent {
 
48
                        get {
 
49
                                return new string ('\t', indentLevel);
 
50
                        }
 
51
                }
 
52
                
 
53
                public static string GetOutput (IMember member)
 
54
                {
 
55
                        var visitor = new DomOutputVisitor ();
 
56
                        member.AcceptVisitor (visitor, null);
 
57
                        return visitor.Output;
 
58
                }
 
59
 
 
60
                
 
61
                public void OututReturnType (IReturnType returnType)
 
62
                {
 
63
                        if (returnType == null) {
 
64
                                output.Append ("<null>");
 
65
                                return;
 
66
                        }
 
67
                        output.Append (returnType.ToInvariantString ());
 
68
                }               
 
69
                
 
70
                public override object Visit (IAttribute attribute, object data)
 
71
                {
 
72
                        output.Append (CurIndent);
 
73
                        output.Append ("[");
 
74
                        output.Append (attribute.Name);
 
75
                        output.AppendLine ("]");
 
76
                        return null;
 
77
                }
 
78
 
 
79
                
 
80
                public void VisitAttributes (IMember member)
 
81
                {
 
82
                        foreach (var attr in member.Attributes)
 
83
                                attr.AcceptVisitor (this, null);
 
84
                }
 
85
                
 
86
                public override object Visit (IEvent evt, object data)
 
87
                {
 
88
                        VisitAttributes (evt);
 
89
                        output.Append (CurIndent);
 
90
                        output.Append (evt.Modifiers.ToString ());
 
91
                        output.Append (" event ");
 
92
                        OututReturnType (evt.ReturnType);
 
93
                        output.Append (" ");
 
94
                        output.Append (evt.Name);
 
95
                        output.AppendLine (";");
 
96
                        return null;
 
97
                }
 
98
                
 
99
                public override object Visit (IField field, object data)
 
100
                {
 
101
                        VisitAttributes (field);
 
102
                        output.Append (CurIndent);
 
103
                        output.Append (field.Modifiers.ToString ());
 
104
                        output.Append (" ");
 
105
                        OututReturnType (field.ReturnType);
 
106
                        output.Append (" ");
 
107
                        output.Append (field.Name);
 
108
                        output.AppendLine (";");
 
109
                        return null;
 
110
                }
 
111
                
 
112
                public override object Visit (IMethod method, object data)
 
113
                {
 
114
                        VisitAttributes (method);
 
115
                        output.Append (CurIndent);
 
116
                        output.Append (method.Modifiers.ToString ());
 
117
                        output.Append (" ");
 
118
                        OututReturnType (method.ReturnType);
 
119
                        output.Append (" ");
 
120
                        output.Append (method.Name);
 
121
                        output.AppendLine (" { }");
 
122
                        return null;
 
123
                }
 
124
                
 
125
                public override object Visit (IProperty property, object data)
 
126
                {
 
127
                        VisitAttributes (property);
 
128
                        output.Append (CurIndent);
 
129
                        output.Append (property.Modifiers.ToString ());
 
130
                        output.Append (" ");
 
131
                        OututReturnType (property.ReturnType);
 
132
                        output.Append (" ");
 
133
                        output.Append (property.Name);
 
134
                        output.Append (" {");
 
135
                        if (property.HasGet)
 
136
                                output.Append (" get;");
 
137
                        if (property.HasSet)
 
138
                                output.Append (" set;");
 
139
                        output.AppendLine ("}");
 
140
                        return null;
 
141
                }
 
142
                
 
143
                public override object Visit (IType type, object data)
 
144
                {
 
145
                        VisitAttributes (type);
 
146
                        output.Append (CurIndent);
 
147
                        output.Append (type.Modifiers.ToString ());
 
148
                        output.Append (" ");
 
149
                        switch (type.ClassType) {
 
150
                        case ClassType.Class:
 
151
                                output.Append ("class");
 
152
                                break;
 
153
                        case ClassType.Struct:
 
154
                                output.Append ("struct");
 
155
                                break;
 
156
                        case ClassType.Delegate:
 
157
                                output.Append ("delegate");
 
158
                                break;
 
159
                        case ClassType.Enum:
 
160
                                output.Append ("enum");
 
161
                                break;
 
162
                        case ClassType.Interface:
 
163
                                output.Append ("interface");
 
164
                                break;
 
165
                        }
 
166
                        output.Append (" ");
 
167
                        output.Append (type.Name);
 
168
                        if (type.BaseTypes.Any ()) {
 
169
                                output.Append (" : ");
 
170
                                foreach (var baseType in type.BaseTypes) {
 
171
                                        output.Append (baseType.ToInvariantString ());
 
172
                                        output.Append (",");
 
173
                                }
 
174
                        }
 
175
                        output.AppendLine (" {");
 
176
                        indentLevel++;
 
177
                        foreach (var member in type.Members)
 
178
                                member.AcceptVisitor (this, null);
 
179
                        indentLevel--;
 
180
                        output.Append (CurIndent);
 
181
                        output.AppendLine ("}");
 
182
                        return null;
 
183
                }
 
184
        }
 
185
}
 
186