* Makefile.am:
[monodevelop.git] / main / src / addins / AspNetAddIn / MonoDevelop.AspNet.Parser / WebFormReferenceManager.cs
blobda663a2ec717ee5d22a01acd0183cd4b70d7e1aa
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 //
11 // This source code is licenced under The MIT License:
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.
33 using System;
34 using System.Globalization;
35 using System.Collections.Generic;
36 using System.Web;
37 using System.Web.UI;
38 using System.Web.UI.Design;
40 using MonoDevelop.AspNet.Parser.Dom;
41 using MonoDevelop.Projects;
42 using MonoDevelop.Ide.Gui;
44 namespace MonoDevelop.AspNet.Parser
46 public class WebFormReferenceManager : DocumentReferenceManager, IWebFormReferenceManager
48 int prefixIndex = 0;
50 public WebFormReferenceManager (Document parent)
51 : base (parent)
55 #region IWebFormReferenceManager Members
57 public Type GetObjectType (string tagPrefix, string typeName)
59 string fullName = GetTypeName (tagPrefix, typeName);
60 if (fullName == null)
61 throw new Exception (string.Format ("The tag type '{0}{1}{2}' has not been registered.", tagPrefix, string.IsNullOrEmpty(tagPrefix)? string.Empty:":", typeName));
62 Type type = System.Type.GetType (fullName, false);
63 if (type != null)
64 return type;
65 throw new NotImplementedException ("Cannot yet load custom types out-of-process.");
68 public string GetRegisterDirectives ()
70 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
72 foreach (RegisterDirective directive in pageRefsList) {
73 AssemblyRegisterDirective ard = directive as AssemblyRegisterDirective;
75 if (ard != null)
76 sb.AppendFormat ("<%@ Register {0}=\"{1}\" {2}=\"{3}\" {4}=\"{5}\" %>", "TagPrefix", ard.TagPrefix, "Namespace", ard.Namespace, "Assembly", ard.Assembly);
77 else {
78 ControlRegisterDirective crd = (ControlRegisterDirective) directive;
79 sb.AppendFormat ("<%@ Register {0}=\"{1}\" {2}=\"{3}\" {4}=\"{5}\" %>", "TagPrefix", crd.TagPrefix, "TagName", crd.TagName, "Src", crd.Src);
83 return sb.ToString ();
86 public string GetTagPrefix (Type objectType)
88 if (objectType.Namespace.StartsWith ("System.Web.UI"))
89 return "asp";
91 foreach (RegisterDirective directive in pageRefsList) {
92 AssemblyRegisterDirective ard = directive as AssemblyRegisterDirective;
94 if (ard != null)
95 if (string.Compare (ard.Namespace, objectType.Namespace, true, CultureInfo.InvariantCulture) == 0)
96 return directive.TagPrefix;
99 throw new Exception ("A tag prefix has not been registered for " + objectType.ToString ());
102 #endregion
104 #region Add/Remove references
106 public void AddReference (Type type)
108 RegisterTagPrefix (type);
111 public void AddReference (Type type, string prefix)
113 if (type.Assembly == typeof(System.Web.UI.WebControls.WebControl).Assembly)
114 return;
116 //check namespace is not already registered foreach (RegisterDirective directive in pageRefsList) {
117 AssemblyRegisterDirective ard = directive as AssemblyRegisterDirective;
118 if (0 == string.Compare (ard.Namespace, type.Namespace, false, CultureInfo.InvariantCulture))
119 throw new Exception ("That namespace is already registered with another prefix");
121 if (0 == string.Compare (directive.TagPrefix, prefix, true, CultureInfo.InvariantCulture)) {
122 //duplicate prefix; generate a new one.
123 //FIXME: possibility of stack overflow with too many default prefixes in existing document
124 AddReference (type);
125 return;
129 doc.Project.References.Add (
130 new ProjectReference (ReferenceType.Assembly, type.Assembly.ToString ()));
132 TODO: insert the reference into the document tree
136 #endregion
138 #region 2.0 WebFormsReferenceManager members
140 public string RegisterTagPrefix (Type type)
142 if (type.Assembly == typeof(System.Web.UI.WebControls.WebControl).Assembly)
143 return "asp";
145 string prefix = null;
147 //check if there's a prefix for this namespace in the assembly
148 TagPrefixAttribute[] atts = (TagPrefixAttribute[]) type.Assembly.GetCustomAttributes (typeof (TagPrefixAttribute), true);
149 foreach (TagPrefixAttribute tpa in atts)
150 if (0 == string.Compare (tpa.NamespaceName, type.Namespace, false, CultureInfo.InvariantCulture))
151 prefix = tpa.TagPrefix;
153 //generate default prefix
154 if (prefix == null) {
155 prefix = "cc" + prefixIndex.ToString ();
156 prefixIndex++;
159 AddReference (type, prefix);
161 return prefix;
164 public string GetUserControlPath (string tagPrefix, string tagName)
166 foreach (RegisterDirective directive in pageRefsList) {
167 ControlRegisterDirective crd = directive as ControlRegisterDirective;
169 if (crd != null)
170 if ((string.Compare (crd.TagPrefix, tagPrefix, true, CultureInfo.InvariantCulture) == 0)
171 && (string.Compare (crd.TagName, tagName, true, CultureInfo.InvariantCulture) == 0))
172 return crd.Src;
175 throw new Exception ("That tag has not been registered");
178 public Type GetType (string tagPrefix, string tagName)
180 return GetObjectType (tagPrefix, tagName);
183 #endregion 2.0 WebFormsReferenceManager members