2 // System.Web.UI.SimpleWebHandlerParser
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
11 using System.CodeDom.Compiler;
12 using System.Collections;
14 using System.Reflection;
17 using System.Web.Compilation;
18 using System.Web.Configuration;
19 using System.Web.Util;
21 namespace System.Web.UI
23 public abstract class SimpleWebHandlerParser
35 ArrayList dependencies;
37 string privateBinPath;
40 CompilationConfiguration compilationConfig;
42 protected SimpleWebHandlerParser (HttpContext context, string virtualPath, string physicalPath)
44 this.context = context;
45 this.vPath = virtualPath;
46 this.physPath = physicalPath;
47 AddDependency (physicalPath);
49 assemblies = new ArrayList ();
50 string location = Context.ApplicationInstance.AssemblyLocation;
51 if (location != typeof (TemplateParser).Assembly.Location)
52 assemblies.Add (location);
54 assemblies.AddRange (CompilationConfig.Assemblies);
55 if (CompilationConfig.AssembliesInBin)
56 AddAssembliesInBin ();
58 language = CompilationConfig.DefaultLanguage;
60 GetDirectivesAndContent ();
64 protected Type GetCompiledTypeFromCache ()
69 void GetDirectivesAndContent ()
71 StreamReader reader = new StreamReader (File.OpenRead (physPath));
73 bool directiveFound = false;
74 StringBuilder content = new StringBuilder ();
76 while ((line = reader.ReadLine ()) != null) {
77 string trimmed = line.Trim ();
78 if (!directiveFound && trimmed == String.Empty)
81 if (trimmed.StartsWith ("<")) {
82 ParseDirective (trimmed);
83 directiveFound = true;
87 content.Append (line + "\n");
88 content.Append (reader.ReadToEnd ());
92 throw new ParseException (null, "No @WebService directive found");
94 this.program = content.ToString ();
98 void TagParsed (ILocation location, System.Web.Compilation.TagType tagtype, string tagid, TagAttributes attributes)
100 if (tagtype != System.Web.Compilation.TagType.Directive)
101 throw new ParseException (location, "Unexpected tag");
103 if (String.Compare (tagid, DefaultDirectiveName, true) == 0) {
104 AddDefaultDirective (location, attributes);
105 } else if (String.Compare (tagid, "Assembly", true) == 0) {
106 AddAssemblyDirective (location, attributes);
108 throw new ParseException (location, "Unexpected directive: " + tagid);
112 void TextParsed (ILocation location, string text)
114 if (text.Trim () != "")
115 throw new ParseException (location, "Text not allowed here");
118 void ParseError (ILocation location, string message)
120 throw new ParseException (location, message);
123 static string GetAndRemove (Hashtable table, string key)
125 string o = table [key] as string;
130 void ParseDirective (string line)
132 AspParser parser = new AspParser (physPath, new StringReader (line));
133 parser.Error += new ParseErrorHandler (ParseError);
134 parser.TagParsed += new TagParsedHandler (TagParsed);
135 parser.TextParsed += new TextParsedHandler (TextParsed);
140 internal virtual void AddDefaultDirective (ILocation location, TagAttributes attrs)
143 throw new ParseException (location, "duplicate " + DefaultDirectiveName + " directive");
146 Hashtable attributes = attrs.GetDictionary (null);
147 className = GetAndRemove (attributes, "class");
148 if (className == null)
149 throw new ParseException (null, "No Class attribute found.");
151 string d = GetAndRemove (attributes, "debug");
153 debug = (String.Compare (d, "true", true) == 0);
154 if (debug == false && String.Compare (d, "false", true) != 0)
155 throw new ParseException (null, "Invalid value for Debug attribute");
158 language = GetAndRemove (attributes, "language");
159 if (language == null)
160 language = CompilationConfig.DefaultLanguage;
162 codeBehind = GetAndRemove (attributes, "codebehind");
163 if (attributes.Count > 0)
164 throw new ParseException (location, "Unrecognized attribute in " +
165 DefaultDirectiveName + " directive");
168 internal virtual void AddAssemblyDirective (ILocation location, TagAttributes attrs)
170 Hashtable tbl = attrs.GetDictionary (null);
171 string name = GetAndRemove (tbl, "Name");
172 string src = GetAndRemove (tbl, "Src");
173 if (name == null && src == null)
174 throw new ParseException (location, "You gotta specify Src or Name");
176 if (name != null && src != null)
177 throw new ParseException (location, "Src and Name cannot be used together");
180 AddAssemblyByName (name, location);
182 GetAssemblyFromSource (src, location);
186 throw new ParseException (location, "Unrecognized attribute in Assembly directive");
189 internal virtual void AddAssembly (Assembly assembly, bool fullPath)
192 anames = new Hashtable ();
194 string name = assembly.GetName ().Name;
195 string loc = assembly.Location;
197 if (!assemblies.Contains (loc)) {
198 assemblies.Add (loc);
202 anames [loc] = assembly;
204 if (!assemblies.Contains (name)) {
205 assemblies.Add (name);
208 anames [name] = assembly;
212 internal virtual Assembly AddAssemblyByName (string name, ILocation location)
215 anames = new Hashtable ();
217 if (anames.Contains (name)) {
218 object o = anames [name];
225 bool fullpath = true;
226 Assembly assembly = LoadAssemblyFromBin (name);
227 if (assembly != null) {
228 AddAssembly (assembly, fullpath);
233 assembly = Assembly.LoadWithPartialName (name);
234 string loc = assembly.Location;
235 fullpath = (Path.GetDirectoryName (loc) == PrivateBinPath);
236 } catch (Exception e) {
237 throw new ParseException (location, "Assembly " + name + " not found", e);
240 AddAssembly (assembly, fullpath);
244 void AddAssembliesInBin ()
246 if (!Directory.Exists (PrivateBinPath))
249 string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
250 foreach (string dll in binDlls) {
252 Assembly assembly = Assembly.LoadFrom (dll);
253 AddAssembly (assembly, true);
254 } catch (Exception e) {
255 throw new Exception ("Error while loading " + dll, e);
260 Assembly LoadAssemblyFromBin (string name)
263 if (!Directory.Exists (PrivateBinPath))
266 string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
267 foreach (string dll in binDlls) {
268 string fn = Path.GetFileName (dll);
269 fn = Path.ChangeExtension (fn, null);
273 assembly = Assembly.LoadFrom (dll);
280 Assembly GetAssemblyFromSource (string vpath, ILocation location)
282 vpath = UrlUtils.Combine (BaseVirtualDir, vpath);
283 string realPath = context.Request.MapPath (vpath);
284 if (!File.Exists (realPath))
285 throw new ParseException (location, "File " + vpath + " not found");
287 AddDependency (realPath);
289 CompilerResults result = CachingCompiler.Compile (realPath, realPath, assemblies);
290 if (result.NativeCompilerReturnValue != 0) {
291 StreamReader reader = new StreamReader (realPath);
292 throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ());
295 AddAssembly (result.CompiledAssembly, true);
296 return result.CompiledAssembly;
299 internal Type GetTypeFromBin (string typeName)
301 if (!Directory.Exists (PrivateBinPath))
302 throw new HttpException (String.Format ("Type {0} not found.", typeName));
304 string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
306 foreach (string dll in binDlls) {
307 Assembly assembly = Assembly.LoadFrom (dll);
308 Type type = assembly.GetType (typeName, false);
311 throw new HttpException (String.Format ("Type {0} is not unique.", typeName));
318 throw new HttpException (String.Format ("Type {0} not found.", typeName));
323 internal virtual void AddDependency (string filename)
325 if (dependencies == null)
326 dependencies = new ArrayList ();
328 if (!dependencies.Contains (filename))
329 dependencies.Add (filename);
333 protected abstract string DefaultDirectiveName { get; }
335 internal HttpContext Context {
336 get { return context; }
339 internal string VirtualPath {
340 get { return vPath; }
343 internal string PhysicalPath {
344 get { return physPath; }
347 internal string ClassName {
348 get { return className; }
351 internal string CodeBehind {
352 get { return codeBehind; }
355 internal bool Debug {
356 get { return debug; }
359 internal string Language {
360 get { return language; }
363 internal string Program {
364 get { return program; }
367 internal ArrayList Assemblies {
368 get { return assemblies; }
371 internal ArrayList Dependencies {
372 get { return dependencies; }
375 internal string PrivateBinPath {
377 if (privateBinPath != null)
378 return privateBinPath;
380 AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
381 privateBinPath = setup.PrivateBinPath;
383 if (!Path.IsPathRooted (privateBinPath)) {
384 string appbase = setup.ApplicationBase;
385 if (appbase.StartsWith ("file://")) {
386 appbase = appbase.Substring (7);
387 if (Path.DirectorySeparatorChar != '/')
388 appbase = appbase.Replace ('/', Path.DirectorySeparatorChar);
390 privateBinPath = Path.Combine (appbase, privateBinPath);
393 return privateBinPath;
397 internal string BaseDir {
400 baseDir = context.Request.MapPath (BaseVirtualDir);
406 internal virtual string BaseVirtualDir {
408 if (baseVDir == null)
409 baseVDir = UrlUtils.GetDirectory (context.Request.FilePath);
415 internal CompilationConfiguration CompilationConfig {
417 if (compilationConfig == null)
418 compilationConfig = CompilationConfiguration.GetInstance (context);
420 return compilationConfig;