示例#1
0
 private void OnXsltLoadCompleted(object sender, PerformanceInfo info)
 {
     if (info != null)
     {
         if (Completed != null)
         {
             Completed(this, info);
         }
         Debug.WriteLine("Browser loaded in {0} milliseconds", info.BrowserMilliseconds);
         info = null;
     }
 }
示例#2
0
        /// <summary>
        /// Run an XSLT transform and show the results.
        /// </summary>
        /// <param name="context">The document to transform</param>
        /// <param name="xsltfilename">The xslt file to use</param>
        /// <param name="outpath">Output file name hint.</param>
        /// <returns>The output file name or null if DisableOutputFile is true</returns>
        public string DisplayXsltResults(XmlDocument context, string xsltfilename, string outpath = null)
        {
            if (!this._webInitialized)
            {
                return(null);
            }

            this.CleanupTempFile();
            Uri resolved = null;

            try
            {
                XslCompiledTransform transform;
                if (string.IsNullOrEmpty(xsltfilename))
                {
                    transform = GetDefaultStylesheet();
                }
                else
                {
                    resolved = new Uri(_baseUri, xsltfilename);
                    if (resolved != this._xsltUri || IsModified())
                    {
                        _xslt        = new XslCompiledTransform();
                        this._loaded = DateTime.Now;
                        var settings = new XsltSettings(true, this.EnableScripts);
                        settings.EnableScript = (_trusted.ContainsKey(resolved));
                        var rs = new XmlReaderSettings();
                        rs.DtdProcessing = this.IgnoreDTD ? DtdProcessing.Ignore : DtdProcessing.Parse;
                        rs.XmlResolver   = _resolver;
                        using (XmlReader r = XmlReader.Create(resolved.AbsoluteUri, rs))
                        {
                            _xslt.Load(r, settings, _resolver);
                        }

                        // the XSLT DOM is also handy to have around for GetOutputMethod
                        this._xsltdoc = new XmlDocument();
                        this._xsltdoc.Load(resolved.AbsoluteUri);
                    }
                    transform = _xslt;
                }

                if (string.IsNullOrEmpty(outpath))
                {
                    if (!DisableOutputFile)
                    {
                        if (!string.IsNullOrEmpty(xsltfilename))
                        {
                            outpath = this.GetXsltOutputFileName(xsltfilename);
                        }
                        else
                        {
                            // default stylesheet produces html
                            this._tempFile = outpath = GetWritableFileName("DefaultXsltOutput.htm");
                        }
                    }
                }
                else
                {
                    var ext      = GetDefaultOutputExtension();
                    var basePath = Path.Combine(Path.GetDirectoryName(outpath), Path.GetFileNameWithoutExtension(outpath));
                    outpath = basePath + ext;
                    outpath = GetWritableFileName(outpath);
                }

                if (null != transform)
                {
                    var dir = Path.GetDirectoryName(outpath);
                    if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }

                    var settings = new XmlReaderSettings();
                    settings.XmlResolver   = new XmlProxyResolver(this._site);
                    settings.DtdProcessing = this.IgnoreDTD ? DtdProcessing.Ignore : DtdProcessing.Parse;
                    var xmlReader = XmlIncludeReader.CreateIncludeReader(context, settings, GetBaseUri().AbsoluteUri);
                    if (string.IsNullOrEmpty(outpath))
                    {
                        using (StringWriter writer = new StringWriter())
                        {
                            transform.Transform(xmlReader, null, writer);
                            this._xsltUri = resolved;
                            Display(writer.ToString());
                        }
                    }
                    else
                    {
                        bool     noBom       = false;
                        Settings appSettings = (Settings)this._site.GetService(typeof(Settings));
                        if (appSettings != null)
                        {
                            noBom = (bool)appSettings["NoByteOrderMark"];
                        }
                        if (noBom)
                        {
                            // cache to an inmemory stream so we can strip the BOM.
                            using (MemoryStream ms = new MemoryStream())
                            {
                                transform.Transform(xmlReader, null, ms);
                                ms.Seek(0, SeekOrigin.Begin);
                                Utilities.WriteFileWithoutBOM(ms, outpath);
                            }
                        }
                        else
                        {
                            using (FileStream writer = new FileStream(outpath, FileMode.OpenOrCreate, FileAccess.Write))
                            {
                                Stopwatch watch = new Stopwatch();
                                watch.Start();
                                transform.Transform(xmlReader, null, writer);
                                watch.Stop();
                                this._info = new PerformanceInfo();
                                this._info.XsltMilliseconds = watch.ElapsedMilliseconds;
                                Debug.WriteLine("Transform in {0} milliseconds", watch.ElapsedMilliseconds);
                                this._xsltUri = resolved;
                            }
                        }

                        DisplayFile(outpath);
                    }
                }
            }
            catch (System.Xml.Xsl.XsltException x)
            {
                if (x.Message.Contains("XsltSettings"))
                {
                    if (!_trusted.ContainsKey(resolved) &&
                        MessageBox.Show(this, SR.XslScriptCodePrompt, SR.XslScriptCodeCaption,
                                        MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                    {
                        _trusted[resolved] = true;
                        return(DisplayXsltResults(context, xsltfilename, outpath));
                    }
                }
                WriteError(x);
            }
            catch (Exception x)
            {
                WriteError(x);
            }

            this._previousOutputFile = outpath;
            return(outpath);
        }