Merge branch 'cecil-light'
[mono.git] / mcs / class / System.Web / System.Web.Compilation / ThemeDirectoryCompiler.cs
1 //
2 // System.Web.UI.ThemeDirectoryCompiler
3 //
4 // Authors:
5 //   Chris Toshok (toshok@ximian.com)
6 //
7 // (C) 2006-2009 Novell, Inc. (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.IO;
34 using System.Web;
35 using System.Web.Compilation;
36 using System.Web.Util;
37
38 namespace System.Web.UI
39 {
40         sealed class ThemeDirectoryCompiler
41         {
42                 public static Type GetCompiledType (string theme, HttpContext context)
43                 {
44                         string virtualPath = "~/App_Themes/" + theme + "/";
45                         string physicalPath = context.Request.MapPath (virtualPath);
46                         if (!Directory.Exists (physicalPath))
47                                 throw new HttpException (String.Format ("Theme '{0}' cannot be found in the application or global theme directories.", theme));
48                         string [] skin_files = Directory.GetFiles (physicalPath, "*.skin");
49
50                         PageThemeParser ptp = new PageThemeParser (new VirtualPath (virtualPath), context);
51                         
52                         string[] css_files = Directory.GetFiles (physicalPath, "*.css");
53                         string[] css_urls = new string[css_files.Length];
54                         for (int i = 0; i < css_files.Length; i++) {
55                                 ptp.AddDependency (css_files [i]);
56                                 css_urls [i] = virtualPath + Path.GetFileName (css_files [i]);
57                         }
58
59                         Array.Sort (css_urls, StringComparer.OrdinalIgnoreCase);
60                         ptp.LinkedStyleSheets = css_urls;
61                         
62                         AspComponentFoundry shared_foundry = new AspComponentFoundry ();
63                         ptp.RootBuilder = new RootBuilder ();
64
65                         string skin_file_url;
66                         for (int i = 0; i < skin_files.Length; i ++) {
67                                 skin_file_url = VirtualPathUtility.Combine (virtualPath, Path.GetFileName (skin_files [i]));
68                                 PageThemeFileParser ptfp = new PageThemeFileParser (new VirtualPath (skin_file_url),
69                                                                                     skin_files[i],
70                                                                                     context);
71
72                                 ptp.AddDependency (skin_files [i]);
73                                 AspGenerator gen = new AspGenerator (ptfp);
74                                 ptfp.RootBuilder.Foundry = shared_foundry;
75                                 gen.Parse ();
76
77                                 if (ptfp.RootBuilder.Children != null)
78                                         foreach (object o in ptfp.RootBuilder.Children) {
79                                                 if (!(o is ControlBuilder))
80                                                         continue;
81                                                 ptp.RootBuilder.AppendSubBuilder ((ControlBuilder)o);
82                                         }
83
84                                 foreach (string ass in ptfp.Assemblies)
85                                         if (!ptp.Assemblies.Contains (ass))
86                                                 ptp.AddAssemblyByFileName (ass);
87                         }
88
89                         PageThemeCompiler compiler = new PageThemeCompiler (ptp);
90                         return compiler.GetCompiledType ();
91                 }
92
93                 public static PageTheme GetCompiledInstance (string theme, HttpContext context)
94                 {
95                         Type t = ThemeDirectoryCompiler.GetCompiledType (theme, context);
96                         if (t == null)
97                                 return null;
98
99                         PageTheme pt = (PageTheme)Activator.CreateInstance (t);
100                         return pt;
101                 }
102         }
103 }
104