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.Util;
20 namespace System.Web.UI
22 public abstract class SimpleWebHandlerParser
34 ArrayList dependencies;
36 string privateBinPath;
40 protected SimpleWebHandlerParser (HttpContext context, string virtualPath, string physicalPath)
42 this.context = context;
43 this.vPath = virtualPath;
44 this.physPath = physicalPath;
45 AddDependency (physicalPath);
47 assemblies = new ArrayList ();
48 assemblies.Add ("System.dll");
49 assemblies.Add ("System.Drawing.dll");
50 assemblies.Add ("System.Data.dll");
51 assemblies.Add ("System.Web.dll");
52 assemblies.Add ("System.Web.Services.dll");
53 assemblies.Add ("System.Xml.dll");
54 AddAssembliesInBin ();
56 GetDirectivesAndContent ();
59 void GetDirectivesAndContent ()
61 StreamReader reader = new StreamReader (File.OpenRead (physPath));
63 bool directiveFound = false;
64 StringBuilder content = new StringBuilder ();
66 while ((line = reader.ReadLine ()) != null) {
67 string trimmed = line.Trim ();
68 if (!directiveFound && trimmed == String.Empty)
71 if (trimmed.StartsWith ("<")) {
72 ParseDirective (trimmed);
73 directiveFound = true;
77 content.Append (line + "\n");
78 content.Append (reader.ReadToEnd ());
82 throw new ParseException (null, "No @WebService directive found");
84 this.program = content.ToString ();
88 void TagParsed (ILocation location, System.Web.Compilation.TagType tagtype, string tagid, TagAttributes attributes)
90 if (tagtype != System.Web.Compilation.TagType.Directive)
91 throw new ParseException (location, "Unexpected tag");
93 if (String.Compare (tagid, DefaultDirectiveName, true) == 0) {
94 AddDefaultDirective (location, attributes);
95 } else if (String.Compare (tagid, "Assembly", true) == 0) {
96 AddAssemblyDirective (location, attributes);
98 throw new ParseException (location, "Unexpected directive: " + tagid);
102 void TextParsed (ILocation location, string text)
104 if (text.Trim () != "")
105 throw new ParseException (location, "Text not allowed here");
108 void ParseError (ILocation location, string message)
110 throw new ParseException (location, message);
113 static string GetAndRemove (Hashtable table, string key)
115 string o = table [key] as string;
120 void ParseDirective (string line)
122 AspParser parser = new AspParser (physPath, new StringReader (line));
123 parser.Error += new ParseErrorHandler (ParseError);
124 parser.TagParsed += new TagParsedHandler (TagParsed);
125 parser.TextParsed += new TextParsedHandler (TextParsed);
130 internal virtual void AddDefaultDirective (ILocation location, TagAttributes attrs)
133 throw new ParseException (location, "duplicate " + DefaultDirectiveName + " directive");
136 Hashtable attributes = attrs.GetDictionary (null);
137 className = GetAndRemove (attributes, "class");
138 if (className == null)
139 throw new ParseException (null, "No Class attribute found.");
141 string d = GetAndRemove (attributes, "debug");
143 debug = (String.Compare (d, "true", true) == 0);
144 if (debug == false && String.Compare (d, "false", true) != 0)
145 throw new ParseException (null, "Invalid value for Debug attribute");
148 language = GetAndRemove (attributes, "language");
149 if (language != null) {
150 if (0 != String.Compare (language, "C#", true))
151 throw new ParseException (null, "Only C# language is supported by now.");
154 codeBehind = GetAndRemove (attributes, "codebehind");
155 if (attributes.Count > 0)
156 throw new ParseException (location, "Unrecognized attribute in " +
157 DefaultDirectiveName + " directive");
160 internal virtual void AddAssemblyDirective (ILocation location, TagAttributes attrs)
162 Hashtable tbl = attrs.GetDictionary (null);
163 string name = GetAndRemove (tbl, "Name");
164 string src = GetAndRemove (tbl, "Src");
165 if (name == null && src == null)
166 throw new ParseException (location, "You gotta specify Src or Name");
168 if (name != null && src != null)
169 throw new ParseException (location, "Src and Name cannot be used together");
172 AddAssemblyByName (name, location);
174 GetAssemblyFromSource (src, location);
178 throw new ParseException (location, "Unrecognized attribute in Assembly directive");
181 internal virtual void AddAssembly (Assembly assembly, bool fullPath)
184 anames = new Hashtable ();
186 string name = assembly.GetName ().Name;
187 string loc = assembly.Location;
189 if (!assemblies.Contains (loc)) {
190 assemblies.Add (loc);
194 anames [loc] = assembly;
196 if (!assemblies.Contains (name)) {
197 assemblies.Add (name);
200 anames [name] = assembly;
204 internal virtual Assembly AddAssemblyByName (string name, ILocation location)
207 anames = new Hashtable ();
209 if (anames.Contains (name)) {
210 object o = anames [name];
217 bool fullpath = true;
218 Assembly assembly = LoadAssemblyFromBin (name);
219 if (assembly != null) {
220 AddAssembly (assembly, fullpath);
225 assembly = Assembly.Load (name);
226 string loc = assembly.Location;
227 fullpath = (Path.GetDirectoryName (loc) == PrivateBinPath);
228 } catch (Exception e) {
229 throw new ParseException (location, "Assembly " + name + " not found", e);
232 AddAssembly (assembly, fullpath);
236 void AddAssembliesInBin ()
238 if (!Directory.Exists (PrivateBinPath))
241 string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
242 foreach (string dll in binDlls) {
243 Assembly assembly = Assembly.LoadFrom (dll);
244 AddAssembly (assembly, true);
248 Assembly LoadAssemblyFromBin (string name)
251 if (!Directory.Exists (PrivateBinPath))
254 string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
255 foreach (string dll in binDlls) {
256 string fn = Path.GetFileName (dll);
257 fn = Path.ChangeExtension (fn, null);
261 assembly = Assembly.LoadFrom (dll);
268 Assembly GetAssemblyFromSource (string vpath, ILocation location)
270 vpath = UrlUtils.Combine (BaseVirtualDir, vpath);
271 string realPath = context.Request.MapPath (vpath);
272 if (!File.Exists (realPath))
273 throw new ParseException (location, "File " + vpath + " not found");
275 AddDependency (realPath);
277 CompilerResults result = CachingCompiler.Compile (realPath, realPath, assemblies);
278 if (result.NativeCompilerReturnValue != 0) {
279 StreamReader reader = new StreamReader (realPath);
280 throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ());
283 AddAssembly (result.CompiledAssembly, true);
284 return result.CompiledAssembly;
287 internal Type GetTypeFromBin (string typeName)
289 if (!Directory.Exists (PrivateBinPath))
290 throw new HttpException (String.Format ("Type {0} not found.", typeName));
292 string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
294 foreach (string dll in binDlls) {
295 Assembly assembly = Assembly.LoadFrom (dll);
296 Type type = assembly.GetType (typeName, false);
299 throw new HttpException (String.Format ("Type {0} is not unique.", typeName));
306 throw new HttpException (String.Format ("Type {0} not found.", typeName));
311 internal virtual void AddDependency (string filename)
313 if (dependencies == null)
314 dependencies = new ArrayList ();
316 if (!dependencies.Contains (filename))
317 dependencies.Add (filename);
321 protected abstract string DefaultDirectiveName { get; }
323 internal HttpContext Context {
324 get { return context; }
327 internal string VirtualPath {
328 get { return vPath; }
331 internal string PhysicalPath {
332 get { return physPath; }
335 internal string ClassName {
336 get { return className; }
339 internal string CodeBehind {
340 get { return codeBehind; }
343 internal bool Debug {
344 get { return debug; }
347 internal string Language {
348 get { return language; }
351 internal string Program {
352 get { return program; }
355 internal ArrayList Assemblies {
356 get { return assemblies; }
359 internal ArrayList Dependencies {
360 get { return dependencies; }
363 internal string PrivateBinPath {
365 if (privateBinPath != null)
366 return privateBinPath;
368 AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
369 privateBinPath = setup.PrivateBinPath;
371 if (!Path.IsPathRooted (privateBinPath)) {
372 string appbase = setup.ApplicationBase;
373 if (appbase.StartsWith ("file://")) {
374 appbase = appbase.Substring (7);
375 if (Path.DirectorySeparatorChar != '/')
376 appbase = appbase.Replace ('/', Path.DirectorySeparatorChar);
378 privateBinPath = Path.Combine (appbase, privateBinPath);
381 return privateBinPath;
385 internal string BaseDir {
388 baseDir = context.Request.MapPath (BaseVirtualDir);
394 internal virtual string BaseVirtualDir {
396 if (baseVDir == null)
397 baseVDir = UrlUtils.GetDirectory (context.Request.FilePath);