~ubuntu-branches/ubuntu/feisty/monodevelop/feisty

« back to all changes in this revision

Viewing changes to Extras/AspNetAddIn/Parser/WebFormReferenceManager.cs

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2006-08-18 00:51:23 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060818005123-5iit07y0j7wjg55f
Tags: 0.11+svn20060818-0ubuntu1
* New SVN snapshot
  + Works with Gtk# 2.9.0
* debian/control:
  + Updated Build-Depends
* debian/patches/use_nunit2.2.dpatch,
  debian/patches/use_real_libs.dpatch:
  + Updated
* debian/patches/versioncontrol_buildfix.dpatch:
  + Fix build failure in the version control addin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// WebFormReferenceManager.cs: Tracks references within an ASP.NET document, and 
 
3
//     resolves types from tag names
 
4
//
 
5
// Authors:
 
6
//   Michael Hutchinson <m.j.hutchinson@gmail.com>
 
7
//
 
8
// Copyright (C) 2006 Michael Hutchinson
 
9
//
 
10
//
 
11
// This source code is licenced under The MIT License:
 
12
//
 
13
// Permission is hereby granted, free of charge, to any person obtaining
 
14
// a copy of this software and associated documentation files (the
 
15
// "Software"), to deal in the Software without restriction, including
 
16
// without limitation the rights to use, copy, modify, merge, publish,
 
17
// distribute, sublicense, and/or sell copies of the Software, and to
 
18
// permit persons to whom the Software is furnished to do so, subject to
 
19
// the following conditions:
 
20
// 
 
21
// The above copyright notice and this permission notice shall be
 
22
// included in all copies or substantial portions of the Software.
 
23
// 
 
24
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
25
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
26
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
27
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
28
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
29
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
30
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
31
//
 
32
 
 
33
using System;
 
34
using System.Globalization;
 
35
using System.Collections;
 
36
using System.Web;
 
37
using System.Web.UI;
 
38
using System.Web.UI.Design;
 
39
 
 
40
using AspNetAddIn.Parser.Tree;
 
41
using MonoDevelop.Projects;
 
42
 
 
43
namespace AspNetAddIn.Parser
 
