2005-05-09 Geoff Norotn <gnorton@customerdna.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / TemplateParser.cs
1 //
2 // System.Web.UI.TemplateParser
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 using System;
32 using System.CodeDom.Compiler;
33 using System.Collections;
34 using System.IO;
35 using System.Reflection;
36 using System.Web;
37 using System.Web.Compilation;
38 using System.Web.Configuration;
39 using System.Web.Util;
40
41 namespace System.Web.UI
42 {
43         public abstract class TemplateParser : BaseParser
44         {
45                 string inputFile;
46                 string text;
47                 string privateBinPath;
48                 Hashtable mainAttributes;
49                 ArrayList dependencies;
50                 ArrayList assemblies;
51                 Hashtable anames;
52                 ArrayList imports;
53                 ArrayList interfaces;
54                 ArrayList scripts;
55                 Type baseType;
56                 string className;
57                 RootBuilder rootBuilder;
58                 bool debug;
59                 string compilerOptions;
60                 string language;
61                 bool strictOn = false;
62                 bool explicitOn = false;
63                 bool output_cache;
64                 int oc_duration;
65                 string oc_header, oc_custom, oc_param, oc_controls;
66                 bool oc_shared;
67                 OutputCacheLocation oc_location;
68                 Assembly srcAssembly;
69                 int appAssemblyIndex = -1;
70                 
71                 internal TemplateParser ()
72                 {
73                         imports = new ArrayList ();
74                         imports.Add ("System");
75                         imports.Add ("System.Collections");
76                         imports.Add ("System.Collections.Specialized");
77                         imports.Add ("System.Configuration");
78                         imports.Add ("System.Text");
79                         imports.Add ("System.Text.RegularExpressions");
80                         imports.Add ("System.Web");
81                         imports.Add ("System.Web.Caching");
82                         imports.Add ("System.Web.Security");
83                         imports.Add ("System.Web.SessionState");
84                         imports.Add ("System.Web.UI");
85                         imports.Add ("System.Web.UI.WebControls");
86                         imports.Add ("System.Web.UI.HtmlControls");
87
88                         assemblies = new ArrayList ();
89                         foreach (string a in CompilationConfig.Assemblies)
90                                 AddAssemblyByName (a);
91                         if (CompilationConfig.AssembliesInBin)
92                                 AddAssembliesInBin ();
93
94                         language = CompilationConfig.DefaultLanguage;
95                 }
96
97                 internal void AddApplicationAssembly ()
98                 {
99                         string location = Context.ApplicationInstance.AssemblyLocation;
100                         if (location != typeof (TemplateParser).Assembly.Location) {
101                                 appAssemblyIndex = assemblies.Add (location);
102                         }
103                 }
104
105                 protected abstract Type CompileIntoType ();
106
107                 internal virtual void HandleOptions (object obj)
108                 {
109                 }
110
111                 internal static string GetOneKey (Hashtable tbl)
112                 {
113                         foreach (object key in tbl.Keys)
114                                 return key.ToString ();
115
116                         return null;
117                 }
118                 
119                 internal virtual void AddDirective (string directive, Hashtable atts)
120                 {
121                         if (String.Compare (directive, DefaultDirectiveName, true) == 0) {
122                                 if (mainAttributes != null)
123                                         ThrowParseException ("Only 1 " + DefaultDirectiveName + " is allowed");
124
125                                 mainAttributes = atts;
126                                 ProcessMainAttributes (mainAttributes);
127                                 return;
128                         }
129
130                         int cmp = String.Compare ("Assembly", directive, true);
131                         if (cmp == 0) {
132                                 string name = GetString (atts, "Name", null);
133                                 string src = GetString (atts, "Src", null);
134
135                                 if (atts.Count > 0)
136                                         ThrowParseException ("Attribute " + GetOneKey (atts) + " unknown.");
137
138                                 if (name == null && src == null)
139                                         ThrowParseException ("You gotta specify Src or Name");
140                                         
141                                 if (name != null && src != null)
142                                         ThrowParseException ("Src and Name cannot be used together");
143
144                                 if (name != null) {
145                                         AddAssemblyByName (name);
146                                 } else {
147                                         GetAssemblyFromSource (src);
148                                 }
149
150                                 return;
151                         }
152
153                         cmp = String.Compare ("Import", directive, true);
154                         if (cmp == 0) {
155                                 string namesp = GetString (atts, "Namespace", null);
156                                 if (atts.Count > 0)
157                                         ThrowParseException ("Attribute " + GetOneKey (atts) + " unknown.");
158                                 
159                                 if (namesp != null && namesp != "")
160                                         AddImport (namesp);
161                                 return;
162                         }
163
164                         cmp = String.Compare ("Implements", directive, true);
165                         if (cmp == 0) {
166                                 string ifacename = GetString (atts, "Interface", "");
167
168                                 if (atts.Count > 0)
169                                         ThrowParseException ("Attribute " + GetOneKey (atts) + " unknown.");
170                                 
171                                 Type iface = LoadType (ifacename);
172                                 if (iface == null)
173                                         ThrowParseException ("Cannot find type " + ifacename);
174
175                                 if (!iface.IsInterface)
176                                         ThrowParseException (iface + " is not an interface");
177
178                                 AddInterface (iface.FullName);
179                                 return;
180                         }
181
182                         cmp = String.Compare ("OutputCache", directive, true);
183                         if (cmp == 0) {
184                                 output_cache = true;
185                                 
186                                 if (atts ["Duration"] == null)
187                                         ThrowParseException ("The directive is missing a 'duration' attribute.");
188                                 if (atts ["VaryByParam"] == null)
189                                         ThrowParseException ("This directive is missing a 'VaryByParam' " +
190                                                         "attribute, which should be set to \"none\", \"*\", " +
191                                                         "or a list of name/value pairs.");
192
193                                 foreach (DictionaryEntry entry in atts) {
194                                         string key = (string) entry.Key;
195                                         switch (key.ToLower ()) {
196                                         case "duration":
197                                                 oc_duration = Int32.Parse ((string) entry.Value);
198                                                 if (oc_duration < 1)
199                                                         ThrowParseException ("The 'duration' attribute must be set " +
200                                                                         "to a positive integer value");
201                                                 break;
202                                         case "varybyparam":
203                                                 oc_param = (string) entry.Value;
204                                                 if (String.Compare (oc_param, "none") == 0)
205                                                         oc_param = null;
206                                                 break;
207                                         case "varybyheader":
208                                                 oc_header = (string) entry.Value;
209                                                 break;
210                                         case "varybycustom":
211                                                 oc_custom = (string) entry.Value;
212                                                 break;
213                                         case "location":
214                                                 if (!(this is PageParser))
215                                                         goto default;
216
217                                                 try {
218                                                         oc_location = (OutputCacheLocation) Enum.Parse (
219                                                                 typeof (OutputCacheLocation), (string) entry.Value, true);
220                                                 } catch {
221                                                         ThrowParseException ("The 'location' attribute is case sensitive and " +
222                                                                         "must be one of the following values: Any, Client, " +
223                                                                         "Downstream, Server, None, ServerAndClient.");
224                                                 }
225                                                 break;
226                                         case "varybycontrol":
227                                                 if (this is PageParser)
228                                                         goto default;
229
230                                                 oc_controls = (string) entry.Value;
231                                                 break;
232                                         case "shared":
233                                                 if (this is PageParser)
234                                                         goto default;
235
236                                                 try {
237                                                         oc_shared = Boolean.Parse ((string) entry.Value);
238                                                 } catch {
239                                                         ThrowParseException ("The 'shared' attribute is case sensitive" +
240                                                                         " and must be set to 'true' or 'false'.");
241                                                 }
242                                                 break;
243                                         default:
244                                                 ThrowParseException ("The '" + key + "' attribute is not " +
245                                                                 "supported by the 'Outputcache' directive.");
246                                                 break;
247                                         }
248                                         
249                                 }
250                                 
251                                 return;
252                         }
253
254                         ThrowParseException ("Unknown directive: " + directive);
255                 }
256
257                 internal Type LoadType (string typeName)
258                 {
259                         // First try loaded assemblies, then try assemblies in Bin directory.
260                         Type type = null;
261                         bool seenBin = false;
262                         Assembly [] assemblies = AppDomain.CurrentDomain.GetAssemblies ();
263                         foreach (Assembly ass in assemblies) {
264                                 type = ass.GetType (typeName);
265                                 if (type == null)
266                                         continue;
267
268                                 if (Path.GetDirectoryName (ass.Location) != PrivateBinPath) {
269                                         AddAssembly (ass, true);
270                                 } else {
271                                         seenBin = true;
272                                 }
273
274                                 AddDependency (ass.Location);
275                                 return type;
276                         }
277
278                         if (seenBin)
279                                 return null;
280
281                         // Load from bin
282                         if (!Directory.Exists (PrivateBinPath))
283                                 return null;
284
285                         string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
286                         foreach (string s in binDlls) {
287                                 Assembly binA = Assembly.LoadFrom (s);
288                                 type = binA.GetType (typeName);
289                                 if (type == null)
290                                         continue;
291
292                                 AddDependency (binA.Location);
293                                 return type;
294                         }
295
296                         return null;
297                 }
298
299                 void AddAssembliesInBin ()
300                 {
301                         if (!Directory.Exists (PrivateBinPath))
302                                 return;
303
304                         string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
305                         foreach (string s in binDlls) {
306                                 assemblies.Add (s);
307                         }
308                 }
309
310                 internal virtual void AddInterface (string iface)
311                 {
312                         if (interfaces == null)
313                                 interfaces = new ArrayList ();
314
315                         if (!interfaces.Contains (iface))
316                                 interfaces.Add (iface);
317                 }
318                 
319                 internal virtual void AddImport (string namesp)
320                 {
321                         if (imports == null)
322                                 imports = new ArrayList ();
323
324                         if (!imports.Contains (namesp))
325                                 imports.Add (namesp);
326                 }
327                 
328                 internal virtual void AddDependency (string filename)
329                 {
330                         if (dependencies == null)
331                                 dependencies = new ArrayList ();
332
333                         if (!dependencies.Contains (filename))
334                                 dependencies.Add (filename);
335                 }
336                 
337                 internal virtual void AddAssembly (Assembly assembly, bool fullPath)
338                 {
339                         if (anames == null)
340                                 anames = new Hashtable ();
341
342                         string name = assembly.GetName ().Name;
343                         string loc = assembly.Location;
344                         if (fullPath) {
345                                 if (!assemblies.Contains (loc)) {
346                                         assemblies.Add (loc);
347                                 }
348
349                                 anames [name] = loc;
350                                 anames [loc] = assembly;
351                         } else {
352                                 if (!assemblies.Contains (name)) {
353                                         assemblies.Add (name);
354                                 }
355
356                                 anames [name] = assembly;
357                         }
358                 }
359
360                 internal virtual Assembly AddAssemblyByName (string name)
361                 {
362                         if (anames == null)
363                                 anames = new Hashtable ();
364
365                         if (anames.Contains (name)) {
366                                 object o = anames [name];
367                                 if (o is string)
368                                         o = anames [o];
369
370                                 return (Assembly) o;
371                         }
372
373                         Assembly assembly = null;
374                         try {
375                                 assembly = Assembly.Load (name);
376                         } catch (Exception) {
377                                 try {
378                                         assembly = Assembly.LoadWithPartialName (name);
379                                 } catch (Exception e) {
380                                         ThrowParseException ("Assembly " + name + " not found", e);
381                                 }
382
383                                 if (assembly == null)
384                                         ThrowParseException ("Assembly " + name + " not found", null);
385                         }
386
387                         AddAssembly (assembly, true);
388                         return assembly;
389                 }
390
391                 internal virtual void ProcessMainAttributes (Hashtable atts)
392                 {
393                         atts.Remove ("Description"); // ignored
394                         atts.Remove ("CodeBehind");  // ignored
395                         atts.Remove ("AspCompat"); // ignored
396
397 #if NET_2_0
398                         atts.Remove ("CodeFile"); // ignored
399 #endif
400
401                         debug = GetBool (atts, "Debug", true);
402                         compilerOptions = GetString (atts, "CompilerOptions", "");
403                         language = GetString (atts, "Language", CompilationConfig.DefaultLanguage);
404                         strictOn = GetBool (atts, "Strict", CompilationConfig.Strict);
405                         explicitOn = GetBool (atts, "Explicit", CompilationConfig.Explicit);
406                         string src = GetString (atts, "Src", null);
407                         if (src != null)
408                                 srcAssembly = GetAssemblyFromSource (src);
409
410                         string inherits = GetString (atts, "Inherits", null);
411                         if (inherits != null)
412                                 SetBaseType (inherits);
413
414                         className = GetString (atts, "ClassName", null);
415                         if (className != null && !CodeGenerator.IsValidLanguageIndependentIdentifier (className))
416                                 ThrowParseException (String.Format ("'{0}' is not valid for 'className'", className));
417
418                         if (atts.Count > 0)
419                                 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
420                 }
421
422                 internal void SetBaseType (string type)
423                 {
424                         if (type == DefaultBaseTypeName)
425                                 return;
426
427                         Type parent = null;
428                         if (srcAssembly != null)
429                                 parent = srcAssembly.GetType (type);
430
431                         if (parent == null)
432                                 parent = LoadType (type);
433
434                         if (parent == null)
435                                 ThrowParseException ("Cannot find type " + type);
436
437                         if (!DefaultBaseType.IsAssignableFrom (parent))
438                                 ThrowParseException ("The parent type does not derive from " + DefaultBaseType);
439
440                         baseType = parent;
441                 }
442
443                 Assembly GetAssemblyFromSource (string vpath)
444                 {
445                         vpath = UrlUtils.Combine (BaseVirtualDir, vpath);
446                         string realPath = MapPath (vpath, false);
447                         if (!File.Exists (realPath))
448                                 ThrowParseException ("File " + vpath + " not found");
449
450                         AddDependency (realPath);
451
452                         CompilerResults result = CachingCompiler.Compile (language, realPath, realPath, assemblies);
453                         if (result.NativeCompilerReturnValue != 0) {
454                                 StreamReader reader = new StreamReader (realPath);
455                                 throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ());
456                         }
457
458                         AddAssembly (result.CompiledAssembly, true);
459                         return result.CompiledAssembly;
460                 }
461                 
462                 internal abstract Type DefaultBaseType { get; }
463                 internal abstract string DefaultBaseTypeName { get; }
464                 internal abstract string DefaultDirectiveName { get; }
465
466                 internal string InputFile
467                 {
468                         get { return inputFile; }
469                         set { inputFile = value; }
470                 }
471
472                 internal string Text
473                 {
474                         get { return text; }
475                         set { text = value; }
476                 }
477
478                 internal Type BaseType
479                 {
480                         get {
481                                 if (baseType == null)
482                                         baseType = DefaultBaseType;
483
484                                 return baseType;
485                         }
486                 }
487                 
488                 internal string ClassName {
489                         get {
490                                 if (className != null)
491                                         return className;
492
493                                 className = Path.GetFileName (inputFile).Replace ('.', '_');
494                                 className = className.Replace ('-', '_'); 
495                                 className = className.Replace (' ', '_');
496
497                                 if (Char.IsDigit(className[0])) {
498                                         className = "_" + className;
499                                 }
500
501                                 return className;
502                         }
503                 }
504
505                 internal string PrivateBinPath {
506                         get {
507                                 if (privateBinPath != null)
508                                         return privateBinPath;
509
510                                 AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
511                                 privateBinPath = Path.Combine (setup.ApplicationBase, setup.PrivateBinPath);
512
513                                 return privateBinPath;
514                         }
515                 }
516
517                 internal ArrayList Scripts {
518                         get {
519                                 if (scripts == null)
520                                         scripts = new ArrayList ();
521
522                                 return scripts;
523                         }
524                 }
525
526                 internal ArrayList Imports {
527                         get { return imports; }
528                 }
529
530                 internal ArrayList Assemblies {
531                         get {
532                                 if (appAssemblyIndex != -1) {
533                                         object o = assemblies [appAssemblyIndex];
534                                         assemblies.RemoveAt (appAssemblyIndex);
535                                         assemblies.Add (o);
536                                         appAssemblyIndex = -1;
537                                 }
538
539                                 return assemblies;
540                         }
541                 }
542
543                 internal ArrayList Interfaces {
544                         get { return interfaces; }
545                 }
546
547                 internal RootBuilder RootBuilder {
548                         get { return rootBuilder; }
549                         set { rootBuilder = value; }
550                 }
551
552                 internal ArrayList Dependencies {
553                         get { return dependencies; }
554                 }
555
556                 internal string CompilerOptions {
557                         get { return compilerOptions; }
558                 }
559
560                 internal string Language {
561                         get { return language; }
562                 }
563
564                 internal bool StrictOn {
565                         get { return strictOn; }
566                 }
567
568                 internal bool ExplicitOn {
569                         get { return explicitOn; }
570                 }
571                 
572                 internal bool Debug {
573                         get { return debug; }
574                 }
575
576                 internal bool OutputCache {
577                         get { return output_cache; }
578                 }
579
580                 internal int OutputCacheDuration {
581                         get { return oc_duration; }
582                 }
583
584                 internal string OutputCacheVaryByHeader {
585                         get { return oc_header; }
586                 }
587
588                 internal string OutputCacheVaryByCustom {
589                         get { return oc_custom; }
590                 }
591
592                 internal string OutputCacheVaryByControls {
593                         get { return oc_controls; }
594                 }
595                 
596                 internal bool OutputCacheShared {
597                         get { return oc_shared; }
598                 }
599                 
600                 internal OutputCacheLocation OutputCacheLocation {
601                         get { return oc_location; }
602                 }
603
604                 internal string OutputCacheVaryByParam {
605                         get { return oc_param; }
606                 }
607
608                 internal PagesConfiguration PagesConfig {
609                         get { return PagesConfiguration.GetInstance (Context); }
610                 }
611         }
612 }
613