private void RunRenderMht(IStreamGen sg)
        {
            OneFileStreamGen temp = null;
            FileStream       fs   = null;

            try
            {
                string tempHtmlReportFileName = Path.ChangeExtension(Path.GetTempFileName(), "htm");
                temp = new OneFileStreamGen(tempHtmlReportFileName, true);
                RunRender(temp, OutputPresentationType.HTML);
                temp.CloseMainStream();

                // Create the mht file (into a temporary file position)
                MhtBuilder mhtConverter = new MhtBuilder();
                string     fileName     = Path.ChangeExtension(Path.GetTempFileName(), "mht");
                mhtConverter.SavePageArchive(fileName, "file://" + tempHtmlReportFileName);

                // clean up the temporary files
                foreach (string tempFileName in temp.FileList)
                {
                    try
                    {
                        File.Delete(tempFileName);
                    }
                    catch {}
                }

                // Copy the mht file to the requested stream
                Stream os = sg.GetStream();
                fs = File.OpenRead(fileName);
                byte[] ba = new byte[4096];
                int    rb = 0;
                while ((rb = fs.Read(ba, 0, ba.Length)) > 0)
                {
                    os.Write(ba, 0, rb);
                }
            }
            catch (Exception ex)
            {
                rl.LogError(8, "Error converting HTML to MHTML " + ex.Message +
                            Environment.NewLine + ex.StackTrace);
            }
            finally
            {
                if (temp != null)
                {
                    temp.CloseMainStream();
                }
                if (fs != null)
                {
                    fs.Close();
                }
                _Cache = new RCache();
            }
        }
		public DateTime ExecutionTime;	// start time of report execution

		/// <summary>
		/// Construct a runtime Report object using the compiled report definition.
		/// </summary>
		/// <param name="r"></param>
		public Report(ReportDefn r)
		{
			_Report = r;
			_Cache = new RCache();
			rl = new ReportLog(r.rl);
			_ReportName = r.Name;
			_UserParameters = null;
			_LURuntimeName = new ListDictionary();	// shouldn't be very many of these
			if (r.Code != null)
				_CodeInstance = r.Code.Load(this);
			if (r.Classes != null)
				r.Classes.Load(this);
		}
        /// <summary>
        /// Build the Pages for this report.
        /// </summary>
        /// <returns></returns>
        public Pages BuildPages()
        {
            PageNumber = 1;                     // reset page numbers
            TotalPages = 1;

            Pages pgs = new Pages(this);

            pgs.PageHeight = _Report.PageHeight.Points;
            pgs.PageWidth  = _Report.PageWidth.Points;
            try
            {
                Page p = new Page(1);                                           // kick it off with a new page
                pgs.AddPage(p);

                // Create all the pages
                _Report.Body.RunPage(pgs);

                if (pgs.LastPage.IsEmpty() && pgs.PageCount > 1)                // get rid of extraneous pages which
                {
                    pgs.RemoveLastPage();                                       //   can be caused by region page break at end
                }
                // Now create the headers and footers for all the pages (as needed)
                if (_Report.PageHeader != null)
                {
                    _Report.PageHeader.RunPage(pgs);
                }
                if (_Report.PageFooter != null)
                {
                    _Report.PageFooter.RunPage(pgs);
                }
                // clear out any runtime clutter
                foreach (Page pg in pgs)
                {
                    pg.ResetPageExpressions();
                }

                pgs.SortPageItems();             // Handle ZIndex ordering of pages
            }
            catch (Exception e)
            {
                rl.LogError(8, "Exception running report\r\n" + e.Message + "\r\n" + e.StackTrace);
            }
            finally
            {
                pgs.CleanUp();                          // always want to make sure we clean this up since
                _Cache = new RCache();
            }

            return(pgs);
        }
        public DateTime ExecutionTime;              // start time of report execution

        /// <summary>
        /// Construct a runtime Report object using the compiled report definition.
        /// </summary>
        /// <param name="r"></param>
        public Report(ReportDefn r)
        {
            _Report         = r;
            _Cache          = new RCache();
            rl              = new ReportLog(r.rl);
            _ReportName     = r.Name;
            _UserParameters = null;
            _LURuntimeName  = new ListDictionary();             // shouldn't be very many of these
            if (r.Code != null)
            {
                _CodeInstance = r.Code.Load(this);
            }
            if (r.Classes != null)
            {
                r.Classes.Load(this);
            }
        }
        /// <summary>
        /// RunRenderPdf will render a Pdf given the page structure
        /// </summary>
        /// <param name="sg"></param>
        /// <param name="pgs"></param>
        public void RunRenderPdf(IStreamGen sg, Pages pgs)
        {
            PageNumber = 1;                     // reset page numbers
            TotalPages = 1;

            IPresent ip = new RenderPdf(this, sg);

            try
            {
                ip.Start();
                ip.RunPages(pgs);
                ip.End();
            }
            finally
            {
                pgs.CleanUp();                          // always want to make sure we cleanup to reduce resource usage
                _Cache = new RCache();
            }

            return;
        }
        /// <summary>
        /// Renders the report using the requested presentation type.
        /// </summary>
        /// <param name="sg">IStreamGen for generating result stream</param>
        /// <param name="type">Presentation type: HTML, XML, PDF, MHT, or ASP compatible HTML</param>
        /// <param name="prefix">For HTML puts prefix allowing unique name generation</param>
        public void RunRender(IStreamGen sg, OutputPresentationType type, string prefix)
        {
            if (sg == null)
            {
                throw new ArgumentException("IStreamGen argument cannot be null.", "sg");
            }
            RenderHtml rh = null;

            PageNumber = 1;                     // reset page numbers
            TotalPages = 1;
            IPresent        ip;
            MemoryStreamGen msg = null;

            switch (type)
            {
            case OutputPresentationType.PDF:
                ip = new RenderPdf(this, sg);
                _Report.Run(ip);
                break;

            case OutputPresentationType.TIF:
                ip = new RenderTif(this, sg);
                _Report.Run(ip);
                break;

            case OutputPresentationType.TIFBW:
                RenderTif rtif = new RenderTif(this, sg);
                rtif.RenderColor = false;
                ip = rtif;
                _Report.Run(ip);
                break;

            case OutputPresentationType.XML:
                if (_Report.DataTransform != null && _Report.DataTransform.Length > 0)
                {
                    msg = new MemoryStreamGen();
                    ip  = new RenderXml(this, msg);
                    _Report.Run(ip);
                    RunRenderXmlTransform(sg, msg);
                }
                else
                {
                    ip = new RenderXml(this, sg);
                    _Report.Run(ip);
                }
                break;

            case OutputPresentationType.MHTML:
                this.RunRenderMht(sg);
                break;

            case OutputPresentationType.CSV:
                ip = new RenderCsv(this, sg);
                _Report.Run(ip);
                break;

            case OutputPresentationType.DMP:
                ip = new RenderDMP(this, sg);
                _Report.Run(ip);
                break;

            case OutputPresentationType.RTF:
                ip = new RenderRtf(this, sg);
                _Report.Run(ip);
                break;

            case OutputPresentationType.Excel:
                ip = new RenderExcel(this, sg);
                _Report.Run(ip);
                break;

            case OutputPresentationType.ASPHTML:
            case OutputPresentationType.HTML:
            default:
                ip        = rh = new RenderHtml(this, sg);
                rh.Asp    = (type == OutputPresentationType.ASPHTML);
                rh.Prefix = prefix;
                _Report.Run(ip);
                // Retain the CSS and JavaScript
                if (rh != null)
                {
                    _CSS        = rh.CSS;
                    _JavaScript = rh.JavaScript;
                }
                break;
            }

            sg.CloseMainStream();
            _Cache = new RCache();
            return;
        }
		/// <summary>
		/// Build the Pages for this report.
		/// </summary>
		/// <returns></returns>
		public Pages BuildPages()
		{
			PageNumber = 1;		// reset page numbers
			TotalPages = 1;

			Pages pgs = new Pages(this);
			pgs.PageHeight = _Report.PageHeight.Points;
			pgs.PageWidth = _Report.PageWidth.Points;
			try
			{
				Page p = new Page(1);				// kick it off with a new page
				pgs.AddPage(p);

				// Create all the pages
				_Report.Body.RunPage(pgs);

				if (pgs.LastPage.IsEmpty() && pgs.PageCount > 1) // get rid of extraneous pages which
					pgs.RemoveLastPage();			//   can be caused by region page break at end

				// Now create the headers and footers for all the pages (as needed)
				if (_Report.PageHeader != null)
					_Report.PageHeader.RunPage(pgs);
				if (_Report.PageFooter != null)
					_Report.PageFooter.RunPage(pgs);
				// clear out any runtime clutter
				foreach (Page pg in pgs)
					pg.ResetPageExpressions();

                pgs.SortPageItems();             // Handle ZIndex ordering of pages
			}
			catch (Exception e)
			{
				rl.LogError(8, "Exception running report\r\n" + e.Message + "\r\n" + e.StackTrace);
			}
			finally
			{
				pgs.CleanUp();		// always want to make sure we clean this up since 
                _Cache = new RCache();
			}

			return pgs;
		}
        /// <summary>
        /// RunRenderTif will render a TIF given the page structure
        /// </summary>
        /// <param name="sg"></param>
        /// <param name="pgs"></param>
        public void RunRenderTif(IStreamGen sg, Pages pgs, bool bColor)
        {
            PageNumber = 1;		// reset page numbers
            TotalPages = 1;

            RenderTif ip = new RenderTif(this, sg);
            ip.RenderColor = bColor;
            try
            {
                ip.Start();
                ip.RunPages(pgs);
                ip.End();
            }
            finally
            {
                pgs.CleanUp();		// always want to make sure we cleanup to reduce resource usage
                _Cache = new RCache();
            }

            return;
        }
		private void RunRenderMht(IStreamGen sg)
		{
			OneFileStreamGen temp = null;
			FileStream fs=null;
			try
			{
				string tempHtmlReportFileName = Path.ChangeExtension(Path.GetTempFileName(), "htm");
				temp = new OneFileStreamGen(tempHtmlReportFileName, true);
				RunRender(temp, OutputPresentationType.HTML);
				temp.CloseMainStream();

				// Create the mht file (into a temporary file position)
				MhtBuilder mhtConverter = new MhtBuilder();
				string fileName = Path.ChangeExtension(Path.GetTempFileName(), "mht");
				mhtConverter.SavePageArchive(fileName, "file://" + tempHtmlReportFileName);

				// clean up the temporary files
				foreach (string tempFileName in temp.FileList)
				{
					try
					{
						File.Delete(tempFileName);
					}
					catch{}
				}

				// Copy the mht file to the requested stream
				Stream os = sg.GetStream();
				fs = File.OpenRead(fileName);
				byte[] ba = new byte[4096];
				int rb=0;
				while ((rb = fs.Read(ba, 0, ba.Length)) > 0)
				{
					os.Write(ba, 0, rb);
				}
				
			}
			catch (Exception ex)
			{
				rl.LogError(8, "Error converting HTML to MHTML " + ex.Message + 
									Environment.NewLine + ex.StackTrace);
			}
			finally
			{
				if (temp != null)
					temp.CloseMainStream();
				if (fs != null)
					fs.Close();
                _Cache = new RCache();
            }
		}
		/// <summary>
		/// Renders the report using the requested presentation type.
		/// </summary>
		/// <param name="sg">IStreamGen for generating result stream</param>
		/// <param name="type">Presentation type: HTML, XML, PDF, MHT, or ASP compatible HTML</param>
		/// <param name="prefix">For HTML puts prefix allowing unique name generation</param>
		public void RunRender(IStreamGen sg, OutputPresentationType type, string prefix)
		{
			if (sg == null)
				throw new ArgumentException("IStreamGen argument cannot be null.", "sg");
			RenderHtml rh=null;

			PageNumber = 1;		// reset page numbers
			TotalPages = 1;
			IPresent ip;
			MemoryStreamGen msg = null;
			switch (type)
			{
				case OutputPresentationType.PDF:
					ip = new RenderPdf(this, sg);
					_Report.Run(ip);
					break;
                case OutputPresentationType.TIF:
                    ip = new RenderTif(this, sg);
                    _Report.Run(ip);
                    break;
                case OutputPresentationType.TIFBW:
                    RenderTif rtif = new RenderTif(this, sg);
                    rtif.RenderColor = false;
                    ip = rtif;
                    _Report.Run(ip);
                    break;
                case OutputPresentationType.XML:
					if (_Report.DataTransform != null && _Report.DataTransform.Length > 0)
					{
						msg = new MemoryStreamGen();
						ip = new RenderXml(this, msg);
						_Report.Run(ip);
						RunRenderXmlTransform(sg, msg);
					}
					else
					{
						ip = new RenderXml(this, sg);    
						_Report.Run(ip);
					}
					break;
				case OutputPresentationType.MHTML:
					this.RunRenderMht(sg);
					break;
                case OutputPresentationType.CSV:
                    ip = new RenderCsv(this, sg);
                    _Report.Run(ip);
                    break;
                case OutputPresentationType.DMP:
                    ip = new RenderDMP(this, sg);
                    _Report.Run(ip);
                    break;
                case OutputPresentationType.RTF:
                    ip = new RenderRtf(this, sg);
                    _Report.Run(ip);
                    break;
                case OutputPresentationType.Excel:
                    ip = new RenderExcel(this, sg);
                    _Report.Run(ip);
                    break;
                case OutputPresentationType.ASPHTML:
				case OutputPresentationType.HTML:
				default:
					ip = rh = new RenderHtml(this, sg);
					rh.Asp = (type == OutputPresentationType.ASPHTML);
					rh.Prefix = prefix;
					_Report.Run(ip);
					// Retain the CSS and JavaScript
					if (rh != null)
					{
						_CSS = rh.CSS;
						_JavaScript = rh.JavaScript;
					}
					break;
			}

			sg.CloseMainStream();
            _Cache = new RCache();
            return;
		}