Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WebFormsRegistrationCache.cs « WebForms « AspNet « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ac1a6ccb6c109048c32c76926db5587b274db5a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// 
// ProjectRegisteredControlList.cs
// 
// Author:
//   Michael Hutchinson <mhutchinson@novell.com>
// 
// Copyright (C) 2009 Novell, Inc (http://www.novell.com)
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Linq;

using MonoDevelop.Core;
using MonoDevelop.Projects;
using MonoDevelop.AspNet.Projects;

namespace MonoDevelop.AspNet.WebForms
{	
	class WebFormsRegistrationCache : ProjectFileCache<AspNetAppProject,RegistrationInfo>
	{
		public WebFormsRegistrationCache (AspNetAppProject project) : base (project)
		{
		}
		
		public IList<RegistrationInfo> GetInfosForPath (FilePath dir)
		{
			var infos = new List<RegistrationInfo> ();
			var projectRootParent = Project.BaseDirectory.ParentDirectory;
			while (dir != null && dir.IsChildPathOf (projectRootParent)) {
				var conf = dir.Combine ("web.config");
				var reg = Get (conf);
				if (reg == null) {
					conf = dir.Combine ("Web.config");
					reg = Get (conf);
				}
				if (reg != null)
					infos.Add (reg);
				dir = dir.ParentDirectory;
			}
			infos.Add (MachineRegistrationInfo);
			return infos;
		}
		
		protected override RegistrationInfo GenerateInfo (string filename)
		{
			try {
				using (var reader = new XmlTextReader (filename) { WhitespaceHandling = WhitespaceHandling.None }) {
					var doc = XDocument.Load (reader);
					return ReadFrom (filename, doc);
				}
			} catch (Exception ex) {
				LoggingService.LogError ("Error reading registration info from file '" + filename + "'", ex);
			}
			return null;
		}
		
		static RegistrationInfo ReadFrom (string filename, XDocument doc)
		{
			var info = new RegistrationInfo ();
			
			if (doc.Root.Name != "configuration")
				return info;
				
			var systWeb = doc.Root.Element ("system.web");
			if (systWeb == null)
				return info;
			
			var pages = systWeb.Element ("pages");
			if (pages != null) {
				var controls = pages.Element ("controls");
				if (controls != null) {
					foreach (var element in controls.Elements ()) {
						bool add = element.Name == "add";
						if (add || element.Name == "remove")
							info.Controls.Add (new ControlRegistration (filename, add,
								(string) element.Attribute ("tagPrefix"),
								(string) element.Attribute ("namespace"),
								(string) element.Attribute ("assembly"),
								(string) element.Attribute ("tagName"),
								(string) element.Attribute ("src")));
					}
				}
				var namespaces = pages.Element ("namespaces");
				if (namespaces != null) {
					foreach (var element in namespaces.Elements ()) {
						bool add = element.Name == "add";
						if (add || element.Name == "remove")
							info.Namespaces.Add (new NamespaceRegistration (add, (string) element.Attribute ("namespace")));
					}
				}
			}
			
			var compilation = systWeb.Element ("compilation");
			if (compilation != null) {
				var assemblies = compilation.Element ("assemblies");
				if (assemblies != null) {
					foreach (var element in assemblies.Elements ()) {
						bool add = element.Name == "add";
						if (add || element.Name == "remove")
							info.Assemblies.Add (new AssemblyRegistration (add, (string) element.Attribute ("assembly")));
					}
				}
			}
			return info;
		}

		
		//FIXME: add more default values, controls, etc. make version dependent.
		//FIXME: use actual machine web.config?
		static RegistrationInfo GetMachineInfo ()
		{
			var info = new RegistrationInfo ();

 			info.Namespaces.AddRange (defaultNamespaces.Select (ns => new NamespaceRegistration (true, ns)));
			info.Assemblies.AddRange (defaultAssemblies.Select (asm => new AssemblyRegistration (true, asm)));
 			
 			return info;
 		}

		static readonly string[] defaultAssemblies = {
			"mscorlib", 
			"System",
			"System.Configuration",
			"System.Web",
			"System.Data",
			"System.Web.Services",
			"System.Xml",
			"System.Drawing",
			"System.EnterpriseServices",
			"System.Web.Mobile",
		};

		//see http://msdn.microsoft.com/en-us/library/eb44kack.aspx
		static readonly string[] defaultNamespaces = {
			"System",
			"System.Collections",
			"System.Collections.Specialized",
			"System.Configuration",
			"System.Text",
			"System.Text.RegularExpressions",
			"System.Web",
			"System.Web.Caching",
			"System.Web.Profile",
			"System.Web.Security",
			"System.Web.SessionState",
			"System.Web.UI",
			"System.Web.UI.HtmlControls",
			"System.Web.UI.WebControls",
			"System.Web.UI.WebControls.WebParts",
		};

