Assembly GetAssemblyFromSource(string vpath, ILocation location)
        {
            vpath = UrlUtils.Combine(BaseVirtualDir, vpath);
            string realPath = context.Request.MapPath(vpath);

            if (!File.Exists(realPath))
            {
                throw new ParseException(location, "File " + vpath + " not found");
            }

            AddDependency(vpath);

            CompilerResults result = CachingCompiler.Compile(language, realPath, realPath, assemblies);

            if (result.NativeCompilerReturnValue != 0)
            {
                using (StreamReader sr = new StreamReader(realPath))
                {
                    throw new CompilationException(realPath, result.Errors, sr.ReadToEnd());
                }
            }

            AddAssembly(result.CompiledAssembly, true);
            return(result.CompiledAssembly);
        }
        internal SimpleWebHandlerParser(HttpContext context, string virtualPath, string physicalPath, TextReader reader)
        {
            this.reader = reader;
            cachedType  = CachingCompiler.GetTypeFromCache(physicalPath);
            if (cachedType != null)
            {
                return; // We don't need anything else.
            }
            // context is obsolete in 2.0+ - MSDN recommends passing null, so we need to
            // take that into account
            if (context != null)
            {
                this.context = context;
            }
            else
            {
                this.context = HttpContext.Current;
            }

            this.vPath = virtualPath;
            AddDependency(virtualPath);

            // physicalPath is obsolete in 2.0+ - same note what for context applies here
            if (physicalPath != null && physicalPath.Length > 0)
            {
                this.physPath = physicalPath;
            }
            else
            {
                HttpRequest req = this.context != null ? context.Request : null;
                if (req != null)
                {
                    this.physPath = req.MapPath(virtualPath);
                }
            }

            assemblies = new ArrayList();
            string location = Context.ApplicationInstance.AssemblyLocation;

            if (location != typeof(TemplateParser).Assembly.Location)
            {
                appAssemblyIndex = assemblies.Add(location);
            }

            bool addAssembliesInBin = false;

            foreach (AssemblyInfo info in CompilationConfig.Assemblies)
            {
                if (info.Assembly == "*")
                {
                    addAssembliesInBin = true;
                }
                else
                {
                    AddAssemblyByName(info.Assembly, null);
                }
            }
            if (addAssembliesInBin)
            {
                AddAssembliesInBin();
            }
            language = CompilationConfig.DefaultLanguage;

            GetDirectivesAndContent();
        }
        void GetDirectivesAndContent()
        {
            string        line;
            bool          directiveFound = false;
            bool          inDirective = false;
            StringBuilder directive = null;
            StringBuilder content = new StringBuilder();
            int           idxStart, idxEnd, length;
            StreamReader  sr;

            if (reader != null)
            {
                sr = reader as StreamReader;
            }
            else
            {
                sr = new StreamReader(File.OpenRead(physPath), WebEncoding.FileEncoding);
            }

            using (sr)
            {
                while ((line = sr.ReadLine()) != null && cachedType == null)
                {
                    length = line.Length;
                    if (length == 0)
                    {
                        content.Append("\n");
                        continue;
                    }

                    idxStart = line.IndexOf("<%");
                    if (idxStart > -1)
                    {
                        idxEnd = line.IndexOf("%>");
                        if (idxStart > 0)
                        {
                            content.Append(line.Substring(0, idxStart));
                        }

                        if (directive == null)
                        {
                            directive = new StringBuilder();
                        }
                        else
                        {
                            directive.Length = 0;
                        }

                        if (idxEnd > -1)
                        {
                            directiveFound = true;
                            inDirective    = false;
                            directive.Append(line.Substring(idxStart, idxEnd - idxStart + 2));
                            if (idxEnd < length - 2)
                            {
                                content.Append(line.Substring(idxEnd + 2, length - idxEnd - 2));
                            }
                        }
                        else
                        {
                            inDirective    = true;
                            directiveFound = false;
                            directive.Append(line.Substring(idxStart));
                            continue;
                        }
                    }

                    if (inDirective)
                    {
                        int idx = line.IndexOf("%>");
                        if (idx > -1)
                        {
                            directive.Append(line.Substring(0, idx + 2));
                            if (idx < length)
                            {
                                content.Append(line.Substring(idx + 2) + "\n");
                            }
                            inDirective    = false;
                            directiveFound = true;
                        }
                        else
                        {
                            directive.Append(line);
                            continue;
                        }
                    }

                    if (directiveFound)
                    {
                        ParseDirective(directive.ToString());
                        directiveFound = false;
                        if (gotDefault)
                        {
                            cachedType = CachingCompiler.GetTypeFromCache(physPath);
                            if (cachedType != null)
                            {
                                break;
                            }
                        }

                        continue;
                    }

                    content.Append(line + "\n");
                }
                directive = null;
            }

            if (!gotDefault)
            {
                throw new ParseException(null, "No @" + DefaultDirectiveName +
                                         " directive found");
            }

            if (cachedType == null)
            {
                this.program = content.ToString();
            }
        }