Преглед на файлове

2007-03-13 Marek Habersack <[email protected]>

        * AppResourceFilesCollection.cs: added separate constructor for
        local resources handling.

        * TemplateControlCompiler.cs: request the local resource object
        with proper virtual path.

        * BaseCompiler.cs: added code to assing AppRelativeVirtualPath
        property in the page/control constructor.

        * AppResourcesCompiler.cs: does not require specifying manually
        whether it's a global or local resource compiler anymore. New
        constructors take care of that.
        Changed to compile local resources on demand, when a control/page
        is parsed.

2007-03-13  Marek Habersack  <[email protected]>

        * AppResourceFilesCollection.cs: added separate constructor for
        local resources handling.

        * TemplateControlCompiler.cs: request the local resource object
        with proper virtual path.

        * BaseCompiler.cs: added code to assing AppRelativeVirtualPath
        property in the page/control constructor.

        * AppResourcesCompiler.cs: does not require specifying manually
        whether it's a global or local resource compiler anymore. New
        constructors take care of that.
        Changed to compile local resources on demand, when a control/page
        is parsed.

2007-03-13  Marek Habersack  <[email protected]>

        * TemplateControl.cs: implement AppRelativeVirtualPath. Closes bug
        #80634.


svn path=/trunk/mcs/; revision=74159
Marek Habersack преди 19 години
родител
ревизия
095b2a7004

+ 16 - 11
mcs/class/System.Web/System.Web.Compilation/AppResourceFilesCollection.cs

@@ -96,28 +96,33 @@ namespace System.Web.Compilation
 			get { return files; }
 		}
 		
-		public AppResourceFilesCollection (HttpContext context, bool isGlobal)
+		public AppResourceFilesCollection (HttpContext context)
 		{
 			if (context == null)
 				throw new ArgumentNullException ("context");
 			
-			//this.context = context;
-			this.isGlobal = isGlobal;
+			this.isGlobal = true;
 			this.files = new List <AppResourceFileInfo> ();
 
 			string resourcePath;
-			if (isGlobal)
-				resourcePath = Path.Combine (HttpRuntime.AppDomainAppPath, "App_GlobalResources");
-			else {
-				HttpRequest request = context.Request;
-				resourcePath = Path.Combine (
-					Path.GetDirectoryName (request.MapPath (request.CurrentExecutionFilePath)),
-					"App_LocalResources");
-			}
+			resourcePath = Path.Combine (HttpRuntime.AppDomainAppPath, "App_GlobalResources");
 			if (Directory.Exists (resourcePath))
 				sourceDir = resourcePath;
 		}
 