		public static bool IsDefaultReference (string reference)
		{
			return defaultAssemblies.Any (r =>
				reference.StartsWith (r, StringComparison.Ordinal) &&
				(reference.Length == r.Length || reference [r.Length] == ',')
			);
		}

		public static readonly RegistrationInfo MachineRegistrationInfo = GetMachineInfo ();
	}
	
	class RegistrationInfo
	{
		public RegistrationInfo ()
		{
			this.Controls = new List<ControlRegistration> ();
			this.Namespaces = new List<NamespaceRegistration> ();
			this.Assemblies = new List<AssemblyRegistration> ();
		}		
		
		public List<NamespaceRegistration> Namespaces { get; private set; }
		public List<ControlRegistration> Controls { get; private set; }
		public List<AssemblyRegistration> Assemblies { get; private set; }
	}
	
	class NamespaceRegistration
	{
		public NamespaceRegistration (bool add, string name)
		{
			this.Add = add;
			this.Namespace = name;
		}
		
		public bool Add { get; private set; }
		public string Namespace { get; private set; }
	}
	
	class AssemblyRegistration
	{
		public AssemblyRegistration (bool add, string name)
		{
			this.Add = add;
			this.Name = name;
		}
		
		public bool Add { get; private set; }
		public string Name { get; private set; }
	}
	
	class ControlRegistration
	{
		public bool Add { get; private set; }
		public string TagPrefix { get; private set; }
		public string Namespace { get; private set; }
		public string Assembly { get; private set; }
		public string TagName { get; private set; }
		public string Source { get; private set; }
		public string ConfigFile { get; private set; }
		
		public bool IsAssembly {
			get {
				return !string.IsNullOrEmpty (Assembly) && !string.IsNullOrEmpty (TagPrefix) && !string.IsNullOrEmpty (Namespace);
			}
		}
		
		public bool IsUserControl {
			get {
				return !string.IsNullOrEmpty (TagName) && !string.IsNullOrEmpty (TagPrefix) && !string.IsNullOrEmpty (Source);
			}
		}
		
		public bool PrefixMatches (string prefix)
		{
			 return 0 == string.Compare (TagPrefix, prefix, StringComparison.OrdinalIgnoreCase);
		}
		
		public bool NameMatches (string name)
		{
			 return 0 == string.Compare (TagName, name, StringComparison.OrdinalIgnoreCase);
		}
		
		public ControlRegistration (string configFile, bool add, string tagPrefix, string _namespace, 
		                            string assembly, string tagName, string src)
		{
			Add = add;
			ConfigFile = configFile;
			TagPrefix = tagPrefix;
			Namespace = _namespace;
			Assembly = assembly;
			TagName = tagName;
			Source = src;
		}
	}
	
	/// <summary>
	/// Caches items for filename keys. Files may not exist, which doesn't matter.
	/// When a project file with that name is cached in any way, the cache item will be flushed.
	/// </summary>
	/// <remarks>Not safe for multithreaded access.</remarks>
	abstract class ProjectFileCache<T,U> : IDisposable where T : Project
	{
		protected T Project { get; private set; }
		
		Dictionary<string, U> cache;
		
		/// <summary>Creates a ProjectFileCache</summary>
		/// <param name="project">The project the cache is bound to</param>
		protected ProjectFileCache (T project)
		{
			this.Project = project;
			cache =  new Dictionary<string, U> ();
			Project.FileChangedInProject += FileChangedInProject;
			Project.FileRemovedFromProject += FileChangedInProject;
			Project.FileAddedToProject += FileChangedInProject;
			Project.FileRenamedInProject += FileRenamedInProject;
		}

		void FileRenamedInProject (object sender, ProjectFileRenamedEventArgs args)
		{
			foreach (ProjectFileRenamedEventInfo e in args)
				cache.Remove (e.OldName);
		}

		void FileChangedInProject (object sender, ProjectFileEventArgs args)
		{
			foreach (ProjectFileEventInfo e in args)
				cache.Remove (e.ProjectFile.Name);
		}
		
		/// <summary>
		/// Queries the cache for an item. If the file does not exist in the project, returns null.
		/// </summary>
		protected U Get (string filename)
		{
			U value;
			if (cache.TryGetValue (filename, out value))
				return value;
			
			var pf = Project.GetProjectFile (filename);
			if (pf != null)
				value = GenerateInfo (filename);
			
			return cache[filename] = value;
		}
		
		/// <summary>
		/// Detaches from the project's events.
		/// </summary>
		public void Dispose ()
		{
			Project.FileChangedInProject -= FileChangedInProject;
			Project.FileRemovedFromProject -= FileChangedInProject;
			Project.FileAddedToProject -= FileChangedInProject;
			Project.FileRenamedInProject -= FileRenamedInProject;
		}
		
		/// <summary>
		/// Generates info for a given filename.
		/// </summary>
		/// <returns>Null if no info could be generated for the requested filename, e.g. if it did not exist.</returns>
		protected abstract U GenerateInfo (string filename);
	}
}