44
{
 
45
        public class WebFormReferenceManager : IWebFormReferenceManager
 
46
        {
 
47
                int prefixIndex = 0;
 
48
                ArrayList list = new ArrayList ();
 
49
                Document doc;
 
50
                
 
51
                public WebFormReferenceManager (Document parent)
 
52
                {
 
53
                        this.doc = parent;      
 
54
                }
 
55
                
 
56
                #region IWebFormReferenceManager Members
 
57
 
 
58
                public Type GetObjectType (string tagPrefix, string typeName)
 
59
                {
 
60
                        if (tagPrefix == null || tagPrefix.Length < 1)
 
61
                                return typeof (System.Web.UI.HtmlControls.HtmlControl).Assembly.GetType ("System.Web.UI.HtmlControls.Html"+typeName, true, true);
 
62
                        
 
63
                        if (0 == string.Compare (tagPrefix, "asp", true, CultureInfo.InvariantCulture))
 
64
                                return typeof (System.Web.UI.WebControls.WebControl).Assembly.GetType ("System.Web.UI.WebControls."+typeName, true, true);
 
65
                        
 
66
                        foreach (RegisterDirective directive in list) {
 
67
                                AssemblyRegisterDirective ard = directive as AssemblyRegisterDirective;
 
68
                                if (ard != null) {
 
69
                                        //TODO: look up from parent project's type resolution
 
70
                                        string fullName = ard.Namespace + "." + typeName;
 
71
                                        /* MonoDevelop.Projects.Parser.IProjectParserContext cxt =
 
72
                                        MonoDevelop.Ide.Gui.IdeApp.ProjectOperations.ParserDatabase.GetProjectParserContext (pf.Project);
 
73
                                        MonoDevelop.Projects.Parser.IClass cls = cxt.GetClass (fullName); */
 
74
                                        
 
75
                                        //TODO: should load referenced assemblies out-of process, or in another appdomain
 
76
                                        
 
77
                                        System.Reflection.Assembly assem = System.Reflection.Assembly.Load (ard.Assembly);
 
78
                                        return assem.GetType (fullName, true, true);
 
79
                                }
 
80
                                
 
81
                                ControlRegisterDirective crd = directive as ControlRegisterDirective;
 
82
                                if (crd != null) {
 
83
                                        //TODO: UserControls - is this correct behaviour?
 
84
                                        ProjectFile pf = doc.ProjectFile.Project.GetProjectFile (crd.Src);
 
85
                                        if (pf != null) {
 
86
                                                string inheritsName = ((AspNetAppProject) pf.Project).GetDocument (pf).Info.InheritedClass;
 
87
                                                //TODO: look up from parent project's type resolution
 
88
                                                return Type.GetType (inheritsName, true, true);
 
89
                                        }
 
90
                                        return typeof (System.Web.UI.UserControl);
 
91
                                }
 
92
                        }
 
93
                        
 
94
                        throw new Exception ("The tag prefix \"" + tagPrefix + "\" has not been registered");
 
95
                }
 
96
                
 
97
                public string GetTypeName (string tagPrefix, string typeName)
 
98
                {
 
99
                        if (tagPrefix == null || tagPrefix.Length < 1)
 
100
                                return "System.Web.UI.HtmlControls."+typeName;
 
101
                        
 
102
                        if (0 == string.Compare (tagPrefix, "asp", true, CultureInfo.InvariantCulture))
 
103
                                return "System.Web.UI.WebControls."+typeName;
 
104
                        
 
105
                        foreach (RegisterDirective directive in list) {
 
106
                                AssemblyRegisterDirective ard = directive as AssemblyRegisterDirective;
 
107
                                if (ard != null)
 
108
                                        return ard.Namespace + "." + typeName;
 
109
                                
 
110
                                ControlRegisterDirective crd = directive as ControlRegisterDirective;
 
111
                                if (crd != null) {
 
112
                                        //TODO: UserControls - is this correct behaviour?
 
113
                                        ProjectFile pf = doc.ProjectFile.Project.GetProjectFile (crd.Src);
 
114
                                        if (pf != null)
 
115
                                                return crd.Src.Replace ('.', '_');
 
116
                                        else
 
117
                                                return "System.Web.UI.UserControl";
 
118
                                }
 
119
                        }
 
120
                        
 
121
                        throw new Exception ("The tag prefix \"" + tagPrefix + "\" has not been registered");
 
122
                }
 
123
 
 
124
                public string GetRegisterDirectives ()
 
125
                {
 
126
                        System.Text.StringBuilder sb = new System.Text.StringBuilder ();
 
127
                        
 
128
                        foreach (RegisterDirective directive in list) {
 
129
                                AssemblyRegisterDirective ard = directive as AssemblyRegisterDirective;
 
130
                                
 
131
                                if (ard != null)
 
132
                                        sb.AppendFormat ("<%@ Register {0}=\"{1}\" {2}=\"{3}\" {4}=\"{5}\" %>", "TagPrefix", ard.TagPrefix, "Namespace", ard.Namespace, "Assembly", ard.Assembly);
 
133
                                else {
 
134
                                        ControlRegisterDirective crd = (ControlRegisterDirective) directive;
 
135
                                        sb.AppendFormat ("<%@ Register {0}=\"{1}\" {2}=\"{3}\" {4}=\"{5}\" %>", "TagPrefix", crd.TagPrefix, "TagName", crd.TagName, "Src", crd.Src);
 
136
                                }
 
137
                        }
 
138
                        
 
139
                        return sb.ToString ();
 
140
                }
 
141
 
 
142
                public string GetTagPrefix (Type objectType)
 
143
                {
 
144
                        if (objectType.Namespace.StartsWith ("System.Web.UI"))
 
145
                                return "asp";
 
146
                        
 
147
                        foreach (RegisterDirective directive in list) {
 
148
                                AssemblyRegisterDirective ard = directive as AssemblyRegisterDirective;
 
149
                                
 
150
                                if (ard != null)
 
151
                                        if (string.Compare (ard.Namespace, objectType.Namespace, true, CultureInfo.InvariantCulture) == 0)
 
152
                                                return directive.TagPrefix;
 
153
                        }
 
154
                        
 
155
                        throw new Exception ("A tag prefix has not been registered for " + objectType.ToString ());
 
156
                }
 
157
 
 
158
                #endregion
 
159
                
 
160
                #region Add/Remove references
 
161
 
 
162
                public void AddReference (Type type)
 
163
                {
 
164
                        RegisterTagPrefix (type);                       
 
165
                }
 
166
 
 
167
                public void AddReference (Type type, string prefix)
 
168
                {
 
169
                        if (type.Assembly == typeof(System.Web.UI.WebControls.WebControl).Assembly)
 
170
                                return;
 
171
 
 
172
                        //check namespace is not already registered
 
 
b'\t\t\tforeach (RegisterDirective directive in list) {'
 
173
                                AssemblyRegisterDirective ard = directive as AssemblyRegisterDirective;
 
174
                                if (0 == string.Compare (ard.Namespace, type.Namespace, false, CultureInfo.InvariantCulture))
 
175
                                        throw new Exception ("That namespace is already registered with another prefix");
 
176
                                
 
177
                                if (0 == string.Compare (directive.TagPrefix, prefix, true, CultureInfo.InvariantCulture)) {
 
178
                                        //duplicate prefix; generate a new one.
 
179
                                        //FIXME: possibility of stack overflow with too many default prefixes in existing document
 
180
                                        AddReference (type);
 
181
                                        return;
 
182
                                }
 
183
                        }
 
184
                        
 
185
                        doc.ProjectFile.Project.ProjectReferences.Add (
 
186
                            new ProjectReference (ReferenceType.Assembly, type.Assembly.ToString ()));
 
187
                /*
 
188
                TODO: insert the reference into the document tree 
 
189
                */
 
190
                }
 
191
 
 
192
                #endregion
 
193
                
 
194
                #region 2.0 WebFormsReferenceManager members
 
195
                
 
196
                public string RegisterTagPrefix (Type type)
 
197
                {
 
198
                        if (type.Assembly == typeof(System.Web.UI.WebControls.WebControl).Assembly)
 
199
                                return "asp";
 
200
 
 
201
                        string prefix = null;
 
202
 
 
203
                        //check if there's a prefix for this namespace in the assembly
 
204
                        TagPrefixAttribute[] atts = (TagPrefixAttribute[]) type.Assembly.GetCustomAttributes (typeof (TagPrefixAttribute), true);
 
205
                        foreach (TagPrefixAttribute tpa in atts)
 
206
                                        if (0 == string.Compare (tpa.NamespaceName, type.Namespace, false, CultureInfo.InvariantCulture))
 
207
                                                prefix = tpa.TagPrefix;
 
208
                        
 
209
                        //generate default prefix
 
210
                        if (prefix == null) {
 
211
                                prefix = "cc" + prefixIndex.ToString ();
 
212
                                prefixIndex++;
 
213
                        }
 
214
                                
 
215
                        AddReference (type, prefix);
 
216
                        
 
217
                        return prefix;
 
218
                }
 
219
                
 
220
                public string GetUserControlPath (string tagPrefix, string tagName)
 
221
                {
 
222
                        foreach (RegisterDirective directive in list) {
 
223
                                ControlRegisterDirective crd = directive as ControlRegisterDirective;
 
224
                                
 
225
                                if (crd != null)
 
226
                                        if ((string.Compare (crd.TagPrefix, tagPrefix, true, CultureInfo.InvariantCulture) == 0)
 
227
                                                        && (string.Compare (crd.TagName, tagName, true, CultureInfo.InvariantCulture) == 0))
 
228
                                                return crd.Src;
 
229
                        }
 
230
                        
 
231
                        throw new Exception ("That tag has not been registered");
 
232
                }
 
233
                
 
234
                public Type GetType (string tagPrefix, string tagName)
 
235
                {
 
236
                        return GetObjectType (tagPrefix, tagName);
 
237
                }
 
238
                
 
239
                #endregion 2.0 WebFormsReferenceManager members
 
240
                
 
241
                #region extra utility members
 
242
                
 
243
                
 
244
                #endregion
 
245
                
 
246
                #region directive classes
 
247
                
 
248
                private abstract class RegisterDirective
 
249
                {
 
250
                        private DirectiveNode node;
 
251
                        
 
252
                        public RegisterDirective (DirectiveNode node)
 
253
                        {
 
254
                                this.node = node;
 
255
                        }
 
256
                        
 
257
                        public DirectiveNode Node {
 
258
                                get { return node; }
 
259
                        }
 
260
                        
 
261
                        public string TagPrefix {
 
262
                                get { return (string) node.Attributes ["TagPrefix"]; }
 
263
                                set { node.Attributes ["TagPrefix"] = value; }
 
264
                        }
 
265
                }
 
266
                
 
267
                private class AssemblyRegisterDirective : RegisterDirective
 
268
                {
 
269
                        public AssemblyRegisterDirective (DirectiveNode node)
 
270
                                : base (node)
 
271
                        {
 
272
                        }
 
273
                        
 
274
                        public string Namespace {
 
275
                                get { return (string) Node.Attributes ["Namespace"]; }
 
276
                                set { Node.Attributes ["Namespace"] = value; }
 
277
                        }
 
278
                        
 
279
                        public string Assembly {
 
280
                                get { return (string) Node.Attributes ["Assembly"]; }
 
281
                                set { Node.Attributes ["Assembly"] = value; }
 
282
                        }
 
283
                        
 
284
                        public override string ToString ()
 
285
                        {       
 
286
                                return String.Format ("<%@ Register {0}=\"{1}\" {2}=\"{3}\" {4}=\"{5}\" %>", "TagPrefix", TagPrefix, "Namespace", Namespace, "Assembly", Assembly);
 
287
                        }
 
288
                }
 
289
                
 
290
                private class ControlRegisterDirective : RegisterDirective
 
291
                {                       
 
292
                        public ControlRegisterDirective (DirectiveNode node)
 
293
                                : base (node)
 
294
                        {
 
295
                        }
 
296
                        
 
297
                        public string TagName {
 
298
                                get { return (string) Node.Attributes ["TagName"]; }
 
299
                                set { Node.Attributes ["TagName"] = value; }
 
300
                        }
 
301
                        
 
302
                        public string Src {
 
303
                                get { return (string) Node.Attributes ["Src"]; }
 
304
                                set { Node.Attributes ["Src"] = value; }
 
305
                        }
 
306
                        
 
307
                        public override string ToString ()
 
308
                        {       
 
309
                                return String.Format ("<%@ Register {0}=\"{1}\" {2}=\"{3}\" {4}=\"{5}\" %>", "TagPrefix", TagPrefix, "TagName", TagName, "Src", Src);
 
310
                        }
 
311
                }
 
312
                
 
313
                private class ReferenceVisitor : Visitor
 
314
                {
 
315
                        WebFormReferenceManager parent;
 
316
                        
 
317
                        public ReferenceVisitor (WebFormReferenceManager parent)
 
318
                        {
 
319
                                this.parent = parent;
 
320
                        }
 
321
                        
 
322
                        public override void Visit (DirectiveNode node)
 
323
                        {
 
324
                                if ((String.Compare (node.Name, "register", true) != 0) || (node.Attributes ["TagPrefix"] == null))
 
325
                                        return;
 
326
                                
 
327
                                if ((node.Attributes ["TagName"] != null) && (node.Attributes ["Src"] != null))
 
328
                                        parent.list.Add (new ControlRegisterDirective (node));
 
329
                                else if ((node.Attributes ["Namespace"] != null) && (node.Attributes ["Assembly"] != null))
 
330
                                        parent.list.Add (new AssemblyRegisterDirective (node));
 
331
                        }       
 
332
                }
 
333
                
 
334
                
 
335
                #endregion classes
 
336
        }
 
337
}