+		public AppResourceFilesCollection (string parserDir)
+		{
+			if (String.IsNullOrEmpty (parserDir))
+				throw new ArgumentException ("parserDir cannot be empty");
+			this.isGlobal = true;
+			this.files = new List <AppResourceFileInfo> ();
+
+			string resourcePath;
+			resourcePath = Path.Combine (parserDir, "App_LocalResources");
+			if (Directory.Exists (resourcePath))
+				sourceDir = resourcePath;
+		}
+		
 		public void Collect ()
 		{
 			if (String.IsNullOrEmpty (sourceDir))

+ 40 - 24
mcs/class/System.Web/System.Web.Compilation/AppResourcesCompiler.cs

@@ -52,6 +52,7 @@ namespace System.Web.Compilation
 		HttpContext context;
 		AppResourceFilesCollection files;
 		string tempDirectory;
+		string virtualPath;
 		
 		string TempDirectory {
 			get {
@@ -61,26 +62,33 @@ namespace System.Web.Compilation
 			}
 		}
 		
-		public AppResourcesCompiler (HttpContext context, bool isGlobal)
+		public AppResourcesCompiler (HttpContext context)
 		{
 			this.context = context;
-			this.isGlobal = isGlobal;
-			this.files = new AppResourceFilesCollection (context, isGlobal);
+			this.isGlobal = true;
+			this.files = new AppResourceFilesCollection (context);
 		}
 
+		public AppResourcesCompiler (string virtualPath)
+		{
+
+			this.virtualPath = virtualPath;
+			this.isGlobal = false;
+			this.files = new AppResourceFilesCollection (HttpContext.Current.Request.MapPath (virtualPath));
+		}
 		
-		public void Compile ()
+		public Assembly Compile ()
 		{
 			files.Collect ();
 			if (!files.HasFiles)
-				return;
+				return null;
 			if (isGlobal)
-				CompileGlobal ();
+				return CompileGlobal ();
 			else
-				CompileLocal ();
+				return CompileLocal ();
 		}
 
-		void CompileGlobal ()
+		Assembly CompileGlobal ()
 		{
 			string assemblyPath = FileUtils.CreateTemporaryFile (TempDirectory,
 									     "App_GlobalResources",
@@ -105,7 +113,7 @@ namespace System.Web.Compilation
 			
 			List <string>[] fileGroups = GroupGlobalFiles (cp);
 			if (fileGroups == null || fileGroups.Length == 0)
-				return;
+				return null;
 
 			CodeCompileUnit unit = new CodeCompileUnit ();
 			CodeNamespace ns = new CodeNamespace (null);
@@ -130,34 +138,37 @@ namespace System.Web.Compilation
 			abuilder.AddCodeCompileUnit (unit);
 
 			CompilerResults results = abuilder.BuildAssembly (cp);
+			Assembly ret = null;
+			
 			if (results.Errors.Count == 0) {
-				BuildManager.TopLevelAssemblies.Add (results.CompiledAssembly);
-				HttpContext.AppGlobalResourcesAssembly = results.CompiledAssembly;
+				ret = results.CompiledAssembly;
+				BuildManager.TopLevelAssemblies.Add (ret);
+				HttpContext.AppGlobalResourcesAssembly = ret;
 			} else {
 				if (context.IsCustomErrorEnabled)
 					throw new ApplicationException ("An error occurred while compiling global resources.");
 				throw new CompilationException (null, results.Errors, null);
 			}
-			HttpRuntime.WritePreservationFile (results.CompiledAssembly, "App_GlobalResources");
+			HttpRuntime.WritePreservationFile (ret, "App_GlobalResources");
 			HttpRuntime.EnableAssemblyMapping (true);
+
+			return ret;
 		}
 
-		void CompileLocal ()
+		Assembly CompileLocal ()
 		{
-			string path = Path.GetDirectoryName (VirtualPathUtility.ToAbsolute (context.Request.CurrentExecutionFilePath));
+			if (String.IsNullOrEmpty (virtualPath))
+				return null;
 			
-			if (String.IsNullOrEmpty (path))
-				throw new ApplicationException ("Unable to determine the request virtual path.");
-
-			Assembly cached = GetCachedLocalResourcesAssembly (path);
+			Assembly cached = GetCachedLocalResourcesAssembly (virtualPath);
 			if (cached != null)
-				return;
+				return cached;
 			
 			string prefix;
-			if (path == "/")
+			if (virtualPath == "/")
 				prefix = "App_LocalResources.root";
 			else
-				prefix = "App_LocalResources" + path.Replace ('/', '.');
+				prefix = "App_LocalResources" + virtualPath.Replace ('/', '.');
 			
 			string assemblyPath = FileUtils.CreateTemporaryFile (TempDirectory,
 									     prefix,
@@ -190,15 +201,20 @@ namespace System.Web.Compilation
 			
 			AssemblyBuilder abuilder = new AssemblyBuilder (provider);
 			CompilerResults results = abuilder.BuildAssembly (cp);
+			Assembly ret = null;
+			
 			if (results.Errors.Count == 0) {
-				AddAssemblyToCache (path, results.CompiledAssembly);
+				ret = results.CompiledAssembly;
+				AddAssemblyToCache (virtualPath, ret);
 			} else {
 				if (context.IsCustomErrorEnabled)
 					throw new ApplicationException ("An error occurred while compiling global resources.");
 				throw new CompilationException (null, results.Errors, null);
 			}
-		}
 
+			return ret;
+		}
+		
 		internal static Assembly GetCachedLocalResourcesAssembly (string path)
 		{
 			Dictionary <string, Assembly> cache;
@@ -208,7 +224,7 @@ namespace System.Web.Compilation
 				return null;
 			return cache [path];
 		}
-
+		
 		void AddAssemblyToCache (string path, Assembly asm)
 		{
 			Cache runtimeCache = HttpRuntime.Cache;

+ 25 - 0
mcs/class/System.Web/System.Web.Compilation/BaseCompiler.cs

@@ -176,6 +176,28 @@ namespace System.Web.Compilation
 			mainClass.Members.Add (fld);
 		}
 
+#if NET_2_0
+		void AssignAppRelativeVirtualPath (CodeConstructor ctor)
+		{
+			Type baseType = parser.BaseType;
+			if (baseType == null)
+				return;
+			if (!baseType.IsSubclassOf (typeof (System.Web.UI.TemplateControl)))
+				return;
+			
+			string arvp = Path.Combine (parser.BaseVirtualDir, Path.GetFileName (parser.InputFile));
+			if (VirtualPathUtility.IsAbsolute (arvp))
+				arvp = "~" + arvp;
+			
+			CodeExpression cast = new CodeCastExpression (baseType, new CodeThisReferenceExpression ());
+			CodePropertyReferenceExpression arvpProp = new CodePropertyReferenceExpression (cast, "AppRelativeVirtualPath");
+			CodeAssignStatement arvpAssign = new CodeAssignStatement ();
+			arvpAssign.Left = arvpProp;
+			arvpAssign.Right = new CodePrimitiveExpression (arvp);
+			ctor.Statements.Add (arvpAssign);
+		}
+#endif
+		
 		protected virtual void CreateConstructor (CodeStatementCollection localVars,
 							  CodeStatementCollection trueStmt)
 		{
@@ -183,6 +205,9 @@ namespace System.Web.Compilation
 			ctor.Attributes = MemberAttributes.Public;
 			mainClass.Members.Add (ctor);
 
+#if NET_2_0
+			AssignAppRelativeVirtualPath (ctor);
+#endif
 			if (localVars != null)
 				ctor.Statements.AddRange (localVars);
 

+ 17 - 0
mcs/class/System.Web/System.Web.Compilation/ChangeLog

@@ -1,3 +1,20 @@
+2007-03-13  Marek Habersack  <[email protected]>
+
+	* AppResourceFilesCollection.cs: added separate constructor for
+	local resources handling.
+
+	* TemplateControlCompiler.cs: request the local resource object
+	with proper virtual path.
+
+	* BaseCompiler.cs: added code to assing AppRelativeVirtualPath
+	property in the page/control constructor.
+
+	* AppResourcesCompiler.cs: does not require specifying manually
+	whether it's a global or local resource compiler anymore. New
+	constructors take care of that.
+	Changed to compile local resources on demand, when a control/page
+	is parsed.
+
 2007-03-12  Marek Habersack  <[email protected]>
 
 	* AspParser.cs: revert r73587 as it breaks more than it	fixes.

+ 11 - 4
mcs/class/System.Web/System.Web.Compilation/TemplateControlCompiler.cs

@@ -685,9 +685,16 @@ namespace System.Web.Compilation
 			} else // should never happen
 				return;
 
-			// __ctrl.Text = System.Convert.ToString(this.GetLocalResourceObject("ButtonResource1.Text"));
-			object obj = HttpContext.GetLocalResourceObject (HttpContext.Current.Request.FilePath,
-									 resname);
+			// __ctrl.Text = System.Convert.ToString(HttpContext.GetLocalResourceObject("ButtonResource1.Text"));
+			string inputFile = parser.InputFile;
+			string physPath = HttpContext.Current.Request.PhysicalApplicationPath;
+	
+			if (StrUtils.StartsWith (inputFile, physPath))
+				inputFile = parser.InputFile.Substring (physPath.Length - 1);
+			 else
+				return;
+			
+			object obj = HttpContext.GetLocalResourceObject (inputFile, resname);
 			if (obj == null)
 				return;
 			
@@ -1335,7 +1342,7 @@ namespace System.Web.Compilation
 			if (!childrenAsProperties && typeof (Control).IsAssignableFrom (builder.ControlType))
 				builder.method.Statements.Add (new CodeMethodReturnStatement (ctrlVar));
 		}
-
+		
 		protected internal override void CreateMethods ()
 		{
 			base.CreateMethods ();

+ 5 - 0
mcs/class/System.Web/System.Web.UI/ChangeLog

@@ -1,3 +1,8 @@
+2007-03-13  Marek Habersack  <[email protected]>
+
+	* TemplateControl.cs: implement AppRelativeVirtualPath. Closes bug
+	#80634.
+
 2007-03-12  Marek Habersack  <[email protected]>
 
 	* RootBuilder.cs: change the error text to be less misleading.

+ 6 - 4
mcs/class/System.Web/System.Web.UI/TemplateControl.cs

@@ -73,6 +73,10 @@ namespace System.Web.UI {
 					    BindingFlags.NonPublic |
 					    BindingFlags.Instance;
 
+#if NET_2_0
+		string _appRelativeVirtualPath;
+#endif
+		
 		#region Constructor
 		protected TemplateControl ()
 		{
@@ -97,11 +101,9 @@ namespace System.Web.UI {
 		}
 
 #if NET_2_0
-		
-		[MonoTODO ("NotImplementedException")]
 		public string AppRelativeVirtualPath {
-			get { throw new NotImplementedException(); }
-			set { throw new NotImplementedException (); }
+			get { return _appRelativeVirtualPath; }
+			set { _appRelativeVirtualPath = value; }
 		}
 #endif
 

+ 11 - 0
mcs/class/System.Web/System.Web/ChangeLog

@@ -1,3 +1,14 @@
+2007-03-13  Marek Habersack  <[email protected]>
+
+	* HttpApplicationFactory.cs: resources compiler no longer accepts
+	a boolean parameter.
+
+	* HttpRuntime.cs: Do not compile local resources here anymore.
+
+	* HttpContext.cs: if App_LocalResources assembly corresponding to
+	the virtual path is not found, compile it here.
+	Look up resources in the "Resources." class path.
+
 2007-03-13 Igor Zelmanovich <[email protected]>
 
 	* HttpUtility.cs: fixed HttpUtillity.HtmlAttributeEncode

+ 1 - 1
mcs/class/System.Web/System.Web/HttpApplicationFactory.cs

@@ -364,7 +364,7 @@ namespace System.Web {
 #endif
 		
 #if NET_2_0 && !TARGET_J2EE
-					AppResourcesCompiler ac = new AppResourcesCompiler (context, true);
+					AppResourcesCompiler ac = new AppResourcesCompiler (context);
 					ac.Compile ();
 				
 					// Todo: Process App_WebResources here

+ 8 - 3
mcs/class/System.Web/System.Web/HttpContext.cs

@@ -418,10 +418,15 @@ namespace System.Web {
 			
 			string path = Path.GetDirectoryName (virtualPath);
 			Assembly asm = AppResourcesCompiler.GetCachedLocalResourcesAssembly (path);
-			if (asm == null)
-				throw new MissingManifestResourceException ("A resource object was not found at the specified virtualPath.");
+			if (asm == null) {
+				AppResourcesCompiler ac = new AppResourcesCompiler (path);
+				asm = ac.Compile ();
+				if (asm == null)
+					throw new MissingManifestResourceException ("A resource object was not found at the specified virtualPath.");
+			}
+			
 			path = Path.GetFileName (virtualPath);
-			return GetResourceObject (path, resourceKey, culture, asm);
+			return GetResourceObject ("Resources." + path, resourceKey, culture, asm);
 		}
 
 		public object GetSection (string name)

+ 0 - 8
mcs/class/System.Web/System.Web/HttpRuntime.cs

@@ -269,14 +269,6 @@ namespace System.Web {
 				HttpContext.Current = null;
 			} else {
 				context.ApplicationInstance = app;
-			
-#if NET_2_0 && !TARGET_JVM
-				//
-				// Compile the local resources, if any
-				//
-				AppResourcesCompiler ac = new AppResourcesCompiler (context, false);
-				ac.Compile ();
-#endif
 				
 				//
 				// Ask application to service the request