merge -r 58784:58785
[mono.git] / mcs / class / System.Web / System.Web.UI / TemplateParser.cs
old mode 100755 (executable)
new mode 100644 (file)
index 582ad06..8a82aca
@@ -6,19 +6,42 @@
 //     Gonzalo Paniagua Javier (gonzalo@ximian.com)
 //
 // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
 //
-using System;
+// 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.CodeDom.Compiler;
 using System.Collections;
 using System.IO;
 using System.Reflection;
-using System.Web;
+using System.Security.Permissions;
 using System.Web.Compilation;
 using System.Web.Configuration;
 using System.Web.Util;
 
-namespace System.Web.UI
-{
+namespace System.Web.UI {
+
+       // CAS
+       [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        public abstract class TemplateParser : BaseParser
        {
                string inputFile;
@@ -37,12 +60,15 @@ namespace System.Web.UI
                bool debug;
                string compilerOptions;
                string language;
+               bool strictOn = false;
+               bool explicitOn = false;
                bool output_cache;
                int oc_duration;
                string oc_header, oc_custom, oc_param, oc_controls;
                bool oc_shared;
                OutputCacheLocation oc_location;
                Assembly srcAssembly;
+               int appAssemblyIndex = -1;
                 
                internal TemplateParser ()
                {
@@ -62,7 +88,8 @@ namespace System.Web.UI
                        imports.Add ("System.Web.UI.HtmlControls");
 
                        assemblies = new ArrayList ();
-                       assemblies.AddRange (CompilationConfig.Assemblies);
+                       foreach (string a in CompilationConfig.Assemblies)
+                               AddAssemblyByName (a);
                        if (CompilationConfig.AssembliesInBin)
                                AddAssembliesInBin ();
 
@@ -72,8 +99,9 @@ namespace System.Web.UI
                internal void AddApplicationAssembly ()
                {
                        string location = Context.ApplicationInstance.AssemblyLocation;
-                       if (location != typeof (TemplateParser).Assembly.Location)
-                               assemblies.Add (location);
+                       if (location != typeof (TemplateParser).Assembly.Location) {
+                               appAssemblyIndex = assemblies.Add (location);
+                       }
                }
 
                protected abstract Type CompileIntoType ();
@@ -185,26 +213,26 @@ namespace System.Web.UI
                                                oc_custom = (string) entry.Value;
                                                break;
                                        case "location":
-                                               if (this is TemplateControlParser)
+                                               if (!(this is PageParser))
                                                        goto default;
 
                                                try {
                                                        oc_location = (OutputCacheLocation) Enum.Parse (
-                                                               typeof (OutputCacheLocation), (string) entry.Value);
+                                                               typeof (OutputCacheLocation), (string) entry.Value, true);
                                                } catch {
                                                        ThrowParseException ("The 'location' attribute is case sensitive and " +
                                                                        "must be one of the following values: Any, Client, " +
-                                                                       "Downstram, Server, None, ServerAndClient.");
+                                                                       "Downstream, Server, None, ServerAndClient.");
                                                }
                                                break;
                                        case "varybycontrol":
-                                               if (!(this is TemplateControlParser))
+                                               if (this is PageParser)
                                                        goto default;
 
                                                 oc_controls = (string) entry.Value;
                                                break;
                                        case "shared":
-                                               if (!(this is TemplateControlParser))
+                                               if (this is PageParser)
                                                        goto default;
 
                                                try {
@@ -231,51 +259,54 @@ namespace System.Web.UI
                internal Type LoadType (string typeName)
                {
                        // First try loaded assemblies, then try assemblies in Bin directory.
-                       // By now i do this 'by hand' but may be this is a runtime/gac task.
                        Type type = null;
+                       bool seenBin = false;
                        Assembly [] assemblies = AppDomain.CurrentDomain.GetAssemblies ();
                        foreach (Assembly ass in assemblies) {
                                type = ass.GetType (typeName);
-                               if (type != null) {
-                                       AddAssembly (ass, false);
-                                       AddDependency (ass.Location);
-                                       return type;
+                               if (type == null)
+                                       continue;
+
+                               if (Path.GetDirectoryName (ass.Location) != PrivateBinPath) {
+                                       AddAssembly (ass, true);
+                               } else {
+                                       seenBin = true;
                                }
+
+                               AddDependency (ass.Location);
+                               return type;
                        }
 
-                       return null;
-               }
+                       if (seenBin)
+                               return null;
 
-               void AddAssembliesInBin ()
-               {
+                       // Load from bin
                        if (!Directory.Exists (PrivateBinPath))
-                               return;
+                               return null;
 
                        string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
-                       foreach (string dll in binDlls) {
-                               Assembly assembly = Assembly.LoadFrom (dll);
-                               AddAssembly (assembly, true);
+                       foreach (string s in binDlls) {
+                               Assembly binA = Assembly.LoadFrom (s);
+                               type = binA.GetType (typeName);
+                               if (type == null)
+                                       continue;
+
+                               AddDependency (binA.Location);
+                               return type;
                        }
+
+                       return null;
                }
 
-               Assembly LoadAssemblyFromBin (string name)
+               void AddAssembliesInBin ()
                {
-                       Assembly assembly;
                        if (!Directory.Exists (PrivateBinPath))
-                               return null;
+                               return;
 
                        string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
-                       foreach (string dll in binDlls) {
-                               string fn = Path.GetFileName (dll);
-                               fn = Path.ChangeExtension (fn, null);
-                               if (fn != name)
-                                       continue;
-
-                               assembly = Assembly.LoadFrom (dll);
-                               return assembly;
+                       foreach (string s in binDlls) {
+                               assemblies.Add (s);
                        }
-
-                       return null;
                }
 
                internal virtual void AddInterface (string iface)
@@ -295,9 +326,21 @@ namespace System.Web.UI
                        if (!imports.Contains (namesp))
                                imports.Add (namesp);
                }
-               
+
+               internal virtual void AddSourceDependency (string filename)
+               {
+                       if (dependencies != null && dependencies.Contains (filename)) {
+                               ThrowParseException ("Circular file references are not allowed. File: " + filename);
+                       }
+
+                       AddDependency (filename);
+               }
+
                internal virtual void AddDependency (string filename)
                {
+                       if (filename == "")
+                               return;
+
                        if (dependencies == null)
                                dependencies = new ArrayList ();
 
@@ -307,6 +350,9 @@ namespace System.Web.UI
                
                internal virtual void AddAssembly (Assembly assembly, bool fullPath)
                {
+                       if (assembly.Location == "")
+                               return;
+
                        if (anames == null)
                                anames = new Hashtable ();
 
@@ -341,45 +387,62 @@ namespace System.Web.UI
                                return (Assembly) o;
                        }
 
-                       bool fullpath = true;
-                       Assembly assembly = LoadAssemblyFromBin (name);
-                       if (assembly != null) {
-                               AddAssembly (assembly, fullpath);
-                               return assembly;
+                       Assembly assembly = null;
+                       Exception error = null;
+                       if (name.IndexOf (',') != -1) {
+                               try {
+                                       assembly = Assembly.Load (name);
+                               } catch (Exception e) { error = e; }
                        }
 
-                       try {
-                               assembly = Assembly.Load (name);
-                               string loc = assembly.Location;
-                               fullpath = (Path.GetDirectoryName (loc) == PrivateBinPath);
-                       } catch (Exception e) {
-                               ThrowParseException ("Assembly " + name + " not found", e);
+                       if (assembly == null) {
+                               try {
+                                       assembly = Assembly.LoadWithPartialName (name);
+                               } catch (Exception e) { error = e; }
                        }
 
-                       AddAssembly (assembly, fullpath);
+                       if (assembly == null)
+                               ThrowParseException ("Assembly " + name + " not found", error);
+
+                       AddAssembly (assembly, true);
                        return assembly;
                }
 
                internal virtual void ProcessMainAttributes (Hashtable atts)
                {
                        atts.Remove ("Description"); // ignored
+#if NET_1_1
                        atts.Remove ("CodeBehind");  // ignored
+#endif
                        atts.Remove ("AspCompat"); // ignored
 
                        debug = GetBool (atts, "Debug", true);
-                       compilerOptions = GetString (atts, "CompilerOptions", null);
+                       compilerOptions = GetString (atts, "CompilerOptions", "");
                        language = GetString (atts, "Language", CompilationConfig.DefaultLanguage);
+                       strictOn = GetBool (atts, "Strict", CompilationConfig.Strict);
+                       explicitOn = GetBool (atts, "Explicit", CompilationConfig.Explicit);
+#if NET_2_0
+                       string src = GetString (atts, "CodeFile", null);
+#else
                        string src = GetString (atts, "Src", null);
+#endif
                        if (src != null)
                                srcAssembly = GetAssemblyFromSource (src);
 
                        string inherits = GetString (atts, "Inherits", null);
+#if NET_2_0
+                       if (srcAssembly == null)
+                               className = inherits;
+                       else
+                               SetBaseType (inherits);
+#else
                        if (inherits != null)
                                SetBaseType (inherits);
 
                        className = GetString (atts, "ClassName", null);
                        if (className != null && !CodeGenerator.IsValidLanguageIndependentIdentifier (className))
                                ThrowParseException (String.Format ("'{0}' is not valid for 'className'", className));
+#endif
 
                        if (atts.Count > 0)
                                ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
@@ -413,11 +476,10 @@ namespace System.Web.UI
                        if (!File.Exists (realPath))
                                ThrowParseException ("File " + vpath + " not found");
 
-                       AddDependency (realPath);
+                       AddSourceDependency (realPath);
 
-                       CompilerResults result = CachingCompiler.Compile (realPath, realPath, assemblies);
+                       CompilerResults result = CachingCompiler.Compile (language, realPath, realPath, assemblies);
                        if (result.NativeCompilerReturnValue != 0) {
-                               StringWriter writer = new StringWriter();
                                StreamReader reader = new StreamReader (realPath);
                                throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ());
                        }
@@ -495,7 +557,16 @@ namespace System.Web.UI
                }
 
                internal ArrayList Assemblies {
-                       get { return assemblies; }
+                       get {
+                               if (appAssemblyIndex != -1) {
+                                       object o = assemblies [appAssemblyIndex];
+                                       assemblies.RemoveAt (appAssemblyIndex);
+                                       assemblies.Add (o);
+                                       appAssemblyIndex = -1;
+                               }
+
+                               return assemblies;
+                       }
                }
 
                internal ArrayList Interfaces {
@@ -509,6 +580,7 @@ namespace System.Web.UI
 
                internal ArrayList Dependencies {
                        get { return dependencies; }
+                       set { dependencies = value; }
                }
 
                internal string CompilerOptions {
@@ -519,6 +591,14 @@ namespace System.Web.UI
                        get { return language; }
                }
 
+               internal bool StrictOn {
+                       get { return strictOn; }
+               }
+
+               internal bool ExplicitOn {
+                       get { return explicitOn; }
+               }
+               
                internal bool Debug {
                        get { return debug; }
                }
@@ -558,7 +638,6 @@ namespace System.Web.UI
                internal PagesConfiguration PagesConfig {
                        get { return PagesConfiguration.GetInstance (Context); }
                }
-                       
        }
 }