Implementation of the 2.0 session state model
[mono.git] / mcs / class / System.Web / System.Web.UI / TemplateParser.cs
index c99f839de2d80da3c50737dec3fedb6b0495f3d7..86679099ebdb35a35a95bb59e9b55508cd81d4f5 100644 (file)
@@ -30,6 +30,7 @@
 
 using System.CodeDom.Compiler;
 using System.Collections;
+using System.Globalization;
 using System.IO;
 using System.Reflection;
 using System.Security.Permissions;
@@ -67,6 +68,7 @@ namespace System.Web.UI {
                string oc_header, oc_custom, oc_param, oc_controls;
                bool oc_shared;
                OutputCacheLocation oc_location;
+               CultureInfo invariantCulture = CultureInfo.InvariantCulture;
 #if NET_2_0
                string src;
                string partialClassName;
@@ -77,6 +79,9 @@ namespace System.Web.UI {
                internal TemplateParser ()
                {
                        imports = new ArrayList ();
+#if NET_2_0
+                       AddNamespaces (imports);
+#else
                        imports.Add ("System");
                        imports.Add ("System.Collections");
                        imports.Add ("System.Collections.Specialized");
@@ -90,6 +95,7 @@ namespace System.Web.UI {
                        imports.Add ("System.Web.UI");
                        imports.Add ("System.Web.UI.WebControls");
                        imports.Add ("System.Web.UI.HtmlControls");
+#endif
 
                        assemblies = new ArrayList ();
 #if NET_2_0
@@ -115,9 +121,12 @@ namespace System.Web.UI {
 
                        language = CompilationConfig.DefaultLanguage;
                }
-
+               
                internal void AddApplicationAssembly ()
                {
+                       if (Context.ApplicationInstance == null)
+                                return; // this may happen if we have Global.asax and have
+                                        // controls registered from Web.Config
                        string location = Context.ApplicationInstance.AssemblyLocation;
                        if (location != typeof (TemplateParser).Assembly.Location) {
                                appAssemblyIndex = assemblies.Add (location);
@@ -126,6 +135,78 @@ namespace System.Web.UI {
 
                protected abstract Type CompileIntoType ();
 
+#if NET_2_0
+               void AddNamespaces (ArrayList imports)
+               {
+                       if (BuildManager.HaveResources)
+                               imports.Add ("System.Resources");
+                       
+                       PagesSection pages = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
+                       if (pages == null)
+                               return;
+
+                       NamespaceCollection namespaces = pages.Namespaces;
+                       if (namespaces == null || namespaces.Count == 0)
+                               return;
+
+                       foreach (NamespaceInfo nsi in namespaces)
+                               imports.Add (nsi.Namespace);
+               }
+               
+               internal void RegisterConfigControls ()
+               {
+                       PagesSection pages = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
+                       if (pages == null)
+                               return;
+
+                       TagPrefixCollection controls = pages.Controls;
+                       if (controls == null || controls.Count == 0)
+                               return;
+
+                       foreach (TagPrefixInfo tpi in controls) {
+                               if (!String.IsNullOrEmpty (tpi.TagName))
+                                       RegisterCustomControl (tpi.TagPrefix, tpi.TagName, tpi.Source);
+                               else if (!String.IsNullOrEmpty (tpi.Namespace))
+                                       RegisterNamespace (tpi.TagPrefix, tpi.Namespace, tpi.Assembly);
+                       }
+               }
+#endif
+               
+               internal void RegisterCustomControl (string tagPrefix, string tagName, string src)
+               {
+                       string realpath = MapPath (src);
+                       if (String.Compare (realpath, inputFile, false, invariantCulture) == 0)
+                               return;
+                       
+                       if (!File.Exists (realpath))
+                               throw new ParseException (Location, "Could not find file \"" + realpath + "\".");
+                       string vpath = UrlUtils.Combine (BaseVirtualDir, src);
+                       Type type = null;
+                       AddDependency (realpath);
+                       try {
+                               ArrayList other_deps = new ArrayList ();
+                               type = UserControlParser.GetCompiledType (vpath, realpath, other_deps, Context);
+                               foreach (string s in other_deps) {
+                                       AddDependency (s);
+                               }
+                       } catch (ParseException pe) {
+                               if (this is UserControlParser)
+                                       throw new ParseException (Location, pe.Message, pe);
+                               throw;
+                       }
+
+                       AddAssembly (type.Assembly, true);
+                       RootBuilder.Foundry.RegisterFoundry (tagPrefix, tagName, type);
+               }
+
+               internal void RegisterNamespace (string tagPrefix, string ns, string assembly)
+               {
+                       AddImport (ns);
+                       Assembly ass = AddAssemblyByName (assembly);
+                       AddDependency (ass.Location);
+                       RootBuilder.Foundry.RegisterFoundry (tagPrefix, ass, ns);
+               }
+               
                internal virtual void HandleOptions (object obj)
                {
                }
@@ -203,6 +284,10 @@ namespace System.Web.UI {
 
                        cmp = String.Compare ("OutputCache", directive, true);
                        if (cmp == 0) {
+                               HttpResponse response = HttpContext.Current.Response;
+                               if (response != null)
+                                       response.Cache.SetValidUntilExpires (true);
+                               
                                output_cache = true;
                                
                                if (atts ["Duration"] == null)