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 ();
63 void GetDirectivesAndContent ()
65 StreamReader reader = new StreamReader (File.OpenRead (physPath));
67 bool directiveFound = false;
68 StringBuilder content = new StringBuilder ();
70 while ((line = reader.ReadLine ()) != null) {
71 string trimmed = line.Trim ();
72 if (!directiveFound && trimmed == String.Empty)
75 if (trimmed.StartsWith ("<")) {
76 ParseDirective (trimmed);
77 directiveFound = true;
81 content.Append (line + "\n");
82 content.Append (reader.ReadToEnd ());
86 throw new ParseException (null, "No @WebService directive found");
88 this.program = content.ToString ();
92 void TagParsed (ILocation location, System.Web.Compilation.TagType tagtype, string tagid, TagAttributes attributes)
94 if (tagtype != System.Web.Compilation.TagType.Directive)
95 throw new ParseException (location, "Unexpected tag");
97 if (String.Compare (tagid, DefaultDirectiveName, true) == 0) {
98 AddDefaultDirective (location, attributes);
99 } else if (String.Compare (tagid, "Assembly", true) == 0) {
100 AddAssemblyDirective (location, attributes);
102 throw new ParseException (location, "Unexpected directive: " + tagid);
106 void TextParsed (ILocation location, string text)
108 if (text.Trim () != "")
109 throw new ParseException (location, "Text not allowed here");
112 void ParseError (ILocation location, string message)
114 throw new ParseException (location, message);
117 static string GetAndRemove (Hashtable table, string key)
119 string o = table [key] as string;
124 void ParseDirective (string line)
126 AspParser parser = new AspParser (physPath, new StringReader (line));
127 parser.Error += new ParseErrorHandler (ParseError);
128 parser.TagParsed += new TagParsedHandler (TagParsed);
129 parser.TextParsed += new TextParsedHandler (TextParsed);
134 internal virtual void AddDefaultDirective (ILocation location, TagAttributes attrs)
137 throw new ParseException (location, "duplicate " + DefaultDirectiveName + " directive");
140 Hashtable attributes = attrs.GetDictionary (null);
141 className = GetAndRemove (attributes, "class");
142 if (className == null)
143 throw new ParseException (null, "No Class attribute found.");
145 string d = GetAndRemove (attributes, "debug");
147 debug = (String.Compare (d, "true", true) == 0);
148 if (debug == false && String.Compare (d, "false", true) != 0)
149 throw new ParseException (null, "Invalid value for Debug attribute");
152 language = GetAndRemove (attributes, "language");
153 if (language == null)
154 language = CompilationConfig.DefaultLanguage;
156 codeBehind = GetAndRemove (attributes, "codebehind");
157 if (attributes.Count > 0)
158 throw new ParseException (location, "Unrecognized attribute in " +
159 DefaultDirectiveName + " directive");
162 internal virtual void AddAssemblyDirective (ILocation location, TagAttributes attrs)
164 Hashtable tbl = attrs.GetDictionary (null);
165 string name = GetAndRemove (tbl, "Name");
166 string src = GetAndRemove (tbl, "Src");
167 if (name == null && src == null)
168 throw new ParseException (location, "You gotta specify Src or Name");
170 if (name != null && src != null)
171 throw new ParseException (location, "Src and Name cannot be used together");
174 AddAssemblyByName (name, location);
176 GetAssemblyFromSource (src, location);
180 throw new ParseException (location, "Unrecognized attribute in Assembly directive");
183 internal virtual void AddAssembly (Assembly assembly, bool fullPath)
186 anames = new Hashtable ();
188 string name = assembly.GetName ().Name;
189 string loc = assembly.Location;
191 if (!assemblies.Contains (loc)) {
192 assemblies.Add (loc);
196 anames [loc] = assembly;
198 if (!assemblies.Contains (name)) {
199 assemblies.Add (name);
202 anames [name] = assembly;
206 internal virtual Assembly AddAssemblyByName (string name, ILocation location)
209 anames = new Hashtable ();
211 if (anames.Contains (name)) {
212 object o = anames [name];
219 bool fullpath = true;
220 Assembly assembly = LoadAssemblyFromBin (name);
221 if (assembly != null) {
222 AddAssembly (assembly, fullpath);
227 assembly = Assembly.Load (name);
228 string loc = assembly.Location;
229 fullpath = (Path.GetDirectoryName (loc) == PrivateBinPath);
230 } catch (Exception e) {
231 throw new ParseException (location, "Assembly " + name + " not found", e);
234 AddAssembly (assembly, fullpath);
238 void AddAssembliesInBin ()
240 if (!Directory.Exists (PrivateBinPath))
243 string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
244 foreach (string dll in binDlls) {
245 Assembly assembly = Assembly.LoadFrom (dll);
246 AddAssembly (assembly, true);
250 Assembly LoadAssemblyFromBin (string name)
253 if (!Directory.Exists (PrivateBinPath))
256 string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
257 foreach (string dll in binDlls) {
258 string fn = Path.GetFileName (dll);
259 fn = Path.ChangeExtension (fn, null);
263 assembly = Assembly.LoadFrom (dll);
270 Assembly GetAssemblyFromSource (string vpath, ILocation location)
272 vpath = UrlUtils.Combine (BaseVirtualDir, vpath);
273 string realPath = context.Request.MapPath (vpath);
274 if (!File.Exists (realPath))
275 throw new ParseException (location, "File " + vpath + " not found");
277 AddDependency (realPath);
279 CompilerResults result = CachingCompiler.Compile (realPath, realPath, assemblies);
280 if (result.NativeCompilerReturnValue != 0) {
281 StreamReader reader = new StreamReader (realPath);
282 throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ());
285 AddAssembly (result.CompiledAssembly, true);
286 return result.CompiledAssembly;
289 internal Type GetTypeFromBin (string typeName)
291 if (!Directory.Exists (PrivateBinPath))
292 throw new HttpException (String.Format ("Type {0} not found.", typeName));
294 string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
296 foreach (string dll in binDlls) {
297 Assembly assembly = Assembly.LoadFrom (dll);
298 Type type = assembly.GetType (typeName, false);
301 throw new HttpException (String.Format ("Type {0} is not unique.", typeName));
308 throw new HttpException (String.Format ("Type {0} not found.", typeName));
313 internal virtual void AddDependency (string filename)
315 if (dependencies == null)
316 dependencies = new ArrayList ();
318 if (!dependencies.Contains (filename))
319 dependencies.Add (filename);
323 protected abstract string DefaultDirectiveName { get; }
325 internal HttpContext Context {
326 get { return context; }
329 internal string VirtualPath {
330 get { return vPath; }
333 internal string PhysicalPath {
334 get { return physPath; }
337 internal string ClassName {
338 get { return className; }
341 internal string CodeBehind {
342 get { return codeBehind; }
345 internal bool Debug {
346 get { return debug; }
349 internal string Language {
350 get { return language; }
353 internal string Program {
354 get { return program; }
357 internal ArrayList Assemblies {
358 get { return assemblies; }
361 internal ArrayList Dependencies {
362 get { return dependencies; }
365 internal string PrivateBinPath {
367 if (privateBinPath != null)
368 return privateBinPath;
370 AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
371 privateBinPath = setup.PrivateBinPath;
373 if (!Path.IsPathRooted (privateBinPath)) {
374 string appbase = setup.ApplicationBase;
375 if (appbase.StartsWith ("file://")) {
376 appbase = appbase.Substring (7);
377 if (Path.DirectorySeparatorChar != '/')
378 appbase = appbase.Replace ('/', Path.DirectorySeparatorChar);
380 privateBinPath = Path.Combine (appbase, privateBinPath);
383 return privateBinPath;
387 internal string BaseDir {
390 baseDir = context.Request.MapPath (BaseVirtualDir);
396 internal virtual string BaseVirtualDir {
398 if (baseVDir == null)
399 baseVDir = UrlUtils.GetDirectory (context.Request.FilePath);
405 internal CompilationConfiguration CompilationConfig {
407 if (compilationConfig == null)
408 compilationConfig = CompilationConfiguration.GetInstance (context);
410 return compilationConfig;